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.ByteArrayInputStream; 27 import java.io.IOException; 28 import java.io.InputStreamReader; 29 30 import javax.xml.parsers.ParserConfigurationException; 31 import javax.xml.parsers.SAXParserFactory; 32 import javax.xml.transform.stream.StreamSource; 33 import javax.xml.validation.Schema; 34 import javax.xml.validation.SchemaFactory; 35 import javax.xml.validation.TypeInfoProvider; 36 import javax.xml.validation.ValidatorHandler; 37 38 import org.testng.Assert; 39 import org.testng.annotations.Test; 40 import org.w3c.dom.TypeInfo; 41 import org.xml.sax.Attributes; 42 import org.xml.sax.InputSource; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.XMLReader; 45 import org.xml.sax.helpers.DefaultHandler; 46 47 /* 48 * @bug 6509668 49 * @summary Test TypeInfoProvider.getElementTypeInfo() for union type when startElement and endElement. 50 */ 51 public class Bug6509668 { 52 53 public static final String XSD = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" 54 + " xmlns:ns='http://example.org/jaxp13_test'\n" + " targetNamespace='http://example.org/jaxp13_test'\n" + " elementFormDefault='qualified'>\n" 55 + " <simpleType name='intOrString'>\n" + " <union memberTypes='int string'/>\n" + " </simpleType>\n" 56 + " <element name='test' type='ns:intOrString'/>\n" + "</schema>\n"; 57 58 public static final String XML = "<?xml version='1.0'?>\n" + "<ns:test xmlns:ns='http://example.org/jaxp13_test'>abc</ns:test>\n"; 59 60 private ValidatorHandler createValidatorHandler(String xsd) throws SAXException { 61 SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 62 63 InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xsd.getBytes())); 64 StreamSource xsdSource = new StreamSource(reader); 65 66 Schema schema = schemaFactory.newSchema(xsdSource); 67 return schema.newValidatorHandler(); 68 } 69 70 private XMLReader createXMLReader() throws ParserConfigurationException, SAXException { 71 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 72 if (!parserFactory.isNamespaceAware()) { 73 parserFactory.setNamespaceAware(true); 74 } 75 76 return parserFactory.newSAXParser().getXMLReader(); 77 } 78 79 private void parse(XMLReader xmlReader, String xml) throws SAXException, IOException { 80 InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xml.getBytes())); 81 InputSource inSource = new InputSource(reader); 82 83 xmlReader.parse(inSource); 84 } 85 86 @Test 87 public void testGetElementTypeInfo() throws ParserConfigurationException, SAXException, IOException { 88 XMLReader xmlReader; 89 xmlReader = createXMLReader(); 90 91 final ValidatorHandler validatorHandler; 92 validatorHandler = createValidatorHandler(XSD); 93 94 xmlReader.setContentHandler(validatorHandler); 95 96 DefaultHandler handler = new DefaultHandler() { 97 public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException { 98 TypeInfoProvider infoProvider = null; 99 synchronized (validatorHandler) { 100 infoProvider = validatorHandler.getTypeInfoProvider(); 101 } 102 if (infoProvider == null) { 103 throw new SAXException("Can't obtain TypeInfoProvider object."); 104 } 105 106 try { 107 TypeInfo typeInfo = infoProvider.getElementTypeInfo(); 108 Assert.assertEquals(typeInfo.getTypeName(), "intOrString"); 109 } catch (IllegalStateException e) { 110 System.out.println(e); 111 throw new SAXException("Unexpected IllegalStateException was thrown."); 112 } 113 } 114 115 public void endElement(String uri, String localName, String qName) throws SAXException { 116 TypeInfoProvider infoProvider = null; 117 synchronized (validatorHandler) { 118 infoProvider = validatorHandler.getTypeInfoProvider(); 119 } 120 if (infoProvider == null) { 121 throw new SAXException("Can't obtain TypeInfoProvider object."); 122 } 123 124 try { 125 TypeInfo typeInfo = infoProvider.getElementTypeInfo(); 126 Assert.assertEquals(typeInfo.getTypeName(), "string"); 127 } catch (IllegalStateException e) { 128 System.out.println(e); 129 throw new SAXException("Unexpected IllegalStateException was thrown."); 130 } 131 } 132 }; 133 validatorHandler.setContentHandler(handler); 134 135 parse(xmlReader, XML); 136 } 137 }