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.internal.ArrayUtils.*; 022 023import java.lang.annotation.*; 024 025import org.apache.juneau.*; 026import org.apache.juneau.annotation.*; 027import org.apache.juneau.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 //----------------------------------------------------------------------------------------------------------------- 040 // Static 041 //----------------------------------------------------------------------------------------------------------------- 042 043 /** Default value */ 044 public static final HtmlLink DEFAULT = create().build(); 045 046 /** 047 * Instantiates a new builder for this class. 048 * 049 * @return A new builder object. 050 */ 051 public static Builder create() { 052 return new Builder(); 053 } 054 055 /** 056 * Instantiates a new builder for this class. 057 * 058 * @param on The targets this annotation applies to. 059 * @return A new builder object. 060 */ 061 public static Builder create(Class<?>...on) { 062 return create().on(on); 063 } 064 065 /** 066 * Instantiates a new builder for this class. 067 * 068 * @param on The targets this annotation applies to. 069 * @return A new builder object. 070 */ 071 public static Builder create(String...on) { 072 return create().on(on); 073 } 074 075 /** 076 * Creates a copy of the specified annotation. 077 * 078 * @param a The annotation to copy.s 079 * @param r The var resolver for resolving any variables. 080 * @return A copy of the specified annotation. 081 */ 082 public static HtmlLink copy(HtmlLink a, VarResolverSession r) { 083 return 084 create() 085 .nameProperty(r.resolve(a.nameProperty())) 086 .on(r.resolve(a.on())) 087 .onClass(a.onClass()) 088 .uriProperty(r.resolve(a.uriProperty())) 089 .build(); 090 } 091 092 //----------------------------------------------------------------------------------------------------------------- 093 // Builder 094 //----------------------------------------------------------------------------------------------------------------- 095 096 /** 097 * Builder class. 098 * 099 * <h5 class='section'>See Also:</h5><ul> 100 * <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)} 101 * </ul> 102 */ 103 public static class Builder extends TargetedAnnotationTBuilder<Builder> { 104 105 String nameProperty="", uriProperty=""; 106 107 /** 108 * Constructor. 109 */ 110 protected Builder() { 111 super(HtmlLink.class); 112 } 113 114 /** 115 * Instantiates a new {@link HtmlLink @HtmlLink} object initialized with this builder. 116 * 117 * @return A new {@link HtmlLink @HtmlLink} object. 118 */ 119 public HtmlLink build() { 120 return new Impl(this); 121 } 122 123 /** 124 * Sets the {@link HtmlLink#nameProperty()} property on this annotation. 125 * 126 * @param value The new value for this property. 127 * @return This object. 128 */ 129 public Builder nameProperty(String value) { 130 this.nameProperty = value; 131 return this; 132 } 133 134 /** 135 * Sets the {@link HtmlLink#uriProperty()} property on this annotation. 136 * 137 * @param value The new value for this property. 138 * @return This object. 139 */ 140 public Builder uriProperty(String value) { 141 this.uriProperty = value; 142 return this; 143 } 144 145 } 146 147 //----------------------------------------------------------------------------------------------------------------- 148 // Implementation 149 //----------------------------------------------------------------------------------------------------------------- 150 151 private static class Impl extends TargetedAnnotationTImpl implements HtmlLink { 152 153 private final String nameProperty, uriProperty; 154 155 Impl(Builder b) { 156 super(b); 157 this.nameProperty = b.nameProperty; 158 this.uriProperty = b.uriProperty; 159 postConstruct(); 160 } 161 162 @Override /* HtmlLink */ 163 public String nameProperty() { 164 return nameProperty; 165 } 166 167 @Override /* HtmlLink */ 168 public String uriProperty() { 169 return uriProperty; 170 } 171 } 172 173 //----------------------------------------------------------------------------------------------------------------- 174 // Appliers 175 //----------------------------------------------------------------------------------------------------------------- 176 177 /** 178 * Applies targeted {@link HtmlLink} annotations to a {@link org.apache.juneau.Context.Builder}. 179 */ 180 public static class Apply extends AnnotationApplier<HtmlLink,Context.Builder> { 181 182 /** 183 * Constructor. 184 * 185 * @param vr The resolver for resolving values in annotations. 186 */ 187 public Apply(VarResolverSession vr) { 188 super(HtmlLink.class, Context.Builder.class, vr); 189 } 190 191 @Override 192 public void apply(AnnotationInfo<HtmlLink> ai, Context.Builder b) { 193 HtmlLink a = ai.inner(); 194 if (isEmptyArray(a.on(), a.onClass())) 195 return; 196 b.annotations(copy(a, vr())); 197 } 198 } 199 200 //----------------------------------------------------------------------------------------------------------------- 201 // Other 202 //----------------------------------------------------------------------------------------------------------------- 203 204 /** 205 * A collection of {@link HtmlLink @HtmlLink annotations}. 206 */ 207 @Documented 208 @Target({METHOD,TYPE}) 209 @Retention(RUNTIME) 210 @Inherited 211 public static @interface Array { 212 213 /** 214 * The child annotations. 215 * 216 * @return The annotation value. 217 */ 218 HtmlLink[] value(); 219 } 220}