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