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.plaintext;
014
015import java.util.*;
016import java.util.concurrent.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.annotation.*;
020import org.apache.juneau.collections.*;
021import org.apache.juneau.serializer.*;
022import org.apache.juneau.transform.*;
023
024/**
025 * Serializes POJOs to plain text using just the <c>toString()</c> method on the serialized object.
026 *
027 * <h5 class='topic'>Media types</h5>
028 *
029 * Handles <c>Accept</c> types:  <bc>text/plain</bc>
030 * <p>
031 * Produces <c>Content-Type</c> types:  <bc>text/plain</bc>
032 *
033 * <h5 class='topic'>Description</h5>
034 *
035 * Essentially converts POJOs to plain text using the <c>toString()</c> method.
036 *
037 * <p>
038 * Also serializes objects using a transform if the object class has an {@link PojoSwap PojoSwap&lt;?,String&gt;}
039 * transform defined on it.
040 */
041@ConfigurableContext
042public class PlainTextSerializer extends WriterSerializer implements PlainTextMetaProvider, PlainTextCommon {
043
044   //-------------------------------------------------------------------------------------------------------------------
045   // Configurable properties
046   //-------------------------------------------------------------------------------------------------------------------
047
048   static final String PREFIX = "PlainTextSerializer";
049
050   //-------------------------------------------------------------------------------------------------------------------
051   // Predefined instances
052   //-------------------------------------------------------------------------------------------------------------------
053
054   /** Default serializer, all default settings.*/
055   public static final PlainTextSerializer DEFAULT = new PlainTextSerializer(PropertyStore.DEFAULT);
056
057
058   //-------------------------------------------------------------------------------------------------------------------
059   // Instance
060   //-------------------------------------------------------------------------------------------------------------------
061
062   private final Map<ClassMeta<?>,PlainTextClassMeta> plainTextClassMetas = new ConcurrentHashMap<>();
063   private final Map<BeanPropertyMeta,PlainTextBeanPropertyMeta> plainTextBeanPropertyMetas = new ConcurrentHashMap<>();
064
065   /**
066    * Constructor.
067    *
068    * @param ps
069    *    The property store containing all the settings for this object.
070    */
071   public PlainTextSerializer(PropertyStore ps) {
072      this(ps, "text/plain", (String)null);
073   }
074
075   /**
076    * Constructor.
077    *
078    * @param ps
079    *    The property store containing all the settings for this object.
080    * @param produces
081    *    The media type that this serializer produces.
082    * @param accept
083    *    The accept media types that the serializer can handle.
084    *    <p>
085    *    Can contain meta-characters per the <c>media-type</c> specification of {@doc ExtRFC2616.section14.1}
086    *    <p>
087    *    If empty, then assumes the only media type supported is <c>produces</c>.
088    *    <p>
089    *    For example, if this serializer produces <js>"application/json"</js> but should handle media types of
090    *    <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be:
091    *    <p class='bcode w800'>
092    *    <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json,text/json"</js>);
093    *    </p>
094    *    <br>...or...
095    *    <p class='bcode w800'>
096    *    <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*&#8203;/json"</js>);
097    *    </p>
098    * <p>
099    * The accept value can also contain q-values.
100    */
101   public PlainTextSerializer(PropertyStore ps, String produces, String accept) {
102      super(ps, produces, accept);
103   }
104
105
106   @Override /* Context */
107   public PlainTextSerializerBuilder builder() {
108      return new PlainTextSerializerBuilder(getPropertyStore());
109   }
110
111   /**
112    * Instantiates a new clean-slate {@link PlainTextSerializerBuilder} object.
113    *
114    * <p>
115    * This is equivalent to simply calling <code><jk>new</jk> PlainTextSerializerBuilder()</code>.
116    *
117    * <p>
118    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
119    * the settings of the object called on.
120    *
121    * @return A new {@link PlainTextSerializerBuilder} object.
122    */
123   public static PlainTextSerializerBuilder create() {
124      return new PlainTextSerializerBuilder();
125   }
126
127   @Override /* Context */
128   public  PlainTextSerializerSession createSession() {
129      return createSession(createDefaultSessionArgs());
130   }
131
132   @Override /* Serializer */
133   public PlainTextSerializerSession createSession(SerializerSessionArgs args) {
134      return new PlainTextSerializerSession(this, args);
135   }
136
137   //-----------------------------------------------------------------------------------------------------------------
138   // Extended metadata
139   //-----------------------------------------------------------------------------------------------------------------
140
141   @Override /* PlainTextMetaProvider */
142   public PlainTextClassMeta getPlainTextClassMeta(ClassMeta<?> cm) {
143      PlainTextClassMeta m = plainTextClassMetas.get(cm);
144      if (m == null) {
145         m = new PlainTextClassMeta(cm, this);
146         plainTextClassMetas.put(cm, m);
147      }
148      return m;
149   }
150
151   @Override /* PlainTextMetaProvider */
152   public PlainTextBeanPropertyMeta getPlainTextBeanPropertyMeta(BeanPropertyMeta bpm) {
153      if (bpm == null)
154         return PlainTextBeanPropertyMeta.DEFAULT;
155      PlainTextBeanPropertyMeta m = plainTextBeanPropertyMetas.get(bpm);
156      if (m == null) {
157         m = new PlainTextBeanPropertyMeta(bpm.getDelegateFor(), this);
158         plainTextBeanPropertyMetas.put(bpm, m);
159      }
160      return m;
161   }
162
163   //-----------------------------------------------------------------------------------------------------------------
164   // Other methods
165   //-----------------------------------------------------------------------------------------------------------------
166
167   @Override /* Context */
168   public OMap toMap() {
169      return super.toMap()
170         .a("PlainTextSerializer", new DefaultFilteringOMap()
171         );
172   }
173}