KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > lazydom > LazyDomFactory


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: LazyDomFactory.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.dom.lazydom;
25
26 import org.enhydra.xml.lazydom.LazyDocument;
27 import org.enhydra.xml.lazydom.LazyDocumentType;
28 import org.enhydra.xml.lazydom.TemplateDOM;
29 import org.enhydra.xml.xmlc.XMLCError;
30 import org.enhydra.xml.xmlc.dom.AccessorGenerator;
31 import org.enhydra.xml.xmlc.dom.DocBuilderGenerator;
32 import org.enhydra.xml.xmlc.dom.XMLCDomFactory;
33 import org.enhydra.xml.xmlc.dom.xerces.XercesDomFactory;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.DocumentType JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37
38 /**
39  * Lazy DOM factory for creating DocumentType and Document objects.
40  */

41 public class LazyDomFactory extends XercesDomFactory
42     implements XMLCDomFactory {
43     /*
44      * Prefix attached to interface name to form the implementation names
45      * (not use on HTML DOM).
46      */

47     private static final String JavaDoc IMPL_CLASS_PREFIX = "Lazy";
48
49     /*
50      * Suffix attached to interface name to form the implementation names
51      * for the HTML DOM.
52      */

53     private static final String JavaDoc IMPL_CLASS_SUFFIX = "Impl";
54
55     /*
56      * Lazy DOM to W3C package name map.
57      * WARNING: Order is important here to handle most significant match.
58      */

59     private static final String JavaDoc[][] DOM_PACKAGES_DOT_MAP = {
60         {"org.enhydra.xml.lazydom.html.", "org.w3c.dom.html."},
61         {"org.enhydra.xml.lazydom.", "org.w3c.dom."},
62     };
63
64     /*
65      * Special-case element names to interface name that can't be
66      * mapped just by changing the package.
67      */

68     private static final String JavaDoc[][] CLASS_INTERFACE_MAP = {
69         {"org.enhydra.xml.lazydom.LazyElementNS", "org.w3c.dom.Element"},
70         {"org.enhydra.xml.lazydom.LazyElementNoNS", "org.w3c.dom.Element"},
71         {"org.enhydra.xml.lazydom.LazyAttrNS", "org.w3c.dom.Attr"},
72         {"org.enhydra.xml.lazydom.LazyAttrNoNS", "org.w3c.dom.Attr"},
73     };
74
75     /**
76      * @see XMLCDomFactory#createDocumentType
77      */

78     public DocumentType createDocumentType(String JavaDoc qualifiedName,
79                                            String JavaDoc publicId,
80                                            String JavaDoc systemId,
81                                            String JavaDoc internalSubset) {
82         return new LazyDocumentType(null, null, qualifiedName,
83                                     publicId, systemId,
84                                     internalSubset);
85     }
86
87     /**
88      * @see XMLCDomFactory#createDocument
89      */

90     public Document createDocument(String JavaDoc namespaceURI,
91                                    String JavaDoc qualifiedName,
92                                    DocumentType docType) {
93         Document doc = new LazyDocument(docType, null);
94         // Create the document element
95
doc.appendChild(doc.createElementNS(namespaceURI, qualifiedName));
96         return doc;
97     }
98
99     /**
100      * Create a LazyDocument from a template.
101      * @see XMLCDomFactory#createDocument
102      */

103     public Document createDocument(TemplateDOM templateDOM) {
104         return new LazyDocument(null, templateDOM);
105     }
106
107     /**
108      * @see XMLCDomFactory#nodeClassToInterface
109      */

110     public String JavaDoc nodeClassToInterface(Node JavaDoc node) {
111         String JavaDoc className = node.getClass().getName();
112
113         // Search explict class name mappings.
114
for (int idx = 0; idx < CLASS_INTERFACE_MAP.length; idx++) {
115             if (className.equals(CLASS_INTERFACE_MAP[idx][0])) {
116                 return CLASS_INTERFACE_MAP[idx][1];
117             }
118         }
119
120         // Search for package name mapping
121
int packageIdx;
122         for (packageIdx = 0; packageIdx < DOM_PACKAGES_DOT_MAP.length; packageIdx++) {
123             if (className.startsWith(DOM_PACKAGES_DOT_MAP[packageIdx][0])) {
124                 break;
125             }
126         }
127         if (packageIdx == DOM_PACKAGES_DOT_MAP.length) {
128             throw new XMLCError("Can't determine DOM interface for node of class \""
129                                 + className
130                                 + "\" (maybe be mismatch between XMLCDomFactory DOM implementation)");
131         }
132
133         String JavaDoc unqualClassName = className.substring(DOM_PACKAGES_DOT_MAP[packageIdx][0].length());
134
135         // Remove the prefix and suffix to get the base class name.
136
int prefixIdx = unqualClassName.lastIndexOf(IMPL_CLASS_PREFIX);
137         if (prefixIdx >= 0) {
138             unqualClassName = unqualClassName.substring(IMPL_CLASS_PREFIX.length());
139         }
140         int suffixIdx = unqualClassName.lastIndexOf(IMPL_CLASS_SUFFIX);
141         if (suffixIdx >= 0) {
142             unqualClassName = unqualClassName.substring(0, suffixIdx);
143         }
144         
145         return DOM_PACKAGES_DOT_MAP[packageIdx][1] + unqualClassName;
146     }
147
148     /**
149      * @see XMLCDomFactory#createAccessorGenerator
150      */

151     public AccessorGenerator createAccessorGenerator(Document document) {
152         return new LazyDOMAccessorGenerator(document);
153     }
154
155     /**
156      * @see XMLCDomFactory#createDocBuilderGenerator
157      */

158     public DocBuilderGenerator createDocBuilderGenerator(Document document) {
159         return new LazyDOMDocBuilderGenerator();
160     }
161 }
162
Popular Tags