KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > ElementFactory


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Oct 8, 2005
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import java.util.*;
27 import org.w3c.dom.html2.*;
28 import org.w3c.dom.*;
29
30 class ElementFactory {
31     private final Map builders = new HashMap();
32     
33     private ElementFactory() {
34         // This sets up builders for each known element tag.
35
Map builders = this.builders;
36         builders.put("HTML", new HTMLElementBuilder.Html());
37         builders.put("TITLE", new HTMLElementBuilder.Title());
38         builders.put("BASE", new HTMLElementBuilder.Base());
39         
40         HTMLElementBuilder div = new HTMLElementBuilder.Div();
41         builders.put("DIV", div);
42         builders.put("DL", div);
43         
44         builders.put("BODY", new HTMLElementBuilder.Body());
45         builders.put("CENTER", new HTMLElementBuilder.Center());
46         builders.put("CAPTION", new HTMLElementBuilder.Center());
47         builders.put("PRE", new HTMLElementBuilder.Pre());
48         builders.put("P", new HTMLElementBuilder.P());
49
50         HTMLElementBuilder bq = new HTMLElementBuilder.Blockquote();
51         builders.put("BLOCKQUOTE", bq);
52         builders.put("DD", bq);
53         
54         builders.put("SPAN", new HTMLElementBuilder.Span());
55         builders.put("SCRIPT", new HTMLElementBuilder.Script());
56         builders.put("IMG", new HTMLElementBuilder.Img());
57         builders.put("STYLE", new HTMLElementBuilder.Style());
58         builders.put("LINK", new HTMLElementBuilder.Link());
59         builders.put("A", new HTMLElementBuilder.Anchor());
60         builders.put("ANCHOR", new HTMLElementBuilder.Anchor());
61         builders.put("TABLE", new HTMLElementBuilder.Table());
62         builders.put("TD", new HTMLElementBuilder.Td());
63         builders.put("TH", new HTMLElementBuilder.Th());
64         builders.put("TR", new HTMLElementBuilder.Tr());
65         builders.put("FORM", new HTMLElementBuilder.Form());
66         builders.put("INPUT", new HTMLElementBuilder.Input());
67         builders.put("BUTTON", new HTMLElementBuilder.Button());
68         builders.put("TEXTAREA", new HTMLElementBuilder.Textarea());
69         builders.put("SELECT", new HTMLElementBuilder.Select());
70         builders.put("OPTION", new HTMLElementBuilder.Option());
71         builders.put("FRAMESET", new HTMLElementBuilder.Frameset());
72         builders.put("FRAME", new HTMLElementBuilder.Frame());
73         builders.put("IFRAME", new HTMLElementBuilder.IFrame());
74         builders.put("UL", new HTMLElementBuilder.Ul());
75         builders.put("OL", new HTMLElementBuilder.Ol());
76         builders.put("LI", new HTMLElementBuilder.Li());
77         builders.put("HR", new HTMLElementBuilder.Hr());
78         builders.put("BR", new HTMLElementBuilder.Br());
79         builders.put("OBJECT", new HTMLElementBuilder.HtmlObject());
80         builders.put("APPLET", new HTMLElementBuilder.Applet());
81         builders.put("EMBED", new HTMLElementBuilder.NonStandard());
82         builders.put("FONT", new HTMLElementBuilder.Font());
83         builders.put("BASEFONT", new HTMLElementBuilder.BaseFont());
84         
85         builders.put("TT", new HTMLElementBuilder.Tt());
86         builders.put("CODE", new HTMLElementBuilder.Code());
87         builders.put("SMALL", new HTMLElementBuilder.Small());
88         builders.put("BIG", new HTMLElementBuilder.Big());
89         builders.put("B", new HTMLElementBuilder.Strong());
90         builders.put("STRONG", new HTMLElementBuilder.Strong());
91         
92         builders.put("U", new HTMLElementBuilder.Underline());
93         builders.put("STRIKE", new HTMLElementBuilder.Strike());
94         
95         HTMLElementBuilder em = new HTMLElementBuilder.Em();
96         builders.put("I", em);
97         builders.put("EM", em);
98         builders.put("CITE", em);
99
100         HTMLElementBuilder heading = new HTMLElementBuilder.Heading();
101         builders.put("H1", heading);
102         builders.put("H2", heading);
103         builders.put("H3", heading);
104         builders.put("H4", heading);
105         builders.put("H5", heading);
106         builders.put("H6", heading);
107     }
108     
109     private static ElementFactory instance = new ElementFactory();
110     
111     public static ElementFactory getInstance() {
112         return instance;
113     }
114     
115     public final HTMLElement createElement(HTMLDocumentImpl document, String JavaDoc name) throws DOMException {
116         String JavaDoc normalName = name.toUpperCase();
117         // No need to synchronize; read-only map at this point.
118
HTMLElementBuilder builder = (HTMLElementBuilder) this.builders.get(normalName);
119         if(builder == null) {
120             //TODO: IE would assume name is html text here?
121
HTMLElementImpl element = new HTMLElementImpl(name);
122             element.setOwnerDocument(document);
123             return element;
124         }
125         else {
126             return builder.create(document, name);
127         }
128     }
129 }
130
Popular Tags