KickJava   Java API By Example, From Geeks To Geeks.

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


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: XHTMLDomFactory.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 XHTMLDomFactory 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.";
44
45     private static final String JavaDoc XHTML_INTERFACE_DOT = "org.enhydra.xml.xhtml.dom.";
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         /*
55         ** Currently empty, but for example...
56         **
57         {"org.enhydra.apache.xerces.dom.ElementNSImpl", "org.w3c.dom.Element"},
58         {"org.enhydra.apache.xerces.dom.AttrNSImpl", "org.w3c.dom.Attr"},
59         */

60     };
61
62     /**
63      * @see XMLCDomFactory#createDocumentType
64      */

65     public DocumentType JavaDoc createDocumentType(String JavaDoc qualifiedName,
66                                            String JavaDoc publicId,
67                                            String JavaDoc systemId,
68                                            String JavaDoc internalSubset) {
69         DOMImplementation JavaDoc domImpl = XHTMLDOMImplementationImpl.getHTMLDOMImplementation();
70         DocumentTypeImpl docType =
71             (DocumentTypeImpl)domImpl.createDocumentType(qualifiedName, publicId, systemId);
72         docType.setInternalSubset(internalSubset);
73         return docType;
74     }
75
76     /**
77      * @see XMLCDomFactory#createDocument
78      */

79     public Document JavaDoc createDocument(String JavaDoc namespaceURI,
80                                    String JavaDoc qualifiedName,
81                                    DocumentType JavaDoc docType) {
82         if (qualifiedName == null) {
83             throw new XMLCError("qualifiedName is null for XHTML document;"
84                                 + " maybe trying to use a HTML parser on XHTML");
85         }
86
87     DOMImplementation JavaDoc domImpl = XHTMLDOMImplementationImpl.getHTMLDOMImplementation();
88     return domImpl.createDocument(namespaceURI, qualifiedName, docType);
89     }
90
91     /**
92      * The mime type for XHTML is a matter of debate. There are arguments
93      * for text/xml, text/xhtml, text/html, application/xml,
94      * or application/xhtml+xml. This returns application/xhtml+xml.
95      * @see XMLCDomFactory#getMIMEType
96      * @see HTMLDomFactory#getMIMEType
97      */

98     public String JavaDoc getMIMEType() {
99         //return "text/xml";
100

101         //more specific xhtml mime-type
102
return "application/xhtml+xml";
103     }
104
105     /**
106      * @see XMLCDomFactory#getBaseClassName
107      */

108     public String JavaDoc getBaseClassName() {
109         return HTMLDomFactoryMethods.getBaseClassName();
110     }
111
112     /**
113      * @see XMLCDomFactory#getInterfaceNames
114      */

115     public String JavaDoc[] getInterfaceNames() {
116         return HTMLDomFactoryMethods.getInterfaceNames();
117     }
118
119     /**
120      * @see XMLCDomFactory#nodeClassToInterface
121      */

122     public String JavaDoc nodeClassToInterface(Node JavaDoc node) {
123         String JavaDoc className = node.getClass().getName();
124
125         // Search explict class name mappings.
126
for (int idx = 0; idx < CLASS_INTERFACE_MAP.length; idx++) {
127             if (className.equals(CLASS_INTERFACE_MAP[idx][0])) {
128                 return CLASS_INTERFACE_MAP[idx][1];
129             }
130         }
131
132     if (className.startsWith(XHTML_IMPLEMENTATION_DOT)) {
133         int suffixIdx = className.lastIndexOf(IMPL_CLASS_SUFFIX);
134         if (suffixIdx < 0) {
135         throw new XMLCError("Class \"" + className
136                     + "\" does not have suffix \"" + IMPL_CLASS_SUFFIX
137                     + "\" (maybe be mismatch between XMLC code DOM implementation)");
138         }
139             return XHTML_INTERFACE_DOT +
140                 className.substring(XHTML_IMPLEMENTATION_DOT.length(), suffixIdx);
141
142     } else {
143             // Unable to find or translate the node implementation class to a suitable
144
// iterface, so throw an exception.
145
throw new XMLCError("Can't determine DOM interface for node of class \""
146                                 + className
147                                 + "\" (maybe be mismatch between XMLCDomFactory DOM implementation)");
148
149         }
150     }
151
152     /**
153      * @see XMLCDomFactory#getElementClassNames
154      */

155     public String JavaDoc[] getElementClassNames(Element JavaDoc element) {
156         return HTMLDomFactoryMethods.getElementClassNames(element);
157     }
158
159     /**
160      * @see XMLCDomFactory#isURLAttribute
161      */

162     public boolean isURLAttribute(Element JavaDoc element,
163                                   String JavaDoc attrName) {
164         return HTMLDomFactoryMethods.isURLAttribute(element, attrName);
165     }
166 }
167
Popular Tags