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<?,String>} 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", null); 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 {@doc RFC2616.section14.1} 071 * <p> 072 * If empty, then assumes the only media type supported is <code>produces</code>. 073 * <p> 074 * For example, if this serializer produces <js>"application/json"</js> but should handle media types of 075 * <js>"application/json"</js> and <js>"text/json"</js>, then the arguments should be: 076 * <p class='bcode w800'> 077 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"application/json,text/json"</js>); 078 * </p> 079 * <br>...or... 080 * <p class='bcode w800'> 081 * <jk>super</jk>(ps, <js>"application/json"</js>, <js>"*​/json"</js>); 082 * </p> 083 * <p> 084 * The accept value can also contain q-values. 085 */ 086 public PlainTextSerializer(PropertyStore ps, String produces, String accept) { 087 super(ps, produces, accept); 088 } 089 090 091 @Override /* Context */ 092 public PlainTextSerializerBuilder builder() { 093 return new PlainTextSerializerBuilder(getPropertyStore()); 094 } 095 096 /** 097 * Instantiates a new clean-slate {@link PlainTextSerializerBuilder} object. 098 * 099 * <p> 100 * This is equivalent to simply calling <code><jk>new</jk> PlainTextSerializerBuilder()</code>. 101 * 102 * <p> 103 * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies 104 * the settings of the object called on. 105 * 106 * @return A new {@link PlainTextSerializerBuilder} object. 107 */ 108 public static PlainTextSerializerBuilder create() { 109 return new PlainTextSerializerBuilder(); 110 } 111 112 @Override /* Serializer */ 113 public WriterSerializerSession createSession(SerializerSessionArgs args) { 114 return new PlainTextSerializerSession(this, args); 115 } 116}