001package com.hfg.html;
002
003import com.hfg.util.Case;
004import com.hfg.xml.XMLNode;
005import com.hfg.xml.XMLTag;
006import com.hfg.image.ImageFormat;
007import com.hfg.util.mime.MimeType;
008
009import static com.hfg.html.HTML.TITLE;
010
011//------------------------------------------------------------------------------
012/**
013 Represents a head (<head>) tag.
014 <div>
015  @author J. Alex Taylor, hairyfatguy.com
016 </div>
017 */
018//------------------------------------------------------------------------------
019// com.hfg XML/HTML Coding Library
020//
021// This library is free software; you can redistribute it and/or
022// modify it under the terms of the GNU Lesser General Public
023// License as published by the Free Software Foundation; either
024// version 2.1 of the License, or (at your option) any later version.
025//
026// This library is distributed in the hope that it will be useful,
027// but WITHOUT ANY WARRANTY; without even the implied warranty of
028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
029// Lesser General Public License for more details.
030//
031// You should have received a copy of the GNU Lesser General Public
032// License along with this library; if not, write to the Free Software
033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
034//
035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
036// jataylor@hairyfatguy.com
037//------------------------------------------------------------------------------
038
039public class Head extends HTMLTag
040{
041
042   //###########################################################################
043   // PRIVATE FIELDS
044   //###########################################################################
045
046   private XMLTag mTitle;
047   private StyleTag mStyle;
048
049   //###########################################################################
050   // CONSTRUCTORS
051   //###########################################################################
052
053   //--------------------------------------------------------------------------
054   public Head()
055   {
056      super(HTML.HEAD);
057      setTitle("Untitled Document");
058   }
059
060   //--------------------------------------------------------------------------
061   /**
062    Constructor that takes the page title.
063    @param inTitle the page title
064    */
065   public Head(String inTitle)
066   {
067      this();
068      setTitle(inTitle);
069   }
070
071   //--------------------------------------------------------------------------
072   public Head(XMLNode inXMLNode)
073   {
074      super(HTML.HEAD);
075      initFromXMLNode(inXMLNode);
076   }
077
078   //###########################################################################
079   // PUBLIC METHODS
080   //###########################################################################
081
082   //--------------------------------------------------------------------------
083   /**
084    Sets the page title.
085    @param inTitle the page title
086    @return this Head object to enable method chaining
087    */
088   public Head setTitle(String inTitle)
089   {
090      if (null == mTitle)
091      {
092         mTitle = new XMLTag(TITLE);
093         addSubtag(mTitle);
094      }
095
096      mTitle.clearContent();
097      mTitle.addContent(inTitle);
098
099      return this;
100   }
101
102   //--------------------------------------------------------------------------
103   /**
104    Returns the page title.
105    @return this page title
106    */
107   public String getTitle()
108   {
109      if (null == mTitle)
110      {
111         mTitle = (HTMLTag) getOptionalSubtagByName(TITLE, Case.INSENSITIVE);
112      }
113
114      return mTitle != null ? mTitle.getContent() : null;
115   }
116
117
118   //--------------------------------------------------------------------------
119   /**
120    Adds a link to an external stylesheet.
121    @param inURL the URL of a javascript file for inclusion via a 'script' tag
122    @return the created 'script' tag
123    */
124   public Script addJavascriptLink(String inURL)
125   {
126      Script tag =  new Script().setType(MimeType.TEXT_JAVASCRIPT);
127      tag.setSrc(inURL);
128      addSubtag(tag);
129      return tag;
130   }
131
132   //--------------------------------------------------------------------------
133   /**
134    Adds the specified javascript to a 'script' block.
135    @param inJavascript javascript for inclusion via a 'script' tag
136    @return the created 'script' tag
137    */
138   public Script addJavascript(String inJavascript)
139   {
140      Script script = new Script().setType(MimeType.TEXT_JAVASCRIPT);
141      addSubtag(script);
142
143//      mJavascript.addContentWithoutEscaping("\n\n" + inJavascript);
144      script.addContent("\n\n");
145      script.addContent(inJavascript);
146      return script;
147   }
148
149   //--------------------------------------------------------------------------
150   /**
151    Removes all style content in the header.
152    */
153   public void clearStyle()
154   {
155      removeSubtag(mStyle);
156      mStyle = null;
157   }
158
159   //--------------------------------------------------------------------------
160   /**
161    Overridden to redirect to addStyleTag() since addStyle doesn't make much
162    sense for the HEAD tag anyway.
163    @deprecated use addStyleTag()
164    @param inStyle CSS style text for inclusion via a 'style' tag
165    @return this Head object to enable method chaining
166    */
167   @Override
168   public Head addStyle(String inStyle)
169   {
170      addStyleTag(inStyle);
171      return this;
172   }
173
174   //--------------------------------------------------------------------------
175   /**
176    Adds the specified text to a 'style' block.
177    @param inStyle CSS style text for inclusion via a 'style' tag
178    */
179   public void addStyleTag(String inStyle)
180   {
181      if (null == mStyle)
182      {
183         mStyle = new StyleTag();
184         addSubtag(mStyle);
185      }
186
187      mStyle.addContentWithoutEscaping(inStyle);
188   }
189
190   //--------------------------------------------------------------------------
191   /**
192    Returns the style content.
193    @return the content of the 'style' tag
194    */
195   @Override
196   public String getStyle()
197   {
198      return mStyle.getContent();
199   }
200
201
202   //--------------------------------------------------------------------------
203   /**
204    Adds a link to an external stylesheet.
205    @param inURL the URL of a CSS file for inclusion via a 'link' tag
206    */
207   public void addStyleSheetLink(String inURL)
208   {
209      addSubtag(new HTMLTag(HTML.LINK)
210            .setAttribute(HTML.REL, "StyleSheet")
211            .setAttribute(HTML.HREF, inURL)
212            .setAttribute(HTML.TYPE, MimeType.TEXT_CSS));
213   }
214
215   //--------------------------------------------------------------------------
216   /**
217    Adds a link to a shortcut icon.
218    @param inURL the URL of a shortcut icon file for inclusion via a 'link' tag
219    */
220   public void setShortcutIconLink(String inURL)
221   {
222      HTMLTag tag = new HTMLTag(HTML.LINK)
223            .setAttribute(HTML.REL, "shortcut icon")
224            .setAttribute(HTML.HREF, inURL);
225
226      ImageFormat format = ImageFormat.guessFormatFromName(inURL);
227      if (format != null)
228      {
229         tag.setAttribute(HTML.TYPE, format.getMimeType());
230      }
231      else
232      {
233         throw new RuntimeException("The shortcut icon image " + inURL + " is not in a recognized format!");
234      }
235
236      addSubtag(tag);
237   }
238
239   //--------------------------------------------------------------------------
240   /**
241    Adds a 'meta' tag to the header
242    @param inHttpEquiv the http-equiv value
243    @param inValue the content attribute value.
244    */
245    public void addMetaTag(String inHttpEquiv, String inValue)
246   {
247      Meta metaTag = new Meta();
248      metaTag.setHttpEquiv(inHttpEquiv);
249      metaTag.setContentAttribute(inValue);
250      addSubtag(metaTag);
251   }
252
253   //--------------------------------------------------------------------------
254   /**
255    Adds a 'meta http-equiv="refresh"' tag to the header
256    @param inTimeSpan delay in seconds before refreshing page.
257    @param inURL the URL of the page to refresh with.
258    */
259    public void addMetaRefresh(int inTimeSpan, String inURL)
260   {
261      Meta metaTag = new Meta();
262      metaTag.setHttpEquiv(HTML.REFRESH);
263      metaTag.setContentAttribute(inTimeSpan + ";url=" + inURL);
264      addSubtag(metaTag);
265   }
266
267   //--------------------------------------------------------------------------
268   /**
269    Adds a '&lt;meta http-equiv="pragma" content="no-cache" /&gt;' tag, a
270    '&lt;meta http-equiv="cache-control" content="no-cache" /&gt;' tag, and a
271    '&lt;meta http-equiv="expires" content="0" /&gt;' tag to the header.
272    */
273    public void addMetaNoCache()
274   {
275      Meta metaTag = new Meta();
276      metaTag.setHttpEquiv(HTML.PRAGMA);
277      metaTag.setContentAttribute("no-cache");
278      addSubtag(metaTag);
279
280      metaTag = new Meta();
281      metaTag.setHttpEquiv(HTML.CACHE_CONTROL);
282      metaTag.setContentAttribute("no-cache");
283      addSubtag(metaTag);
284
285      metaTag = new Meta();
286      metaTag.setHttpEquiv(HTML.EXPIRES);
287      metaTag.setContentAttribute("0");
288      addSubtag(metaTag);
289   }
290
291}