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  * @summary Test Transforming from SAX to DOM.
  50  */
  51 @Listeners({jaxp.library.FilePolicy.class})
  52 public class SAX2DOMTest {
  53 
  54     @Test
  55     public void test() throws Exception {
  56         SAXParserFactory fac = SAXParserFactory.newInstance();
  57         fac.setNamespaceAware(true);
  58         SAXParser saxParser = fac.newSAXParser();
  59 
  60         StreamSource sr = new StreamSource(this.getClass().getResourceAsStream("SAX2DOMTest.xml"));
  61         InputSource is = SAXSource.sourceToInputSource(sr);
  62         RejectDoctypeSaxFilter rf = new RejectDoctypeSaxFilter(saxParser);
  63         SAXSource src = new SAXSource(rf, is);
  64         Transformer transformer = TransformerFactory.newInstance().newTransformer();
  65         DOMResult result = new DOMResult();
  66         transformer.transform(src, result);
  67 
  68         Document doc = (Document) result.getNode();
  69         System.out.println("Name" + doc.getDocumentElement().getLocalName());
  70 
  71         String id = "XWSSGID-11605791027261938254268";
  72         Element selement = doc.getElementById(id);
  73         if (selement == null) {
  74             System.out.println("getElementById returned null");
  75         }
  76 
  77     }
  78 
  79     public static class RejectDoctypeSaxFilter extends XMLFilterImpl implements XMLReader, LexicalHandler {
  80 
  81         /** Standard SAX 2.0 ext property */
  82         static final String LEXICAL_HANDLER_PROP = "http://xml.org/sax/properties/lexical-handler";
  83 
  84         static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern();
  85         static final String SIGNATURE_LNAME = "Signature".intern();
  86         static final String ENCRYPTED_DATA_LNAME = "EncryptedData".intern();
  87         static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern();
  88         static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern();
  89         static final String ID_NAME = "ID".intern();
  90 
  91         /** LexicalHandler to forward events to, if any */
  92         private LexicalHandler lexicalHandler;
  93 
  94         public RejectDoctypeSaxFilter(SAXParser saxParser) throws Exception {
  95             XMLReader xmlReader;
  96             try {
  97                 xmlReader = saxParser.getXMLReader();
  98             } catch (Exception e) {
  99                 throw new Exception("Couldn't get an XMLReader while constructing a RejectDoctypeSaxFilter", e);
 100             }
 101 
 102             // Set ourselves up to be the SAX LexicalHandler
 103             try {
 104                 xmlReader.setProperty(LEXICAL_HANDLER_PROP, this);
 105             } catch (Exception e) {
 106                 throw new Exception("Couldn't set the lexical handler property while constructing a RejectDoctypeSaxFilter", e);
 107             }
 108 
 109             // Set the parent XMLReader of this SAX filter
 110             setParent(xmlReader);
 111         }
 112 
 113         /*
 114          * Override setProperty() to capture any LexicalHandler that is set for
 115          * forwarding of events.
 116          */
 117         public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
 118             if (LEXICAL_HANDLER_PROP.equals(name)) {
 119                 lexicalHandler = (LexicalHandler) value;
 120             } else {
 121                 super.setProperty(name, value);
 122             }
 123         }
 124 
 125         //
 126         // Beginning of SAX LexicalHandler callbacks...
 127         //
 128 
 129         public void startDTD(String name, String publicId, String systemId) throws SAXException {
 130             throw new SAXException("Document Type Declaration is not allowed");
 131         }
 132 
 133         public void endDTD() throws SAXException {
 134         }
 135 
 136         public void startEntity(String name) throws SAXException {
 137             if (lexicalHandler != null) {
 138                 lexicalHandler.startEntity(name);
 139             }
 140         }
 141 
 142         public void endEntity(String name) throws SAXException {
 143             if (lexicalHandler != null) {
 144                 lexicalHandler.endEntity(name);
 145             }
 146         }
 147 
 148         public void startCDATA() throws SAXException {
 149             if (lexicalHandler != null) {
 150                 lexicalHandler.startCDATA();
 151             }
 152         }
 153 
 154         public void endCDATA() throws SAXException {
 155             if (lexicalHandler != null) {
 156                 lexicalHandler.endCDATA();
 157             }
 158         }
 159 
 160         public void comment(char[] ch, int start, int length) throws SAXException {
 161             if (lexicalHandler != null) {
 162                 lexicalHandler.comment(ch, start, length);
 163             }
 164         }
 165 
 166         //
 167         // End of SAX LexicalHandler callbacks
 168         //
 169 
 170         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
 171             if (atts != null) {
 172                 boolean eos = false;
 173                 if (namespaceURI == DSIG_NS || XENC_NS == namespaceURI) {
 174                     eos = true;
 175                 }
 176                 int length = atts.getLength();
 177                 AttributesImpl attrImpl = new AttributesImpl();
 178                 for (int i = 0; i < length; i++) {
 179                     String name = atts.getLocalName(i);
 180                     if (name != null && (name.equals("Id"))) {
 181                         if (eos || atts.getURI(i) == WSU_NS) {
 182                             attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), ID_NAME, atts.getValue(i));
 183                         } else {
 184                             attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i));
 185                         }
 186                     } else {
 187                         attrImpl.addAttribute(atts.getURI(i), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i));
 188                     }
 189                 }
 190                 super.startElement(namespaceURI, localName, qName, attrImpl);
 191             } else {
 192                 super.startElement(namespaceURI, localName, qName, atts);
 193             }
 194         }
 195     }
 196 }