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