1 /* 2 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package transform; 25 26 import javax.xml.parsers.SAXParser; 27 import javax.xml.parsers.SAXParserFactory; 28 import javax.xml.transform.Transformer; 29 import javax.xml.transform.TransformerFactory; 30 import javax.xml.transform.dom.DOMResult; 31 import javax.xml.transform.sax.SAXSource; 32 import javax.xml.transform.stream.StreamSource; 33 34 import org.testng.annotations.Listeners; 35 import org.testng.annotations.Test; 36 import org.w3c.dom.Document; 37 import org.w3c.dom.Element; 38 import org.xml.sax.Attributes; 39 import org.xml.sax.InputSource; 40 import org.xml.sax.SAXException; 41 import org.xml.sax.SAXNotRecognizedException; 42 import org.xml.sax.SAXNotSupportedException; 43 import org.xml.sax.XMLReader; 44 import org.xml.sax.ext.LexicalHandler; 45 import org.xml.sax.helpers.AttributesImpl; 46 import org.xml.sax.helpers.XMLFilterImpl; 47 48 /* 49 * @test 50 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 51 * @run testng/othervm -DrunSecMngr=true transform.SAX2DOMTest 52 * @run testng/othervm transform.SAX2DOMTest 53 * @summary Test Transforming from SAX to DOM. 54 */ 55 @Listeners({jaxp.library.FilePolicy.class}) 56 public class SAX2DOMTest { 57 58 @Test 59 public void test() throws Exception { 60 SAXParserFactory fac = SAXParserFactory.newInstance(); 61 fac.setNamespaceAware(true); 62 SAXParser saxParser = fac.newSAXParser(); 63 64 StreamSource sr = new StreamSource(this.getClass().getResourceAsStream("SAX2DOMTest.xml")); 65 InputSource is = SAXSource.sourceToInputSource(sr); 66 RejectDoctypeSaxFilter rf = new RejectDoctypeSaxFilter(saxParser); 67 SAXSource src = new SAXSource(rf, is); 68 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 69 DOMResult result = new DOMResult(); 70 transformer.transform(src, result); 71 72 Document doc = (Document) result.getNode(); 73 System.out.println("Name" + doc.getDocumentElement().getLocalName()); 74 75 String id = "XWSSGID-11605791027261938254268"; 76 Element selement = doc.getElementById(id); 77 if (selement == null) { 78 System.out.println("getElementById returned null"); 79 } 80 81 } 82 83 public static class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler { 84 85 /** Standard SAX 2.0 ext property */ 86 static final String LEXICAL_HANDLER_PROP = "http://xml.org/sax/properties/lexical-handler"; 87 88 static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern(); 89 static final String SIGNATURE_LNAME = "Signature".intern(); 90 static final String ENCRYPTED_DATA_LNAME = "EncryptedData".intern(); 91 static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern(); 92 static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern(); 93 static final String ID_NAME = "ID".intern(); 94 95 /** LexicalHandler to forward events to, if any */ 96 private LexicalHandler lexicalHandler; 97 98 public RejectDoctypeSaxFilter(SAXParser saxParser) throws Exception { 99 XMLReader xmlReader; 100 try { 101 xmlReader = saxParser.getXMLReader(); 102 } catch (Exception e) { 103 throw new Exception("Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter", e); 104 } 105 106 // Set ourselves up to be the SAX LexicalHandler 107 try { 108 xmlReader.setProperty(LEXICAL_HANDLER_PROP, this); 109 } catch (Exception e) { 110 throw new Exception("Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter", e); 111 } 112 113 // Set the parent XMLReader of this SAX filter 114 setParent(xmlReader); 115 } 116 117 /* 118 * Override setProperty() to capture any LexicalHandler that is set for 119 * forwarding of events. 120 */ 121 public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException { 122 if (LEXICAL_HANDLER_PROP.equals(name)) { 123 lexicalHandler = (LexicalHandler) value; 124 } else { 125 super.setProperty(name, value); 126 } 127 } 128 129 // 130 // Beginning of SAX LexicalHandler callbacks... 131 // 132 133 public void startDTD(String name, String publicId, String systemId) throws SAXException { 134 throw new SAXException("Document Type Declaration is not allowed"); 135 } 136 137 public void endDTD() throws SAXException { 138 } 139 140 public void startEntity(String name) throws SAXException { 141 if (lexicalHandler != null) { 142 lexicalHandler.startEntity(name); 143 } 144 } 145 146 public void endEntity(String name) throws SAXException { 147 if (lexicalHandler != null) { 148 lexicalHandler.endEntity(name); 149 } 150 } 151 152 public void startCDATA() throws SAXException { 153 if (lexicalHandler != null) { 154 lexicalHandler.startCDATA(); 155 } 156 } 157 158 public void endCDATA() throws SAXException { 159 if (lexicalHandler != null) { 160 lexicalHandler.endCDATA(); 161 } 162 } 163 164 public void comment(char[] ch, int start, int length) throws SAXException { 165 if (lexicalHandler != null) { 166 lexicalHandler.comment(ch, start, length); 167 } 168 } 169 170 // 171 // End of SAX LexicalHandler callbacks 172 // 173 174 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 175 if (atts != null) { 176 boolean eos = false; 177 if (namespaceURI == DSIG_NS || XENC_NS == namespaceURI) { 178 eos = true; 179 } 180 int length = atts.getLength(); 181 AttributesImpl attrImpl = new AttributesImpl(); 182 for (int i = 0; i < length; i++) { 183 String name = atts.getLocalName(i); 184 if (name != null && (name.equals("Id"))) { 185 if (eos || atts.getURI(i) == WSU_NS) { 186 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), ID_NAME, atts.getValue(i)); 187 } else { 188 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i)); 189 } 190 } else { 191 attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i)); 192 } 193 } 194 super.startElement(namespaceURI, localName, qName, attrImpl); 195 } else { 196 super.startElement(namespaceURI, localName, qName, atts); 197 } 198 } 199 } 200 } 201