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;

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

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


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

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

   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(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(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(SELECT, Select.class);
      sTagToClassMap.put(SPAN, Span.class);
      sTagToClassMap.put(STYLE, StyleTag.class);
      sTagToClassMap.put(TABLE, Table.class);
      sTagToClassMap.put(TBODY, TBody.class);
      sTagToClassMap.put(THEAD, THead.class);
      sTagToClassMap.put(TD, Td.class);
      sTagToClassMap.put(TR, Tr.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 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;
   }

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

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy