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

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

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

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

import com.hfg.util.StringUtil;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import com.hfg.xml.XMLizable;

//------------------------------------------------------------------------------
/**
 * Represents an html (<html>) tag and also contains tag name constants.
 *
 * @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 HTML extends HTMLTag
{
   //###########################################################################
   // PUBLIC FIELDS
   //###########################################################################

   // Tag names
   public static final String A            = "a";
   public static final String ADDRESS      = "address";
   public static final String APPLET       = "applet";
   public static final String AREA         = "area";
   public static final String B            = "b";
   public static final String BODY         = "body";
   public static final String BR           = "br";
   public static final String CAPTION      = "caption";
   public static final String CENTER       = "center";
   public static final String CHAR         = "char";
   public static final String CHAROFF      = "charoff";
   public static final String COL          = "col";
   public static final String COLGROUP     = "colgroup";
   public static final String DATA         = "data";
   public static final String DISABLED     = "disabled";
   public static final String DIV          = "div";
   public static final String EM           = "em";
   public static final String FORM         = "form";
   public static final String FRAME        = "frame";
   public static final String FRAMESET     = "frameset";
   public static final String H1           = "h1";
   public static final String H2           = "h2";
   public static final String H3           = "h3";
   public static final String HEAD         = "head";
   public static final String HR           = "hr";
   public static final String HTML         = "html";
   public static final String IFRAME       = "iframe";
   public static final String IMG          = "img";
   public static final String INPUT        = "input";
   public static final String LABEL        = "label";
   public static final String LI           = "li";
   public static final String LINK         = "link";
   public static final String MAP          = "map";
   public static final String META         = "meta";
   public static final String NAV          = "nav";
   public static final String NOBR         = "nobr";
   public static final String OBJECT       = "object";
   public static final String OL           = "ol";
   public static final String OPTGROUP     = "optgroup";
   public static final String OPTION       = "option";
   public static final String P            = "p";
   public static final String PRE          = "pre";
   public static final String S            = "s";
   public static final String SCRIPT       = "script";
   public static final String SECTION      = "section";
   public static final String SELECT       = "select";
   public static final String SPAN         = "span";
   public static final String STRONG       = "strong";
   public static final String STYLE        = "style";
   public static final String TABLE        = "table";
   public static final String TBODY        = "tbody";
   public static final String TD           = "td";
   public static final String TEXTAREA     = "textarea";
   public static final String TFOOT        = "tfoot";
   public static final String TH           = "th";
   public static final String THEAD        = "thead";
   public static final String TITLE        = "title";
   public static final String TR           = "tr";
   public static final String U            = "u";
   public static final String UL           = "ul";

   // Attribute names
   public static final String ACTION       = "action";
   public static final String ALIGN        = "align";
   public static final String ALT          = "alt";
   public static final String ARCHIVE      = "archive";
   public static final String AUTOFOCUS    = "autofocus";
   public static final String BACKGROUND   = "background";
   public static final String BGCOLOR      = "bgcolor";
   public static final String BORDER       = "border";
   public static final String CELLPADDING  = "cellpadding";
   public static final String CELLSPACING  = "cellspacing";
   public static final String CHARSET      = "charset";
   public static final String CLASS        = "class";
   public static final String CLASSID      = "classid";
   public static final String CODE         = "code";
   public static final String CODEBASE     = "codebase";
   public static final String CODETYPE     = "codetype";
   public static final String COLS         = "cols";
   public static final String COLSPAN      = "colspan";
   public static final String CONTENT      = "content";
   public static final String COORDS       = "coords";
   public static final String DECLARE      = "declare";
   public static final String DEFER        = "defer";
   public static final String DRAGGABLE    = "draggable"; // HTML5
   public static final String ENCTYPE      = "enctype";
   public static final String FOR          = "for";
   public static final String FRAMEBORDER  = "frameborder";
   public static final String FRAMESPACING = "framespacing";
   public static final String HEIGHT       = "height";
   public static final String HREF         = "href";
   public static final String HSPACE       = "hspace";
   public static final String HTTP_EQUIV   = "http-equiv";
   public static final String ID           = "id";
   public static final String LANGUAGE     = "language";
   public static final String MAX          = "max";
   public static final String MAX_LENGTH   = "maxlength";
   public static final String MAYSCRIPT    = "mayscript";
   public static final String METHOD       = "method";
   public static final String MIN          = "min";
   public static final String MULTIPLE     = "multiple";
   public static final String NAME         = "name";
   public static final String NONCE        = "nonce";
   public static final String ONBLUR       = "onblur";
   public static final String ONCHANGE     = "onchange";
   public static final String ONCLICK      = "onclick";
   public static final String ONDBLCLICK   = "ondblclick";
   public static final String ONDRAG       = "ondrag";  // HTML5
   public static final String ONDRAGEND    = "ondragend";  // HTML5
   public static final String ONDRAGENTER  = "ondragenter";  // HTML5
   public static final String ONDRAGLEAVE  = "ondragleave";  // HTML5
   public static final String ONDRAGOVER   = "ondragover";  // HTML5
   public static final String ONDRAGSTART  = "ondragstart";  // HTML5
   public static final String ONDROP       = "ondrop";  // HTML5
   public static final String ONFOCUS      = "onfocus";
   public static final String ONINPUT      = "oninput";
   public static final String ONKEYDOWN    = "onkeydown";
   public static final String ONKEYPRESS   = "onkeypress";
   public static final String ONKEYUP      = "onkeyup";
   public static final String ONLOAD       = "onload";
   public static final String ONMOUSEDOWN  = "onmousedown";
   public static final String ONMOUSEMOVE  = "onmousemove";
   public static final String ONMOUSEOVER  = "onmouseover";
   public static final String ONMOUSEOUT   = "onmouseout";
   public static final String ONMOUSEUP    = "onmouseup";
   public static final String ONMOUSEWHEEL = "onmousewheel";  // HTML5
   public static final String ONRESET      = "onreset";   // No longer supported in HTML5
   public static final String ONSCROLL     = "onscroll";  // HTML5
   public static final String ONSELECT     = "onselect";
   public static final String ONSUBMIT     = "onsubmit";
   public static final String PARAM        = "param";
   public static final String PATTERN      = "pattern";
   public static final String PLACEHOLDER  = "placeholder";
   public static final String REL          = "rel";
   public static final String REQUIRED     = "required";
   public static final String RESIZABLE    = "resizable";
   public static final String ROWS         = "rows";
   public static final String ROWSPAN      = "rowspan";
   public static final String SCOPED       = "scoped";
   public static final String SCROLLING    = "scrolling";
   public static final String SHAPE        = "shape";
   public static final String SIZE         = "size";
   public static final String SRC          = "src";
   public static final String STANDBY      = "standby";
   public static final String STEP         = "setp";
   public static final String TABINDEX     = "tabindex";
   public static final String TARGET       = "target";
   public static final String TYPE         = "type";
   public static final String USEMAP       = "usemap";
   public static final String VALIGN       = "valign";
   public static final String VALUE        = "value";
   public static final String VSPACE       = "vspace";
   public static final String WIDTH        = "width";
   public static final String WRAP         = "wrap";


   // Standard attribute values
   public static final String BUTTON       = "button";
   public static final String CACHE_CONTROL = "cache-control";
   public static final String CHECKED      = "checked";
   public static final String EXPIRES      = "expires";
   public static final String FILE         = "file";
   public static final String NUMBER       = "number";
   public static final String OFF          = "off";
   public static final String PASSWORD     = "password";
   public static final String PRAGMA       = "pragma";
   public static final String REFRESH      = "refresh";
   public static final String SELECTED     = "selected";
   public static final String MULTIPART_FORM_DATA = "multipart/form-data";

   private static final Map sTagToClassMap = new HashMap<>(50);

   static
   {
      sTagToClassMap.put(A, Link.class);
      sTagToClassMap.put(APPLET, Applet.class);
      sTagToClassMap.put(AREA, Area.class);
      sTagToClassMap.put(BODY, Body.class);
      sTagToClassMap.put(CENTER, Center.class);
      sTagToClassMap.put(COL, Col.class);
      sTagToClassMap.put(COLGROUP, Colgroup.class);
      sTagToClassMap.put(DIV, Div.class);
      sTagToClassMap.put(EM, Em.class);
      sTagToClassMap.put(FORM, Form.class);
      sTagToClassMap.put(FRAME, Frame.class);
      sTagToClassMap.put(FRAMESET, Frameset.class);
      sTagToClassMap.put(HEAD, Head.class);
      sTagToClassMap.put(H1, H1.class);
      sTagToClassMap.put(H2, H2.class);
      sTagToClassMap.put(H3, H3.class);
      sTagToClassMap.put(HR, Hr.class);
      sTagToClassMap.put(HTML, HTML.class);
      sTagToClassMap.put(IFRAME, IFrame.class);
      sTagToClassMap.put(IMG, Img.class);
      sTagToClassMap.put(LI, Li.class);
      sTagToClassMap.put(LABEL, Label.class);
      sTagToClassMap.put(MAP, ImageMap.class);
      sTagToClassMap.put(META, Meta.class);
      sTagToClassMap.put(NAV, Nav.class);
      sTagToClassMap.put(NOBR, Nobr.class);
      sTagToClassMap.put(OL, Ol.class);
      sTagToClassMap.put(OBJECT, ObjectTag.class);
      sTagToClassMap.put(OPTGROUP, OptGroup.class);
      sTagToClassMap.put(OPTION, Option.class);
      sTagToClassMap.put(P, P.class);
      sTagToClassMap.put(PARAM, Param.class);
      sTagToClassMap.put(PRE, Pre.class);
      sTagToClassMap.put(SCRIPT, Script.class);
      sTagToClassMap.put(SECTION, Section.class);
      sTagToClassMap.put(SELECT, Select.class);
      sTagToClassMap.put(SPAN, Span.class);
      sTagToClassMap.put(STYLE, StyleTag.class);
      sTagToClassMap.put(TABLE, Table.class);
      sTagToClassMap.put(THEAD, THead.class);
      sTagToClassMap.put(TBODY, TBody.class);
      sTagToClassMap.put(TR, Tr.class);
      sTagToClassMap.put(TH, Th.class);
      sTagToClassMap.put(TD, Td.class);
      sTagToClassMap.put(UL, Ul.class);

   }

   //###########################################################################
   // PRIVATE FIELDS
   //###########################################################################

   private Head     mHead;
   private Body     mBody;
   private Frameset mFrameset;


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

   //--------------------------------------------------------------------------
   public HTML()
   {
      super(HTML);
      initialize();
   }

   //--------------------------------------------------------------------------
   public HTML(String inTitle)
   {
      this();
      getHead().setTitle(inTitle);
   }

   //---------------------------------------------------------------------------
   public HTML(XMLNode inXMLNode)
   {
      super(HTML);
      initFromXMLNode(inXMLNode);
   }

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

   //--------------------------------------------------------------------------
   public Head getHead()
   {
      if (null == mHead)
      {
         mHead = (Head) getOptionalSubtagByName(HEAD);
      }

      return mHead;
   }

   //--------------------------------------------------------------------------
   public Body getBody()
   {
      if (null == mBody)
      {
         mBody = (Body) getOptionalSubtagByName(BODY);

         if (null == mBody)
         {
            mBody = new Body();
            addSubtag(mBody);
         }
      }

      return mBody;
   }

   //--------------------------------------------------------------------------
   public Frameset getFrameset()
   {
      if (null == mFrameset)
      {
         mFrameset = new Frameset();
         addSubtag(mFrameset);
      }

      return mFrameset;
   }

   //---------------------------------------------------------------------------
   public static HTMLTag  allocatTagByName(String inName)
   {
      HTMLTag htmlTag;

      Class clazz = sTagToClassMap.get(inName.toLowerCase());
      if (clazz != null)
      {
         try
         {
            Constructor constructor = clazz.getConstructor();

            htmlTag = (HTMLTag) constructor.newInstance();
         }
         catch (NoSuchMethodException e)
         {
            throw new HTMLParseException("The class " + StringUtil.singleQuote(clazz) + " needs a default constructor.", e);
         }
         catch (Exception e)
         {
            throw new HTMLParseException("Problem during invocation of class " + StringUtil.singleQuote(clazz) + "'s constructor!", e);
         }
      }
      else
      {
         //        throw new HTMLParseException("The tag " + StringUtil.singleQuote(inXMLTag.getTagName()) + " could not be mapped to an HTML class!");
         clazz = HTMLTag.class;

         try
         {
            Constructor constructor = clazz.getConstructor(String.class);

            htmlTag = (HTMLTag) constructor.newInstance(inName);
         }
         catch (NoSuchMethodException e)
         {
            throw new HTMLParseException("The class " + StringUtil.singleQuote(clazz) + " needs a String constructor.", e);
         }
         catch (Exception e)
         {
            throw new HTMLParseException("Problem during invocation of class " + StringUtil.singleQuote(clazz) + "'s constructor!", e);
         }
      }



      return htmlTag;
   }

   //---------------------------------------------------------------------------
   public static HTMLTag constructFromXMLNode(XMLNode inXMLNode)
   {
      Class clazz = sTagToClassMap.get(inXMLNode.getTagName().toLowerCase());
      if (null == clazz)
      {
 //        throw new HTMLParseException("The tag " + StringUtil.singleQuote(inXMLTag.getTagName()) + " could not be mapped to an HTML class!");
         clazz = HTMLTag.class;
      }

      HTMLTag htmlTag;
      try
      {
         Constructor constructor = clazz.getConstructor(XMLNode.class);

         htmlTag = (HTMLTag) constructor.newInstance(inXMLNode);
      }
      catch (NoSuchMethodException e)
      {
         throw new HTMLParseException("The class " + StringUtil.singleQuote(clazz) + " needs a constructor that takes an XMLNode.", e);
      }
      catch (Exception e)
      {
         throw new HTMLParseException("Problem during invocation of class " + StringUtil.singleQuote(clazz) + "'s constructor!", e);
      }

      return htmlTag;
   }

   //--------------------------------------------------------------------------
   @Override
   public void addSubtag(XMLizable inSubtag)
   {
      if (inSubtag instanceof XMLTag)
      {
         XMLTag subtag =  (XMLTag) inSubtag;
         if (subtag.getTagName().equalsIgnoreCase(HEAD))
         {
            if (mHead != null)
            {
               removeSubtag(mHead);
            }

            mHead = (Head) inSubtag;
         }
         else if (subtag.getTagName().equalsIgnoreCase(BODY))
         {
            if (mBody != null)
            {
               removeSubtag(mBody);
            }

            mBody = (Body) inSubtag;
         }
         // TODO: Other cases where there should only be a single subtag of that type?

         super.addSubtag(inSubtag);
      }
      else
      {
         super.addSubtag(inSubtag);
      }
   }

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

   //--------------------------------------------------------------------------
   // Initialization is done to ensure that the head will appear first.
   private void initialize()
   {
      mHead = new Head();
      super.addSubtag(mHead);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy