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 validation;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 
  29 import javax.xml.parsers.DocumentBuilder;
  30 import javax.xml.parsers.DocumentBuilderFactory;
  31 import javax.xml.parsers.ParserConfigurationException;
  32 
  33 import org.testng.annotations.Test;
  34 import org.testng.Assert;
  35 import org.w3c.dom.Document;
  36 import org.xml.sax.ErrorHandler;
  37 import org.xml.sax.SAXException;
  38 import org.xml.sax.SAXParseException;
  39 
  40 /*
  41  * @bug 6740048
  42  * @summary Test DocumentBuilder can be reused when the DocumentBuilderFactory sets schema.
  43  */
  44 public class CR6740048 {
  45     private static final String TAG_INFO = "containerInfo";
  46     private static final String SCHEMA_LANGUAGE_URL = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  47     private static final String SCHEMA_SOURCE_URL = "http://java.sun.com/xml/jaxp/properties/schemaSource";
  48     private static final String XML_SCHEMA_URL = "http://www.w3.org/2001/XMLSchema";
  49 
  50     @Test
  51     public final void testReusingDocumentBuilder() {
  52 
  53         try {
  54             //ClassLoader loader = CR6740048.class.getClassLoader();
  55 
  56             // Object xsd = loader.getResourceAsStream("CR6740048.xsd");
  57             InputStream xsd = this.getClass().getResourceAsStream("CR6740048.xsd");
  58             // create document builder
  59             DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  60             docBuilderFactory.setNamespaceAware(true);
  61 
  62             if (xsd != null) {
  63                 docBuilderFactory.setValidating(true);
  64                 docBuilderFactory.setAttribute(SCHEMA_LANGUAGE_URL, XML_SCHEMA_URL);
  65                 docBuilderFactory.setAttribute(SCHEMA_SOURCE_URL, xsd);
  66             }
  67 
  68             final DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder();
  69             documentBuilder.setErrorHandler(new ErrorHandler() {
  70 
  71                 public void error(SAXParseException exception) throws SAXException {
  72                     throw exception;
  73                 }
  74 
  75                 public void fatalError(SAXParseException exception) throws SAXException {
  76                     throw exception;
  77                 }
  78 
  79                 public void warning(SAXParseException exception) throws SAXException {
  80                     throw exception;
  81                 }
  82             });
  83 
  84             // TODO set the system properties in order to match the ones from
  85             // the server environment
  86             /**
  87              * Properties props = new Properties();
  88              * props.load(loader.getResourceAsStream("sysprops.properties"));
  89              * System.setProperties(props);
  90              */
  91 
  92             // now parse the document
  93             // InputStream is = loader.getResourceAsStream("CR6740048.xml");
  94             InputStream is = this.getClass().getResourceAsStream("CR6740048.xml");
  95             try {
  96                 Document doc = documentBuilder.parse(is);
  97             } catch (Exception se) {
  98 
  99                 se.printStackTrace();
 100                 Assert.fail(se.getMessage());
 101 
 102             } finally {
 103                 is.close();
 104             }
 105 
 106             // now use the parser object as second time
 107             // is = loader.getResourceAsStream("CR6740048.xml");
 108             is = this.getClass().getResourceAsStream("CR6740048.xml");
 109             try {
 110                 Document doc = documentBuilder.parse(is);
 111             } catch (Exception se) {
 112 
 113                 se.printStackTrace();
 114                 Assert.fail(se.getMessage());
 115 
 116             } finally {
 117                 is.close();
 118             }
 119 
 120             System.err.println("Parse successful");
 121 
 122             is.close();
 123         } catch (ParserConfigurationException pce) {
 124             pce.printStackTrace();
 125         } catch (IOException ioe) {
 126             ioe.printStackTrace();
 127         }
 128     }
 129 
 130 }