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.xml;
014
015/**
016 * Serializes POJOs to HTTP responses as XML.
017 *
018 * <h5 class='topic'>Media types</h5>
019 *
020 * Handles <c>Accept</c> types:  <bc>text/xml</bc>
021 * <p>
022 * Produces <c>Content-Type</c> types:  <bc>text/xml</bc>
023 *
024 * <h5 class='topic'>Description</h5>
025 *
026 * Same as {@link XmlSerializer}, except prepends <code><xt>&lt;?xml</xt> <xa>version</xa>=<xs>'1.0'</xs>
027 * <xa>encoding</xa>=<xs>'UTF-8'</xs><xt>?&gt;</xt></code> to the response to make it a valid XML document.
028 *
029 * <h5 class='section'>Notes:</h5><ul>
030 *    <li class='note'>This class is thread safe and reusable.
031 * </ul>
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.XmlDetails">XML Details</a>
035 * </ul>
036 */
037public class XmlDocSerializer extends XmlSerializer {
038
039   //-------------------------------------------------------------------------------------------------------------------
040   // Static
041   //-------------------------------------------------------------------------------------------------------------------
042
043   /**
044    * Creates a new builder for this object.
045    *
046    * @return A new builder.
047    */
048   public static Builder create() {
049      return new Builder().type(XmlDocSerializer.class);
050   }
051
052   //-------------------------------------------------------------------------------------------------------------------
053   // Static subclasses
054   //-------------------------------------------------------------------------------------------------------------------
055
056   /** Default serializer without namespaces. */
057   public static class Ns extends XmlDocSerializer {
058
059      /**
060       * Constructor.
061       *
062       * @param builder The builder for this object.
063       */
064      public Ns(XmlSerializer.Builder builder) {
065         super(builder.enableNamespaces());
066      }
067   }
068
069   //-------------------------------------------------------------------------------------------------------------------
070   // Instance
071   //-------------------------------------------------------------------------------------------------------------------
072
073   /**
074    * Constructor.
075    *
076    * @param builder The builder for this object.
077    */
078   public XmlDocSerializer(XmlSerializer.Builder builder) {
079      super(builder);
080   }
081
082   @Override /* Context */
083   public Builder copy() {
084      return new Builder(this);
085   }
086
087   @Override /* Context */
088   public XmlDocSerializerSession.Builder createSession() {
089      return XmlDocSerializerSession.create(this);
090   }
091
092   @Override /* Context */
093   public XmlDocSerializerSession getSession() {
094      return createSession().build();
095   }
096}