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