1 /*
   2  * Copyright (c) 2005, 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 org.w3c.dom.ptests;
  24 
  25 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
  26 import static org.testng.Assert.assertEquals;
  27 
  28 import java.io.StringReader;
  29 
  30 import javax.xml.parsers.DocumentBuilder;
  31 import javax.xml.parsers.DocumentBuilderFactory;
  32 
  33 import org.testng.annotations.Listeners;
  34 import org.testng.annotations.Test;
  35 import org.w3c.dom.Document;
  36 import org.w3c.dom.Element;
  37 import org.w3c.dom.TypeInfo;
  38 import org.xml.sax.InputSource;
  39 
  40 /*
  41  * @test
  42  * @library /javax/xml/jaxp/libs
  43  * @run testng/othervm -DrunSecMngr=true org.w3c.dom.ptests.TypeInfoTest
  44  * @run testng/othervm org.w3c.dom.ptests.TypeInfoTest
  45  * @summary Test getTypeName and getTypeNamespace methods of TypeInfo interface
  46  */
  47 @Listeners({jaxp.library.BasePolicy.class})
  48 public class TypeInfoTest {
  49     /*
  50      * Get the TypeInfo of the root element, and verify it.
  51      */
  52     @Test
  53     public void test() throws Exception {
  54         TypeInfo typeInfo = getTypeOfRoot(SCHEMA_INSTANCE, "<?xml version='1.0'?>\n" + "<test1 xmlns=\"testNS\"><code/></test1>\n");
  55 
  56         assertEquals(typeInfo.getTypeName(), "Test");
  57         assertEquals(typeInfo.getTypeNamespace(), "testNS");
  58 
  59     }
  60 
  61     private TypeInfo getTypeOfRoot(String schemaText, String docText) throws Exception {
  62         Element root = getRoot(schemaText, docText);
  63         return root.getSchemaTypeInfo();
  64     }
  65 
  66     private Element getRoot(String schemaText, String docText) throws Exception {
  67         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  68 
  69         InputSource inSchema = new InputSource(new StringReader(schemaText));
  70         inSchema.setSystemId("schema.xsd");
  71         dbf.setNamespaceAware(true);
  72         dbf.setValidating(true);
  73         dbf.setAttribute(SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
  74         dbf.setAttribute(SCHEMA_SOURCE, inSchema);
  75 
  76         DocumentBuilder parser = dbf.newDocumentBuilder();
  77 
  78         InputSource inSource = new InputSource(new StringReader(docText));
  79         inSource.setSystemId("doc.xml");
  80         Document document = parser.parse(inSource);
  81 
  82         return document.getDocumentElement();
  83     }
  84 
  85     private static final String SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  86 
  87     private static final String SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
  88 
  89     /*
  90      * Schema instance
  91      */
  92     private static final String SCHEMA_INSTANCE =
  93             "<?xml version=\"1.0\"?>\n"
  94             + "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n"
  95             + "            xmlns:testNS=\"testNS\"\n"
  96             + "            targetNamespace=\"testNS\" elementFormDefault=\"qualified\">\n"
  97             + "    <xsd:element name=\"test1\" type=\"testNS:Test\"/>\n"
  98             + "    \n"
  99             + "    <xsd:complexType name=\"Test\">\n"
 100             + "        <xsd:sequence>\n"
 101             + "            <xsd:element name=\"description\" minOccurs=\"0\"/>\n"
 102             + "            <xsd:element name=\"code\"/>\n"
 103             + "        </xsd:sequence>\n"
 104             + "    </xsd:complexType>\n"
 105             + "\n"
 106             + "    <xsd:element name=\"test2\">\n"
 107             + "        <xsd:complexType>\n"
 108             + "            <xsd:sequence>\n"
 109             + "                <xsd:element name=\"description\" minOccurs=\"0\"/>\n"
 110             + "                <xsd:element name=\"code\"/>\n"
 111             + "            </xsd:sequence>\n"
 112             + "        </xsd:complexType>\n"
 113             + "    </xsd:element>\n"
 114             + "\n"
 115             + "    <xsd:element name=\"test3\" type=\"xsd:string\"/>\n"
 116             + "\n"
 117             + "    <xsd:element name=\"test4\" type=\"testNS:Test1\"/>\n"
 118             + "\n"
 119             + "    <xsd:simpleType name=\"Test1\">\n"
 120             + "        <xsd:restriction base=\"xsd:string\"/>\n"
 121             + "    </xsd:simpleType>\n"
 122             + "\n"
 123             + "    <xsd:element name=\"test5\">\n"
 124             + "        <xsd:simpleType>\n"
 125             + "            <xsd:restriction base=\"xsd:string\"/>\n"
 126             + "        </xsd:simpleType>\n"
 127             + "    </xsd:element>\n"
 128             + "\n"
 129             + "    <xsd:element name=\"test6\">\n"
 130             + "        <xsd:complexType>\n"
 131             + "            <xsd:complexContent>\n"
 132             + "                <xsd:extension base=\"testNS:Test\">\n"
 133             + "                    <xsd:attribute name=\"attr\" type=\"xsd:string\"/>\n"
 134             + "                </xsd:extension>\n"
 135             + "            </xsd:complexContent>\n"
 136             + "        </xsd:complexType>\n"
 137             + "    </xsd:element>\n"
 138             + "\n"
 139             + "</xsd:schema>\n";
 140 
 141 
 142 }