KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > platform > xml > jaxp > JAXPPlatform


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.platform.xml.jaxp;
23
24 import java.net.URL JavaDoc;
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import oracle.toplink.essentials.platform.xml.XMLNamespaceResolver;
28 import oracle.toplink.essentials.platform.xml.XMLParser;
29 import oracle.toplink.essentials.platform.xml.XMLPlatform;
30 import oracle.toplink.essentials.platform.xml.XMLPlatformException;
31 import oracle.toplink.essentials.platform.xml.XMLTransformer;
32 import org.w3c.dom.Attr JavaDoc;
33 import org.w3c.dom.DOMImplementation JavaDoc;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.DocumentType JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40 import org.xml.sax.ErrorHandler JavaDoc;
41
42 public class JAXPPlatform implements XMLPlatform {
43     public JAXPPlatform() {
44         super();
45     }
46
47     /**
48      * Execute advanced XPath statements that are required for TopLink EIS.
49      * @param contextNode the node relative to which the XPath
50      * statement will be executed.
51      * xPath the XPath statement
52      * namespaceResolver used to resolve namespace prefixes
53      * to the corresponding namespace URI
54      * @return the XPath result
55      * @throws XMLPlatformException
56      */

57     public NodeList JavaDoc selectNodesAdvanced(Node JavaDoc contextNode, String JavaDoc xPath, XMLNamespaceResolver xmlNamespaceResolver) throws XMLPlatformException {
58         throw oracle.toplink.essentials.exceptions.ValidationException.operationNotSupported("selectNodesAdvanced");
59     }
60
61     /**
62      * Execute advanced XPath statements that are required for TopLink EIS.
63      * @param contextNode
64      * @param xPath
65      * @param xmlNamespaceResolver
66      * @return
67      * @throws XMLPlatformException
68      */

69     public Node JavaDoc selectSingleNodeAdvanced(Node JavaDoc contextNode, String JavaDoc xPath, XMLNamespaceResolver xmlNamespaceResolver) throws XMLPlatformException {
70         throw oracle.toplink.essentials.exceptions.ValidationException.operationNotSupported("selectSingleNodeAdvanced");
71     }
72
73     public boolean isWhitespaceNode(Text JavaDoc text) {
74         String JavaDoc value = text.getNodeValue();
75         if (null == value) {
76             return false;
77         } else {
78             return value.trim().equals("");
79         }
80     }
81
82     public XMLParser newXMLParser() {
83         return new JAXPParser();
84     }
85
86     public XMLTransformer newXMLTransformer() {
87         return new JAXPTransformer();
88     }
89
90     public Document JavaDoc createDocument() throws XMLPlatformException {
91         try {
92             DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
93             documentBuilderFactory.setNamespaceAware(true);
94             DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
95             return documentBuilder.newDocument();
96         } catch (Exception JavaDoc e) {
97             throw XMLPlatformException.xmlPlatformCouldNotCreateDocument(e);
98         }
99     }
100
101     public Document JavaDoc createDocumentWithPublicIdentifier(String JavaDoc name, String JavaDoc publicIdentifier, String JavaDoc systemIdentifier) throws XMLPlatformException {
102         try {
103             if (null == publicIdentifier) {
104                 return createDocumentWithSystemIdentifier(name, systemIdentifier);
105             }
106
107             DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
108             documentBuilderFactory.setNamespaceAware(true);
109             DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
110             DOMImplementation JavaDoc domImpl = documentBuilder.getDOMImplementation();
111             DocumentType JavaDoc docType = domImpl.createDocumentType(name, publicIdentifier, systemIdentifier);
112             Document JavaDoc document = domImpl.createDocument(null, name, docType);
113             return document;
114         } catch (Exception JavaDoc e) {
115             throw XMLPlatformException.xmlPlatformCouldNotCreateDocument(e);
116         }
117     }
118
119     public Document JavaDoc createDocumentWithSystemIdentifier(String JavaDoc name, String JavaDoc systemIdentifier) throws XMLPlatformException {
120         try {
121             Document JavaDoc document = null;
122
123             if (null == systemIdentifier) {
124                 document = createDocument();
125                 Element JavaDoc rootElement = document.createElement(name);
126                 document.appendChild(rootElement);
127                 return document;
128             }
129
130             DocumentBuilderFactory JavaDoc documentBuilderFactory = DocumentBuilderFactory.newInstance();
131             documentBuilderFactory.setNamespaceAware(true);
132             DocumentBuilder JavaDoc documentBuilder = documentBuilderFactory.newDocumentBuilder();
133             DOMImplementation JavaDoc domImpl = documentBuilder.getDOMImplementation();
134             DocumentType JavaDoc docType = domImpl.createDocumentType(name, null, systemIdentifier);
135             document = domImpl.createDocument(null, name, docType);
136             return document;
137         } catch (Exception JavaDoc e) {
138             throw XMLPlatformException.xmlPlatformCouldNotCreateDocument(e);
139         }
140     }
141
142     public String JavaDoc resolveNamespacePrefix(Node JavaDoc contextNode, String JavaDoc namespacePrefix) throws XMLPlatformException {
143         if (namespacePrefix.equals(contextNode.getPrefix())) {
144             return contextNode.getNamespaceURI();
145         }
146
147         if (contextNode.getNodeType() == Node.ELEMENT_NODE) {
148             Element JavaDoc contextElement = (Element JavaDoc)contextNode;
149             Attr JavaDoc namespaceDeclaration = contextElement.getAttributeNode("xmlns:" + namespacePrefix);
150             if (null != namespaceDeclaration) {
151                 return namespaceDeclaration.getValue();
152             }
153         }
154
155         Node JavaDoc parentNode = contextNode.getParentNode();
156         if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
157             return resolveNamespacePrefix((Element JavaDoc)parentNode, namespacePrefix);
158         }
159
160         return null;
161     }
162
163     public boolean validateDocument(Document JavaDoc document, URL JavaDoc xmlSchemaURL, ErrorHandler JavaDoc errorHandler) throws XMLPlatformException {
164         return true;
165     }
166 }
167
Popular Tags