KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xhtml > HTMLDomFactory


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Original Code is DigitalSesame
15  * Portions created by DigitalSesame are Copyright (C) 1997-2000 DigitalSesame
16  * All Rights Reserved.
17  *
18  * Contributor(s):
19  *
20  * $Id: HTMLDomFactory.java,v 1.3 2005/01/26 08:28:45 jkjome Exp $
21  */

22
23 package org.enhydra.xml.xhtml;
24
25 import org.enhydra.apache.xerces.dom.DocumentTypeImpl;
26 import org.enhydra.xml.xhtml.dom.xerces.XHTMLDOMImplementationImpl;
27 import org.enhydra.xml.xmlc.XMLCError;
28 import org.enhydra.xml.xmlc.dom.HTMLDomFactoryMethods;
29 import org.enhydra.xml.xmlc.dom.XMLCDomFactory;
30 import org.enhydra.xml.xmlc.dom.xerces.XercesDomFactory;
31 import org.w3c.dom.DOMImplementation JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.DocumentType JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36
37 /**
38  * XMLC DOM factory for XHTML.
39  * @see XMLCDomFactory
40  */

41 public class HTMLDomFactory extends XercesDomFactory implements XMLCDomFactory {
42     private static final String JavaDoc IMPL_CLASS_SUFFIX = "Impl";
43     private static final String JavaDoc XHTML_IMPLEMENTATION_DOT = "org.enhydra.xml.xhtml.dom.xerces.X";
44
45     private static final String JavaDoc XHTML_INTERFACE_DOT = "org.w3c.dom.html.";
46
47     private java.util.HashMap JavaDoc ClassNameMap = new java.util.HashMap JavaDoc();
48
49     /*
50      * Special-case element names to interface name that can't be
51      * mapped just by changing the package.
52      */

53     private static final String JavaDoc[][] CLASS_INTERFACE_MAP = {
54       // Note: the following list was auto-generated by test2 in the
55
// src/tests/org/enhydra/xml/xhtml directory, as the output file
56
// 'classes.map'
57
{"org.enhydra.xml.xhtml.dom.xerces.XHTMLEmElementImpl", "org.w3c.dom.html.HTMLElement"},
58       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLSmallElementImpl", "org.w3c.dom.html.HTMLElement"},
59       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLNoframesElementImpl", "org.w3c.dom.html.HTMLElement"},
60       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLBdoElementImpl", "org.w3c.dom.html.HTMLElement"},
61       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLDtElementImpl", "org.w3c.dom.html.HTMLElement"},
62       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLSpanElementImpl", "org.w3c.dom.html.HTMLElement"},
63       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLStrongElementImpl", "org.w3c.dom.html.HTMLElement"},
64       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLKbdElementImpl", "org.w3c.dom.html.HTMLElement"},
65       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLDdElementImpl", "org.w3c.dom.html.HTMLElement"},
66       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLBigElementImpl", "org.w3c.dom.html.HTMLElement"},
67       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLCodeElementImpl", "org.w3c.dom.html.HTMLElement"},
68       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLTbodyElementImpl", "org.w3c.dom.html.HTMLTableSectionElement"},
69       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLTheadElementImpl", "org.w3c.dom.html.HTMLTableSectionElement"},
70       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLTfootElementImpl", "org.w3c.dom.html.HTMLTableSectionElement"},
71       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLUElementImpl", "org.w3c.dom.html.HTMLElement"},
72       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLSElementImpl", "org.w3c.dom.html.HTMLElement"},
73       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLIElementImpl", "org.w3c.dom.html.HTMLElement"},
74       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLBElementImpl", "org.w3c.dom.html.HTMLElement"},
75       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLTtElementImpl", "org.w3c.dom.html.HTMLElement"},
76       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLCenterElementImpl", "org.w3c.dom.html.HTMLElement"},
77       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLSampElementImpl", "org.w3c.dom.html.HTMLElement"},
78       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLDfnElementImpl", "org.w3c.dom.html.HTMLElement"},
79       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLNoscriptElementImpl", "org.w3c.dom.html.HTMLElement"},
80       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLSupElementImpl", "org.w3c.dom.html.HTMLElement"},
81       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLStrikeElementImpl", "org.w3c.dom.html.HTMLElement"},
82       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLAcronymElementImpl", "org.w3c.dom.html.HTMLElement"},
83       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLSubElementImpl", "org.w3c.dom.html.HTMLElement"},
84       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLCiteElementImpl", "org.w3c.dom.html.HTMLElement"},
85       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLVarElementImpl", "org.w3c.dom.html.HTMLElement"},
86       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLAbbrElementImpl", "org.w3c.dom.html.HTMLElement"},
87       {"org.enhydra.xml.xhtml.dom.xerces.XHTMLAddressElementImpl", "org.w3c.dom.html.HTMLElement"},
88     };
89
90     /**
91      * @see XMLCDomFactory#createDocumentType
92      */

93     public DocumentType JavaDoc createDocumentType(String JavaDoc qualifiedName,
94                                            String JavaDoc publicId,
95                                            String JavaDoc systemId,
96                                            String JavaDoc internalSubset) {
97         DOMImplementation JavaDoc domImpl = XHTMLDOMImplementationImpl.getHTMLDOMImplementation();
98         DocumentTypeImpl docType =
99             (DocumentTypeImpl)domImpl.createDocumentType(qualifiedName, publicId, systemId);
100         docType.setInternalSubset(internalSubset);
101         return docType;
102     }
103
104     /**
105      * @see XMLCDomFactory#createDocument
106      */

107     public Document JavaDoc createDocument(String JavaDoc namespaceURI,
108                                    String JavaDoc qualifiedName,
109                                    DocumentType JavaDoc docType) {
110         if (qualifiedName == null) {
111             throw new XMLCError("qualifiedName is null for XHTML document;"
112                                 + " maybe trying to use a HTML parser on XHTML");
113         }
114
115     DOMImplementation JavaDoc domImpl = XHTMLDOMImplementationImpl.getHTMLDOMImplementation();
116     return domImpl.createDocument(namespaceURI, qualifiedName, docType);
117     }
118
119     /**
120      * The mime type for XHTML is a matter of debate. There are arguments
121      * for text/xml, text/xhtml, text/html, application/xml,
122      * or application/xhtml+xml. This returns text/html.
123      * @see XMLCDomFactory#getMIMEType
124      * @see XHTMLDomFactory#getMIMEType
125      */

126     public String JavaDoc getMIMEType() {
127         //return "text/xml";
128

129         //text/html for compatibility with non-XHTML compliant user agents.
130
//See http://www.w3.org/TR/xhtml1/#guidelines C.9
131
return "text/html";
132     }
133
134     /**
135      * @see XMLCDomFactory#getBaseClassName
136      */

137     public String JavaDoc getBaseClassName() {
138         return HTMLDomFactoryMethods.getBaseClassName();
139     }
140
141     /**
142      * @see XMLCDomFactory#getInterfaceNames
143      */

144     public String JavaDoc[] getInterfaceNames() {
145         return HTMLDomFactoryMethods.getInterfaceNames();
146     }
147
148     /**
149      * @see XMLCDomFactory#nodeClassToInterface
150      */

151     public String JavaDoc nodeClassToInterface(Node JavaDoc node) {
152         String JavaDoc className = node.getClass().getName();
153
154         // Search explict class name mappings.
155
for (int idx = 0; idx < CLASS_INTERFACE_MAP.length; idx++) {
156             if (className.equals(CLASS_INTERFACE_MAP[idx][0])) {
157                 return CLASS_INTERFACE_MAP[idx][1];
158             }
159         }
160
161     if (className.startsWith(XHTML_IMPLEMENTATION_DOT)) {
162         int suffixIdx = className.lastIndexOf(IMPL_CLASS_SUFFIX);
163         if (suffixIdx < 0) {
164         throw new XMLCError("Class \"" + className
165                     + "\" does not have suffix \"" + IMPL_CLASS_SUFFIX
166                     + "\" (maybe be mismatch between XMLC code DOM implementation)");
167         }
168             return XHTML_INTERFACE_DOT +
169                 className.substring(XHTML_IMPLEMENTATION_DOT.length(), suffixIdx);
170
171     } else {
172             // Unable to find or translate the node implementation class to a suitable
173
// iterface, so throw an exception.
174
throw new XMLCError("Can't determine DOM interface for node of class \""
175                                 + className
176                                 + "\" (maybe be mismatch between XMLCDomFactory DOM implementation)");
177
178         }
179     }
180
181     /**
182      * @see XMLCDomFactory#getElementClassNames
183      */

184     public String JavaDoc[] getElementClassNames(Element JavaDoc element) {
185         return HTMLDomFactoryMethods.getElementClassNames(element);
186     }
187
188     /**
189      * @see XMLCDomFactory#isURLAttribute
190      */

191     public boolean isURLAttribute(Element JavaDoc element,
192                                   String JavaDoc attrName) {
193         return HTMLDomFactoryMethods.isURLAttribute(element, attrName);
194     }
195 }
196
Popular Tags