1 21 package oracle.toplink.essentials.platform.xml.jaxp; 23 24 import java.net.URL ; 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 33 import org.w3c.dom.DOMImplementation ; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.DocumentType ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 import org.w3c.dom.Text ; 40 import org.xml.sax.ErrorHandler ; 41 42 public class JAXPPlatform implements XMLPlatform { 43 public JAXPPlatform() { 44 super(); 45 } 46 47 57 public NodeList selectNodesAdvanced(Node contextNode, String xPath, XMLNamespaceResolver xmlNamespaceResolver) throws XMLPlatformException { 58 throw oracle.toplink.essentials.exceptions.ValidationException.operationNotSupported("selectNodesAdvanced"); 59 } 60 61 69 public Node selectSingleNodeAdvanced(Node contextNode, String xPath, XMLNamespaceResolver xmlNamespaceResolver) throws XMLPlatformException { 70 throw oracle.toplink.essentials.exceptions.ValidationException.operationNotSupported("selectSingleNodeAdvanced"); 71 } 72 73 public boolean isWhitespaceNode(Text text) { 74 String 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 createDocument() throws XMLPlatformException { 91 try { 92 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 93 documentBuilderFactory.setNamespaceAware(true); 94 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 95 return documentBuilder.newDocument(); 96 } catch (Exception e) { 97 throw XMLPlatformException.xmlPlatformCouldNotCreateDocument(e); 98 } 99 } 100 101 public Document createDocumentWithPublicIdentifier(String name, String publicIdentifier, String systemIdentifier) throws XMLPlatformException { 102 try { 103 if (null == publicIdentifier) { 104 return createDocumentWithSystemIdentifier(name, systemIdentifier); 105 } 106 107 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 108 documentBuilderFactory.setNamespaceAware(true); 109 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 110 DOMImplementation domImpl = documentBuilder.getDOMImplementation(); 111 DocumentType docType = domImpl.createDocumentType(name, publicIdentifier, systemIdentifier); 112 Document document = domImpl.createDocument(null, name, docType); 113 return document; 114 } catch (Exception e) { 115 throw XMLPlatformException.xmlPlatformCouldNotCreateDocument(e); 116 } 117 } 118 119 public Document createDocumentWithSystemIdentifier(String name, String systemIdentifier) throws XMLPlatformException { 120 try { 121 Document document = null; 122 123 if (null == systemIdentifier) { 124 document = createDocument(); 125 Element rootElement = document.createElement(name); 126 document.appendChild(rootElement); 127 return document; 128 } 129 130 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 131 documentBuilderFactory.setNamespaceAware(true); 132 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 133 DOMImplementation domImpl = documentBuilder.getDOMImplementation(); 134 DocumentType docType = domImpl.createDocumentType(name, null, systemIdentifier); 135 document = domImpl.createDocument(null, name, docType); 136 return document; 137 } catch (Exception e) { 138 throw XMLPlatformException.xmlPlatformCouldNotCreateDocument(e); 139 } 140 } 141 142 public String resolveNamespacePrefix(Node contextNode, String namespacePrefix) throws XMLPlatformException { 143 if (namespacePrefix.equals(contextNode.getPrefix())) { 144 return contextNode.getNamespaceURI(); 145 } 146 147 if (contextNode.getNodeType() == Node.ELEMENT_NODE) { 148 Element contextElement = (Element )contextNode; 149 Attr namespaceDeclaration = contextElement.getAttributeNode("xmlns:" + namespacePrefix); 150 if (null != namespaceDeclaration) { 151 return namespaceDeclaration.getValue(); 152 } 153 } 154 155 Node parentNode = contextNode.getParentNode(); 156 if (parentNode.getNodeType() == Node.ELEMENT_NODE) { 157 return resolveNamespacePrefix((Element )parentNode, namespacePrefix); 158 } 159 160 return null; 161 } 162 163 public boolean validateDocument(Document document, URL xmlSchemaURL, ErrorHandler errorHandler) throws XMLPlatformException { 164 return true; 165 } 166 } 167 | Popular Tags |