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