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.html.annotation;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021import static org.apache.juneau.commons.utils.CollectionUtils.*;
022
023import java.lang.annotation.*;
024
025import org.apache.juneau.*;
026import org.apache.juneau.commons.annotation.*;
027import org.apache.juneau.commons.reflect.*;
028import org.apache.juneau.svl.*;
029
030/**
031 * Utility classes and methods for the {@link HtmlLink @HtmlLink} annotation.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HtmlBasics">HTML Basics</a>
035 * </ul>
036 */
037public class HtmlLinkAnnotation {
038   /**
039    * Applies targeted {@link HtmlLink} annotations to a {@link org.apache.juneau.Context.Builder}.
040    */
041   public static class Apply extends AnnotationApplier<HtmlLink,Context.Builder> {
042
043      /**
044       * Constructor.
045       *
046       * @param vr The resolver for resolving values in annotations.
047       */
048      public Apply(VarResolverSession vr) {
049         super(HtmlLink.class, Context.Builder.class, vr);
050      }
051
052      @Override
053      public void apply(AnnotationInfo<HtmlLink> ai, Context.Builder b) {
054         HtmlLink a = ai.inner();
055         if (isEmptyArray(a.on()) && isEmptyArray(a.onClass()))
056            return;
057         b.annotations(copy(a, vr()));
058      }
059   }
060
061   /**
062    * A collection of {@link HtmlLink @HtmlLink annotations}.
063    */
064   @Documented
065   @Target({ METHOD, TYPE })
066   @Retention(RUNTIME)
067   @Inherited
068   public static @interface Array {
069
070      /**
071       * The child annotations.
072       *
073       * @return The annotation value.
074       */
075      HtmlLink[] value();
076   }
077
078   /**
079    * Builder class.
080    *
081    * <h5 class='section'>See Also:</h5><ul>
082    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
083    * </ul>
084    */
085   public static class Builder extends AppliedAnnotationObject.BuilderT {
086
087      private String[] description = {};
088      private String nameProperty = "", uriProperty = "";
089
090      /**
091       * Constructor.
092       */
093      protected Builder() {
094         super(HtmlLink.class);
095      }
096
097      /**
098       * Instantiates a new {@link HtmlLink @HtmlLink} object initialized with this builder.
099       *
100       * @return A new {@link HtmlLink @HtmlLink} object.
101       */
102      public HtmlLink build() {
103         return new Object(this);
104      }
105
106      /**
107       * Sets the description property on this annotation.
108       *
109       * @param value The new value for this property.
110       * @return This object.
111       */
112      public Builder description(String...value) {
113         description = value;
114         return this;
115      }
116
117      /**
118       * Sets the {@link HtmlLink#nameProperty()} property on this annotation.
119       *
120       * @param value The new value for this property.
121       * @return This object.
122       */
123      public Builder nameProperty(String value) {
124         nameProperty = value;
125         return this;
126      }
127
128      /**
129       * Sets the {@link HtmlLink#uriProperty()} property on this annotation.
130       *
131       * @param value The new value for this property.
132       * @return This object.
133       */
134      public Builder uriProperty(String value) {
135         uriProperty = value;
136         return this;
137      }
138
139      @Override /* Overridden from AppliedAnnotationObject.Builder */
140      public Builder on(String...value) {
141         super.on(value);
142         return this;
143      }
144
145      @Override /* Overridden from AppliedAnnotationObject.BuilderT */
146      public Builder on(Class<?>...value) {
147         super.on(value);
148         return this;
149      }
150
151      @Override /* Overridden from AppliedOnClassAnnotationObject.Builder */
152      public Builder onClass(Class<?>...value) {
153         super.onClass(value);
154         return this;
155      }
156
157      @Override /* Overridden from AppliedAnnotationObject.BuilderT */
158      public Builder on(ClassInfo...value) {
159         super.on(value);
160         return this;
161      }
162
163      @Override /* Overridden from AppliedAnnotationObject.BuilderT */
164      public Builder onClass(ClassInfo...value) {
165         super.onClass(value);
166         return this;
167      }
168
169   }
170
171   private static class Object extends AppliedOnClassAnnotationObject implements HtmlLink {
172
173      private final String[] description;
174      private final String nameProperty, uriProperty;
175
176      Object(HtmlLinkAnnotation.Builder b) {
177         super(b);
178         description = copyOf(b.description);
179         nameProperty = b.nameProperty;
180         uriProperty = b.uriProperty;
181      }
182
183      @Override /* Overridden from HtmlLink */
184      public String nameProperty() {
185         return nameProperty;
186      }
187
188      @Override /* Overridden from HtmlLink */
189      public String uriProperty() {
190         return uriProperty;
191      }
192
193      @Override /* Overridden from annotation */
194      public String[] description() {
195         return description;
196      }
197   }
198
199   /** Default value */
200   public static final HtmlLink DEFAULT = create().build();
201
202   /**
203    * Creates a copy of the specified annotation.
204    *
205    * @param a The annotation to copy.s
206    * @param r The var resolver for resolving any variables.
207    * @return A copy of the specified annotation.
208    */
209   public static HtmlLink copy(HtmlLink a, VarResolverSession r) {
210      // @formatter:off
211      return
212         create()
213         .nameProperty(r.resolve(a.nameProperty()))
214         .on(r.resolve(a.on()))
215         .onClass(a.onClass())
216         .uriProperty(r.resolve(a.uriProperty()))
217         .build();
218      // @formatter:on
219   }
220
221   /**
222    * Instantiates a new builder for this class.
223    *
224    * @return A new builder object.
225    */
226   public static Builder create() {
227      return new Builder();
228   }
229
230   /**
231    * Instantiates a new builder for this class.
232    *
233    * @param on The targets this annotation applies to.
234    * @return A new builder object.
235    */
236   public static Builder create(Class<?>...on) {
237      return create().on(on);
238   }
239
240   /**
241    * Instantiates a new builder for this class.
242    *
243    * @param on The targets this annotation applies to.
244    * @return A new builder object.
245    */
246   public static Builder create(String...on) {
247      return create().on(on);
248   }
249}