You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by el...@apache.org on 2003/03/27 17:09:24 UTC

cvs commit: xml-xerces/java/tests/schema Test.java personal.xsd personal-schema.xml

elena       2003/03/27 08:09:24

  Added:       java/tests/schema Test.java personal.xsd personal-schema.xml
  Log:
  add tests for external-schema location. Submitter: Khaled Noaman
  
  Revision  Changes    Path
  1.1                  xml-xerces/java/tests/schema/Test.java
  
  Index: Test.java
  ===================================================================
  /*
   *
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xerces" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 2003, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package schema;
  
  import org.xml.sax.SAXException;
  import org.xml.sax.SAXParseException;
  import org.xml.sax.helpers.DefaultHandler;
  import org.apache.xerces.parsers.SAXParser;
  
  /**
   * Test Schema processing
   *
   * @author Khaled Noaman, IBM
   */
  public class Test extends DefaultHandler {
  
  
      // feature ids
  
      /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
      protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
  
      /** Namespace prefixes feature id (http://xml.org/sax/features/namespace-prefixes). */
      protected static final String NAMESPACE_PREFIXES_FEATURE_ID = "http://xml.org/sax/features/namespace-prefixes";
  
      /** Validation feature id (http://xml.org/sax/features/validation). */
      protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
  
      /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
      protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema";
  
      /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */
      protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking";
  
      /** Dynamic validation feature id (http://apache.org/xml/features/validation/dynamic). */
      protected static final String DYNAMIC_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/dynamic";
  
  
      // property ids
  
      /** schema noNamespaceSchemaLocation property id (http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation). */
      protected static final String SCHEMA_NONS_LOCATION_ID = "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
  
  
      // Default data source location
      protected static final String SOURCE_LOC_ID = "./tests/schema/";
  
  
      // Default constructor
      public Test() {
      }
  
  
      // ErrorHandler methods
  
      public void warning(SAXParseException ex) throws SAXException {
          printError("Warning", ex);
      }
  
      public void error(SAXParseException ex) throws SAXException {
          printError("Error", ex);
      }
  
      public void fatalError(SAXParseException ex) throws SAXException {
          printError("Fatal Error", ex);
      }
  
  
      // Protected methods
      protected void printError(String type, SAXParseException ex) {
  
          System.err.print("[");
          System.err.print(type);
          System.err.print("] ");
          if (ex== null) {
              System.out.println("!!!");
          }
          String systemId = ex.getSystemId();
          if (systemId != null) {
              int index = systemId.lastIndexOf('/');
              if (index != -1)
                  systemId = systemId.substring(index + 1);
              System.err.print(systemId);
          }
          System.err.print(':');
          System.err.print(ex.getLineNumber());
          System.err.print(':');
          System.err.print(ex.getColumnNumber());
          System.err.print(": ");
          System.err.print(ex.getMessage());
          System.err.println();
          System.err.flush();
      }
  
      public void testSettingNoNamespaceSchemaLocation() throws Exception {
  
          System.err.println("#");
          System.err.println("# Testing Setting noNamespaceSchemaLocation property");
          System.err.println("#");
  
          try {
              SAXParser parser = new org.apache.xerces.parsers.SAXParser();
  
              // Set features
              parser.setFeature(NAMESPACES_FEATURE_ID, true);
              parser.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, false);
              parser.setFeature(VALIDATION_FEATURE_ID, true);
              parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
              parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, false);
              parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, false);
  
              // Set properties
              parser.setProperty(SCHEMA_NONS_LOCATION_ID, "personal.xsd");
  
              // Set handlers
              parser.setContentHandler(this);
              parser.setErrorHandler(this);
  
              // parse document
              parser.parse(SOURCE_LOC_ID + "personal-schema.xml");
              System.err.println("Pass: " + SOURCE_LOC_ID + "personal-schema.xml");
              System.err.println();
          }
          catch (SAXException e) {
              System.err.println("Fail:" + e.getMessage());
          }
      }
  
  
      /** Main program entry point. */
      public static void main(String argv[]) throws Exception {
  
          Test test = new Test();
          test.testSettingNoNamespaceSchemaLocation();
      }
  }
  
  
  
  1.1                  xml-xerces/java/tests/schema/personal.xsd
  
  Index: personal.xsd
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  
   <xs:element name="personnel">
    <xs:complexType>
     <xs:sequence>
       <xs:element ref="person" minOccurs='1' maxOccurs='unbounded'/>
     </xs:sequence>
    </xs:complexType>
  
    <xs:unique name="unique1">
     <xs:selector xpath="person"/>
     <xs:field xpath="name/given"/>
     <xs:field xpath="name/family"/>
    </xs:unique>
    <xs:key name='empid'>
     <xs:selector xpath="person"/>
     <xs:field xpath="@id"/>
    </xs:key>
    <xs:keyref name="keyref1" refer='empid'>
     <xs:selector xpath="person"/> 
     <xs:field xpath="link/@manager"/>  
    </xs:keyref>
  
   </xs:element>
  
   <xs:element name="person">
    <xs:complexType>
     <xs:sequence>
       <xs:element ref="name"/>
       <xs:element ref="email" minOccurs='0' maxOccurs='unbounded'/>
       <xs:element ref="url"   minOccurs='0' maxOccurs='unbounded'/>
       <xs:element ref="link"  minOccurs='0' maxOccurs='1'/>
     </xs:sequence>
     <xs:attribute name="id"  type="xs:ID" use='required'/>
     <xs:attribute name="note" type="xs:string"/>
     <xs:attribute name="contr" default="false">
      <xs:simpleType>
       <xs:restriction base = "xs:string">
         <xs:enumeration value="true"/>
         <xs:enumeration value="false"/>
       </xs:restriction>
      </xs:simpleType>
     </xs:attribute>
     <xs:attribute name="salary" type="xs:integer"/>
    </xs:complexType>
   </xs:element>
  
   <xs:element name="name">
    <xs:complexType>
     <xs:all>
      <xs:element ref="family"/>
      <xs:element ref="given"/>
     </xs:all>
    </xs:complexType>
   </xs:element>
  
   <xs:element name="family" type='xs:string'/>
  
   <xs:element name="given" type='xs:string'/>
  
   <xs:element name="email" type='xs:string'/>
  
   <xs:element name="url">
    <xs:complexType>
     <xs:attribute name="href" type="xs:string" default="http://"/>
    </xs:complexType>
   </xs:element>
  
   <xs:element name="link">
    <xs:complexType>
     <xs:attribute name="manager" type="xs:IDREF"/>
     <xs:attribute name="subordinates" type="xs:IDREFS"/>
    </xs:complexType>
   </xs:element>
  
   <xs:notation name='gif' public='-//APP/Photoshop/4.0' system='photoshop.exe'/>
  
  </xs:schema>
  
  
  
  1.1                  xml-xerces/java/tests/schema/personal-schema.xml
  
  Index: personal-schema.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <personnel>
  
    <person id="Big.Boss" >
      <name><family>Boss</family> <given>Big</given></name>
      <email>chief@foo.com</email>
      <link subordinates="one.worker two.worker three.worker four.worker five.worker"/>
    </person>
  
    <person id="one.worker">
      <name><family>Worker</family> <given>One</given></name>
      <email>one@foo.com</email>
      <link manager="Big.Boss"/>
    </person>
  
    <person id="two.worker">
      <name><family>Worker</family> <given>Two</given></name>
      <email>two@foo.com</email>
      <link manager="Big.Boss"/>
    </person>
  
    <person id="three.worker">
      <name><family>Worker</family> <given>Three</given></name>
      <email>three@foo.com</email>
      <link manager="Big.Boss"/>
    </person>
  
    <person id="four.worker">
      <name><family>Worker</family> <given>Four</given></name>
      <email>four@foo.com</email>
      <link manager="Big.Boss"/>
    </person>
  
    <person id="five.worker">
      <name><family>Worker</family> <given>Five</given></name>
      <email>five@foo.com</email>
      <link manager="Big.Boss"/>
    </person>
  
  </personnel>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org