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.lang.reflect.*;
016import java.util.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.http.*;
020import org.apache.juneau.httppart.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.serializer.*;
023import org.apache.juneau.transform.*;
024
025/**
026 * Serializes POJOs to plain text using just the <code>toString()</code> method on the serialized object.
027 *
028 * <h5 class='topic'>Media types</h5>
029 *
030 * Handles <code>Accept</code> types:  <code><b>text/plain</b></code>
031 * <p>
032 * Produces <code>Content-Type</code> types:  <code><b>text/plain</b></code>
033 *
034 * <h5 class='topic'>Description</h5>
035 *
036 * Essentially converts POJOs to plain text using the <code>toString()</code> method.
037 *
038 * <p>
039 * Also serializes objects using a transform if the object class has an {@link PojoSwap PojoSwap&lt;?,String&gt;}
040 * transform defined on it.
041 */
042public class PlainTextSerializer extends WriterSerializer {
043
044   //-------------------------------------------------------------------------------------------------------------------
045   // Predefined instances
046   //-------------------------------------------------------------------------------------------------------------------
047
048   /** Default serializer, all default settings.*/
049   public static final PlainTextSerializer DEFAULT = new PlainTextSerializer(PropertyStore.DEFAULT);
050
051
052   //-------------------------------------------------------------------------------------------------------------------
053   // Instance
054   //-------------------------------------------------------------------------------------------------------------------
055
056   /**
057    * Constructor.
058    *
059    * @param ps
060    *    The property store containing all the settings for this object.
061    */
062   public PlainTextSerializer(PropertyStore ps) {
063      this(ps, "text/plain", (String)null);
064   }
065
066   /**
067    * Constructor.
068    *
069    * @param ps
070    *    The property store containing all the settings for this object.
071    * @param produces
072    *    The media type that this serializer produces.
073    * @param accept
074    *    The accept media types that the serializer can handle.
075    *    <p>
076    *    Can contain meta-characters per the <code>media-type</code> specification of {@doc RFC2616.section14.1}
077    *    <p>
078    *    If empty, then assumes the only media type supported is <code>produces</code>.
079    *    <p>
080    *    For example, if this serializer produces <js>"application/json"</js> but should handle media types of
081    *    <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be:
082    *    <p class='bcode w800'>
083    *    <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json,text/json"</js>);
084    *    </p>
085    *    <br>...or...
086    *    <p class='bcode w800'>
087    *    <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*&#8203;/json"</js>);
088    *    </p>
089    * <p>
090    * The accept value can also contain q-values.
091    */
092   public PlainTextSerializer(PropertyStore ps, String produces, String accept) {
093      super(ps, produces, accept);
094   }
095
096
097   @Override /* Context */
098   public PlainTextSerializerBuilder builder() {
099      return new PlainTextSerializerBuilder(getPropertyStore());
100   }
101
102   /**
103    * Instantiates a new clean-slate {@link PlainTextSerializerBuilder} object.
104    *
105    * <p>
106    * This is equivalent to simply calling <code><jk>new</jk> PlainTextSerializerBuilder()</code>.
107    *
108    * <p>
109    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
110    * the settings of the object called on.
111    *
112    * @return A new {@link PlainTextSerializerBuilder} object.
113    */
114   public static PlainTextSerializerBuilder create() {
115      return new PlainTextSerializerBuilder();
116   }
117
118   @Override /* Serializer */
119   public WriterSerializerSession createSession(SerializerSessionArgs args) {
120      return new PlainTextSerializerSession(this, args);
121   }
122
123   /**
124    * @deprecated Use {@link SerializerSessionArgs#SerializerSessionArgs(ObjectMap, Method, Locale, TimeZone, MediaType, HttpPartSchema, Boolean, UriContext, Boolean)}
125    */
126   @Deprecated
127   @SuppressWarnings("javadoc")
128   public PlainTextSerializer(PropertyStore ps, String produces, String...accept) {
129      super(ps, produces, StringUtils.join(accept, ','));
130   }
131}