001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.swaps;
018
019import static org.apache.juneau.common.utils.IOUtils.*;
020
021import java.io.*;
022
023import org.apache.juneau.*;
024import org.apache.juneau.html.*;
025import org.apache.juneau.json.*;
026import org.apache.juneau.parser.*;
027import org.apache.juneau.swap.*;
028import org.apache.juneau.uon.*;
029import org.apache.juneau.urlencoding.*;
030import org.apache.juneau.xml.*;
031
032/**
033 * Transforms the contents of a {@link Reader} into an {@code Object}.
034 *
035 * <h5 class='topic'>Description</h5>
036 *
037 * The {@code Reader} must contain JSON, Juneau-generated XML (output from {@link XmlSerializer}), or Juneau-generated
038 * HTML (output from {@link JsonSerializer}) in order to be parsed correctly.
039 *
040 * <p>
041 * Useful for serializing models that contain {@code Readers} created by {@code RestCall} instances.
042 *
043 * <p>
044 * This is a one-way transform, since {@code Readers} cannot be reconstituted.
045 *
046 * <h5 class='topic'>Behavior-specific subclasses</h5>
047 *
048 * The following direct subclasses are provided for convenience:
049 * <ul>
050 *    <li>{@link Json} - Parses JSON text.
051 *    <li>{@link Xml} - Parses XML text.
052 *    <li>{@link Html} - Parses HTML text.
053 *    <li>{@link PlainText} - Parses plain text.
054 * </ul>
055 *
056 * <h5 class='section'>See Also:</h5><ul>
057 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SwapBasics">Swap Basics</a>
058
059 * </ul>
060 */
061public class ParsedReaderSwap extends ObjectSwap<Reader,Object> {
062
063   /** Reader transform for reading JSON text. */
064   public static class Json extends ParsedReaderSwap {
065      /** Constructor */
066      public Json() {
067         super(JsonParser.DEFAULT);
068      }
069   }
070
071   /** Reader transform for reading XML text. */
072   public static class Xml extends ParsedReaderSwap {
073      /** Constructor */
074      public Xml() {
075         super(XmlParser.DEFAULT);
076      }
077   }
078
079   /** Reader transform for reading HTML text. */
080   public static class Html extends ParsedReaderSwap {
081      /** Constructor */
082      public Html() {
083         super(HtmlParser.DEFAULT);
084      }
085   }
086
087   /** Reader transform for reading plain text. */
088   public static class PlainText extends ParsedReaderSwap {
089      /** Constructor */
090      public PlainText() {
091         super(null);
092      }
093   }
094
095   /** Reader transform for reading plain text. */
096   public static class Uon extends ParsedReaderSwap {
097      /** Constructor */
098      public Uon() {
099         super(UonParser.DEFAULT);
100      }
101   }
102
103   /** Reader transform for reading plain text. */
104   public static class UrlEncoding extends ParsedReaderSwap {
105      /** Constructor */
106      public UrlEncoding() {
107         super(UrlEncodingParser.DEFAULT);
108      }
109   }
110
111   /** The parser to use to parse the contents of the Reader. */
112   private ReaderParser parser;
113
114   /**
115    * @param parser The parser to use to convert the contents of the reader to Java objects.
116    */
117   public ParsedReaderSwap(ReaderParser parser) {
118      this.parser = parser;
119   }
120
121   /**
122    * Converts the specified {@link Reader} to an {@link Object} whose type is determined by the contents of the reader.
123    */
124   @Override /* ObjectSwap */
125   public Object swap(BeanSession session, Reader o) throws Exception {
126      if (parser == null)
127         return read(o);
128      return parser.parse(o, Object.class);
129   }
130}