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