001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.platform.xml.jaxp;
038:
039: import java.net.URL;
040: import javax.xml.parsers.DocumentBuilder;
041: import javax.xml.parsers.DocumentBuilderFactory;
042: import oracle.toplink.essentials.platform.xml.XMLNamespaceResolver;
043: import oracle.toplink.essentials.platform.xml.XMLParser;
044: import oracle.toplink.essentials.platform.xml.XMLPlatform;
045: import oracle.toplink.essentials.platform.xml.XMLPlatformException;
046: import oracle.toplink.essentials.platform.xml.XMLTransformer;
047: import org.w3c.dom.Attr;
048: import org.w3c.dom.DOMImplementation;
049: import org.w3c.dom.Document;
050: import org.w3c.dom.DocumentType;
051: import org.w3c.dom.Element;
052: import org.w3c.dom.Node;
053: import org.w3c.dom.NodeList;
054: import org.w3c.dom.Text;
055: import org.xml.sax.ErrorHandler;
056:
057: public class JAXPPlatform implements XMLPlatform {
058: public JAXPPlatform() {
059: super ();
060: }
061:
062: /**
063: * Execute advanced XPath statements that are required for TopLink EIS.
064: * @param contextNode the node relative to which the XPath
065: * statement will be executed.
066: * xPath the XPath statement
067: * namespaceResolver used to resolve namespace prefixes
068: * to the corresponding namespace URI
069: * @return the XPath result
070: * @throws XMLPlatformException
071: */
072: public NodeList selectNodesAdvanced(Node contextNode, String xPath,
073: XMLNamespaceResolver xmlNamespaceResolver)
074: throws XMLPlatformException {
075: throw oracle.toplink.essentials.exceptions.ValidationException
076: .operationNotSupported("selectNodesAdvanced");
077: }
078:
079: /**
080: * Execute advanced XPath statements that are required for TopLink EIS.
081: * @param contextNode
082: * @param xPath
083: * @param xmlNamespaceResolver
084: * @return
085: * @throws XMLPlatformException
086: */
087: public Node selectSingleNodeAdvanced(Node contextNode,
088: String xPath, XMLNamespaceResolver xmlNamespaceResolver)
089: throws XMLPlatformException {
090: throw oracle.toplink.essentials.exceptions.ValidationException
091: .operationNotSupported("selectSingleNodeAdvanced");
092: }
093:
094: public boolean isWhitespaceNode(Text text) {
095: String value = text.getNodeValue();
096: if (null == value) {
097: return false;
098: } else {
099: return value.trim().equals("");
100: }
101: }
102:
103: public XMLParser newXMLParser() {
104: return new JAXPParser();
105: }
106:
107: public XMLTransformer newXMLTransformer() {
108: return new JAXPTransformer();
109: }
110:
111: public Document createDocument() throws XMLPlatformException {
112: try {
113: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
114: .newInstance();
115: documentBuilderFactory.setNamespaceAware(true);
116: DocumentBuilder documentBuilder = documentBuilderFactory
117: .newDocumentBuilder();
118: return documentBuilder.newDocument();
119: } catch (Exception e) {
120: throw XMLPlatformException
121: .xmlPlatformCouldNotCreateDocument(e);
122: }
123: }
124:
125: public Document createDocumentWithPublicIdentifier(String name,
126: String publicIdentifier, String systemIdentifier)
127: throws XMLPlatformException {
128: try {
129: if (null == publicIdentifier) {
130: return createDocumentWithSystemIdentifier(name,
131: systemIdentifier);
132: }
133:
134: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
135: .newInstance();
136: documentBuilderFactory.setNamespaceAware(true);
137: DocumentBuilder documentBuilder = documentBuilderFactory
138: .newDocumentBuilder();
139: DOMImplementation domImpl = documentBuilder
140: .getDOMImplementation();
141: DocumentType docType = domImpl.createDocumentType(name,
142: publicIdentifier, systemIdentifier);
143: Document document = domImpl.createDocument(null, name,
144: docType);
145: return document;
146: } catch (Exception e) {
147: throw XMLPlatformException
148: .xmlPlatformCouldNotCreateDocument(e);
149: }
150: }
151:
152: public Document createDocumentWithSystemIdentifier(String name,
153: String systemIdentifier) throws XMLPlatformException {
154: try {
155: Document document = null;
156:
157: if (null == systemIdentifier) {
158: document = createDocument();
159: Element rootElement = document.createElement(name);
160: document.appendChild(rootElement);
161: return document;
162: }
163:
164: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
165: .newInstance();
166: documentBuilderFactory.setNamespaceAware(true);
167: DocumentBuilder documentBuilder = documentBuilderFactory
168: .newDocumentBuilder();
169: DOMImplementation domImpl = documentBuilder
170: .getDOMImplementation();
171: DocumentType docType = domImpl.createDocumentType(name,
172: null, systemIdentifier);
173: document = domImpl.createDocument(null, name, docType);
174: return document;
175: } catch (Exception e) {
176: throw XMLPlatformException
177: .xmlPlatformCouldNotCreateDocument(e);
178: }
179: }
180:
181: public String resolveNamespacePrefix(Node contextNode,
182: String namespacePrefix) throws XMLPlatformException {
183: if (namespacePrefix.equals(contextNode.getPrefix())) {
184: return contextNode.getNamespaceURI();
185: }
186:
187: if (contextNode.getNodeType() == Node.ELEMENT_NODE) {
188: Element contextElement = (Element) contextNode;
189: Attr namespaceDeclaration = contextElement
190: .getAttributeNode("xmlns:" + namespacePrefix);
191: if (null != namespaceDeclaration) {
192: return namespaceDeclaration.getValue();
193: }
194: }
195:
196: Node parentNode = contextNode.getParentNode();
197: if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
198: return resolveNamespacePrefix((Element) parentNode,
199: namespacePrefix);
200: }
201:
202: return null;
203: }
204:
205: public boolean validateDocument(Document document,
206: URL xmlSchemaURL, ErrorHandler errorHandler)
207: throws XMLPlatformException {
208: return true;
209: }
210: }
|