Package

Source Code of Html2Ecs

/*
* Copyright (c) 1999 The Java Apache Project.  All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in
*    the documentation and/or other materials provided with the
*    distribution.
*
* 3. All advertising materials mentioning features or use of this
*    software must display the following acknowledgment:
*    "This product includes software developed by the Java Apache
*    Project. <http://java.apache.org/>"
*
* 4. The names "Java Apache Element Construction Set", "Java Apache ECS" and
*    "Java Apache Project" must not be used to endorse or promote products
*    derived from this software without prior written permission.
*
* 5. Products derived from this software may not be called
*    "Java Apache Element Construction Set" nor "Java Apache ECS" appear
*    in their names without prior written permission of the
*    Java Apache Project.
*
* 6. Redistributions of any form whatsoever must retain the following
*    acknowledgment:
*    "This product includes software developed by the Java Apache
*    Project. <http://java.apache.org/>"
*   
* THIS SOFTWARE IS PROVIDED BY THE JAVA APACHE PROJECT "AS IS" AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Java Apache Project. For more information
* on the Java Apache Project please see <http://java.apache.org/>.
*
*/
import java.util.Hashtable;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import org.openxml.io.Parser;
import org.openxml.parser.ParseException;
import org.openxml.x3p.Publisher;
import org.openxml.x3p.StreamFormat;
import org.openxml.DOMFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Attr;
import org.apache.ecs.filter.CharacterFilter;
import org.apache.ecs.Entities;

/**
  This class is a really weak attempt to try to come up
  with a solution where we convert a Html file into ECS code.
  This could potentially make it easier for a designer to hand
  an Html file to the programmer who would then convert it into
  ECS code and copy/paste it into a servlet.
  <p>
  Contributors who want to help make this class actually work well
  are welcomed!
*/
public class Html2Ecs extends Hashtable
{
    private static org.w3c.dom.Document doc;
    private static org.openxml.io.Parser parser;
    private static InputStream input;
    private static Publisher pub;
    private static StreamFormat format;

    // Private Initializer
    // Map XML Elements to ECS Elements
    {
        put("A","org.apache.ecs.A");
        put("ABBR","org.apache.ecs.Abbr");
        put("ACRONYM","org.apache.ecs.Acronym");
        put("ADDRESS","org.apache.ecs.Address");
        put("APPLET","org.apache.ecs.Applet");
        put("AREA","org.apache.ecs.Area");
        put("B","org.apache.ecs.B");
        put("BASE","org.apache.ecs.Base");
        put("BASEFONT","org.apache.ecs.BaseFont");
        put("BDO","org.apache.ecs.Bdo");
        put("BIG","org.apache.ecs.Big");
        put("BLINK","org.apache.ecs.Blink");
        put("BLOCKQUOTE","org.apache.ecs.BlockQuote");
        put("BODY","org.apache.ecs.Body");
        put("BR","org.apache.ecs.BR");
        put("BUTTON","org.apache.ecs.Button");
        put("CAPTION","org.apache.ecs.Caption");
        put("CENTER","org.apache.ecs.Center");
        put("CITE","org.apache.ecs.Cite");
        put("CODE","org.apache.ecs.Code");
        put("COL","org.apache.ecs.Col");
        put("COLGROUP","org.apache.ecs.ColGroup");
        put("COMMENT","org.apache.ecs.Comment");
        put("DD","org.apache.ecs.DD");
        put("DEL","org.apache.ecs.Del");
        put("DFN","org.apache.ecs.Dfn");
        put("DIV","org.apache.ecs.Div");
        put("DL","org.apache.ecs.DL");
        put("EM","org.apache.ecs.EM");
        put("FILEDSET","org.apache.ecs.FieldSet");
        put("FONT","org.apache.ecs.Font");
        put("FORM","org.apache.ecs.Form");
        put("FRAME","org.apache.ecs.Frame");
        put("FRAMESET","org.apache.ecs.FrameSet");
        put("H1","org.apache.ecs.H1");
        put("H2","org.apache.ecs.H2");
        put("H3","org.apache.ecs.H3");
        put("H4","org.apache.ecs.H4");
        put("H5","org.apache.ecs.H5");
        put("H6","org.apache.ecs.H6");
        put("HEAD","org.apache.ecs.Head");
        put("HR","org.apache.ecs.HR");
        put("HTML","org.apache.ecs.Html");
        put("I","org.apache.ecs.I");
        put("IFRAME","org.apache.ecs.IFrame");
        put("IMG","org.apache.ecs.IMG");
        put("INPUT","org.apache.ecs.Input");
        put("INS","org.apache.ecs.Ins");
        put("KBD","org.apache.ecs.Kbd");
        put("LABEL","org.apache.ecs.Label");
        put("LEGEND","org.apache.ecs.Legend");
        put("LI","org.apache.ecs.LI");
        put("LINK","org.apache.ecs.Link");
        put("MAP","org.apache.ecs.Map");
        put("META","org.apache.ecs.Meta");
        put("NOBR","org.apache.ecs.NOBR");
        put("NOFRAMES","org.apache.ecs.NoFrames");
        put("NOSCRIPT","org.apache.ecs.NoScript");
        put("OBJECT","org.apache.ecs.ObjectElement");
        put("OL","org.apache.ecs.OL");
        put("OPTGROUP","org.apache.ecs.OptGroup");
        put("OPTION","org.apache.ecs.Option");
        put("P","org.apache.ecs.P");
        put("PARAM","org.apache.ecs.Param");
        put("PRE","org.apache.ecs.PRE");
        put("Q","org.apache.ecs.Q");
        put("S","org.apache.ecs.S");
        put("SAMP","org.apache.ecs.Samp");
        put("SCRIPT","org.apache.ecs.Script");
        put("SELECT","org.apache.ecs.Select");
        put("SMALL","org.apache.ecs.Small");
        put("SPAN","org.apache.ecs.Span");
        put("STRIKE","org.apache.ecs.Strike");
        put("STRONG","org.apache.ecs.Strong");
        put("STYLE","org.apache.ecs.Style");
        put("SUB","org.apache.ecs.Sub");
        put("SUP","org.apache.ecs.Sup");
        put("TABLE","org.apache.ecs.Table");
        put("TBODY","org.apache.ecs.TBody");
        put("TD","org.apache.ecs.TD");
        put("THEAD","org.apache.ecs.THead");
        put("TITLE","org.apache.ecs.Title");
        put("TR","org.apache.ecs.TR");
        put("TT","org.apache.ecs.TT");
        put("U","org.apache.ecs.U");
        put("UL","org.apache.ecs.UL");
        put("VAR","org.apache.ecs.Var");

    }

