KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ecs > html2ecs > Html2Ecs


1 package org.apache.ecs.html2ecs;
2
3 /*
4  * ====================================================================
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "The Jakarta Project", "Jakarta Element Construction Set",
31  * "Jakarta ECS" , and "Apache Software Foundation" must not be used
32  * to endorse or promote products derived
33  * from this software without prior written permission. For written
34  * permission, please contact apache@apache.org.
35  *
36  * 5. Products derived from this software may not be called "Apache",
37  * "Jakarta Element Construction Set" nor "Jakarta ECS" nor may "Apache"
38  * appear in their names without prior written permission of the Apache Group.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Apache Software Foundation. For more
56  * information on the Apache Software Foundation, please see
57  * <http://www.apache.org/>.
58  *
59  */

60
61 import java.util.Hashtable JavaDoc;
62 import java.io.InputStream JavaDoc;
63 import java.io.IOException JavaDoc;
64 import java.io.FileInputStream JavaDoc;
65 import java.io.FileNotFoundException JavaDoc;
66 import java.lang.reflect.Method JavaDoc;
67 import org.apache.xerces.parsers.DOMParser;
68 import org.xml.sax.InputSource JavaDoc;
69 import org.w3c.dom.Node JavaDoc;
70 import org.w3c.dom.NodeList JavaDoc;
71 import org.w3c.dom.NamedNodeMap JavaDoc;
72 import org.w3c.dom.Attr JavaDoc;
73 import org.apache.ecs.filter.CharacterFilter;
74 import org.apache.ecs.Entities;
75 import org.xml.sax.SAXException JavaDoc;
76
77 /**
78     The point of this class is to create an HTML file -> ECS code converter.
79     It is <strong>NOT</strong> required for ECS core execution. If it does not
80     compile, it is because you need to get the Xerces XML parser for Java from
81     <a HREF="http://xml.apache.org/">xml.apache.org</a>.
82 <p>
83     This class is presently fairly broken and is really only shown as an example.
84
85     Contributions towards making this class would be MOST appreciated. Please subscribe to
86     the <a HREF="http://java.apache.org/main/mail.html">ECS mailing list</a> and express
87     your interest there.
88 */

