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.transforms; 014 015import static org.apache.juneau.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.transform.*; 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 */ 052public class ReaderSwap extends PojoSwap<Reader,Object> { 053 054 /** Reader transform for reading JSON text. */ 055 public static class Json extends ReaderSwap { 056 /** Constructor */ 057 public Json() { 058 super(JsonParser.DEFAULT); 059 } 060 } 061 062 /** Reader transform for reading XML text. */ 063 public static class Xml extends ReaderSwap { 064 /** Constructor */ 065 public Xml() { 066 super(XmlParser.DEFAULT); 067 } 068 } 069 070 /** Reader transform for reading HTML text. */ 071 public static class Html extends ReaderSwap { 072 /** Constructor */ 073 public Html() { 074 super(HtmlParser.DEFAULT); 075 } 076 } 077 078 /** Reader transform for reading plain text. */ 079 public static class PlainText extends ReaderSwap { 080 /** Constructor */ 081 public PlainText() { 082 super(null); 083 } 084 } 085 086 /** Reader transform for reading plain text. */ 087 public static class Uon extends ReaderSwap { 088 /** Constructor */ 089 public Uon() { 090 super(UonParser.DEFAULT); 091 } 092 } 093 094 /** Reader transform for reading plain text. */ 095 public static class UrlEncoding extends ReaderSwap { 096 /** Constructor */ 097 public UrlEncoding() { 098 super(UrlEncodingParser.DEFAULT); 099 } 100 } 101 102 /** The parser to use to parse the contents of the Reader. */ 103 private ReaderParser parser; 104 105 /** 106 * @param parser The parser to use to convert the contents of the reader to Java objects. 107 */ 108 public ReaderSwap(ReaderParser parser) { 109 this.parser = parser; 110 } 111 112 /** 113 * Converts the specified {@link Reader} to an {@link Object} whose type is determined by the contents of the reader. 114 */ 115 @Override /* PojoSwap */ 116 public Object swap(BeanSession session, Reader o) throws Exception { 117 if (parser == null) 118 return read(o); 119 return parser.parse(o, Object.class); 120 } 121}