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.xml.annotation;
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/XmlBasics">XML Basics</a>
024
025 * </ul>
026 */
027public enum XmlFormat {
028
029   /**
030    * Normal formatting (default).
031    *
032    * <p>
033    * On a bean class, implies {@link #ELEMENTS} 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&lt;Object,Object&gt;</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   ELEMENTS,
091
092   /**
093    * Same as {@link #ELEMENTS} 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>&lt;pre&gt;</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    * Prevents collections and arrays from being enclosed in <xt>&lt;array&gt;</xt> elements.
149    *
150    * <p>
151    * Can only be applied to properties (methods/fields) of type collection or array, or collection classes.
152    */
153   COLLAPSED,
154
155   /**
156    * Identifies a void element.
157    *
158    * <p>
159    * Only applicable for bean classes.
160    *
161    * <p>
162    * Identifies an element that never contains content.
163    *
164    * <p>
165    * The main difference in behavior is how non-void empty elements are handled in the HTML serializer.
166    * Void elements are serialized as collapsed nodes (e.g. <js>"&lt;br/&gt;"</js>) whereas non-void empty elements are
167    * serialized with an end tag (e.g. "&lt;p&gt;&lt;/p&gt;").
168    */
169   VOID;
170
171   /**
172    * Returns <jk>true</jk> if this format is one of those specified.
173    *
174    * @param formats The formats to match against.
175    * @return <jk>true</jk> if this format is one of those specified.
176    */
177   public boolean isOneOf(XmlFormat...formats) {
178      for (XmlFormat format : formats)
179         if (format == this)
180            return true;
181      return false;
182   }
183}