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.xml; 018 019 020import org.apache.juneau.marshall.serializer.*; 021import static org.apache.juneau.commons.utils.Shorts.*; 022 023/** 024 * Session object that lives for the duration of a single use of {@link XmlDocSerializer}. 025 * 026 * <h5 class='section'>Notes:</h5><ul> 027 * <li class='warn'>This class is not thread safe and is typically discarded after one use. 028 * </ul> 029 * 030 * <h5 class='section'>See Also:</h5><ul> 031 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/XmlSupport">XML</a> 032 * </ul> 033 */ 034@SuppressWarnings({ 035 "java:S110", // Session classes inherit many parameters from base 036 "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants 037 "resource" // Writer managed by SerializerPipe; caller closes 038}) 039public class XmlDocSerializerSession extends XmlSerializerSession { 040 041 /** 042 * Builder class. 043 */ 044 public static class Builder extends XmlSerializerSession.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(XmlDocSerializer ctx) { 053 super(reqnn("ctx", ctx)); 054 } 055 056 @Override 057 public XmlDocSerializerSession build() { 058 return new XmlDocSerializerSession(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(XmlDocSerializer 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 XmlDocSerializerSession(Builder builder) { 080 super(builder); 081 } 082 083 @Override /* Overridden from SerializerSession */ 084 protected void doWrite(SerializerPipe out, Object o) throws java.io.IOException, SerializeException { 085 try (var w = getXmlWriter(out)) { 086 w.append("<?xml").attr("version", "1.0").attr("encoding", "UTF-8").appendln("?>"); 087 w.flush(); 088 super.doWrite(out, o); 089 } 090 } 091}