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/** 020 * XML format to use when serializing a POJO. 021 * 022 * <h5 class='section'>See Also:</h5><ul> 023 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/XmlSupport">XML</a> 024 025 * </ul> 026 */ 027public enum XmlFormat { 028 029 /** 030 * Normal formatting (default). 031 * 032 * <p> 033 * On a bean class, implies {@link #FREEFORM} meaning bean properties will be serialized as child elements by default. 034 * 035 * <p> 036 * On a bean property, implies {@link #ELEMENT} meaning the bean property will be serialized as a child element. 037 */ 038 DEFAULT, 039 040 /** 041 * Render a bean property as an attribute instead of an element. 042 * 043 * <p> 044 * Only applicable for bean properties, not bean classes. 045 * 046 * <p> 047 * Can only be applied to properties (methods/fields) of class types that can be convertible to <c>Strings</c>. 048 */ 049 ATTR, 050 051 /** 052 * Render property as attributes instead of an element. 053 * 054 * <p> 055 * On a bean class, implies bean properties will be serialized as attributes instead of child elements by default. 056 * 057 * <p> 058 * On bean properties, implies that the bean property value itself should be serialized as attributes on the bean 059 * element. 060 * The bean property data type must be of class type <c>Map<Object,Object></c> where both 061 * objects are convertible to <c>Strings</c>. 062 */ 063 ATTRS, 064 065 /** 066 * Render property as an element instead of an attribute. 067 * 068 * <p> 069 * Only applicable for bean properties, not bean classes. 070 * 071 * <p> 072 * Used to override the behavior of the {@link #ATTRS} format applied to the bean class. 073 */ 074 ELEMENT, 075 076 /** 077 * Render property value directly as the contents of the element. 078 * 079 * <p> 080 * On a bean class, implies that bean properties will be serialized as child elements. 081 * Note that this is equivalent to {@link #DEFAULT}. 082 * 083 * <p> 084 * Only applicable for objects of type array/Collection. 085 * 086 * <p> 087 * On a bean property, implies that the bean property value itself should be serialized as child elements of the 088 * bean element. 089 */ 090 FREEFORM, 091 092 /** 093 * Same as {@link #FREEFORM} except primitive types (string/boolean/number/null for example) are not wrapped in elements. 094 * 095 * <p> 096 * Only applicable for bean properties, not bean classes. 097 * 098 * <p> 099 * Only applicable for objects of type array/Collection. 100 * 101 * <p> 102 * Use of this format may cause data type loss during parsing if the types cannot be inferred through reflection. 103 */ 104 MIXED, 105 106 /** 107 * Same as {@link XmlFormat#MIXED}, but whitespace in text nodes are not trimmed during parsing. 108 * 109 * <p> 110 * An example use is HTML5 <xt><pre></xt> where whitespace should not be discarded. 111 */ 112 MIXED_PWS, 113 114 /** 115 * Render property value as the text content of the element. 116 * 117 * <p> 118 * Similar to {@link #MIXED} but value must be a single value, not a collection. 119 * 120 * <p> 121 * Only applicable for bean properties, not bean classes. 122 * 123 * <p> 124 * Use of this format may cause data type loss during parsing if the type cannot be inferred through reflection. 125 */ 126 TEXT, 127 128 /** 129 * Same as {@link XmlFormat#TEXT}, but whitespace in text node is not trimmed during parsing. 130 */ 131 TEXT_PWS, 132 133 /** 134 * Same as {@link #TEXT} except the content is expected to be fully-formed XML that will get serialized as-is. 135 * 136 * <p> 137 * During parsing, this XML text will be re-serialized and set on the property. 138 * 139 * <p> 140 * Only applicable for bean properties, not bean classes. 141 * 142 * <p> 143 * Use of this format may cause data type loss during parsing if the type cannot be inferred through reflection. 144 */ 145 XMLTEXT, 146 147 /** 148 * Render property value as the raw, verbatim text content of the element. 149 * 150 * <p> 151 * Similar to {@link #TEXT_PWS} (single, whitespace-preserving value), but the value is emitted with NO XML entity 152 * escaping (no <c>&amp;</c>/<c>&lt;</c>/<c>&gt;</c> substitution) and NO <c>_x####_</c> 153 * whitespace/invalid-char encoding. 154 * This matches HTML5 "raw text" element semantics (<xt><script></xt>/<xt><style></xt>) where browsers do 155 * not decode character references. 156 * 157 * <p> 158 * Unlike {@link #XMLTEXT}, the content is NOT re-parsed as XML on parse; it is read back verbatim as a text node 159 * (no <c>_x####_</c> decoding, no trimming). 160 * 161 * <p> 162 * Only applicable for scalar (non-collection) bean properties. 163 * 164 * <p> 165 * <b>Round-trip caveat:</b> because Juneau parses via a StAX {@link javax.xml.stream.XMLStreamReader}, a value 166 * containing a raw <js>'<'</js> or a bare <js>'&'</js> will NOT be parseable back. 167 * Callers embedding markup-bearing content are responsible for escaping <js>'<'</js> (e.g. to the JS/CSS unicode 168 * escape <js>"\\u003c"</js>) before use, exactly as required for browser-safe raw-text content. 169 */ 170 RAWTEXT, 171 172 /** 173 * Prevents collections and arrays from being enclosed in <xt><array></xt> elements. 174 * 175 * <p> 176 * Can only be applied to properties (methods/fields) of type collection or array, or collection classes. 177 */ 178 COLLAPSED, 179 180 /** 181 * Identifies a void element. 182 * 183 * <p> 184 * Only applicable for bean classes. 185 * 186 * <p> 187 * Identifies an element that never contains content. 188 * 189 * <p> 190 * The main difference in behavior is how non-void empty elements are handled in the HTML serializer. 191 * Void elements are serialized as collapsed nodes (e.g. <js>"<br/>"</js>) whereas non-void empty elements are 192 * serialized with an end tag (e.g. "<p></p>"). 193 */ 194 VOID; 195 196 /** 197 * Returns <jk>true</jk> if this format is one of those specified. 198 * 199 * @param formats The formats to match against. 200 * @return <jk>true</jk> if this format is one of those specified. 201 */ 202 public boolean isOneOf(XmlFormat...formats) { 203 for (var format : formats) 204 if (format == this) 205 return true; 206 return false; 207 } 208}