    public Html2Ecs()
    {
    }

    private void process( Node node )
    {
        while( node != null)
        {
            constructElement(node);

            // If it has children loop through them
            if ( node.hasChildNodes() &&
                !node.getNodeName().equals("#document") &&
                get(node.getNodeName()) == null )
            {
                System.out.print(".addElement(");
                process ( node.getFirstChild() );
                System.out.println(")");
            }
            else
            {
                process ( node.getFirstChild() );
            }
            // move to the next node.
            node = node.getNextSibling();
            System.out.print("\n");
        }
    }

    private void constructElement( Node node )
    {
        if( get(node.getNodeName()) != null )
        {
            System.out.print("new ");
            System.out.print(get(node.getNodeName()));
            System.out.print("()");
        }
        constructAttributes( node );
        constructValue( node );
    }

    private void constructValue( Node node )
    {
        CharacterFilter cf = new CharacterFilter();
        cf.addAttribute("\"",Entities.QUOT);
       
        if( node.getNodeName().equals("#text")
            && node.getNodeValue().length() > 1 )
        {
            System.out.print(".addElement(\"");
            System.out.print(node.getNodeValue());
            System.out.print("\")");
        }
    }

    private void constructAttributes( Node node )
    {
        NamedNodeMap attrList = node.getAttributes();
        Attr attr;
        Class c = null;

        try
        {
            c = Class.forName( (String) get(node.getNodeName()) );
        }
        catch(Exception e)
        {
        }

        if( attrList != null && c != null)
        {
            for(int x = 0; x < attrList.getLength(); x++)
            {
                attr = (Attr) attrList.item(x);

                Method[] m = c.getMethods();
                for(int y = 0; y < m.length; ++y)
                {
                    if(m[y].getName().toLowerCase().endsWith(attr.getName().toLowerCase()))
                    {
                        System.out.print(".");
                        System.out.print(m[y].getName());
                        System.out.print("(\"");
                        System.out.print(attr.getValue());
                        System.out.print("\")");
                        break;
                    }
                }
            }
        }
    }

    public static void main(String[] args)
    {
        Html2Ecs html2ecs = new Html2Ecs();
        try
        {
            input = new FileInputStream( args[0] );
            parser = DOMFactory.createParser( input, null, DOMFactory.DOCUMENT_HTML );
            doc = parser.parseDocument();
        }
        catch(FileNotFoundException fnfe)
        {
            System.out.println(fnfe.toString());
        }
        catch(ParseException pe)
        {
            System.out.println(pe.toString());
        }
        catch(IOException ioe)
        {
            System.out.println(ioe.toString());
        }
        html2ecs.process( doc );
    }
}
TOP

Related Classes of Html2Ecs

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.