< prev index next >

test/javax/xml/jaxp/unittest/transform/SourceTest.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 java.io.File;
  27 import java.io.StringWriter;
  28 
  29 import javax.xml.transform.Source;
  30 import javax.xml.transform.Transformer;
  31 import javax.xml.transform.TransformerFactory;
  32 import javax.xml.transform.dom.DOMSource;
  33 import javax.xml.transform.sax.SAXSource;
  34 import javax.xml.transform.stream.StreamResult;
  35 import javax.xml.transform.stream.StreamSource;
  36 
  37 import org.testng.Assert;

  38 import org.testng.annotations.Test;
  39 
  40 /*
  41  * @summary Test Sources.
  42  */

  43 public class SourceTest {
  44 
  45     @Test
  46     public final void testDOMSource() {
  47         String xml = getClass().getResource("SourceTest.xml").getFile();
  48         xml = "file://" + xml;
  49         File xsl = new File(getClass().getResource("SourceTest.xsl").getFile());
  50         try {
  51             TransformerFactory tFactory = TransformerFactory.newInstance();
  52             Transformer transformer = tFactory.newTransformer();
  53             StreamSource source = new StreamSource(xsl);
  54             transformer = tFactory.newTransformer(source);
  55             // the xml result
  56             StringWriter xmlResultString = new StringWriter();
  57             StreamResult xmlResultStream = new StreamResult(xmlResultString);
  58 
  59             Source xmlSource = new DOMSource();
  60             xmlSource.setSystemId(xml);
  61 
  62             transformer.transform(xmlSource, xmlResultStream);


   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 java.io.File;
  27 import java.io.StringWriter;
  28 
  29 import javax.xml.transform.Source;
  30 import javax.xml.transform.Transformer;
  31 import javax.xml.transform.TransformerFactory;
  32 import javax.xml.transform.dom.DOMSource;
  33 import javax.xml.transform.sax.SAXSource;
  34 import javax.xml.transform.stream.StreamResult;
  35 import javax.xml.transform.stream.StreamSource;
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.Listeners;
  39 import org.testng.annotations.Test;
  40 
  41 /*
  42  * @summary Test Sources.
  43  */
  44 @Listeners({jaxp.library.FilePolicy.class})
  45 public class SourceTest {
  46 
  47     @Test
  48     public final void testDOMSource() {
  49         String xml = getClass().getResource("SourceTest.xml").getFile();
  50         xml = "file://" + xml;
  51         File xsl = new File(getClass().getResource("SourceTest.xsl").getFile());
  52         try {
  53             TransformerFactory tFactory = TransformerFactory.newInstance();
  54             Transformer transformer = tFactory.newTransformer();
  55             StreamSource source = new StreamSource(xsl);
  56             transformer = tFactory.newTransformer(source);
  57             // the xml result
  58             StringWriter xmlResultString = new StringWriter();
  59             StreamResult xmlResultStream = new StreamResult(xmlResultString);
  60 
  61             Source xmlSource = new DOMSource();
  62             xmlSource.setSystemId(xml);
  63 
  64             transformer.transform(xmlSource, xmlResultStream);


< prev index next >