Class Link<T extends Link<T>>

java.lang.Object
com.oorian.html.Element<T>
com.oorian.html.elements.Link<T>
All Implemented Interfaces:
HeadElement
Direct Known Subclasses:
CssLink

public class Link<T extends Link<T>> extends Element<T> implements HeadElement
Represents an HTML <link> element for external resource linking.

The <link> element defines the relationship between the current document and an external resource. It is most commonly used to link to external stylesheets, but can also specify favicons, alternate document versions, prefetch resources, and other document metadata. The link element is a self-closing tag that appears in the document head section.

Features:

  • Links to external stylesheets and resources
  • Specifies document relationships and metadata
  • Supports media queries for responsive stylesheets
  • Can define favicons and touch icons
  • Supports CORS and preloading configurations

Usage:


 // Link to a stylesheet
 Link cssLink = new Link("/styles/main.css", Relationship.STYLESHEET, "text/css");

 // Specify a favicon
 Link favicon = new Link("/favicon.ico", "icon", "image/x-icon", "16x16");

 // Responsive stylesheet with media query
 Link responsiveLink = new Link();
 responsiveLink.setHref("/styles/mobile.css");
 responsiveLink.setRel(Relationship.STYLESHEET);
 responsiveLink.setMedia("screen and (max-width: 600px)");

 // Alternate language version
 Link alternate = new Link();
 alternate.setHref("/es/page.html");
 alternate.setRel("alternate");
 alternate.setHrefLang("es");
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Link

      public Link()
      Constructs an empty <link> element.

      Creates a link element without attributes. The href, rel, and other attributes should be set using the appropriate setter methods. This constructor excludes the id attribute by default.

    • Link

      public Link(String href, Relationship rel, String mediaType)
      Constructs a <link> element with href, relationship, and media type.
      Parameters:
      href - the URL of the linked resource
      rel - the relationship type (typically STYLESHEET)
      mediaType - the MIME type of the linked resource
    • Link

      public Link(String href, String rel, String mediaType, String sizes)
      Constructs a <link> element with href, relationship, media type, and sizes.

      This constructor is useful for specifying icons with specific dimensions.

      Parameters:
      href - the URL of the linked resource
      rel - the relationship type as a string
      mediaType - the MIME type of the linked resource
      sizes - the sizes specification (e.g., "16x16", "32x32")
    • Link

      public Link(String href, String rel, String sizes)
      Constructs a <link> element with href, relationship, and sizes.
      Parameters:
      href - the URL of the linked resource
      rel - the relationship type as a string
      sizes - the sizes specification (e.g., "16x16", "32x32")
    • Link

      public Link(String href, String rel)
      Constructs a <link> element with href and relationship.
      Parameters:
      href - the URL of the linked resource
      rel - the relationship type as a string
  • Method Details

    • setAs

      public final T setAs(String as)
      Sets the resource type hint for preloading.

      Used with rel="preload" or rel="modulepreload" to specify the type of resource being loaded. This helps the browser prioritize and handle the resource correctly.

      Parameters:
      as - the resource type (e.g., "script", "style", "image", "font", "fetch")
      Returns:
      this element for method chaining
    • setBlocking

      public final T setBlocking(String blocking)
      Sets the blocking attribute indicating that rendering should be blocked until this resource is fetched.

      The value "render" prevents the browser from rendering content until the resource is loaded.

      Parameters:
      blocking - The blocking token(s).
      Returns:
      This element for method chaining.
    • setCrossOrigin

      public final T setCrossOrigin(CrossOrigin crossOrigin)
      Sets the cross-origin attribute using the CrossOrigin enum.
      Parameters:
      crossOrigin - The CORS mode.
      Returns:
      This element for method chaining.
    • setCrossOrigin

      public final T setCrossOrigin(String crossOrigin)
      Sets the crossorigin attribute.

      Specifies how the element handles cross-origin requests. The value "anonymous" sends requests without credentials; "use-credentials" includes cookies and authentication.

      Parameters:
      crossOrigin - The cross-origin value (anonymous, use-credentials).
      Returns:
      This element for method chaining.
    • setDisabled

      public final T setDisabled(boolean disabled)
      Sets the disabled boolean attribute.

      When present on a <link> element for a stylesheet, the stylesheet will not be loaded and applied to the document. A disabled stylesheet can be enabled later via JavaScript.

      Parameters:
      disabled - True to disable, false to enable.
      Returns:
      This element for method chaining.
    • setFetchPriority

      public final T setFetchPriority(String fetchPriority)
      Sets the fetchpriority attribute.

      Provides a hint to the browser about the relative priority of fetching this resource. Values include "high", "low", and "auto" (default).

      Parameters:
      fetchPriority - The fetch priority (high, low, auto).
      Returns:
      This element for method chaining.
    • setHref

      public final T setHref(String url)
      Sets the URL of the linked resource.

      If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended.

      Parameters:
      url - The URL of the linked resource.
      Returns:
      This element for method chaining.
    • setHrefLang

      public final T setHrefLang(String languageCode)
      Sets the hreflang attribute.

      Specifies the language of the linked resource as a BCP 47 language tag (e.g., "en", "fr-CA"). This is advisory only and does not affect browser behavior.

      Parameters:
      languageCode - The language of the linked resource.
      Returns:
      This element for method chaining.
    • setMedia

      public final T setMedia(String mediaQuery)
      Sets the media query that the linked resource applies to.

      The resource is only used when the media query matches. Common values include "screen", "print", and responsive breakpoints.

      Parameters:
      mediaQuery - The media query for the resource.
      Returns:
      This element for method chaining.
    • setRel

      public final T setRel(Relationship rel)
      Sets the rel attribute using the Relationship enum.
      Parameters:
      rel - The link relationship.
      Returns:
      This element for method chaining.
    • setRel

      public final T setRel(String rel)
      Sets the rel attribute.

      Specifies the relationship between the current document and the linked resource. Common values include "stylesheet", "icon", "preload", "preconnect", and "dns-prefetch".

      Parameters:
      rel - The relationship of the linked resource.
      Returns:
      This element for method chaining.
    • setSizes

      public T setSizes(String sizes)
      Sets the sizes attribute specifying icon sizes.

      Used to specify the sizes of icons when used with rel="icon". The value is a space-separated list of sizes (e.g., "16x16", "32x32", "any").

      Parameters:
      sizes - The icon sizes specification.
      Returns:
      This element for method chaining.
    • setImageSizes

      public final T setImageSizes(String imageSizes)
      Sets the image sizes attribute for preloading responsive images.

      Used with rel="preload" and as="image" in conjunction with setImageSrcSet(String). Specifies image sizes for different viewport conditions.

      Parameters:
      imageSizes - the sizes descriptor (e.g., "(max-width: 600px) 100vw, 50vw")
      Returns:
      this element for method chaining
    • setImageSrcSet

      public final T setImageSrcSet(String imageSrcSet)
      Sets the imagesrcset attribute for preloading responsive images.

      Used with rel="preload" and as="image" in conjunction with imagesizes.

      Parameters:
      imageSrcSet - The image source set for preloading.
      Returns:
      This element for method chaining.
    • setIntegrity

      public final T setIntegrity(String integrity)
      Sets the subresource integrity hash for verifying the fetched resource has not been tampered with.

      The browser verifies the fetched resource matches the hash before using it, protecting against CDN tampering.

      Parameters:
      integrity - The subresource integrity hash.
      Returns:
      This element for method chaining.
    • setReferrerPolicy

      public final T setReferrerPolicy(String referrerPolicy)
      Sets the referrerpolicy attribute.

      Specifies how much referrer information to send when fetching this resource. Common values include "no-referrer", "origin", and "strict-origin-when-cross-origin".

      Parameters:
      referrerPolicy - The referrer policy value.
      Returns:
      This element for method chaining.
    • setType

      public final T setType(String mediaType)
      Sets the MIME type of the linked resource.

      Specifies the media type of the linked content (e.g., "text/css", "image/x-icon"). Helps the browser determine how to handle the resource.

      Parameters:
      mediaType - The MIME type of the linked resource.
      Returns:
      This element for method chaining.