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.bean.html5; 018 019import java.net.*; 020 021import org.apache.juneau.marshall.*; 022 023/** 024 * DTO for an HTML <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#the-link-element"><link></a> 025 * element. 026 * 027 * <p> 028 * The link element specifies relationships between the current document and an external resource. 029 * It is most commonly used to link to stylesheets, but can also be used to establish site icons, 030 * prefetch resources, define alternate versions of the document, and more. Link elements are typically 031 * placed in the head section of an HTML document. 032 * 033 * <h5 class='section'>Examples:</h5> 034 * <p class='bcode w800'> 035 * <jc>// Stylesheet link</jc> 036 * Link <jv>stylesheet</jv> = <jsm>link</jsm>() 037 * .rel(<js>"stylesheet"</js>) 038 * .href(<js>"/css/styles.css"</js>); 039 * 040 * <jc>// Favicon link</jc> 041 * Link <jv>favicon</jv> = <jsm>link</jsm>() 042 * .rel(<js>"icon"</js>) 043 * .type(<js>"image/x-icon"</js>) 044 * .href(<js>"/favicon.ico"</js>); 045 * 046 * <jc>// Preload resource</jc> 047 * Link <jv>preload</jv> = <jsm>link</jsm>() 048 * .rel(<js>"preload"</js>) 049 * .href(<js>"/fonts/myfont.woff2"</js>) 050 * .as_(<js>"font"</js>) 051 * .type(<js>"font/woff2"</js>) 052 * .crossorigin(<js>"anonymous"</js>); 053 * 054 * <jc>// Alternate language version</jc> 055 * Link <jv>alternate</jv> = <jsm>link</jsm>() 056 * .rel(<js>"alternate"</js>) 057 * .href(<js>"/es/page.html"</js>) 058 * .hreflang(<js>"es"</js>); 059 * 060 * <jc>// Responsive stylesheet with media query</jc> 061 * Link <jv>print</jv> = <jsm>link</jsm>() 062 * .rel(<js>"stylesheet"</js>) 063 * .href(<js>"/css/print.css"</js>) 064 * .media(<js>"print"</js>); 065 * 066 * <jc>// Canonical URL for SEO</jc> 067 * Link <jv>canonical</jv> = <jsm>link</jsm>() 068 * .rel(<js>"canonical"</js>) 069 * .href(<js>"https://example.com/page"</js>); 070 * 071 * <jc>// DNS prefetch for performance</jc> 072 * Link <jv>dnsPrefetch</jv> = <jsm>link</jsm>() 073 * .rel(<js>"dns-prefetch"</js>) 074 * .href(<js>"https://cdn.example.com"</js>); 075 * </p> 076 * 077 * <p> 078 * The following convenience methods are provided for constructing instances of this bean: 079 * <ul class='javatree'> 080 * <li class='jc'>{@link HtmlBuilder} 081 * <ul class='javatree'> 082 * <li class='jm'>{@link HtmlBuilder#link() link()} 083 * </ul> 084 * </ul> 085 * </p> 086 * 087 * <h5 class='section'>See Also:</h5><ul> 088 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanHtml5">juneau-bean-html5</a> 089 * </ul> 090 */ 091@Marshalled(typeName = "link") 092public class Link extends HtmlElementVoid<Link> { 093 094 /** 095 * Creates an empty {@link Link} element. 096 */ 097 public Link() {} 098 099 /** 100 * Creates a {@link Link} element with the specified {@link Link#href(Object)} attribute. 101 * 102 * @param href The {@link Link#href(Object)} attribute. Can be <jk>null</jk>, in which case the attribute is stored with a <jk>null</jk> value (not removed). 103 */ 104 public Link(Object href) { 105 href(href); 106 } 107 108 /** 109 * <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#attr-link-as">as</a> attribute. 110 * 111 * <p> 112 * Specifies the type of content being loaded by a {@code rel="preload"} or {@code rel="modulepreload"} link. 113 * This allows the browser to prioritize resource loading appropriately. 114 * 115 * <p> 116 * Common values: 117 * <ul> 118 * <li><js>"audio"</js> - Audio file</li> 119 * <li><js>"document"</js> - HTML document</li> 120 * <li><js>"fetch"</js> - Resource fetched by fetch or XHR</li> 121 * <li><js>"font"</js> - Font file</li> 122 * <li><js>"image"</js> - Image file</li> 123 * <li><js>"script"</js> - JavaScript file</li> 124 * <li><js>"style"</js> - CSS stylesheet</li> 125 * <li><js>"video"</js> - Video file</li> 126 * <li><js>"worker"</js> - Web worker script</li> 127 * </ul> 128 * 129 * @param value The type of content being loaded. Can be <jk>null</jk> to unset the attribute. 130 * @return This object. 131 */ 132 @SuppressWarnings({ 133 "java:S100" // Method name uses underscore suffix for consistency with other keyword-escape methods 134 }) 135 public Link as_(String value) { 136 attr("as", value); 137 return this; 138 } 139 140 /** 141 * <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#attr-link-crossorigin">crossorigin</a> 142 * attribute. 143 * 144 * <p> 145 * Specifies how the element handles cross-origin requests for CORS (Cross-Origin Resource Sharing). 146 * 147 * <p> 148 * Possible values: 149 * <ul> 150 * <li><js>"anonymous"</js> - Cross-origin requests are made without credentials</li> 151 * <li><js>"use-credentials"</js> - Cross-origin requests include credentials</li> 152 * </ul> 153 * 154 * @param value How to handle cross-origin requests. Can be <jk>null</jk> to unset the attribute. 155 * @return This object. 156 */ 157 public Link crossorigin(String value) { 158 attr("crossorigin", value); 159 return this; 160 } 161 162 /** 163 * <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#attr-link-href">href</a> attribute. 164 * 165 * <p> 166 * Address of the hyperlink. 167 * 168 * <p> 169 * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}. 170 * Strings must be valid URIs. 171 * 172 * <p> 173 * URIs defined by {@link UriResolver} can be used for values. 174 * 175 * @param value 176 * The new value for this attribute. 177 * Typically a {@link URL} or {@link String}. 178 * Can be <jk>null</jk>, in which case the attribute is stored with a <jk>null</jk> value (not removed). 179 * @return This object. 180 */ 181 public Link href(Object value) { 182 attrUri("href", value); 183 return this; 184 } 185 186 /** 187 * <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#attr-link-hreflang">hreflang</a> 188 * attribute. 189 * 190 * <p> 191 * Specifies the language of the linked resource. Used for SEO and accessibility purposes. 192 * 193 * <p> 194 * Examples: 195 * <ul> 196 * <li><js>"en"</js> - English</li> 197 * <li><js>"es"</js> - Spanish</li> 198 * <li><js>"fr"</js> - French</li> 199 * <li><js>"de"</js> - German</li> 200 * <li><js>"zh"</js> - Chinese</li> 201 * <li><js>"ja"</js> - Japanese</li> 202 * </ul> 203 * 204 * @param value The language code of the linked resource. Can be <jk>null</jk> to unset the attribute. 205 * @return This object. 206 */ 207 public Link hreflang(String value) { 208 attr("hreflang", value); 209 return this; 210 } 211 212 /** 213 * <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#attr-link-media">media</a> attribute. 214 * 215 * <p> 216 * Specifies which media types the linked resource applies to. Used primarily with stylesheets. 217 * 218 * <p> 219 * Common values: 220 * <ul> 221 * <li><js>"all"</js> - All media types (default)</li> 222 * <li><js>"screen"</js> - Computer screens</li> 223 * <li><js>"print"</js> - Printers and print preview</li> 224 * <li><js>"handheld"</js> - Handheld devices</li> 225 * <li><js>"projection"</js> - Projectors</li> 226 * <li><js>"(max-width: 768px)"</js> - Media queries</li> 227 * </ul> 228 * 229 * @param value The media types the linked resource applies to. Can be <jk>null</jk> to unset the attribute. 230 * @return This object. 231 */ 232 public Link media(String value) { 233 attr("media", value); 234 return this; 235 } 236 237 /** 238 * <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#attr-link-rel">rel</a> attribute. 239 * 240 * <p> 241 * Specifies the relationship between the current document and the linked resource. 242 * 243 * <p> 244 * Common values: 245 * <ul> 246 * <li><js>"stylesheet"</js> - External CSS stylesheet</li> 247 * <li><js>"icon"</js> - Favicon or site icon</li> 248 * <li><js>"canonical"</js> - Canonical URL for SEO</li> 249 * <li><js>"alternate"</js> - Alternative version of the page</li> 250 * <li><js>"preload"</js> - Resource to preload</li> 251 * <li><js>"prefetch"</js> - Resource to prefetch</li> 252 * <li><js>"dns-prefetch"</js> - DNS lookup to prefetch</li> 253 * <li><js>"next"</js> - Next page in a sequence</li> 254 * <li><js>"prev"</js> - Previous page in a sequence</li> 255 * </ul> 256 * 257 * @param value The relationship between the document and linked resource. Can be <jk>null</jk> to unset the attribute. 258 * @return This object. 259 */ 260 public Link rel(String value) { 261 attr("rel", value); 262 return this; 263 } 264 265 /** 266 * <a class="doclink" href="https://www.w3.org/TR/html5/links.html#attr-link-sizes">sizes</a> attribute. 267 * 268 * <p> 269 * Specifies the sizes of icons for different device contexts. Used with <c>rel="icon"</c> or <c>rel="apple-touch-icon"</c>. 270 * 271 * <p> 272 * Common values: 273 * <ul> 274 * <li><js>"16x16"</js> - Small favicon</li> 275 * <li><js>"32x32"</js> - Standard favicon</li> 276 * <li><js>"180x180"</js> - Apple touch icon</li> 277 * <li><js>"192x192"</js> - Android icon</li> 278 * <li><js>"512x512"</js> - Large icon</li> 279 * <li><js>"any"</js> - Any size</li> 280 * </ul> 281 * 282 * @param value The sizes of the linked icon resource. Can be <jk>null</jk> to unset the attribute. 283 * @return This object. 284 */ 285 public Link sizes(String value) { 286 attr("sizes", value); 287 return this; 288 } 289 290 /** 291 * <a class="doclink" href="https://www.w3.org/TR/html5/document-metadata.html#attr-link-type">type</a> attribute. 292 * 293 * <p> 294 * Specifies the MIME type of the linked resource. Helps browsers determine how to handle the resource. 295 * 296 * <p> 297 * Common values: 298 * <ul> 299 * <li><js>"text/css"</js> - CSS stylesheet</li> 300 * <li><js>"text/javascript"</js> - JavaScript file</li> 301 * <li><js>"application/json"</js> - JSON data</li> 302 * <li><js>"image/png"</js> - PNG image</li> 303 * <li><js>"image/jpeg"</js> - JPEG image</li> 304 * <li><js>"image/svg+xml"</js> - SVG image</li> 305 * <li><js>"font/woff2"</js> - Web font</li> 306 * </ul> 307 * 308 * @param value The MIME type of the linked resource. Can be <jk>null</jk> to unset the attribute. 309 * @return This object. 310 */ 311 public Link type(String value) { 312 attr("type", value); 313 return this; 314 } 315}