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