com.hfg.html.Head Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.html;
import com.hfg.xml.XMLTag;
import com.hfg.image.ImageFormat;
import com.hfg.util.mime.MimeType;
//------------------------------------------------------------------------------
/**
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 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);
addSubtag(mTitle);
}
mTitle.clearContent();
mTitle.addContent(inTitle);
return this;
}
//--------------------------------------------------------------------------
/**
Returns the page title.
@return this page title
*/
public String getTitle()
{
return mTitle.getContent();
}
//--------------------------------------------------------------------------
/**
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(String 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)
{
XMLTag tag = new XMLTag(HTML.LINK);
tag.setAttribute(HTML.REL, "StyleSheet");
tag.setAttribute(HTML.HREF, inURL);
tag.setAttribute(HTML.TYPE, MimeType.TEXT_CSS);
addSubtag(tag);
}
//--------------------------------------------------------------------------
/**
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)
{
XMLTag tag = new XMLTag(HTML.LINK);
tag.setAttribute(HTML.REL, "shortcut icon");
tag.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="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);
}
}