1 /*
   2  * Copyright (c) 2002, 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 package test.astro;
  24 
  25 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  26 import static org.testng.Assert.assertEquals;
  27 import static org.testng.Assert.assertNotNull;
  28 import static org.testng.Assert.assertNull;
  29 import static org.testng.Assert.assertTrue;
  30 import static org.w3c.dom.ls.DOMImplementationLS.MODE_SYNCHRONOUS;
  31 import static test.astro.AstroConstants.ASTROCAT;
  32 
  33 import java.io.FileInputStream;
  34 import java.io.FileOutputStream;
  35 import java.io.InputStream;
  36 import java.io.OutputStream;
  37 import java.io.Writer;
  38 
  39 import javax.xml.parsers.DocumentBuilder;
  40 import javax.xml.parsers.DocumentBuilderFactory;
  41 import javax.xml.parsers.ParserConfigurationException;
  42 
  43 import jaxp.library.JAXPFileBaseTest;
  44 
  45 import org.testng.annotations.Test;
  46 import org.w3c.dom.Document;
  47 import org.w3c.dom.Element;
  48 import org.w3c.dom.ls.DOMImplementationLS;
  49 import org.w3c.dom.ls.LSInput;
  50 import org.w3c.dom.ls.LSOutput;
  51 import org.w3c.dom.ls.LSParser;
  52 import org.w3c.dom.ls.LSSerializer;
  53 
  54 /*
  55  * @summary org.w3c.dom.ls tests
  56  */
  57 public class DocumentLSTest extends JAXPFileBaseTest {
  58     /*
  59      * Test creating an empty Document
  60      */
  61     @Test
  62     public void testNewDocument() throws ParserConfigurationException {
  63         Document doc = getDocumentBuilder().newDocument();
  64         assertNull(doc.getDocumentElement());
  65     }
  66 
  67     /*
  68      * Test creating an LSInput instance, and parsing ByteStream
  69      */
  70     @Test
  71     public void testLSInputParsingByteStream() throws Exception {
  72         DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
  73         LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
  74         LSInput src = impl.createLSInput();
  75 
  76         try (InputStream is = new FileInputStream(ASTROCAT)) {
  77             src.setByteStream(is);
  78             assertNotNull(src.getByteStream());
  79             // set certified accessor methods
  80             boolean origCertified = src.getCertifiedText();
  81             src.setCertifiedText(true);
  82             assertTrue(src.getCertifiedText());
  83             src.setCertifiedText(origCertified); // set back to orig
  84 
  85             src.setSystemId(filenameToURL(ASTROCAT));
  86 
  87             Document doc = domParser.parse(src);
  88             Element result = doc.getDocumentElement();
  89             assertEquals(result.getTagName(), "stardb");
  90         }
  91     }
  92 
  93     /*
  94      * Test creating an LSInput instance, and parsing String
  95      */
  96     @Test
  97     public void testLSInputParsingString() throws Exception {
  98         DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
  99         String xml = "<?xml version='1.0'?><test>runDocumentLS_Q6</test>";
 100 
 101         LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
 102         LSSerializer domSerializer = impl.createLSSerializer();
 103         // turn off xml decl in serialized string for comparison
 104         domSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE);
 105         LSInput src = impl.createLSInput();
 106         src.setStringData(xml);
 107         assertEquals(src.getStringData(), xml);
 108 
 109         Document doc = domParser.parse(src);
 110         String result = domSerializer.writeToString(doc);
 111 
 112         assertEquals(result, "<test>runDocumentLS_Q6</test>");
 113     }
 114 
 115     /*
 116      * Test writing with an LSOutput instance
 117      */
 118     @Test
 119     public void testLSOutput() throws Exception {
 120         DocumentBuilder db = getDocumentBuilder();
 121         // Create the Document using the supplied builder...
 122         Document doc = db.parse(ASTROCAT);
 123 
 124         DOMImplementationLS impl = null;
 125 
 126         impl = (DOMImplementationLS) db.getDOMImplementation();
 127         LSSerializer domSerializer = impl.createLSSerializer();
 128         MyDOMOutput mydomoutput = new MyDOMOutput();
 129         try (OutputStream os = new FileOutputStream("test.out")) {
 130             mydomoutput.setByteStream(os);
 131             mydomoutput.setEncoding("UTF-8");
 132             assertTrue(domSerializer.write(doc, mydomoutput));
 133         }
 134     }
 135 
 136     private static class MyDOMOutput implements LSOutput {
 137         private OutputStream bytestream = null;
 138         private String encoding = null;
 139         private String sysId = null;
 140         private Writer writer = null;
 141 
 142         public OutputStream getByteStream() {
 143             return bytestream;
 144         }
 145 
 146         public Writer getCharacterStream() {
 147             return writer;
 148         }
 149 
 150         public String getEncoding() {
 151             return encoding;
 152         }
 153 
 154         public String getSystemId() {
 155             return sysId;
 156         }
 157 
 158         public void setByteStream(OutputStream bs) {
 159             bytestream = bs;
 160         }
 161 
 162         public void setCharacterStream(Writer cs) {
 163             writer = cs;
 164         }
 165 
 166         public void setEncoding(String enc) {
 167             encoding = enc;
 168         }
 169 
 170         public void setSystemId(String sysId) {
 171             this.sysId = sysId;
 172         }
 173     }
 174 
 175     private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
 176         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 177         dbf.setNamespaceAware(true);
 178         return dbf.newDocumentBuilder();
 179     }
 180 }