< prev index next >

test/javax/xml/jaxp/unittest/transform/DOMResultTest.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 org.testng.annotations.Test;
  27 import org.testng.Assert;
  28 
  29 import java.io.File;
  30 import java.io.FileInputStream;
  31 import java.io.FileNotFoundException;
  32 import java.io.IOException;
  33 
  34 import javax.xml.parsers.DocumentBuilder;
  35 import javax.xml.parsers.DocumentBuilderFactory;
  36 import javax.xml.parsers.ParserConfigurationException;
  37 import javax.xml.transform.Transformer;
  38 import javax.xml.transform.TransformerConfigurationException;
  39 import javax.xml.transform.TransformerException;
  40 import javax.xml.transform.TransformerFactory;
  41 import javax.xml.transform.dom.DOMResult;
  42 import javax.xml.transform.dom.DOMSource;
  43 
  44 import org.w3c.dom.Document;
  45 import org.w3c.dom.Node;
  46 import org.xml.sax.SAXException;
  47 
  48 /*
  49  * @summary Test DOMResult.
  50  */

  51 public class DOMResultTest {
  52 
  53     @Test
  54     public void testDOMResult1() {
  55         try {
  56             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  57             DocumentBuilder db = dbf.newDocumentBuilder();
  58             String xml = this.getClass().getResource("toys.xml").getFile();
  59             Document doc = db.parse(new FileInputStream(new File(xml)));
  60             TransformerFactory tff = TransformerFactory.newInstance();
  61             Transformer tf = tff.newTransformer();
  62             // get <toys> element node
  63             Node toys = doc.getChildNodes().item(1);
  64             // supposed to insert new node at index=4
  65             int index = 4;
  66             String systemId = "customSysId";
  67             DOMResult result = new DOMResult(toys, systemId);
  68             result.setNextSibling(result.getNode().getChildNodes().item(index));
  69             int length = result.getNode().getChildNodes().getLength();
  70             // copy the first <toy> element node and insert it to position


   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 org.testng.annotations.Listeners;
  27 import org.testng.annotations.Test;
  28 import org.testng.Assert;
  29 
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 import java.io.FileNotFoundException;
  33 import java.io.IOException;
  34 
  35 import javax.xml.parsers.DocumentBuilder;
  36 import javax.xml.parsers.DocumentBuilderFactory;
  37 import javax.xml.parsers.ParserConfigurationException;
  38 import javax.xml.transform.Transformer;
  39 import javax.xml.transform.TransformerConfigurationException;
  40 import javax.xml.transform.TransformerException;
  41 import javax.xml.transform.TransformerFactory;
  42 import javax.xml.transform.dom.DOMResult;
  43 import javax.xml.transform.dom.DOMSource;
  44 
  45 import org.w3c.dom.Document;
  46 import org.w3c.dom.Node;
  47 import org.xml.sax.SAXException;
  48 
  49 /*
  50  * @summary Test DOMResult.
  51  */
  52 @Listeners({jaxp.library.FilePolicy.class})
  53 public class DOMResultTest {
  54 
  55     @Test
  56     public void testDOMResult1() {
  57         try {
  58             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  59             DocumentBuilder db = dbf.newDocumentBuilder();
  60             String xml = this.getClass().getResource("toys.xml").getFile();
  61             Document doc = db.parse(new FileInputStream(new File(xml)));
  62             TransformerFactory tff = TransformerFactory.newInstance();
  63             Transformer tf = tff.newTransformer();
  64             // get <toys> element node
  65             Node toys = doc.getChildNodes().item(1);
  66             // supposed to insert new node at index=4
  67             int index = 4;
  68             String systemId = "customSysId";
  69             DOMResult result = new DOMResult(toys, systemId);
  70             result.setNextSibling(result.getNode().getChildNodes().item(index));
  71             int length = result.getNode().getChildNodes().getLength();
  72             // copy the first <toy> element node and insert it to position


< prev index next >