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