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