KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > wireless > wml > WMLDomFactory


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: WMLDomFactory.java,v 1.2 2005/01/26 08:28:45 jkjome Exp $
21  */

22
23 package org.enhydra.wireless.wml;
24
25 import java.util.HashSet JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.enhydra.apache.xerces.dom.DocumentTypeImpl;
29 import org.enhydra.wireless.wml.dom.WMLDocument;
30 import org.enhydra.wireless.wml.dom.xerces.WMLDOMImplementationImpl;
31 import org.enhydra.xml.xmlc.XMLCError;
32 import org.enhydra.xml.xmlc.dom.XMLCDomFactory;
33 import org.enhydra.xml.xmlc.dom.xerces.XercesDomFactory;
34 import org.w3c.dom.DOMImplementation JavaDoc;
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  * XMLC DOM factory for creating WML-specified DocumentType and Document
42  * objects. Specifying this class as the <code>XMLCDomFctory</code> to xmlc
43  * will produce XMLC document class that are <code>WMLDocument</code> classes.
44  * This is specified using:
45  *
46  * <pre>
47  * xmlc&nbsp;-dom-factory&nbsp;org.enhydra.wireless.wml.WMLDomFactory
48  * </pre>
49  */

50 public class WMLDomFactory extends XercesDomFactory {
51     /**
52      * WML URL attributes.
53      */

54     private static String JavaDoc[] URL_ATTRIBUTES = {
55         "onenterforward", "onenterbackward",
56         "ontimer", "href",
57         "onpick", "src"
58     };
59
60     /**
61      * HashSet built from URL_ATTRIBUTES.
62      */

63     private static final HashSet JavaDoc urlAttributes;
64
65
66     private static final String JavaDoc IMPL_CLASS_SUFFIX = "Impl";
67     private static final String JavaDoc WML_IMPLEMENTATION_DOT = "org.enhydra.wireless.wml.dom.xerces";
68     private static final String JavaDoc WML_INTERFACE_DOT = "org.enhydra.wireless.wml.dom";
69
70     /**
71      * Class initializer.
72      */

73     static {
74         urlAttributes = new HashSet JavaDoc(URL_ATTRIBUTES.length);
75         for (int idx = 0; idx < URL_ATTRIBUTES.length; idx++) {
76             urlAttributes.add(URL_ATTRIBUTES[idx]);
77         }
78     }
79         
80     /**
81      * @see XMLCDomFactory#createDocumentType
82      */

83     public DocumentType JavaDoc createDocumentType(String JavaDoc qualifiedName,
84                                            String JavaDoc publicId,
85                                            String JavaDoc systemId,
86                                            String JavaDoc internalSubset) {
87         DOMImplementation JavaDoc domImpl = WMLDOMImplementationImpl.getDOMImplementation();
88         DocumentTypeImpl docType = (DocumentTypeImpl)domImpl.createDocumentType(qualifiedName, publicId, systemId);
89         docType.setInternalSubset(internalSubset);
90         return docType;
91     }
92
93     /**
94      * @see XMLCDomFactory#createDocument
95      */

96     public Document createDocument(String JavaDoc namespaceURI,
97                                    String JavaDoc qualifiedName,
98                                    DocumentType JavaDoc docType) {
99     DOMImplementation JavaDoc domImpl = WMLDOMImplementationImpl.getDOMImplementation();
100         Document doc = domImpl.createDocument(namespaceURI, qualifiedName,
101                                                docType);
102         return doc;
103     }
104
105     /**
106      * @see XMLCDomFactory#getMIMEType
107      */

108     public String JavaDoc getMIMEType() {
109         return "text/vnd.wap.wml";
110     }
111
112     /**
113      * @see XMLCDomFactory#getInterfaceNames
114      */

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

122     public String JavaDoc nodeClassToInterface(Node JavaDoc node) {
123     String JavaDoc ret = null;
124
125         String JavaDoc className = node.getClass().getName();
126     if (className.startsWith(WML_IMPLEMENTATION_DOT)) {
127         int suffixIdx = className.lastIndexOf(IMPL_CLASS_SUFFIX);
128         if (suffixIdx < 0) {
129         throw new XMLCError("Class \"" + className
130                     + "\" does not have suffix \"" + IMPL_CLASS_SUFFIX
131                     + "\" (maybe be mismatch between XMLC code DOM implementation)");
132         }
133         ret = WML_INTERFACE_DOT +
134         className.substring(WML_IMPLEMENTATION_DOT.length(), suffixIdx);
135     } else
136         ret = super.nodeClassToInterface(node);
137     return ret;
138     }
139
140     /**
141      * @see XMLCDomFactory#getElementClassNames
142      */

143     public String JavaDoc[] getElementClassNames(Element JavaDoc elem) {
144     String JavaDoc classNames = elem.getAttribute("class");
145         if ((classNames == null) || (classNames.length() ==0)) {
146             return null;
147         }
148
149         // Parse the class name.
150
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(classNames);
151         String JavaDoc[] names = new String JavaDoc[tokens.countTokens()];
152         for (int idx = 0; idx < names.length; idx++) {
153             names[idx] = tokens.nextToken();
154         }
155         return names;
156     }
157
158     /**
159      * @see XMLCDomFactory#isURLAttribute
160      */

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