1 /*
   2  * Copyright (c) 1999, 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 package javax.xml.validation.ptests;
  24 
  25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  26 import static javax.xml.validation.ptests.ValidationTestConst.XML_DIR;
  27 import static jaxp.library.JAXPTestUtilities.filenameToURL;
  28 import static org.testng.Assert.assertEquals;
  29 import static org.testng.Assert.assertFalse;
  30 import static org.testng.Assert.assertTrue;
  31 
  32 import java.io.File;
  33 import java.io.IOException;
  34 
  35 import javax.xml.parsers.ParserConfigurationException;
  36 import javax.xml.parsers.SAXParserFactory;
  37 import javax.xml.validation.Schema;
  38 import javax.xml.validation.SchemaFactory;
  39 import javax.xml.validation.TypeInfoProvider;
  40 import javax.xml.validation.ValidatorHandler;
  41 
  42 import jaxp.library.JAXPFileBaseTest;
  43 
  44 import org.testng.annotations.Test;
  45 import org.xml.sax.Attributes;
  46 import org.xml.sax.InputSource;
  47 import org.xml.sax.SAXException;
  48 import org.xml.sax.XMLReader;
  49 import org.xml.sax.helpers.DefaultHandler;
  50 
  51 /*
  52  * @summary test ValidatorHandler.getTypeInfoProvider()
  53  */
  54 public class TypeInfoProviderTest extends JAXPFileBaseTest {
  55 
  56     private ValidatorHandler validatorHandler;
  57 
  58     @Test
  59     public void test() throws SAXException, ParserConfigurationException, IOException {
  60 
  61         SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
  62         Schema schema = sf.newSchema(new File(XML_DIR + "shiporder11.xsd"));
  63         validatorHandler = schema.newValidatorHandler();
  64         MyDefaultHandler myDefaultHandler = new MyDefaultHandler();
  65         validatorHandler.setContentHandler(myDefaultHandler);
  66 
  67         InputSource is = new InputSource(filenameToURL(XML_DIR + "shiporder11.xml"));
  68 
  69         SAXParserFactory parserFactory = SAXParserFactory.newInstance();
  70         parserFactory.setNamespaceAware(true);
  71         XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
  72         xmlReader.setContentHandler(validatorHandler);
  73         xmlReader.parse(is);
  74 
  75     }
  76 
  77     private class MyDefaultHandler extends DefaultHandler {
  78 
  79         public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
  80             TypeInfoProvider typeInfoProvider = validatorHandler.getTypeInfoProvider();
  81             int index = atts.getIndex("orderid");
  82             if (index != -1) {
  83                 System.out.println(" Index " + index);
  84                 System.out.println(" ElementType " + typeInfoProvider.getElementTypeInfo().getTypeName());
  85                 assertEquals(typeInfoProvider.getAttributeTypeInfo(index).getTypeName(), "string");
  86                 assertTrue(typeInfoProvider.isSpecified(index));
  87                 assertFalse(typeInfoProvider.isIdAttribute(index));
  88             }
  89 
  90         }
  91 
  92     }
  93 }