< prev index next >

test/javax/xml/jaxp/unittest/transform/SAX2DOMTest.java

Print this page


   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";


   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";


< prev index next >