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