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.serializer; 014 015import static org.apache.juneau.serializer.WriterSerializer.*; 016 017import java.io.*; 018import java.nio.charset.*; 019 020import org.apache.juneau.*; 021 022/** 023 * Subclass of {@link SerializerSession} for character-based serializers. 024 * 025 * <h5 class='topic'>Description</h5> 026 * 027 * This class is typically the parent class of all character-based serializers. 028 * <br>It has 1 abstract method to implement... 029 * <ul class='spaced-list'> 030 * <li> 031 * {@link #doSerialize(SerializerPipe, Object)} 032 * </ul> 033 * 034 * <p> 035 * This class is NOT thread safe. 036 * It is typically discarded after one-time use although it can be reused within the same thread. 037 */ 038public abstract class WriterSerializerSession extends SerializerSession { 039 040 private final WriterSerializer ctx; 041 private final boolean useWhitespace; 042 private final Charset streamCharset, fileCharset; 043 044 /** 045 * Create a new session using properties specified in the context. 046 * 047 * @param ctx 048 * The context creating this session object. 049 * The context contains all the configuration settings for this object. 050 * @param args 051 * Runtime arguments. 052 * These specify session-level information such as locale and URI context. 053 * It also include session-level properties that override the properties defined on the bean and 054 * serializer contexts. 055 */ 056 protected WriterSerializerSession(WriterSerializer ctx, SerializerSessionArgs args) { 057 super(ctx, args); 058 this.ctx = ctx; 059 this.streamCharset = getProperty(WSERIALIZER_streamCharset, Charset.class, ctx.getStreamCharset()); 060 this.fileCharset = getProperty(WSERIALIZER_fileCharset, Charset.class, ctx.getFileCharset()); 061 this.useWhitespace = getProperty(WSERIALIZER_useWhitespace, Boolean.class, ctx.isUseWhitespace()); 062 } 063 064 /** 065 * Constructor for sessions that don't require context. 066 * 067 * @param args 068 * Runtime session arguments. 069 */ 070 protected WriterSerializerSession(SerializerSessionArgs args) { 071 this(WriterSerializer.DEFAULT, args); 072 } 073 074 @Override /* SerializerSession */ 075 public final boolean isWriterSerializer() { 076 return true; 077 } 078 079 @Override /* SerializerSession */ 080 protected SerializerPipe createPipe(Object output) { 081 return new SerializerPipe(output, streamCharset, fileCharset); 082 } 083 084 /** 085 * Convenience method for serializing an object to a <c>String</c>. 086 * 087 * @param o The object to serialize. 088 * @return The output serialized to a string. 089 * @throws SerializeException If a problem occurred trying to convert the output. 090 */ 091 @Override /* SerializerSession */ 092 public final String serialize(Object o) throws SerializeException { 093 StringWriter w = new StringWriter(); 094 try { 095 serialize(o, w); 096 } catch (IOException e) { 097 throw new SerializeException(e); // Shouldn't happen. 098 } 099 return w.toString(); 100 } 101 102 @Override /* SerializerSession */ 103 public final String serializeToString(Object o) throws SerializeException { 104 return serialize(o); 105 } 106 107 //----------------------------------------------------------------------------------------------------------------- 108 // Properties 109 //----------------------------------------------------------------------------------------------------------------- 110 111 /** 112 * Configuration property: Maximum indentation. 113 * 114 * @see WriterSerializer#WSERIALIZER_maxIndent 115 * @return 116 * The maximum indentation level in the serialized document. 117 */ 118 protected final int getMaxIndent() { 119 return ctx.getMaxIndent(); 120 } 121 122 /** 123 * Configuration property: Quote character. 124 * 125 * @see WriterSerializer#WSERIALIZER_quoteChar 126 * @return 127 * The character used for quoting attributes and values. 128 */ 129 protected final char getQuoteChar() { 130 return ctx.getQuoteChar(); 131 } 132 133 /** 134 * Configuration property: Use whitespace. 135 * 136 * @see WriterSerializer#WSERIALIZER_useWhitespace 137 * @return 138 * The character used for quoting attributes and values. 139 */ 140 protected final boolean isUseWhitespace() { 141 return useWhitespace; 142 } 143 144 //----------------------------------------------------------------------------------------------------------------- 145 // Other methods 146 //----------------------------------------------------------------------------------------------------------------- 147 148 @Override /* Session */ 149 public ObjectMap toMap() { 150 return super.toMap() 151 .append("WriterSerializerSession", new DefaultFilteringObjectMap() 152 ); 153 } 154}