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 * @test 49 * @bug 6946312 50 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest 51 * @run testng/othervm -DrunSecMngr=true validation.Bug6946312Test 52 * @run testng/othervm validation.Bug6946312Test 53 * @summary Test XML parser shall callback to ContentHandler when receiving characters data. 54 */ 55 @Listeners({jaxp.library.FilePolicy.class}) 56 public class Bug6946312Test { 57 static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 58 static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 59 String xmlSchema = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + "<xs:element name=\"root\">\n" + "<xs:complexType>\n" 60 + "<xs:sequence>\n" + "<xs:any namespace=\"##any\" processContents=\"skip\"/>\n" + "</xs:sequence>\n" + "</xs:complexType>\n" + "</xs:element>\n" 61 + "</xs:schema>"; 62 63 boolean charEvent = false; 64 65 @Test 66 public void test() throws SAXException, ParserConfigurationException, IOException { 67 Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(new StringReader(xmlSchema))); 68 69 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 70 saxParserFactory.setNamespaceAware(true); 71 saxParserFactory.setSchema(schema); 72 // saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace", 73 // true); 74 75 SAXParser saxParser = saxParserFactory.newSAXParser(); 76 77 XMLReader xmlReader = saxParser.getXMLReader(); 78 79 xmlReader.setContentHandler(new MyContentHandler()); 80 81 // InputStream input = 82 // ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml"); 83 84 InputStream input = getClass().getResourceAsStream("Bug6946312.xml"); 85 System.out.println("Parse InputStream:"); 86 xmlReader.parse(new InputSource(input)); 87 if (!charEvent) { 88 Assert.fail("missing character event"); 89 } 90 } 91 92 public class MyContentHandler implements ContentHandler { 93 public void characters(char[] ch, int start, int length) { 94 charEvent = true; 95 System.out.println("Characters called: " + new String(ch, start, length)); 96 } 97 98 public void endDocument() throws SAXException { 99 } 100 101 public void endElement(String arg0, String arg1, String arg2) throws SAXException { 102 } 103 104 public void endPrefixMapping(String arg0) throws SAXException { 105 } 106 107 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 108 System.out.println("ignorableWhitespace called: " + new String(ch, start, length)); 109 } 110 111 public void processingInstruction(String arg0, String arg1) throws SAXException { 112 } 113 114 public void setDocumentLocator(Locator arg0) { 115 } 116 117 public void skippedEntity(String arg0) throws SAXException { 118 } 119 120 public void startDocument() throws SAXException { 121 } 122 123 public void startElement(String arg0, String arg1, String arg2, Attributes arg3) throws SAXException { 124 } 125 126 public void startPrefixMapping(String arg0, String arg1) throws SAXException { 127 } 128 } 129 130 } 131