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