1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58:
64: public class DomHTMLDocument
65: extends DomDocument
66: implements HTMLDocument
67: {
68:
69: private static final Class[] ELEMENT_PT = new Class[] {
70: DomHTMLDocument.class,
71: String.class,
72: String.class
73: };
74:
75: private static Map ELEMENT_CLASSES;
76: static
77: {
78: Map map = new HashMap();
79: map.put("a", DomHTMLAnchorElement.class);
80: map.put("applet", DomHTMLAppletElement.class);
81: map.put("area", DomHTMLAreaElement.class);
82: map.put("base", DomHTMLBaseElement.class);
83: map.put("basefont", DomHTMLBaseFontElement.class);
84: map.put("body", DomHTMLBodyElement.class);
85: map.put("br", DomHTMLBRElement.class);
86: map.put("button", DomHTMLButtonElement.class);
87: map.put("dir", DomHTMLDirectoryElement.class);
88: map.put("div", DomHTMLDivElement.class);
89: map.put("dlist", DomHTMLDListElement.class);
90: map.put("fieldset", DomHTMLFieldSetElement.class);
91: map.put("font", DomHTMLFontElement.class);
92: map.put("form", DomHTMLFormElement.class);
93: map.put("frame", DomHTMLFrameElement.class);
94: map.put("frameset", DomHTMLFrameSetElement.class);
95: map.put("head", DomHTMLHeadElement.class);
96: map.put("h1", DomHTMLHeadingElement.class);
97: map.put("h2", DomHTMLHeadingElement.class);
98: map.put("h3", DomHTMLHeadingElement.class);
99: map.put("h4", DomHTMLHeadingElement.class);
100: map.put("h5", DomHTMLHeadingElement.class);
101: map.put("h6", DomHTMLHeadingElement.class);
102: map.put("html", DomHTMLHtmlElement.class);
103: map.put("iframe", DomHTMLIFrameElement.class);
104: map.put("img", DomHTMLImageElement.class);
105: map.put("input", DomHTMLInputElement.class);
106: map.put("isindex", DomHTMLIsIndexElement.class);
107: map.put("label", DomHTMLLabelElement.class);
108: map.put("legend", DomHTMLLegendElement.class);
109: map.put("li", DomHTMLLIElement.class);
110: map.put("link", DomHTMLLinkElement.class);
111: map.put("map", DomHTMLMapElement.class);
112: map.put("menu", DomHTMLMenuElement.class);
113: map.put("meta", DomHTMLMetaElement.class);
114: map.put("ins", DomHTMLModElement.class);
115: map.put("del", DomHTMLModElement.class);
116: map.put("object", DomHTMLObjectElement.class);
117: map.put("ol", DomHTMLOListElement.class);
118: map.put("optgroup", DomHTMLOptGroupElement.class);
119: map.put("option", DomHTMLOptionElement.class);
120: map.put("p", DomHTMLParagraphElement.class);
121: map.put("param", DomHTMLParamElement.class);
122: map.put("pre", DomHTMLPreElement.class);
123: map.put("q", DomHTMLQuoteElement.class);
124: map.put("blockquote", DomHTMLQuoteElement.class);
125: map.put("script", DomHTMLScriptElement.class);
126: map.put("select", DomHTMLSelectElement.class);
127: map.put("style", DomHTMLStyleElement.class);
128: map.put("caption", DomHTMLTableCaptionElement.class);
129: map.put("th", DomHTMLTableCellElement.class);
130: map.put("td", DomHTMLTableCellElement.class);
131: map.put("col", DomHTMLTableColElement.class);
132: map.put("colgroup", DomHTMLTableColElement.class);
133: map.put("table", DomHTMLTableElement.class);
134: map.put("tr", DomHTMLTableRowElement.class);
135: map.put("thead", DomHTMLTableSectionElement.class);
136: map.put("tfoot", DomHTMLTableSectionElement.class);
137: map.put("tbody", DomHTMLTableSectionElement.class);
138: map.put("textarea", DomHTMLTextAreaElement.class);
139: map.put("title", DomHTMLTitleElement.class);
140: map.put("ul", DomHTMLUListElement.class);
141: ELEMENT_CLASSES = Collections.unmodifiableMap(map);
142: }
143:
144: private static Set HTML_NS_URIS;
145: static
146: {
147: Set set = new HashSet();
148: set.add("http://www.w3.org/TR/html4/strict");
149: set.add("http://www.w3.org/TR/html4/loose");
150: set.add("http://www.w3.org/TR/html4/frameset");
151: set.add("http://www.w3.org/1999/xhtml");
152: set.add("http://www.w3.org/TR/xhtml1/strict");
153: set.add("http://www.w3.org/TR/xhtml1/loose");
154: set.add("http://www.w3.org/TR/xhtml1/frameset");
155: HTML_NS_URIS = Collections.unmodifiableSet(set);
156: }
157:
158:
161: public DomHTMLDocument()
162: {
163: this(new DomHTMLImpl());
164: }
165:
166:
170: public DomHTMLDocument(DomHTMLImpl impl)
171: {
172: super(impl);
173: }
174:
175: private Node getChildNodeByName(Node parent, String name)
176: {
177: for (Node ctx = parent.getFirstChild(); ctx != null;
178: ctx = ctx.getNextSibling())
179: {
180: if (name.equalsIgnoreCase(ctx.getNodeName()))
181: {
182: return ctx;
183: }
184: }
185: return null;
186: }
187:
188: public String getTitle()
189: {
190: Node html = getDocumentElement();
191: if (html != null)
192: {
193: Node head = getChildNodeByName(html, "head");
194: if (head != null)
195: {
196: Node title = getChildNodeByName(head, "title");
197: if (title != null)
198: {
199: return title.getTextContent();
200: }
201: }
202: }
203: return null;
204: }
205:
206: public void setTitle(String title)
207: {
208: Node html = getDocumentElement();
209: if (html == null)
210: {
211: html = createElement("html");
212: appendChild(html);
213: }
214: Node head = getChildNodeByName(html, "head");
215: if (head == null)
216: {
217: head = createElement("head");
218: Node first = html.getFirstChild();
219: if (first != null)
220: {
221: html.insertBefore(first, head);
222: }
223: else
224: {
225: html.appendChild(head);
226: }
227: }
228: Node titleNode = getChildNodeByName(head, "title");
229: if (titleNode == null)
230: {
231: titleNode = createElement("title");
232: Node first = head.getFirstChild();
233: if (first != null)
234: {
235: head.insertBefore(first, titleNode);
236: }
237: else
238: {
239: head.appendChild(titleNode);
240: }
241: }
242: titleNode.setTextContent(title);
243: }
244:
245: public String getReferrer()
246: {
247:
248: return null;
249: }
250:
251: public String getDomain()
252: {
253: try
254: {
255: URL url = new URL(getDocumentURI());
256: return url.getHost();
257: }
258: catch (MalformedURLException e)
259: {
260: return null;
261: }
262: }
263:
264: public String getURL()
265: {
266: return getDocumentURI();
267: }
268:
269: public HTMLElement getBody()
270: {
271: Node html = getDocumentElement();
272: if (html != null)
273: {
274: Node body = getChildNodeByName(html, "body");
275: if (body == null)
276: {
277: body = getChildNodeByName(html, "frameset");
278: }
279: return (HTMLElement) body;
280: }
281: return null;
282: }
283:
284: public void setBody(HTMLElement body)
285: {
286: Node html = getDocumentElement();
287: if (html == null)
288: {
289: html = createElement("html");
290: appendChild(html);
291: }
292: Node ref = getBody();
293: if (ref == null)
294: {
295: html.appendChild(body);
296: }
297: else
298: {
299: html.replaceChild(body, ref);
300: }
301: }
302:
303: public HTMLCollection getImages()
304: {
305: DomHTMLCollection ret = new DomHTMLCollection(this, this);
306: ret.addNodeName("img");
307: ret.evaluate();
308: return ret;
309: }
310:
311: public HTMLCollection getApplets()
312: {
313: DomHTMLCollection ret = new DomHTMLCollection(this, this);
314: ret.addNodeName("object");
315: ret.addNodeName("applet");
316: ret.evaluate();
317: return ret;
318: }
319:
320: public HTMLCollection getLinks()
321: {
322: DomHTMLCollection ret = new DomHTMLCollection(this, this);
323: ret.addNodeName("area");
324: ret.addNodeName("a");
325: ret.evaluate();
326: return ret;
327: }
328:
329: public HTMLCollection getForms()
330: {
331: DomHTMLCollection ret = new DomHTMLCollection(this, this);
332: ret.addNodeName("form");
333: ret.evaluate();
334: return ret;
335: }
336:
337: public HTMLCollection getAnchors()
338: {
339: DomHTMLCollection ret = new DomHTMLCollection(this, this);
340: ret.addNodeName("a");
341: ret.addAttributeName("name");
342: ret.evaluate();
343: return ret;
344: }
345:
346: public String getCookie()
347: {
348:
349: return null;
350: }
351:
352: public void setCookie(String cookie)
353: {
354:
355: }
356:
357: public void open()
358: {
359:
360: }
361:
362: public void close()
363: {
364:
365: }
366:
367: public void write(String text)
368: {
369:
370: }
371:
372: public void writeln(String text)
373: {
374:
375: }
376:
377: public NodeList getElementsByName(String name)
378: {
379: DomHTMLCollection ret = new DomHTMLCollection(this, this);
380: ret.addNodeName(name);
381: ret.evaluate();
382: return ret;
383:
384: }
385:
386: public Element createElement(String tagName)
387: {
388: return createElementNS(null, tagName);
389: }
390:
391: public Element createElementNS(String uri, String qName)
392: {
393:
394: if (uri != null && !HTML_NS_URIS.contains(uri))
395: {
396: return super.createElementNS(uri, qName);
397: }
398: String localName = qName.toLowerCase();
399: int ci = qName.indexOf(':');
400: if (ci != -1)
401: {
402: localName = qName.substring(ci + 1);
403: }
404: Class t = (Class) ELEMENT_CLASSES.get(localName);
405:
406: if (t == null)
407: {
408: return super.createElementNS(uri, qName);
409: }
410: try
411: {
412: Constructor c = t.getDeclaredConstructor(ELEMENT_PT);
413: Object[] args = new Object[] { this, uri, qName };
414: return (Element) c.newInstance(args);
415: }
416: catch (Exception e)
417: {
418: DOMException e2 = new DomDOMException(DOMException.TYPE_MISMATCH_ERR);
419: e2.initCause(e);
420: throw e2;
421: }
422: }
423:
424: }