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

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

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

import java.io.*;
import java.nio.charset.Charset;

import com.hfg.util.StringBuilderPlus;
import com.hfg.xml.XMLDoc;
import com.hfg.xml.Doctype;
import com.hfg.util.mime.MimeType;
import com.hfg.xml.XMLException;

import javax.servlet.http.HttpServletResponse;

//------------------------------------------------------------------------------
/**
 Represents an HTML document.

 @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 HTMLDoc extends XMLDoc
{
   private Meta mContentTypeMetaTag;

   private static Doctype DEFAULT_DOCTYPE = Doctype.HTML_4_01_TRANSITIONAL;

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public HTMLDoc()
   {
      super();
      setRootNode(new HTML());
      init();
   }

   //---------------------------------------------------------------------------
   public HTMLDoc(Doctype inDoctype)
   {
      this();
      setDoctype(inDoctype);
   }

   //---------------------------------------------------------------------------
   public HTMLDoc(HTML inRootTag)
   {
      super(inRootTag);
//      init();
   }

   //---------------------------------------------------------------------------
   public HTMLDoc(File inFile)
         throws XMLException, IOException
   {
      super(inFile);
//      init();
   }

   //---------------------------------------------------------------------------
   private void init()
   {
      if (null == getDoctype())
      {
         setDoctype(DEFAULT_DOCTYPE);
      }

      setMetaContentTypeAndEncoding();

   }


   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //--------------------------------------------------------------------------
   /**
    Used by toXML() and toHTML() for content placed at the start of the document.
    */
   @Override
   public String getHeader()
   {
      String header = "";
      if (getDoctype() != null)
      {
         if (getDoctype().getMimeType() == MimeType.APPLICATION_XHTML)
         {
            header = super.getHeader(); // Adds XML header. ex: ""
         }
         else
         {
            header = getDoctype().toString() + NL;
         }
      }

      return header;
   }

   //---------------------------------------------------------------------------
   @Override
   public HTMLDoc setDoctype(Doctype inValue)
   {
      super.setDoctype(inValue);
      setMetaContentTypeAndEncoding();
      return this;
   }

   //---------------------------------------------------------------------------
   @Override
   public HTMLDoc setEncoding(Charset inValue)
   {
      super.setEncoding(inValue);
      setMetaContentTypeAndEncoding();
      return this;
   }

   //---------------------------------------------------------------------------
   public String toHTML()
   {
      return toXML();
   }

   //---------------------------------------------------------------------------
   public void toHTML(OutputStream inStream)
   {
      toXML(inStream);
   }

   //---------------------------------------------------------------------------
   public void toHTML(PrintWriter inWriter)
   {
      toXML(inWriter);
   }

   //---------------------------------------------------------------------------
   public void toHTML(File inFile)
   {
      toXML(inFile);
   }

   //---------------------------------------------------------------------------
   public void toHTML(HttpServletResponse inResponse)
   {
      if (getDoctype() != null)
      {
         inResponse.setContentType(generateContentTypeString());
      }

      try
      {
         Writer writer = null;
         try
         {
            writer = inResponse.getWriter();
            toXML(writer);
         }
         finally
         {
            if (writer != null)
            {
               writer.close();
            }
         }
      }
      catch (IOException e)
      {
         throw new XMLException(e);
      }
   }

   //--------------------------------------------------------------------------
   public String toIndentedHTML(int inInitialIndentLevel, int inIndentSize)
   {
      return toIndentedXML(inInitialIndentLevel, inIndentSize);
   }

   //--------------------------------------------------------------------------
   public void toIndentedHTML(OutputStream inOutputStream, int inInitialIndentLevel, int inIndentSize)
   {
      toIndentedXML(inOutputStream, inInitialIndentLevel, inIndentSize);
   }

   //--------------------------------------------------------------------------
   public void toIndentedHTML(PrintWriter inWriter, int inInitialIndentLevel, int inIndentSize)
   {
      toIndentedXML(inWriter, inInitialIndentLevel, inIndentSize);
   }


   //--------------------------------------------------------------------------
   public void toIndentedHTML(File inFile, int inInitialIndentLevel, int inIndentSize)
   {
      toIndentedXML(inFile, inInitialIndentLevel, inIndentSize);
   }


   //---------------------------------------------------------------------------
   private void setMetaContentTypeAndEncoding()
   {
      if (null == mContentTypeMetaTag
          && getRootNode() instanceof HTML)
      {
         mContentTypeMetaTag = new Meta();
         ((HTML)getRootNode()).getHead().addSubtag(mContentTypeMetaTag);
      }

      if (mContentTypeMetaTag != null)
      {
         mContentTypeMetaTag.setHttpEquiv("Content-Type").setContentAttribute(generateContentTypeString());
      }
   }

   //---------------------------------------------------------------------------
   private String generateContentTypeString()
   {
      StringBuilderPlus contentType = new StringBuilderPlus().setDelimiter("; ");
      if (getDoctype() != null)
      {
         contentType.append(getDoctype().getMimeType());
         contentType.delimitedAppend("charset=" + getEncoding());
      }

      return contentType.toString();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy