KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > xerces > XercesDomFactory


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 Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: XercesDomFactory.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.dom.xerces;
25
26 import org.enhydra.apache.xerces.dom.DocumentImpl;
27 import org.enhydra.apache.xerces.dom.DocumentTypeImpl;
28 import org.enhydra.xml.xmlc.XMLCError;
29 import org.enhydra.xml.xmlc.XMLObject;
30 import org.enhydra.xml.xmlc.XMLObjectLink;
31 import org.enhydra.xml.xmlc.dom.AccessorGenerator;
32 import org.enhydra.xml.xmlc.dom.DocBuilderGenerator;
33 import org.enhydra.xml.xmlc.dom.XMLCDomFactory;
34 import org.enhydra.xml.xmlc.dom.generic.GenericAccessorGenerator;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.DocumentType JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39
40
41 /**
42  * XMLC DOM factory for creating DocumentType and Document objects based on
43  * the Xerces DOM.
44  */

45 public class XercesDomFactory implements XMLCDomFactory {
46     // FIXME: deprecated in 4.0 beta 2, can be removed when object code
47
// compatibility XMLC 2.0.1 is no longer required. This was removed
48
// since generate a variable name to an inner class broke forte.
49
/**
50      * Class that adds XMLObjectLink support to the Xerces Document.
51      * @deprecated Remains for compaitiblity with objects compiled
52      * with XMLC 2.0.1
53      */

54     public class LinkedXercesDocument extends DocumentImpl
55                                       implements XMLObjectLink {
56         /** Reference to XMLObject containing this Document. */
57         private XMLObject fXmlObjectLink;
58
59         /** Constructor. */
60         public LinkedXercesDocument(DocumentType JavaDoc docType) {
61             super(docType);
62         }
63
64         /** @see XMLObjectLink#setXMLObject */
65         public void setXMLObject(XMLObject xmlObject) {
66             fXmlObjectLink = xmlObject;
67         }
68
69         /** @see XMLObjectLink#getXMLObject */
70         public XMLObject getXMLObject() {
71             return fXmlObjectLink;
72         }
73     }
74
75     /*
76      * Suffix attached to interface name to form the implementation name.
77      */

78     private static final String JavaDoc IMPL_CLASS_SUFFIX = "Impl";
79
80     /*
81      * Xerces DOM to W3C package name map.
82      */

83     private static final String JavaDoc[][] DOM_PACKAGES_DOT_MAP = {
84         {"org.enhydra.apache.xerces.dom.", "org.w3c.dom."},
85         {"org.enhydra.apache.html.dom.", "org.w3c.dom.html."},
86     };
87
88     /*
89      * Special-case element names to interface name that can't be
90      * mapped just by changing the package.
91      */

92     private static final String JavaDoc[][] CLASS_INTERFACE_MAP = {
93         {"org.enhydra.apache.xerces.dom.ElementNSImpl", "org.w3c.dom.Element"},
94         {"org.enhydra.apache.xerces.dom.AttrNSImpl", "org.w3c.dom.Attr"},
95     };
96
97     /**
98      * @see XMLCDomFactory#getMIMEType
99      */

100     public String JavaDoc getMIMEType() {
101         return null;
102     }
103
104     /**
105      * @see XMLCDomFactory#getBaseClassName
106      */

107     public String JavaDoc getBaseClassName() {
108         return "org.enhydra.xml.xmlc.XMLObjectImpl";
109     }
110
111     /**
112      * @see XMLCDomFactory#getInterfaceNames
113      */

114     public String JavaDoc[] getInterfaceNames() {
115         return null;
116     }
117
118     /**
119      * @see XMLCDomFactory#getElementClassNames
120      */

121     public String JavaDoc[] getElementClassNames(Element JavaDoc element) {
122         return null; // Not specified in XML.
123
}
124
125     /**
126      * @see XMLCDomFactory#isURLAttribute
127      */

128     public boolean isURLAttribute(Element JavaDoc element,
129                                   String JavaDoc attrName) {
130         return false; // FIXME: Hook up to XLink??
131
}
132
133     /**
134      * @see XMLCDomFactory#createDocumentType
135      */

136     public DocumentType JavaDoc createDocumentType(String JavaDoc qualifiedName,
137                                            String JavaDoc publicId,
138                                            String JavaDoc systemId,
139                                            String JavaDoc internalSubset) {
140         DocumentTypeImpl docType = new DocumentTypeImpl(null, qualifiedName,
141                                                         publicId, systemId);
142         docType.setInternalSubset(internalSubset);
143         return docType;
144     }
145
146     /**
147      * This creates a Document object that extends the Xerces Document
148      * class and implements XMLObjectLink.
149      * @see XMLCDomFactory#createDocument
150      */

151     public Document JavaDoc createDocument(String JavaDoc namespaceURI,
152                                    String JavaDoc qualifiedName,
153                                    DocumentType JavaDoc docType) {
154         Document JavaDoc doc = new XercesLinkedDocument(docType);
155         // Create the document element
156
doc.appendChild(doc.createElementNS(namespaceURI, qualifiedName));
157         return doc;
158     }
159
160     /**
161      * @see XMLCDomFactory#nodeClassToInterface
162      */

163     public String JavaDoc nodeClassToInterface(Node JavaDoc node) {
164         String JavaDoc className = node.getClass().getName();
165
166         // Search explict class name mappings.
167
for (int idx = 0; idx < CLASS_INTERFACE_MAP.length; idx++) {
168             if (className.equals(CLASS_INTERFACE_MAP[idx][0])) {
169                 return CLASS_INTERFACE_MAP[idx][1];
170             }
171         }
172
173         // Search for package name mapping
174
int packageIdx;
175         for (packageIdx = 0; packageIdx < DOM_PACKAGES_DOT_MAP.length; packageIdx++) {
176             if (className.startsWith(DOM_PACKAGES_DOT_MAP[packageIdx][0])) {
177
178                 break;
179             }
180         }
181         if (packageIdx == DOM_PACKAGES_DOT_MAP.length) {
182             throw new XMLCError("Can't determine DOM interface for node of class \""
183                                 + className
184                                 + "\" (maybe be mismatch between XMLCDomFactory DOM implementation)");
185         }
186
187         // Remove the suffix to get the base class name.
188
int suffixIdx = className.lastIndexOf(IMPL_CLASS_SUFFIX);
189         if (suffixIdx < 0) {
190             throw new XMLCError("Class \"" + className
191                                 + "\" does not have suffix \"" + IMPL_CLASS_SUFFIX
192                                 + "\" (maybe be mismatch between XMLCDomFactory DOM implementation)");
193         }
194         
195         return DOM_PACKAGES_DOT_MAP[packageIdx][1]
196             + className.substring(DOM_PACKAGES_DOT_MAP[packageIdx][0].length(),
197                                   suffixIdx);
198     }
199
200     //FIXME: document still needed.
201

202     /**
203      * @see XMLCDomFactory#createAccessorGenerator
204      */

205     public AccessorGenerator createAccessorGenerator(Document JavaDoc document) {
206         return new GenericAccessorGenerator();
207     }
208
209     //FIXME: this method is getting weird...
210
/**
211      * @see XMLCDomFactory#createDocBuilderGenerator
212      */

213     public DocBuilderGenerator createDocBuilderGenerator(Document JavaDoc document) {
214         return new XercesDocBuilderGenerator();
215     }
216 }
217
Popular Tags