Class Head

java.lang.Object
com.oorian.html.Element<Head>
com.oorian.html.elements.Head

public final class Head extends Element<Head>
Represents an HTML <head> element.

The <head> element is a container for document metadata and provides information about the HTML document that is not displayed directly on the page. It typically contains the document title, character set declaration, links to stylesheets and scripts, meta tags for SEO and social media, and other metadata.

The <head> element restricts its content to metadata elements only: <title>, <base>, <link>, <meta>, <style>, <script>, <noscript>, and <template>. All valid child types implement the HeadElement marker interface. Use

invalid reference
#addElement(HeadElement)
or the convenience methods (addMeta(java.lang.String, java.lang.String), addJavaScript(java.lang.String), addCssLink(java.lang.String), etc.) to add children.

Usage:


 // Create a head element with metadata
 Head head = new Head();
 head.setTitle("My Web Page");
 head.setDescription("A description of my page");
 head.setKeywords("java, html, web development");

 // Add external resources
 head.addCssLink("/styles/main.css");
 head.addJavaScript("/scripts/app.js");

 // Add resources with cache-busting timestamps
 head.addCssLink("/styles/theme.css", true);
 head.addJavaScript("/scripts/utils.js", true);

 // Add custom meta tags
 head.addMeta("author", "John Doe");
 head.addMeta("viewport", "width=device-width, initial-scale=1.0");
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Head

      public Head()
      Constructs an empty <head> element.

      Creates a head element for containing document metadata. Child elements such as title, meta tags, links, and scripts should be added using the appropriate setter and add methods.

  • Method Details

    • removeAllElements

      public void removeAllElements()
      Removes all child elements from this head.
      Overrides:
      removeAllElements in class Element<Head>
    • setTitle

      public Head setTitle(String title)
      Sets the document title by creating and adding a <title> element.

      The title appears in the browser tab, bookmarks, and search engine results. This method overrides the parent implementation to add a Title element to the head.

      Parameters:
      title - the title text for the document
      Returns:
      this Head element for method chaining
    • setKeywords

      public final void setKeywords(String keywords)
      Sets the keywords meta tag for search engine optimization.

      Creates a meta element with name="keywords" containing a comma-separated list of keywords relevant to the page content.

      Parameters:
      keywords - comma-separated list of keywords for SEO
    • setDescription

      public final void setDescription(String descr)
      Sets the description meta tag for search engine optimization.

      Creates a meta element with name="description" containing a brief description of the page content, typically displayed in search results.

      Parameters:
      descr - the page description for SEO; should be 150-160 characters
    • setRobots

      public final void setRobots(String robots)
      Sets the robots meta tag to control search engine crawler behavior.

      Creates a meta element with name="robots" to specify directives for search engine crawlers. Common values include "index,follow", "noindex,nofollow", "index,nofollow", etc.

      Parameters:
      robots - comma-separated crawler directives (e.g., "index,follow")
    • setOpenGraph

      public final void setOpenGraph(String title, String description, String imageUrl, String pageUrl)
      Sets the core Open Graph meta tags for social media sharing.

      Creates four meta tags with property attributes for og:title, og:description, og:image, and og:url. These are the minimum recommended Open Graph tags for rich link previews on social media platforms such as Facebook, LinkedIn, and others.

      Parameters:
      title - the page title as it should appear in social media previews
      description - a brief description of the page content
      imageUrl - the URL of the image to display in social media previews
      pageUrl - the canonical URL of the page
    • setOpenGraphType

      public final void setOpenGraphType(String type)
      Sets the Open Graph type meta tag.

      Specifies the type of content the page represents. Common values include "website", "article", "product", "profile", and "video.movie".

      Parameters:
      type - the Open Graph content type (e.g., "website", "article")
    • setOpenGraphSiteName

      public final void setOpenGraphSiteName(String siteName)
      Sets the Open Graph site name meta tag.

      Specifies the name of the overall website or application. This is displayed alongside the page title in social media previews.

      Parameters:
      siteName - the name of the website (e.g., "Oorian")
    • setOpenGraphLocale

      public final void setOpenGraphLocale(String locale)
      Sets the Open Graph locale meta tag.

      Specifies the locale of the page content using a language_TERRITORY format. Defaults to "en_US" if not specified.

      Parameters:
      locale - the locale in language_TERRITORY format (e.g., "en_US", "fr_FR")
    • setTwitterCard

      public final void setTwitterCard(String card, String title, String description, String imageUrl)
      Sets the core Twitter Card meta tags for Twitter sharing.

      Creates four meta tags with name attributes for twitter:card, twitter:title, twitter:description, and twitter:image. Common card types include "summary", "summary_large_image", "app", and "player".

      Parameters:
      card - the card type (e.g., "summary_large_image", "summary")
      title - the title to display in the Twitter card
      description - a brief description of the page content
      imageUrl - the URL of the image to display in the Twitter card
    • setTwitterSite

      public final void setTwitterSite(String site)
      Sets the Twitter site meta tag.

      Specifies the Twitter handle of the website or organization that owns the content. The handle should include the @ prefix.

      Parameters:
      site - the Twitter handle of the site (e.g., "@oorian")
    • setContentSecurityPolicy

      public final void setContentSecurityPolicy(ContentSecurityPolicy csp)
      Sets the Content Security Policy for the document via a <meta> element.

      Creates a <meta http-equiv="content-security-policy" content="..."> element using the directives configured in the provided ContentSecurityPolicy builder. This enforces the policy and blocks any content that violates it.

      Parameters:
      csp - the Content Security Policy builder containing the configured directives
    • setContentSecurityPolicyReportOnly

      public final void setContentSecurityPolicyReportOnly(ContentSecurityPolicy csp)
      Sets the Content Security Policy in report-only mode via a <meta> element.

      Creates a <meta http-equiv="content-security-policy-report-only" content="..."> element using the directives configured in the provided ContentSecurityPolicy builder. In report-only mode, violations are reported but not enforced, allowing developers to test a policy before enabling enforcement.

      Parameters:
      csp - the Content Security Policy builder containing the configured directives
    • addMeta

      public final void addMeta(String attribute, String value)
      Adds a custom meta tag with the specified attribute name and value.

      Creates a meta element with a single attribute-value pair.

      Parameters:
      attribute - the attribute name for the meta tag
      value - the value for the attribute
    • addMeta

      public final void addMeta(String attribute, String value, String content)
      Adds a custom meta tag with an attribute, value, and content.

      Creates a meta element with an attribute-value pair and content attribute.

      Parameters:
      attribute - the attribute name for the meta tag
      value - the value for the attribute
      content - the content value for the meta tag
    • addMeta

      public final void addMeta(Meta meta)
      Adds a pre-configured Meta element to the head.
      Parameters:
      meta - the meta element to add
    • addJavaScript

      public final void addJavaScript(String url)
      Adds an external JavaScript file to the head by URL.

      Creates a <script> element linking to the specified JavaScript file.

      Parameters:
      url - the URL of the JavaScript file; may be absolute or relative
    • addJavaScript

      public final void addJavaScript(String url, boolean includeTimeStamp)
      Adds an external JavaScript file with optional cache-busting timestamp.

      Creates a <script> element linking to the specified JavaScript file. If includeTimeStamp is true, appends a query parameter with the current timestamp to prevent browser caching of stale resources.

      Parameters:
      url - the URL of the JavaScript file; may be absolute or relative
      includeTimeStamp - if true, appends a timestamp to the URL for cache-busting
    • addJavaScript

      public final void addJavaScript(JavaScript script)
      Adds a pre-configured JavaScript element to the head.
      Parameters:
      script - the JavaScript element to add
    • addCssLink

      public final void addCssLink(String url)
      Adds an external CSS stylesheet link to the head by URL.

      Creates a <link> element referencing the specified CSS file.

      Parameters:
      url - the URL of the CSS file; may be absolute or relative
    • addCssLink

      public final void addCssLink(String url, boolean includeTimeStamp)
      Adds an external CSS stylesheet link with optional cache-busting timestamp.

      Creates a <link> element referencing the specified CSS file. If includeTimeStamp is true, appends a query parameter with the current timestamp to prevent browser caching of stale resources.

      Parameters:
      url - the URL of the CSS file; may be absolute or relative
      includeTimeStamp - if true, appends a timestamp to the URL for cache-busting
    • addCssLink

      public final void addCssLink(CssLink link)
      Adds a pre-configured CssLink element to the head.
      Parameters:
      link - the CSS link element to add
    • addCssStyleSheet

      public final void addCssStyleSheet(CssStyleSheet styleSheet)
      Adds an inline CSS stylesheet to the head.

      Creates a <style> element containing the CSS rules from the provided stylesheet object. This embeds the styles directly in the HTML rather than linking to an external file.

      Parameters:
      styleSheet - the CSS stylesheet to embed inline