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.dto.swagger;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017import static org.apache.juneau.internal.ConverterUtils.*;
018
019import java.util.*;
020
021import org.apache.juneau.annotation.*;
022import org.apache.juneau.internal.*;
023
024/**
025 * A metadata object that allows for more fine-tuned XML model definitions.
026 *
027 * <p>
028 * When using arrays, XML element names are not inferred (for singular/plural forms) and the name property should be
029 * used to add that information.
030 *
031 * <h5 class='section'>Example:</h5>
032 * <p class='bjava'>
033 *    <jc>// Construct using SwaggerBuilder.</jc>
034 *    Xml <jv>xml</jv> = <jsm>xml</jsm>()
035 *       .name(<js>"foo"</js>)
036 *       .namespace(<js>"http://foo"</js>)
037 *
038 *    <jc>// Serialize using JsonSerializer.</jc>
039 *    String <jv>json</jv> = JsonSerializer.<jsf>DEFAULT</jsf>.toString(<jv>xml</jv>);
040 *
041 *    <jc>// Or just use toString() which does the same as above.</jc>
042 *  <jv>json</jv> = <jv>xml</jv>.toString();
043 * </p>
044 * <p class='bjson'>
045 *    <jc>// Output</jc>
046 *    {
047 *       <js>"name"</js>: <js>"foo"</js>,
048 *       <js>"namespace"</js>: <js>"http://foo"</js>
049 *    }
050 * </p>
051 *
052 * <h5 class='section'>See Also:</h5><ul>
053 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Swagger">Overview &gt; juneau-rest-server &gt; Swagger</a>
054 * </ul>
055 */
056@Bean(properties="name,namespace,prefix,attribute,wrapped,*")
057@FluentSetters
058public class Xml extends SwaggerElement {
059
060   private String
061      name,
062      namespace,
063      prefix;
064   private Boolean
065      attribute,
066      wrapped;
067
068   /**
069    * Default constructor.
070    */
071   public Xml() {}
072
073   /**
074    * Copy constructor.
075    *
076    * @param copyFrom The object to copy.
077    */
078   public Xml(Xml copyFrom) {
079      super(copyFrom);
080
081      this.attribute = copyFrom.attribute;
082      this.name = copyFrom.name;
083      this.namespace = copyFrom.namespace;
084      this.prefix = copyFrom.prefix;
085      this.wrapped = copyFrom.wrapped;
086   }
087
088   /**
089    * Make a deep copy of this object.
090    *
091    * @return A deep copy of this object.
092    */
093   public Xml copy() {
094      return new Xml(this);
095   }
096
097   //-----------------------------------------------------------------------------------------------------------------
098   // Properties
099   //-----------------------------------------------------------------------------------------------------------------
100
101   /**
102    * Bean property getter:  <property>attribute</property>.
103    *
104    * <p>
105    * Declares whether the property definition translates to an attribute instead of an element.
106    *
107    * @return The property value, or <jk>null</jk> if it is not set.
108    */
109   public Boolean getAttribute() {
110      return attribute;
111   }
112
113   /**
114    * Bean property setter:  <property>attribute</property>.
115    *
116    * <p>
117    * Declares whether the property definition translates to an attribute instead of an element.
118    *
119    * @param value
120    *    The new value for this property.
121    *    <br>Default value is <jk>false</jk>.
122    *    <br>Can be <jk>null</jk> to unset the property.
123    * @return This object.
124    */
125   public Xml setAttribute(Boolean value) {
126      attribute = value;
127      return this;
128   }
129
130   /**
131    * Bean property getter:  <property>name</property>.
132    *
133    * <p>
134    * The name of the element/attribute used for the described schema property.
135    *
136    * @return The property value, or <jk>null</jk> if it is not set.
137    */
138   public String getName() {
139      return name;
140   }
141
142   /**
143    * Bean property setter:  <property>name</property>.
144    *
145    * <p>
146    * The name of the element/attribute used for the described schema property.
147    *
148    * @param value
149    *    The new value for this property.
150    *    <br>Can be <jk>null</jk> to unset the property.
151    * @return This object.
152    */
153   public Xml setName(String value) {
154      name = value;
155      return this;
156   }
157
158   /**
159    * Bean property getter:  <property>namespace</property>.
160    *
161    * <p>
162    * The URL of the namespace definition.
163    *
164    * @return The property value, or <jk>null</jk> if it is not set.
165    */
166   public String getNamespace() {
167      return namespace;
168   }
169
170   /**
171    * Bean property setter:  <property>namespace</property>.
172    *
173    * <p>
174    * The URL of the namespace definition.
175    *
176    * @param value
177    *    The new value for this property.
178    *    <br>Can be <jk>null</jk> to unset the property.
179    * @return This object.
180    */
181   public Xml setNamespace(String value) {
182      namespace = value;
183      return this;
184   }
185
186   /**
187    * Bean property getter:  <property>prefix</property>.
188    *
189    * <p>
190    * The prefix to be used for the name.
191    *
192    * @return The property value, or <jk>null</jk> if it is not set.
193    */
194   public String getPrefix() {
195      return prefix;
196   }
197
198   /**
199    * Bean property setter:  <property>prefix</property>.
200    *
201    * <p>
202    * The prefix to be used for the name.
203    *
204    * @param value
205    *    The new value for this property.
206    *    <br>Can be <jk>null</jk> to unset the property.
207    * @return This object.
208    */
209   public Xml setPrefix(String value) {
210      prefix = value;
211      return this;
212   }
213
214   /**
215    * Bean property getter:  <property>wrapped</property>.
216    *
217    * <p>
218    * Signifies whether the array is wrapped (for example,
219    * <c>&lt;books&gt;&lt;book/&gt;&lt;book/&gt;&lt;books&gt;</c>) or unwrapped
220    * (<c>&lt;book/&gt;&lt;book/&gt;</c>).
221    * <br>The definition takes effect only when defined alongside <c>type</c> being <c>array</c>
222    * (outside the <c>items</c>).
223    *
224    * @return The property value, or <jk>null</jk> if it is not set.
225    */
226   public Boolean getWrapped() {
227      return wrapped;
228   }
229
230   /**
231    * Bean property setter:  <property>wrapped</property>.
232    *
233    *
234    * <p>
235    * Signifies whether the array is wrapped (for example,
236    * <c>&lt;books&gt;&lt;book/&gt;&lt;book/&gt;&lt;books&gt;</c>) or unwrapped
237    * (<c>&lt;book/&gt;&lt;book/&gt;</c>).
238    * <br>The definition takes effect only when defined alongside <c>type</c> being <c>array</c>
239    * (outside the <c>items</c>).
240    *
241    * @param value
242    *    The new value for this property.
243    *    <br>Can be <jk>null</jk> to unset the property.
244    * @return This object.
245    */
246   public Xml setWrapped(Boolean value) {
247      this.wrapped = value;
248      return this;
249   }
250
251   // <FluentSetters>
252
253   // </FluentSetters>
254
255   @Override /* SwaggerElement */
256   public <T> T get(String property, Class<T> type) {
257      if (property == null)
258         return null;
259      switch (property) {
260         case "attribute": return toType(getAttribute(), type);
261         case "name": return toType(getName(), type);
262         case "namespace": return toType(getNamespace(), type);
263         case "prefix": return toType(getPrefix(), type);
264         case "wrapped": return toType(getWrapped(), type);
265         default: return super.get(property, type);
266      }
267   }
268
269   @Override /* SwaggerElement */
270   public Xml set(String property, Object value) {
271      if (property == null)
272         return this;
273      switch (property) {
274         case "attribute": return setAttribute(toBoolean(value));
275         case "name": return setName(stringify(value));
276         case "namespace": return setNamespace(stringify(value));
277         case "prefix": return setPrefix(stringify(value));
278         case "wrapped": return setWrapped(toBoolean(value));
279         default:
280            super.set(property, value);
281            return this;
282      }
283   }
284
285   @Override /* SwaggerElement */
286   public Set<String> keySet() {
287      Set<String> s = setBuilder(String.class)
288         .addIf(attribute != null, "attribute")
289         .addIf(name != null, "name")
290         .addIf(namespace != null, "namespace")
291         .addIf(prefix != null, "prefix")
292         .addIf(wrapped != null, "wrapped")
293         .build();
294      return new MultiSet<>(s, super.keySet());
295   }
296}