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