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