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 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021/** 022 * Serializes POJOs to HTTP responses as XML. 023 * 024 * <h5 class='topic'>Media types</h5> 025 * 026 * Handles <c>Accept</c> types: <bc>text/xml</bc> 027 * <p> 028 * Produces <c>Content-Type</c> types: <bc>text/xml</bc> 029 * 030 * <h5 class='topic'>Description</h5> 031 * 032 * Same as {@link XmlSerializer}, except prepends <code><xt><?xml</xt> <xa>version</xa>=<xs>'1.0'</xs> 033 * <xa>encoding</xa>=<xs>'UTF-8'</xs><xt>?></xt></code> to the response to make it a valid XML document. 034 * 035 * <h5 class='section'>Notes:</h5><ul> 036 * <li class='note'>This class is thread safe and reusable. 037 * </ul> 038 * 039 * <h5 class='section'>See Also:</h5><ul> 040 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/XmlSupport">XML</a> 041 * </ul> 042 */ 043@SuppressWarnings({ 044 "java:S110", // Inheritance depth acceptable for this class hierarchy 045 "java:S1192", // Duplicated literals (argument/property names) read more clearly inline than as constants 046 "java:S9149" // Per-format static factories intentionally shadow the parent's. 047}) 048public class XmlDocSerializer extends XmlSerializer { 049 050 /** Default serializer without namespaces. */ 051 public static class Ns extends XmlDocSerializer { 052 053 /** 054 * Constructor. 055 * 056 * @param builder The builder for this object. 057 * <br>Cannot be <jk>null</jk>. 058 */ 059 public Ns(XmlSerializer.Builder<?> builder) { 060 super(reqnn("builder", builder).enableNamespaces()); 061 } 062 } 063 064 /** 065 * Creates a new builder for this object. 066 * 067 * @return A new builder. 068 */ 069 @SuppressWarnings({ 070 "java:S1452" // Builder<?> wildcard return intentional; callers chain via fluent API without needing the concrete type 071 }) 072 public static Builder<?> create() { 073 return new DefaultBuilder().type(XmlDocSerializer.class); 074 } 075 076 /** 077 * Constructor. 078 * 079 * @param builder The builder for this object. 080 * <br>Cannot be <jk>null</jk>. 081 */ 082 public XmlDocSerializer(XmlSerializer.Builder<?> builder) { 083 super(reqnn("builder", builder)); 084 } 085 086 @Override /* Overridden from Context */ 087 public Builder<?> copy() { 088 return new DefaultBuilder(this); 089 } 090 091 @Override /* Overridden from Context */ 092 public XmlDocSerializerSession.Builder createSession() { 093 return XmlDocSerializerSession.create(this); 094 } 095 096 @Override /* Overridden from Context */ 097 public XmlDocSerializerSession getSession() { return createSession().build(); } 098}