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.marshall.swaps;
018
019import static org.apache.juneau.commons.utils.IoUtils.*;
020
021import java.io.*;
022
023import org.apache.juneau.marshall.*;
024import org.apache.juneau.marshall.html.*;
025import org.apache.juneau.marshall.json.*;
026import org.apache.juneau.marshall.json5.*;
027import org.apache.juneau.marshall.parser.*;
028import org.apache.juneau.marshall.swap.spi.*;
029import org.apache.juneau.marshall.uon.*;
030import org.apache.juneau.marshall.urlencoding.*;
031import org.apache.juneau.marshall.xml.*;
032
033/**
034 * Transforms the contents of a {@link Reader} into an {@code Object}.
035 *
036 * <h5 class='topic'>Description</h5>
037 *
038 * The {@code Reader} must contain JSON, Juneau-generated XML (output from {@link XmlSerializer}), or Juneau-generated
039 * HTML (output from {@link JsonSerializer}) in order to be parsed correctly.
040 *
041 * <p>
042 * Useful for serializing models that contain {@code Readers} created by {@code RestCall} instances.
043 *
044 * <p>
045 * This is a one-way transform, since {@code Readers} cannot be reconstituted.
046 *
047 * <h5 class='topic'>Behavior-specific subclasses</h5>
048 *
049 * The following direct subclasses are provided for convenience:
050 * <ul>
051 *    <li>{@link Json} - Parses JSON text.
052 *    <li>{@link Xml} - Parses XML text.
053 *    <li>{@link Html} - Parses HTML text.
054 *    <li>{@link PlainText} - Parses plain text.
055 * </ul>
056 *
057 * <h5 class='section'>See Also:</h5><ul>
058 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Swaps">Swap</a>
059
060 * </ul>
061 */
062public class ParsedReaderSwap extends ObjectSwap<Reader,Object> {
063
064   /** Reader transform for reading HTML text. */
065   public static class Html extends ParsedReaderSwap {
066
067      /** Constructor */
068      public Html() {
069         super(HtmlParser.DEFAULT);
070      }
071   }
072
073   /** Reader transform for reading JSON text. */
074   public static class Json extends ParsedReaderSwap {
075
076      /** Constructor */
077      public Json() {
078         super(Json5Parser.DEFAULT);
079      }
080   }
081
082   /** Reader transform for reading plain text. */
083   public static class PlainText extends ParsedReaderSwap {
084
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
094      /** Constructor */
095      public Uon() {
096         super(UonParser.DEFAULT);
097      }
098   }
099
100   /** Reader transform for reading plain text. */
101   public static class UrlEncoding extends ParsedReaderSwap {
102
103      /** Constructor */
104      public UrlEncoding() {
105         super(UrlEncodingParser.DEFAULT);
106      }
107   }
108
109   /** Reader transform for reading XML text. */
110   public static class Xml extends ParsedReaderSwap {
111
112      /** Constructor */
113      public Xml() {
114         super(XmlParser.DEFAULT);
115      }
116   }
117
118   /** The parser to use to parse the contents of the Reader. */
119   private final ReaderParser parser;
120
121   /**
122    * Constructor.
123    *
124    * @param parser
125    *    The parser to use to convert the contents of the reader to Java objects.
126    *    <br>Can be <jk>null</jk>, in which case the reader contents are read as plain text.
127    */
128   public ParsedReaderSwap(ReaderParser parser) {
129      this.parser = parser;
130   }
131
132   /**
133    * Converts the specified {@link Reader} to an {@link Object} whose type is determined by the contents of the reader.
134    */
135   @Override /* Overridden from ObjectSwap */
136   public Object swap(MarshallingSession session, Reader o) throws Exception {
137      if (parser == null)
138         return read(o);
139      return parser.read(o, Object.class);
140   }
141}