89 public class Html2Ecs extends Hashtable JavaDoc
90 {
91     private static org.w3c.dom.Document JavaDoc doc;
92     private static DOMParser parser;
93     private static InputStream JavaDoc input;
94
95     // Private Initializer
96
// Map XML Elements to ECS Elements
97
{
98         put("A","org.apache.ecs.html.A");
99         put("ABBR","org.apache.ecs.html.Abbr");
100         put("ACRONYM","org.apache.ecs.html.Acronym");
101         put("ADDRESS","org.apache.ecs.html.Address");
102         put("APPLET","org.apache.ecs.html.Applet");
103         put("AREA","org.apache.ecs.html.Area");
104         put("B","org.apache.ecs.B");
105         put("BASE","org.apache.ecs.html.Base");
106         put("BASEFONT","org.apache.ecs.html.BaseFont");
107         put("BDO","org.apache.ecs.html.Bdo");
108         put("BIG","org.apache.ecs.html.Big");
109         put("BLINK","org.apache.ecs.html.Blink");
110         put("BLOCKQUOTE","org.apache.ecs.html.BlockQuote");
111         put("BODY","org.apache.ecs.html.Body");
112         put("BR","org.apache.ecs.html.BR");
113         put("BUTTON","org.apache.ecs.html.Button");
114         put("CAPTION","org.apache.ecs.html.Caption");
115         put("CENTER","org.apache.ecs.html.Center");
116         put("CITE","org.apache.ecs.html.Cite");
117         put("CODE","org.apache.ecs.html.Code");
118         put("COL","org.apache.ecs.html.Col");
119         put("COLGROUP","org.apache.ecs.html.ColGroup");
120         put("COMMENT","org.apache.ecs.html.Comment");
121         put("DD","org.apache.ecs.html.DD");
122         put("DEL","org.apache.ecs.html.Del");
123         put("DFN","org.apache.ecs.html.Dfn");
124         put("DIV","org.apache.ecs.html.Div");
125         put("DL","org.apache.ecs.html.DL");
126         put("EM","org.apache.ecs.html.EM");
127         put("FILEDSET","org.apache.ecs.html.FieldSet");
128         put("FONT","org.apache.ecs.html.Font");
129         put("FORM","org.apache.ecs.html.Form");
130         put("FRAME","org.apache.ecs.html.Frame");
131         put("FRAMESET","org.apache.ecs.html.FrameSet");
132         put("H1","org.apache.ecs.html.H1");
133         put("H2","org.apache.ecs.html.H2");
134         put("H3","org.apache.ecs.html.H3");
135         put("H4","org.apache.ecs.html.H4");
136         put("H5","org.apache.ecs.html.H5");
137         put("H6","org.apache.ecs.html.H6");
138         put("HEAD","org.apache.ecs.html.Head");
139         put("HR","org.apache.ecs.html.HR");
140         put("HTML","org.apache.ecs.html.Html");
141         put("I","org.apache.ecs.html.I");
142         put("IFRAME","org.apache.ecs.html.IFrame");
143         put("IMG","org.apache.ecs.html.IMG");
144         put("INPUT","org.apache.ecs.html.Input");
145         put("INS","org.apache.ecs.html.Ins");
146         put("KBD","org.apache.ecs.html.Kbd");
147         put("LABEL","org.apache.ecs.html.Label");
148         put("LEGEND","org.apache.ecs.html.Legend");
149         put("LI","org.apache.ecs.html.LI");
150         put("LINK","org.apache.ecs.html.Link");
151         put("MAP","org.apache.ecs.html.Map");
152         put("META","org.apache.ecs.html.Meta");
153         put("NOBR","org.apache.ecs.html.NOBR");
154         put("NOFRAMES","org.apache.ecs.html.NoFrames");
155         put("NOSCRIPT","org.apache.ecs.html.NoScript");
156         put("OBJECT","org.apache.ecs.html.ObjectElement");
157         put("OL","org.apache.ecs.html.OL");
158         put("OPTGROUP","org.apache.ecs.html.OptGroup");
159         put("OPTION","org.apache.ecs.html.Option");
160         put("P","org.apache.ecs.html.P");
161         put("PARAM","org.apache.ecs.html.Param");
162         put("PRE","org.apache.ecs.html.PRE");
163         put("Q","org.apache.ecs.html.Q");
164         put("S","org.apache.ecs.html.S");
165         put("SAMP","org.apache.ecs.html.Samp");
166         put("SCRIPT","org.apache.ecs.html.Script");
167         put("SELECT","org.apache.ecs.html.Select");
168         put("SMALL","org.apache.ecs.html.Small");
169         put("SPAN","org.apache.ecs.html.Span");
170         put("STRIKE","org.apache.ecs.html.Strike");
171         put("STRONG","org.apache.ecs.html.Strong");
172         put("STYLE","org.apache.ecs.html.Style");
173         put("SUB","org.apache.ecs.html.Sub");
174         put("SUP","org.apache.ecs.html.Sup");
175         put("TABLE","org.apache.ecs.html.Table");
176         put("TBODY","org.apache.ecs.html.TBody");
177         put("TD","org.apache.ecs.html.TD");
178         put("THEAD","org.apache.ecs.html.THead");
179         put("TITLE","org.apache.ecs.html.Title");
180         put("TR","org.apache.ecs.html.TR");
181         put("TT","org.apache.ecs.html.TT");
182         put("U","org.apache.ecs.html.U");
183         put("UL","org.apache.ecs.html.UL");
184         put("VAR","org.apache.ecs.html.Var");
185
186     }
187
188     public Html2Ecs()
189     {
190         super(4);
191     }
192
193     private void process( Node JavaDoc node )
194     {
195         while( node != null)
196         {
197             constructElement(node);
198
199             // If it has children loop through them
200
if ( node.hasChildNodes() &&
201                 !node.getNodeName().equals("#document") &&
202                 get(node.getNodeName()) == null )
203             {
204                 System.out.print(".addElement(");
205                 process ( node.getFirstChild() );
206                 System.out.println(")");
207             }
208             else
209             {
210                 process ( node.getFirstChild() );
211             }
212             // move to the next node.
213
node = node.getNextSibling();
214             System.out.print("\n");
215         }
216     }
217
218     private void constructElement( Node JavaDoc node )
219     {
220         if( get(node.getNodeName().toUpperCase()) != null )
221         {
222             System.out.print("new ");
223             System.out.print(get(node.getNodeName().toUpperCase()));
224             System.out.print("()");
225         }
226         constructAttributes( node );
227         constructValue( node );
228     }
229
230     private void constructValue( Node JavaDoc node )
231     {
232         CharacterFilter cf = new CharacterFilter();
233         cf.addAttribute("\"",Entities.QUOT);
234
235         if( node.getNodeName().equals("#text")
236             && node.getNodeValue().length() > 1 )
237         {
238             System.out.print(".addElement(\"");
239             System.out.print(node.getNodeValue());
240             System.out.print("\")");
241         }
242     }
243
244     private void constructAttributes( Node JavaDoc node )
245     {
246         NamedNodeMap JavaDoc attrList = node.getAttributes();
247         Attr JavaDoc attr;
248         Class JavaDoc c = null;
249
250         try
251         {
252             c = Class.forName( (String JavaDoc) get(node.getNodeName().toUpperCase()) );
253         }
254         catch(Exception JavaDoc e)
255         {
256         }
257
258         if( attrList != null && c != null)
259         {
260             for(int x = 0; x < attrList.getLength(); x++)
261             {
262                 attr = (Attr JavaDoc) attrList.item(x);
263
264                 Method JavaDoc[] m = c.getMethods();
265                 for(int y = 0; y < m.length; ++y)
266                 {
267                     if(m[y].getName().toLowerCase().endsWith(attr.getName().toLowerCase()))
268                     {
269                         System.out.print(".");
270                         System.out.print(m[y].getName());
271                         System.out.print("(\"");
272                         System.out.print(attr.getValue());
273                         System.out.print("\")");
274                         break;
275                     }
276                 }
277             }
278         }
279     }
280
281     public static void main(String JavaDoc[] args)
282     {
283         Html2Ecs html2ecs = new Html2Ecs();
284         try
285         {
286             input = new FileInputStream JavaDoc( args[0] );
287             parser = new DOMParser();
288             parser.parse(new InputSource JavaDoc(input));
289             doc = parser.getDocument();
290         }
291         catch(SAXException JavaDoc se)
292         {
293             System.out.println(se.toString());
294         }
295         catch(FileNotFoundException JavaDoc fnfe)
296         {
297             System.out.println(fnfe.toString());
298         }
299         catch(IOException JavaDoc ioe)
300         {
301             System.out.println(ioe.toString());
302         }
303         html2ecs.process( doc );
304     }
305 }
306
Popular Tags