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 javax.servlet.http.HttpServletResponse;

import com.hfg.util.StringBuilderPlus;
import com.hfg.xml.XMLBasedDoc;
import com.hfg.xml.Doctype;
import com.hfg.util.mime.MimeType;
import com.hfg.xml.XMLException;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import com.hfg.xml.parser.SaxyParser;
import com.hfg.xml.parser.XMLTagReader;

//------------------------------------------------------------------------------
/**
 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 XMLBasedDoc
{
   private Meta mContentTypeMetaTag;

   private static Doctype DEFAULT_DOCTYPE = Doctype.HTML_5;

   //###########################################################################
   // 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();
   }

   //---------------------------------------------------------------------------
   public HTMLDoc(BufferedReader inReader)
   {
      super(inReader);
   }

   //---------------------------------------------------------------------------
   public HTMLDoc(BufferedInputStream inStream)
   {
      super(inStream);
   }

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


   //###########################################################################
   // 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);
      return this;
   }

   //---------------------------------------------------------------------------
   @Override
   public HTMLDoc setEncoding(Charset inValue)
   {
      super.setEncoding(inValue);
      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);
   }

   //---------------------------------------------------------------------------
   public void setRootNode(HTML inValue)
   {
      super.setRootNode(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public HTML getRootNode()
   {
      XMLNode rootNode = super.getRootNode();
      if (!(rootNode instanceof HTML))
      {
         rootNode = new HTML(rootNode);
         setRootNode((HTML) rootNode);
      }

      return (HTML) rootNode;
   }

   //###########################################################################
   // PROTECTED METHODS
   //###########################################################################

   //--------------------------------------------------------------------------
   protected void  prepareForOutput()
   {
      // If we read in an HTML document we don't really want to alter its content.
      // If however we constructed the document, let's be complete and set a meta tag for the content type.
      if (isConstructedContent())
      {
         setMetaContentTypeAndEncoding();
      }
   }

   //--------------------------------------------------------------------------
   protected XMLTagReader getTagReader()
   {
      XMLTagReader xmlTagReader = new XMLTagReader();
      if (xmlTagReader.getParser() instanceof SaxyParser)
      {
         ((SaxyParser) xmlTagReader.getParser()).setLenientHTMLParsing(true);
      }

      return xmlTagReader;
   }

   //###########################################################################
   // PRIVATE METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   private void setMetaContentTypeAndEncoding()
   {
      if (getRootNode() != null)
      {
         if (null == mContentTypeMetaTag
             && getRootNode().getHead() != null)
         {
            mContentTypeMetaTag = new Meta();
            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