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