001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.marshall.plaintext; 018 019 020import java.io.*; 021 022import org.apache.juneau.marshall.serializer.*; 023import org.apache.juneau.marshall.stream.*; 024import static org.apache.juneau.commons.utils.Shorts.*; 025 026/** 027 * Session object that lives for the duration of a single use of {@link PlainTextSerializer}. 028 * 029 * <h5 class='section'>Notes:</h5><ul> 030 * <li class='warn'>This class is not thread safe and is typically discarded after one use. 031 * </ul> 032 * 033 */ 034@SuppressWarnings({ 035 "java:S110", // Inheritance depth acceptable for this class hierarchy 036 "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants 037 "resource" // Resource management handled externally 038}) 039public class PlainTextSerializerSession extends WriterSerializerSession implements RecordWritable { 040 041 /** 042 * Builder class. 043 */ 044 public static class Builder extends WriterSerializerSession.Builder<Builder> { 045 046 /** 047 * Constructor 048 * 049 * @param ctx The context creating this session. 050 * <br>Cannot be <jk>null</jk>. 051 */ 052 protected Builder(PlainTextSerializer ctx) { 053 super(reqnn("ctx", ctx)); 054 } 055 056 @Override 057 public PlainTextSerializerSession build() { 058 return new PlainTextSerializerSession(this); 059 } 060 061 } 062 063 /** 064 * Creates a new builder for this object. 065 * 066 * @param ctx The context creating this session. 067 * <br>Cannot be <jk>null</jk>. 068 * @return A new builder. 069 */ 070 public static Builder create(PlainTextSerializer ctx) { 071 return new Builder(reqnn("ctx", ctx)); 072 } 073 074 /** 075 * Constructor. 076 * 077 * @param builder The builder for this object. 078 */ 079 protected PlainTextSerializerSession(Builder builder) { 080 super(builder); 081 } 082 083 @Override /* RecordWritable */ 084 public RecordWriter writeRecords(Object output) throws IOException { 085 return RecordAdapter.writer(this, output); 086 } 087 088 @Override /* RecordWritable */ 089 public boolean isRecordStreaming() { 090 return false; 091 } 092 093 @Override /* Overridden from SerializerSession */ 094 protected void doWrite(SerializerPipe out, Object o) throws IOException, SerializeException { 095 out.getWriter().write(o == null ? "null" : convertToType(o, String.class)); 096 } 097}