All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.html.Head Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.html;

import com.hfg.util.Case;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import com.hfg.image.ImageFormat;
import com.hfg.util.mime.MimeType;
import com.hfg.xml.XMLizable;


//------------------------------------------------------------------------------
/**
 Represents a head (<head>) tag.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class Head extends HTMLTag { //########################################################################### // PRIVATE FIELDS //########################################################################### private XMLTag mTitle; private StyleTag mStyle; //########################################################################### // CONSTRUCTORS //########################################################################### //-------------------------------------------------------------------------- public Head() { super(HTML.HEAD); setTitle("Untitled Document"); } //-------------------------------------------------------------------------- /** Constructor that takes the page title. @param inTitle the page title */ public Head(String inTitle) { this(); setTitle(inTitle); } //-------------------------------------------------------------------------- public Head(XMLNode inXMLNode) { super(HTML.HEAD); initFromXMLNode(inXMLNode); } //########################################################################### // PUBLIC METHODS //########################################################################### //-------------------------------------------------------------------------- /** Sets the page title. @param inTitle the page title @return this Head object to enable method chaining */ public Head setTitle(String inTitle) { if (null == mTitle) { mTitle = new XMLTag(HTML.TITLE); super.addSubtag(mTitle); } mTitle.clearContent(); mTitle.addContent(inTitle); return this; } //-------------------------------------------------------------------------- /** Returns the page title. @return this page title */ public String getTitle() { if (null == mTitle) { mTitle = getOptionalSubtagByName(HTML.TITLE, Case.INSENSITIVE); } return mTitle != null ? mTitle.getContent() : null; } //-------------------------------------------------------------------------- /** Adds a link to an external stylesheet. @param inURL the URL of a javascript file for inclusion via a 'script' tag @return the created 'script' tag */ public Script addJavascriptLink(String inURL) { Script tag = new Script().setType(MimeType.TEXT_JAVASCRIPT); tag.setSrc(inURL); addSubtag(tag); return tag; } //-------------------------------------------------------------------------- /** Adds the specified javascript to a 'script' block. @param inJavascript javascript for inclusion via a 'script' tag @return the created 'script' tag */ public Script addJavascript(String inJavascript) { Script script = new Script().setType(MimeType.TEXT_JAVASCRIPT); addSubtag(script); // mJavascript.addContentWithoutEscaping("\n\n" + inJavascript); script.addContent("\n\n"); script.addContent(inJavascript); return script; } //-------------------------------------------------------------------------- /** Removes all style content in the header. */ public void clearStyle() { removeSubtag(mStyle); mStyle = null; } //-------------------------------------------------------------------------- /** Overridden to redirect to addStyleTag() since addStyle doesn't make much sense for the HEAD tag anyway. @deprecated use addStyleTag() @param inStyle CSS style text for inclusion via a 'style' tag @return this Head object to enable method chaining */ @Override public Head addStyle(String inStyle) { addStyleTag(inStyle); return this; } //-------------------------------------------------------------------------- /** Adds the specified text to a 'style' block. @param inStyle CSS style text for inclusion via a 'style' tag */ public void addStyleTag(CharSequence inStyle) { if (null == mStyle) { mStyle = new StyleTag(); addSubtag(mStyle); } mStyle.addContentWithoutEscaping(inStyle); } //-------------------------------------------------------------------------- /** Returns the style content. @return the content of the 'style' tag */ @Override public String getStyle() { return mStyle.getContent(); } //-------------------------------------------------------------------------- /** Adds a link to an external stylesheet. @param inURL the URL of a CSS file for inclusion via a 'link' tag */ public void addStyleSheetLink(String inURL) { addSubtag(new HTMLTag(HTML.LINK) .setAttribute(HTML.REL, "StyleSheet") .setAttribute(HTML.HREF, inURL) .setAttribute(HTML.TYPE, MimeType.TEXT_CSS)); } //-------------------------------------------------------------------------- /** Adds a link to a shortcut icon. @param inURL the URL of a shortcut icon file for inclusion via a 'link' tag */ public void setShortcutIconLink(String inURL) { HTMLTag tag = new HTMLTag(HTML.LINK) .setAttribute(HTML.REL, "shortcut icon") .setAttribute(HTML.HREF, inURL); ImageFormat format = ImageFormat.guessFormatFromName(inURL); if (format != null) { tag.setAttribute(HTML.TYPE, format.getMimeType()); } else { throw new RuntimeException("The shortcut icon image " + inURL + " is not in a recognized format!"); } addSubtag(tag); } //-------------------------------------------------------------------------- /** Adds a 'meta' tag to the header @param inHttpEquiv the http-equiv value @param inValue the content attribute value. */ public void addMetaTag(String inHttpEquiv, String inValue) { Meta metaTag = new Meta(); metaTag.setHttpEquiv(inHttpEquiv); metaTag.setContentAttribute(inValue); addSubtag(metaTag); } //-------------------------------------------------------------------------- /** Adds a 'meta http-equiv="refresh"' tag to the header @param inTimeSpan delay in seconds before refreshing page. @param inURL the URL of the page to refresh with. */ public void addMetaRefresh(int inTimeSpan, String inURL) { Meta metaTag = new Meta(); metaTag.setHttpEquiv(HTML.REFRESH); metaTag.setContentAttribute(inTimeSpan + ";url=" + inURL); addSubtag(metaTag); } //-------------------------------------------------------------------------- /** Adds a 'meta http-equiv="Content-Security-Policy"' tag to the header. @param inValue the CSP content. */ public void addContentSecurityPolicy(String inValue) { Meta metaTag = new Meta(); metaTag.setHttpEquiv(Meta.CONTENT_SECURITY_POLICY); metaTag.setContentAttribute(inValue); addSubtag(metaTag); } //-------------------------------------------------------------------------- /** Adds a '<meta http-equiv="pragma" content="no-cache" />' tag, a '<meta http-equiv="cache-control" content="no-cache" />' tag, and a '<meta http-equiv="expires" content="0" />' tag to the header. */ public void addMetaNoCache() { Meta metaTag = new Meta(); metaTag.setHttpEquiv(HTML.PRAGMA); metaTag.setContentAttribute("no-cache"); addSubtag(metaTag); metaTag = new Meta(); metaTag.setHttpEquiv(HTML.CACHE_CONTROL); metaTag.setContentAttribute("no-cache"); addSubtag(metaTag); metaTag = new Meta(); metaTag.setHttpEquiv(HTML.EXPIRES); metaTag.setContentAttribute("0"); addSubtag(metaTag); } //-------------------------------------------------------------------------- @Override public void addSubtag(XMLizable inSubtag) { if (inSubtag instanceof XMLTag) { XMLTag subtag = (XMLTag) inSubtag; if (subtag.getTagName().equalsIgnoreCase(HTML.TITLE)) { if (mTitle != null) { removeSubtag(mTitle); } mTitle = (XMLTag) inSubtag; } // TODO: Other cases where there should only be a single subtag of that type? super.addSubtag(inSubtag); } else { super.addSubtag(inSubtag); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy