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.xml.annotation;
014
015import static java.lang.annotation.ElementType.*;
016import static java.lang.annotation.RetentionPolicy.*;
017import static org.apache.juneau.internal.ArrayUtils.*;
018
019import java.lang.annotation.*;
020import java.lang.reflect.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.annotation.*;
024import org.apache.juneau.reflect.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Utility classes and methods for the {@link Xml @Xml} annotation.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.XmlDetails">XML Details</a>
032 * </ul>
033 */
034public class XmlAnnotation {
035
036   //-----------------------------------------------------------------------------------------------------------------
037   // Static
038   //-----------------------------------------------------------------------------------------------------------------
039
040   /** Default value */
041   public static final Xml DEFAULT = create().build();
042
043   /**
044    * Instantiates a new builder for this class.
045    *
046    * @return A new builder object.
047    */
048   public static Builder create() {
049      return new Builder();
050   }
051
052   /**
053    * Instantiates a new builder for this class.
054    *
055    * @param on The targets this annotation applies to.
056    * @return A new builder object.
057    */
058   public static Builder create(Class<?>...on) {
059      return create().on(on);
060   }
061
062   /**
063    * Instantiates a new builder for this class.
064    *
065    * @param on The targets this annotation applies to.
066    * @return A new builder object.
067    */
068   public static Builder create(String...on) {
069      return create().on(on);
070   }
071
072   /**
073    * Creates a copy of the specified annotation.
074    *
075    * @param a The annotation to copy.s
076    * @param r The var resolver for resolving any variables.
077    * @return A copy of the specified annotation.
078    */
079   public static Xml copy(Xml a, VarResolverSession r) {
080      return
081         create()
082         .childName(r.resolve(a.childName()))
083         .format(a.format())
084         .namespace(r.resolve(a.namespace()))
085         .on(r.resolve(a.on()))
086         .onClass(a.onClass())
087         .prefix(r.resolve(a.prefix()))
088         .build();
089   }
090
091   //-----------------------------------------------------------------------------------------------------------------
092   // Builder
093   //-----------------------------------------------------------------------------------------------------------------
094
095   /**
096    * Builder class.
097    *
098    * <h5 class='section'>See Also:</h5><ul>
099    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
100    * </ul>
101    */
102   public static class Builder extends TargetedAnnotationTMFBuilder {
103
104      String childName="", namespace="", prefix="";
105      XmlFormat format=XmlFormat.DEFAULT;
106
107      /**
108       * Constructor.
109       */
110      protected Builder() {
111         super(Xml.class);
112      }
113
114      /**
115       * Instantiates a new {@link Xml @Xml} object initialized with this builder.
116       *
117       * @return A new {@link Xml @Xml} object.
118       */
119      public Xml build() {
120         return new Impl(this);
121      }
122
123      /**
124       * Sets the {@link Xml#childName} property on this annotation.
125       *
126       * @param value The new value for this property.
127       * @return This object.
128       */
129      public Builder childName(String value) {
130         this.childName = value;
131         return this;
132      }
133
134      /**
135       * Sets the {@link Xml#format} property on this annotation.
136       *
137       * @param value The new value for this property.
138       * @return This object.
139       */
140      public Builder format(XmlFormat value) {
141         this.format = value;
142         return this;
143      }
144
145      /**
146       * Sets the {@link Xml#namespace} property on this annotation.
147       *
148       * @param value The new value for this property.
149       * @return This object.
150       */
151      public Builder namespace(String value) {
152         this.namespace = value;
153         return this;
154      }
155
156      /**
157       * Sets the {@link Xml#prefix} property on this annotation.
158       *
159       * @param value The new value for this property.
160       * @return This object.
161       */
162      public Builder prefix(String value) {
163         this.prefix = value;
164         return this;
165      }
166
167      // <FluentSetters>
168
169      @Override /* GENERATED - TargetedAnnotationBuilder */
170      public Builder on(String...values) {
171         super.on(values);
172         return this;
173      }
174
175      @Override /* GENERATED - TargetedAnnotationTBuilder */
176      public Builder on(java.lang.Class<?>...value) {
177         super.on(value);
178         return this;
179      }
180
181      @Override /* GENERATED - TargetedAnnotationTBuilder */
182      public Builder onClass(java.lang.Class<?>...value) {
183         super.onClass(value);
184         return this;
185      }
186
187      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
188      public Builder on(Field...value) {
189         super.on(value);
190         return this;
191      }
192
193      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
194      public Builder on(Method...value) {
195         super.on(value);
196         return this;
197      }
198
199      // </FluentSetters>
200   }
201
202   //-----------------------------------------------------------------------------------------------------------------
203   // Implementation
204   //-----------------------------------------------------------------------------------------------------------------
205
206   private static class Impl extends TargetedAnnotationTImpl implements Xml {
207
208      private final String childName, namespace, prefix;
209      private final XmlFormat format;
210
211      Impl(Builder b) {
212         super(b);
213         this.childName = b.childName;
214         this.format = b.format;
215         this.namespace = b.namespace;
216         this.prefix = b.prefix;
217         postConstruct();
218      }
219
220      @Override /* Xml */
221      public String childName() {
222         return childName;
223      }
224
225      @Override /* Xml */
226      public XmlFormat format() {
227         return format;
228      }
229
230      @Override /* Xml */
231      public String namespace() {
232         return namespace;
233      }
234
235      @Override /* Xml */
236      public String prefix() {
237         return prefix;
238      }
239   }
240
241   //-----------------------------------------------------------------------------------------------------------------
242   // Appliers
243   //-----------------------------------------------------------------------------------------------------------------
244
245   /**
246    * Applies targeted {@link Xml} annotations to a {@link org.apache.juneau.Context.Builder}.
247    */
248   public static class Apply extends AnnotationApplier<Xml,Context.Builder> {
249
250      /**
251       * Constructor.
252       *
253       * @param vr The resolver for resolving values in annotations.
254       */
255      public Apply(VarResolverSession vr) {
256         super(Xml.class, Context.Builder.class, vr);
257      }
258
259      @Override
260      public void apply(AnnotationInfo<Xml> ai, Context.Builder b) {
261         Xml a = ai.inner();
262         if (isEmptyArray(a.on(), a.onClass()))
263            return;
264         b.annotations(copy(a, vr()));
265      }
266   }
267
268   //-----------------------------------------------------------------------------------------------------------------
269   // Other
270   //-----------------------------------------------------------------------------------------------------------------
271
272   /**
273    * A collection of {@link Xml @Xml annotations}.
274    */
275   @Documented
276   @Target({METHOD,TYPE})
277   @Retention(RUNTIME)
278   @Inherited
279   public static @interface Array {
280
281      /**
282       * The child annotations.
283       *
284       * @return The annotation value.
285       */
286      Xml[] value();
287   }
288}