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