KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > LayoutFactory


1 package org.joshy.html;
2
3 import java.util.HashMap JavaDoc;
4 import org.w3c.dom.Node JavaDoc;
5 import org.w3c.dom.Element JavaDoc;
6 import org.joshy.u;
7 import org.joshy.html.forms.InputButton;
8 import org.joshy.html.forms.*;
9
10 /**
11 Returns the appropriate layout for a given node. Currently this hard codes
12 specific elements to specific layouts (for example, h1 is always an inline layout).
13 Instead this should use the display attribute to figure it out. The Inline and
14 Box layouts work together so that you don't need to specify one or the other,
15 always just use Inline. For lists, tables, and comments, though, we still need
16 to specify the particular layout here.
17
18 */

19 public class LayoutFactory {
20     private static HashMap JavaDoc element_map = new HashMap JavaDoc();
21     public static Layout getLayout(Node JavaDoc elem) {
22         //u.p("node = " + elem);
23
//u.p("getting layout for: " + elem.getNodeName());
24
if(elem.getNodeType() == elem.ELEMENT_NODE) {
25             if(elem.getNodeName().equals("div")) { return new InlineLayout(); }
26             if(elem.getNodeName().equals("p")) { return new InlineLayout(); }
27             if(elem.getNodeName().equals("span")) { return new InlineLayout(); }
28             if(elem.getNodeName().equals("u")) { return new InlineLayout(); }
29             /* deprectated in xhtml strict
30             if(elem.getNodeName().equals("strike")) { return new InlineLayout(); }
31             */

32             if(elem.getNodeName().equals("pre")) { return new InlineLayout(); }
33
34             /* these are not deprecated in xhtml strict but aren't used much.
35                here just for completeness */

36             if(elem.getNodeName().equals("tt")) { return new InlineLayout(); }
37             if(elem.getNodeName().equals("b")) { return new InlineLayout(); }
38             if(elem.getNodeName().equals("i")) { return new InlineLayout(); }
39             if(elem.getNodeName().equals("big")) { return new InlineLayout(); }
40             if(elem.getNodeName().equals("small")) { return new InlineLayout(); }
41             if(elem.getNodeName().equals("em")) { return new InlineLayout(); }
42             if(elem.getNodeName().equals("strong")) { return new InlineLayout(); }
43             if(elem.getNodeName().equals("dfn")) { return new InlineLayout(); }
44             if(elem.getNodeName().equals("code")) { return new InlineLayout(); }
45             if(elem.getNodeName().equals("samp")) { return new InlineLayout(); }
46             if(elem.getNodeName().equals("kbd")) { return new InlineLayout(); }
47             if(elem.getNodeName().equals("var")) { return new InlineLayout(); }
48             if(elem.getNodeName().equals("cite")) { return new InlineLayout(); }
49             if(elem.getNodeName().equals("ins")) { return new InlineLayout(); }
50             if(elem.getNodeName().equals("del")) { return new InlineLayout(); }
51             if(elem.getNodeName().equals("sup")) { return new InlineLayout(); }
52             if(elem.getNodeName().equals("sub")) { return new InlineLayout(); }
53
54             if(elem.getNodeName().equals("a")) { return new InlineLayout(); }
55             if(elem.getNodeName().equals("h1")) { return new InlineLayout(); }
56             if(elem.getNodeName().equals("h2")) { return new InlineLayout(); }
57             if(elem.getNodeName().equals("h3")) { return new InlineLayout(); }
58             if(elem.getNodeName().equals("h4")) { return new InlineLayout(); }
59             if(elem.getNodeName().equals("h5")) { return new InlineLayout(); }
60             if(elem.getNodeName().equals("h6")) { return new InlineLayout(); }
61
62             if(elem.getNodeName().equals("ul")) { return new ListLayout(); }
63             if(elem.getNodeName().equals("li")) { return new InlineLayout(); }
64             if(elem.getNodeName().equals("img")) { return new ImageLayout(); }
65
66             if(elem.getNodeName().equals("table")) { return new TableLayout2(); }
67             if(elem.getNodeName().equals("td")) { return new TableCellLayout(); }
68             if(elem.getNodeName().equals("th")) { return new TableCellLayout(); }
69
70             if(elem.getNodeName().equals("br")) { return new InlineLayout(); }
71             if(elem.getNodeName().equals("font")) { return new InlineLayout(); }
72             if(elem.getNodeName().equals("hr")) { return new NullLayout(); }
73             if(elem.getNodeName().equals("form")) { return new InlineLayout(); }
74             if(elem.getNodeName().equals("input")) {
75                 Element JavaDoc el = (Element JavaDoc)elem;
76                 if(el.getAttribute("type").equals("text")) {
77                     return new InputText();
78                 }
79                 return new InputButton();
80             }
81         }
82
83         // skip whitespace only nodes
84
if(elem.getNodeType() == elem.TEXT_NODE) {
85             if(elem.getNodeValue().trim().equals("")) {
86                 //u.p("skipping an empty text node");
87
return new NullLayout();
88             }
89         }
90
91         if(elem.getNodeType() == elem.TEXT_NODE) {
92             //return new SpanLayout();
93
return new AnonymousBoxLayout();
94         }
95
96
97         if(elem.getNodeType() == elem.COMMENT_NODE) {
98             return new NullLayout();
99         }
100         
101         Layout lyt = getCustomLayout(elem);
102         if(lyt != null) {
103             return lyt;
104         }
105         u.p("error! returning null! type = " + elem.getNodeType());
106         u.p("name = " + elem.getNodeName());
107         /*
108         u.p("node = " + elem);
109         u.p("attribute = " + elem.ATTRIBUTE_NODE);
110         u.p("cdata = " + elem.CDATA_SECTION_NODE);
111         u.p("coomment = " + elem.COMMENT_NODE);
112         u.p("frag = " + elem.DOCUMENT_FRAGMENT_NODE);
113         u.p("document = " + elem.DOCUMENT_NODE);
114         u.p("doctype = " + elem.DOCUMENT_TYPE_NODE);
115         u.p("element = " + elem.ELEMENT_NODE);
116         u.p("entity = " + elem.ENTITY_NODE);
117         u.p("entity ref = " + elem.ENTITY_REFERENCE_NODE);
118         u.p("notation = " + elem.NOTATION_NODE);
119         u.p("processing inst = " + elem.PROCESSING_INSTRUCTION_NODE);
120         u.p("text node = " + elem.TEXT_NODE);
121         */

122         return null;
123     }
124     
125     
126     public static void addCustomLayout(String JavaDoc elem_name, Layout layout) {
127         element_map.put(elem_name,layout);
128     }
129     
130     private static Layout getCustomLayout(Node JavaDoc elem) {
131         if(element_map.containsKey(elem.getNodeName())) {
132             return (Layout) element_map.get(elem.getNodeName());
133         }
134         return null;
135     }
136     
137     
138     
139     
140     
141     
142     
143     
144     
145     
146     
147 }
148
149
Popular Tags