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 import java.io.StringReader; 29 30 import javax.xml.parsers.ParserConfigurationException; 31 import javax.xml.parsers.SAXParser; 32 import javax.xml.parsers.SAXParserFactory; 33 import javax.xml.transform.stream.StreamSource; 34 import javax.xml.validation.Schema; 35 import javax.xml.validation.SchemaFactory; 36 37 import org.testng.Assert; 38 import org.testng.annotations.Test; 39 import org.xml.sax.Attributes; 40 import org.xml.sax.ContentHandler; 41 import org.xml.sax.InputSource; 42 import org.xml.sax.Locator; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.XMLReader; 45 46 /* 47 * @bug 6946312 48 * @summary Test XML parser shall callback to ContentHandler when receiving characters data. 49 */ 50 public class Bug6946312Test { 51 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 52 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 53 String xmlSchema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + "<xs:element name=\"root\">\n" + "<xs:complexType>\n" 54 + "<xs:sequence>\n" + "<xs:any namespace=\"##any\" processContents=\"skip\"/>\n" + "</xs:sequence>\n" + "</xs:complexType>\n" + "</xs:element>\n" 55 + "</xs:schema>"; 56 57 boolean charEvent = false; 58 59 @Test 60 public void test() throws SAXException, ParserConfigurationException, IOException { 61 Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(new StringReader(xmlSchema))); 62 63 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 64 saxParserFactory.setNamespaceAware(true); 65 saxParserFactory.setSchema(schema); 66 // saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace", 67 // true); 68 69 SAXParser saxParser = saxParserFactory.newSAXParser(); 70 71 XMLReader xmlReader = saxParser.getXMLReader(); 72 73 xmlReader.setContentHandler(new MyContentHandler()); 74 75 // InputStream input = 76 // ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml"); 77 78 InputStream input = getClass().getResourceAsStream("Bug6946312.xml"); 79 System.out.println("Parse InputStream:"); 80 xmlReader.parse(new InputSource(input)); 81 if (!charEvent) { 82 Assert.fail("missing character event"); 83 } 84 } 85 86 public class MyContentHandler implements ContentHandler { 87 public void characters(char[] ch, int start, int length) { 88 charEvent = true; 89 System.out.println("Characters called: " + new String(ch, start, length)); 90 } 91 92 public void endDocument() throws SAXException { 93 } 94 95 public void endElement(String arg0, String arg1, String arg2) throws SAXException { 96 } 97 98 public void endPrefixMapping(String arg0) throws SAXException { 99 } 100 101 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 102 System.out.println("ignorableWhitespace called: " + new String(ch, start, length)); 103 } 104 105 public void processingInstruction(String arg0, String arg1) throws SAXException { 106 } | 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 import java.io.StringReader; 29 30 import javax.xml.parsers.ParserConfigurationException; 31 import javax.xml.parsers.SAXParser; 32 import javax.xml.parsers.SAXParserFactory; 33 import javax.xml.transform.stream.StreamSource; 34 import javax.xml.validation.Schema; 35 import javax.xml.validation.SchemaFactory; 36 37 import org.testng.Assert; 38 import org.testng.annotations.Listeners; 39 import org.testng.annotations.Test; 40 import org.xml.sax.Attributes; 41 import org.xml.sax.ContentHandler; 42 import org.xml.sax.InputSource; 43 import org.xml.sax.Locator; 44 import org.xml.sax.SAXException; 45 import org.xml.sax.XMLReader; 46 47 /* 48 * @bug 6946312 49 * @summary Test XML parser shall callback to ContentHandler when receiving characters data. 50 */ 51 @Listeners({jaxp.library.FilePolicy.class}) 52 public class Bug6946312Test { 53 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 54 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 55 String xmlSchema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + "<xs:element name=\"root\">\n" + "<xs:complexType>\n" 56 + "<xs:sequence>\n" + "<xs:any namespace=\"##any\" processContents=\"skip\"/>\n" + "</xs:sequence>\n" + "</xs:complexType>\n" + "</xs:element>\n" 57 + "</xs:schema>"; 58 59 boolean charEvent = false; 60 61 @Test 62 public void test() throws SAXException, ParserConfigurationException, IOException { 63 Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(new StringReader(xmlSchema))); 64 65 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 66 saxParserFactory.setNamespaceAware(true); 67 saxParserFactory.setSchema(schema); 68 // saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace", 69 // true); 70 71 SAXParser saxParser = saxParserFactory.newSAXParser(); 72 73 XMLReader xmlReader = saxParser.getXMLReader(); 74 75 xmlReader.setContentHandler(new MyContentHandler()); 76 77 // InputStream input = 78 // ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml"); 79 80 InputStream input = getClass().getResourceAsStream("Bug6946312.xml"); 81 System.out.println("Parse InputStream:"); 82 xmlReader.parse(new InputSource(input)); 83 if (!charEvent) { 84 Assert.fail("missing character event"); 85 } 86 } 87 88 @Listeners({jaxp.library.BasePolicy.class}) 89 public class MyContentHandler implements ContentHandler { 90 public void characters(char[] ch, int start, int length) { 91 charEvent = true; 92 System.out.println("Characters called: " + new String(ch, start, length)); 93 } 94 95 public void endDocument() throws SAXException { 96 } 97 98 public void endElement(String arg0, String arg1, String arg2) throws SAXException { 99 } 100 101 public void endPrefixMapping(String arg0) throws SAXException { 102 } 103 104 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 105 System.out.println("ignorableWhitespace called: " + new String(ch, start, length)); 106 } 107 108 public void processingInstruction(String arg0, String arg1) throws SAXException { 109 } |