You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by sa...@ca.ibm.com on 2002/05/14 17:19:34 UTC

[Schema CM API] How it works with grammar loading

With the Schema Component Model API, users can access various properties of
schema components. Then there comes the question: how to get an instance of
XSModel from schema grammars.

One obvious approach is via PSVI: after an instance is validated, there
will be a [schema information] property in the PSVI item of the validation
root element. This property can have a method to return an *XSModel*, which
contains all components corresponding to the [schema components] property
in the PSVI. (3.15.5)

But there are users who are interested in loading a schema grammar, then
accessing the components, without validating an instance. So we need the
ability to load schema grammars, and get *XSModel* from them. Before going
into the options we have here, I'll first review 1) our grammar loading
design; 2) XSModel and its future.

1. Grammar loading

Basically there will be loader classes for different kinds of grammars.
They have *loadGrammar* methods (or similar names), which return grammar
objects (implementations/subclasses of *Grammar/XMLGrammar* in XNI).

In the case of XML Schema, *loadGrammar* returns instances of
SchemaGrammar. SchemaGrammar contains components from one target namespace,
and has links to other schema grammars that it imports. This is what I'd
like to call the "one-grammar-per-namespace" rule. Note that the schema
spec doesn't require this, and other parser might choose other ways to
store schema components.

2. XSModel

XSModel models a "schema" defined in the schema spec, so it's a collection
of components from a list of target namespaces. These namespaces might not
be connected by importing. For example, in the instance document
  xsi:schemaLocation="ns1 xsd1 ns2 xsd2"
After validation (if both ns1 and ns2 are referenced in the instance), the
final schema/XSModel contains components from both namespaces.

Various things could happen to XSModel in the future:
1) Some organization standardizes Schema CM API, which could be
substantially different from our XSModel. Then we'll (have to) implement
such standard API, and (quite possibly) drop XSModel, because supporting
both APIs would confuse the users.
2) XSModel interfaces are adopted by some standard organization as the
Schema CM API. Then these interface would be independent of Xerces.
3) Neither of the above happens, so XSModel becomes part of XNI, and lives
there (forever).


Now let's get back to our subject: how to get XSModel from loaded grammars.
There could be 3 options:

1. XSModel extending Grammar

Then our SchemaGrammar (implementation class) implements XSModel. After
*loadGrammar* returns, the user can cast the returned Grammar to XSModel,
and access the components.

Pros:
- The relation between Grammar/XSModel/SchemaGrammar is explicit, and the
user can easily know how to cast  one to another.
Cons:
- XSModel depends on an XNI interface. So if XSModel is adopted by some
organization to become Xerces-independent, we need to come back and
reconsider this problem again.
- Because XSModel != (the current) SchemaGrammar, as discussed above, we
need to alter the "one-grammar-per-namespace" rule a little bit. We'll need
a flag in SchemaGrammar to indicate whether it's a container grammar
(without a target namespace, containing other SchemaGrammar objects) or a
real grammar (with a target namespace).
- SchemaGrammar needs to carry the burden of implementing XSModel. But this
may not affect the performance of validating instance documents.

2. No explicit relation between XSModel and Grammar interfaces

SchemaGrammar implements both XSModel and Grammar. After *loadGrammar*
returns, the user needs cast the returned Grammar to XSModel, and access
the components.

Pros:
- XSModel doesn't depend on XNI.
Cons:
- Casting Grammar to XSModel might be *weird*, because there is connection
between the two interfaces.  (It's like cast a DocumentHandler object to a
DeclHander object.)
- Because XSModel != (the current) SchemaGrammar, as discussed above, we
need to alter the "one-grammar-per-namespace" rule a little bit. We'll need
a flag in SchemaGrammar to indicate whether it's a container grammar
(without a target namespace, containing other SchemaGrammar objects) or a
real grammar (with a target namespace).
- SchemaGrammar needs to carry the burden of implementing XSModel. But this
may not affect the performance of validating instance documents.

3. Construct an XSModel from SchemaGrammar objects

XSModel doesn't depend on XNI. We'll have an XMLSchemaGrammar deriving from
Grammar. A method *toXSModel* on such interface returns an XSModel that
contains all components from this schema grammar; it could also have a
overloaded *toXSModel* method that takes a list of XMLSchemaGrammar
objects, and returns an XSModel.

Pros:
- XSModel doesn't depend on XNI.
- XSModel and Grammar doesn't need to be connected
- "one-grammar-per-namespace" rule doesn't need to be altered, because
XSModel contains a list of XMLSchemaGrammar
Cons:
- More complicated design: one more interface in XNI
- Whenever *toXSModel* is called, a new XSModel is created. This may or may
not be a performance problem, depending on how XSModel is used.


These are the options we could think of. What do folks think? Are there
other solutions?

Cheers,
Sandy Gao
Software Developer, IBM Canada
(1-905) 413-3255
sandygao@ca.ibm.com


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


An interesting XML Schema validation error.

Posted by Harun Altay <ha...@grids.ucs.indiana.edu>.
Hello,

I have a problem with xml document validation.

Here is a source code, the xml document to be validated and the error
message. This source code is running without any error with the xerces of
JBuilder6. But failing to run on the standard xerces jar files from
www.apache.org.

Fact-1:

xerces coming with JBuilder6 : 1.767 bytes

xerces 1.4.3 from Apache : 769 bytes
xerces 2.0.0. from Apache : 769 bytes
xerces 2.0.1 from Apache : 769 bytes

conclusion: JBuilder's xerces includes something extra.

Fact-2:

I have 3 different examples, 1 from www.wrox.com, for the book: XML Schema,
2 examples from http://java.sun.com.  All of them are running on JBuilder's
xerces, but all of them giving error in apache's xerces: 1.4.3, 2.0.0,
2.1.0.

Question:

How can I run the same xml schema validation programs on the standard xerces
from apache?



please look at the example below:
---------------------------------------------------------

1. CODE
2. ERROR
3. XML DOCUMENT


1. CODE
----------------------------------------------------------
        parser.setFeature( "http://xml.org/sax/features/validation", true);

    //    "instance" is the String variable for the address of the xml
document.

    //    parser.parse(instance); --> THIS MUST BE A BUG! This line is
causing problem!

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

NOT: Please find complete source code at the end.



2. ERROR
-----------------------------------------------------------

[Error] Element type "okc" must be declared. line 11 column
2 -C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "xsi:schemaLocation" must be declared for element type
"okc". line 11 column 2 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "version" must be declared for element type "okc". line 11
column 2 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "event" must be declared. line 13 column 8 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 14 column 56 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
14 column 56 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
14 column 56 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 15 column 71 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
15 column 71 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
15 column 71 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 16 column 57 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
16 column 57 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
16 column 57 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 17 column 50 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
17 column 50 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found

[Error] Attribute "uri" must be declared for element type "definition". line
17 column 50 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "comment" must be declared. line 18 column 11 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "sender" must be declared. line 19 column 10 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "distribution" must be declared. line 20 column 16 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "organization" must be declared. line 21 column 16 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "update" must be declared. line 23 column 36 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "createuri" must be declared for element type "update".
line 23 column 36 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "keywords" must be declared. line 25 column 12 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "message" must be declared. line 26 column 33 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "whitespace" must be declared for element type "message".
line 26 column 33 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "keyword" must be declared. line 27 column 40 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "filingdate" must be declared. line 29 column 14 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 30 column 31 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 30 column 31 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found

[Error] Element type "cg:category" must be declared. line 31 column 42 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 31 column 42 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 31 column 42 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 32 column 43 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 32 column 43 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 32 column 43 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found

[Error] Element type "cg:category" must be declared. line 33 column 48 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 33 column 48 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "other" must be declared for element type "cg:category".
line 33 column 48 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 34 column 75 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "other" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found

Complete 0 warnings 44 errors 0 fatal errors
[Error] Element type "cg:messageType" must be declared. line 35 column 18 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:messageType" must be declared. line 36 column 18 -
C:\jprojects\swing\swing2\template2-v2.xml




3. XML DOCUMENT
-----------------------------------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<okc
 xmlns="http://grids.ucs.indiana.edu/okc/schema/admin/ver/2"
 xmlns:cg="http://grids.ucs.indiana.edu/okc/schema/cg/ver/1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=
  "http://grids.ucs.indiana.edu/okc/schema/admin/ver/2
  http://grids.ucs.indiana.edu/schemas/okc-v2.xsd
  http://grids.ucs.indiana.edu/okc/schema/cg/ver/1
  http://grids.ucs.indiana.edu/schemas/commgrids-v1.xsd" version="2"
>

<event>
 <definition ref="anydefinition" uri="gxos://someuri"/>
 <definition ref="anotherdefinition" uri="gxos://someuri/anotheruri"/>
 <definition ref="me" uri="gxos://someuri/users/$user"/>
 <definition ref="ideas" uri="$me/ideas/$today"/>
 <comment>comments here</comment>
 <sender>sender's name</sender>
 <distribution>destination of this message</distribution>
 <organization>name of the organization where this message will be
distributed</organization>

 <update createuri="$ideas/$next"/>

 <keywords>keywords go here</keywords>
 <message whitespace="preserve">
  You message goes here. your <keyword>keywords</keyword> can be
highlighted.
 </message>
 <filingdate>12/31/2000</filingdate>  <!-- MM/DD/YYYY -->
 <cg:category main="general"/>
 <cg:category main="facility" sub="okc"/>
 <cg:category main="research" sub="gxos"/>
 <cg:category main="other" other="a_category"/>
 <cg:category main="other" sub="other" other="a_category/a_sub_category"/>
 <cg:messageType>WeeklyReport</cg:messageType>
 <cg:messageType>Other</cg:messageType>
</event>

</okc>





COMPLETE SOURCE CODE
---------------------------------------

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.net.*;
import java.io.*;

import org.xml.sax.InputSource;

import org.apache.xerces.parsers.DOMParser;

public class validate implements ErrorHandler {
  private String instance = null;
  private URL instanceURL = null;
  private boolean noSchema = false;
  private String schema = null;
  private int warnings = 0;
  private int errors = 0;
  private int fatalErrors = 0;
  private org.apache.xerces.parsers.DOMParser parser
                = new org.apache.xerces.parsers.DOMParser();
  public boolean valid = false;

  public validate()                 {   parser.setErrorHandler(this);   }
  public void setInstance(String s) {   instance = s;  }
  public void setInstance(URL u)    {   instanceURL = u;  }
  public void setSchema(String s)   {   schema = s; }
    //  System.out.println("Currently you must specify the schema utilizing"
+
    //  " xsi:schemaLocation " + "or xsi:noNamespaceSchemaLocation - using
default");



  public boolean doCheckSourceDocumentURL()throws Exception {

    try {
      try {
        // Turn validation off
        parser.setFeature( "http://xml.org/sax/features/validation", false);
        // Get the document
        System.out.println("");
        System.out.println("Checking source document...");

        parser.parse(new org.xml.sax.InputSource(new InputStreamReader(
this.instanceURL.openStream())) );
//        parser.parse(instance);

        // Get the document
        Document doc = parser.getDocument();
        if (doc != null) {
          // Check if the schema is null
          if (schema != null) {

            // Check for xsi:schemaLocation
            // Check for xsi:noNamespaceSchemaLocation !! assuming no
namespace for now

doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation",
schema);
            doc.getDocumentElement().setAttribute("xmlns:xsi",

org.apache.xerces.validators.schema.SchemaSymbols.URI_XSI);


          // Check if they specified the location
          } else if
((!doc.getDocumentElement().hasAttribute("xsi:schemaLocation"))
            &&
(!doc.getDocumentElement().hasAttribute("xsi:noNamespaceSchemaLocation"))) {

            this.noSchema = true;

            // Check for a namespace decl for the root element
            // Slam it for RDDL
            // Slam it for a Schema
            // No location - except out
//            throw new Exception("[Error] You must specify the schema for "
+ instance +
  //            " by including the attribute xsi:schemaLocation " +
    //          "or xsi:noNamespaceSchemaLocation on the root element");
          }
        }
      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
        return false;
      }
      // Made it through with no exceptions...
      return true;
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }




  public boolean doCheckSourceDocument() throws Exception {
    try {
      try {
        // Turn validation off
        parser.setFeature( "http://xml.org/sax/features/validation", false);
        // Get the document
        System.out.println("");
        System.out.println("Checking source document...");

    //    parser.parse(instance);

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

        // Get the document
        Document doc = parser.getDocument();
        if (doc != null) {
          // Check if the schema is null
          if (schema != null) {
            // Check for xsi:schemaLocation
            // Check for xsi:noNamespaceSchemaLocation !! assuming no
namespace for now

doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation",
schema);
            doc.getDocumentElement().setAttribute("xmlns:xsi",

org.apache.xerces.validators.schema.SchemaSymbols.URI_XSI);

          // Check if they specified the location
          } else if
((!doc.getDocumentElement().hasAttribute("xsi:schemaLocation")) &&

(!doc.getDocumentElement().hasAttribute("xsi:noNamespaceSchemaLocation"))) {

            this.noSchema = true;

            // Check for a namespace decl for the root element
            // Slam it for RDDL
            // Slam it for a Schema
            // No location - except out
//            throw new Exception("[Error] You must specify the schema for "
+ instance +
  //            " by including the attribute xsi:schemaLocation " +
    //          "or xsi:noNamespaceSchemaLocation on the root element");
          } // else if---
        }//else if---

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
        return false;
      }
      // Made it through with no exceptions...
      return true;
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }


  public boolean doValidate()throws Exception {

  if (this.noSchema) {
        System.out.println("There is no schema specified in the document, so
there will not be any validation.");
        this.noSchema = false;
        return false;
  }

    try {
      warnings = 0;
      errors = 0;
      fatalErrors = 0;
      try {
        // Now reparse with the validation turned on
        parser.setFeature( "http://xml.org/sax/features/validation", true);
        System.out.println("Validating source document...");

    //    parser.parse(instance);

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
      }

      // Return true if we made it this far with no errors
      System.out.println("");
      System.out.println("Complete " + warnings + " warnings " +
                        errors + " errors " + fatalErrors + " fatal
errors");
      return ((errors == 0) && (fatalErrors == 0));
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }

  public boolean doValidateURL() throws Exception{

  if (this.noSchema) {
        this.noSchema = false;
        System.out.println("There is no schema specified in the document, so
there will not be any validation.");
        return false;
  }

    try {
      warnings = 0;
      errors = 0;
      fatalErrors = 0;
      try {
        // Now reparse with the validation turned on
        parser.setFeature( "http://xml.org/sax/features/validation", true);
        System.out.println("Validating source document...");

        parser.parse(new org.xml.sax.InputSource(new InputStreamReader(
this.instanceURL.openStream())) );
        // parser.parse(instance);

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
      }

      // Return true if we made it this far with no errors
      System.out.println("");
      System.out.println("Complete " + warnings + " warnings " +
                        errors + " errors " + fatalErrors + " fatal
errors");
      return ((errors == 0) && (fatalErrors == 0));
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }

  public void warning(SAXParseException ex) {
     System.out.println("");
     System.err.println("[Warning] " + ex.getMessage() +
       " line " + ex.getLineNumber() + " column " + ex.getColumnNumber() +
       " - " + instance);
     warnings++;
  }

  public void error(SAXParseException ex) {
     System.out.println("");
     System.err.println("[Error] " + ex.getMessage() +
       " line " + ex.getLineNumber() + " column " + ex.getColumnNumber() +
       " - " + instance);
     errors++;
  }

  public void fatalError(SAXParseException ex) throws SAXException {
     System.out.println("");
     System.err.println("[Fatal Error] " + ex.getMessage() + " line " +
ex.getLineNumber() +
       " column " + ex.getColumnNumber() + " - " + instance);
     fatalErrors++;
//     throw ex;
  }


  public static void main(String[] args) {
    validate validate1 = new validate();
    if (args.length == 0) {
      System.out.println("Usage : java validate <instance> <schema>");
      return;
    }
    if (args.length >= 1) {           // Set the instance
      validate1.setInstance(args[0]);
    }
    if (args.length >= 2) {               // Set the Schema
      validate1.setSchema(args[1]);
    }
    // Validate - currently doCheckSourceDocument does nothing to modify the
    // validating document... it can be removed to speed this up...
//    if (!validate1.doCheckSourceDocument()) return;
//    if (!validate1.doValidate()) return;
  }
}








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


An interesting XML Schema validation error.

Posted by Harun Altay <ha...@grids.ucs.indiana.edu>.
Hello,

I have a problem with xml document validation.

Here is a source code, the xml document to be validated and the error
message. This source code is running without any error with the xerces of
JBuilder6. But failing to run on the standard xerces jar files from
www.apache.org.

Fact-1:

xerces coming with JBuilder6 : 1.767 bytes

xerces 1.4.3 from Apache : 769 bytes
xerces 2.0.0. from Apache : 769 bytes
xerces 2.0.1 from Apache : 769 bytes

conclusion: JBuilder's xerces includes something extra.

Fact-2:

I have 3 different examples, 1 from www.wrox.com, for the book: XML Schema,
2 examples from http://java.sun.com.  All of them are running on JBuilder's
xerces, but all of them giving error in apache's xerces: 1.4.3, 2.0.0,
2.1.0.

Question:

How can I run the same xml schema validation programs on the standard xerces
from apache?



please look at the example below:
---------------------------------------------------------

1. CODE
2. ERROR
3. XML DOCUMENT


1. CODE
----------------------------------------------------------
        parser.setFeature( "http://xml.org/sax/features/validation", true);

    //    "instance" is the String variable for the address of the xml
document.

    //    parser.parse(instance); --> THIS MUST BE A BUG! This line is
causing problem!

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

NOT: Please find complete source code at the end.



2. ERROR
-----------------------------------------------------------

[Error] Element type "okc" must be declared. line 11 column
2 -C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "xsi:schemaLocation" must be declared for element type
"okc". line 11 column 2 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "version" must be declared for element type "okc". line 11
column 2 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "event" must be declared. line 13 column 8 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 14 column 56 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
14 column 56 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
14 column 56 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 15 column 71 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
15 column 71 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
15 column 71 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 16 column 57 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
16 column 57 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "uri" must be declared for element type "definition". line
16 column 57 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "definition" must be declared. line 17 column 50 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "ref" must be declared for element type "definition". line
17 column 50 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found

[Error] Attribute "uri" must be declared for element type "definition". line
17 column 50 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "comment" must be declared. line 18 column 11 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "sender" must be declared. line 19 column 10 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "distribution" must be declared. line 20 column 16 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "organization" must be declared. line 21 column 16 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "update" must be declared. line 23 column 36 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "createuri" must be declared for element type "update".
line 23 column 36 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "keywords" must be declared. line 25 column 12 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "message" must be declared. line 26 column 33 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "whitespace" must be declared for element type "message".
line 26 column 33 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "keyword" must be declared. line 27 column 40 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "filingdate" must be declared. line 29 column 14 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 30 column 31 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 30 column 31 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found

[Error] Element type "cg:category" must be declared. line 31 column 42 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 31 column 42 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 31 column 42 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 32 column 43 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 32 column 43 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 32 column 43 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found

[Error] Element type "cg:category" must be declared. line 33 column 48 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 33 column 48 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "other" must be declared for element type "cg:category".
line 33 column 48 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:category" must be declared. line 34 column 75 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "main" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "sub" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml
[Error] Attribute "other" must be declared for element type "cg:category".
line 34 column 75 - C:\jprojects\swing\swing2\template2-v2.xml

http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found
http://grids.ucs.indiana.edu/okc/schema/cg/ver/1 grammar not found
http://grids.ucs.indiana.edu/okc/schema/admin/ver/2 grammar not found

Complete 0 warnings 44 errors 0 fatal errors
[Error] Element type "cg:messageType" must be declared. line 35 column 18 -
C:\jprojects\swing\swing2\template2-v2.xml
[Error] Element type "cg:messageType" must be declared. line 36 column 18 -
C:\jprojects\swing\swing2\template2-v2.xml




3. XML DOCUMENT
-----------------------------------------------------------


<?xml version="1.0" encoding="UTF-8"?>
<okc
 xmlns="http://grids.ucs.indiana.edu/okc/schema/admin/ver/2"
 xmlns:cg="http://grids.ucs.indiana.edu/okc/schema/cg/ver/1"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=
  "http://grids.ucs.indiana.edu/okc/schema/admin/ver/2
  http://grids.ucs.indiana.edu/schemas/okc-v2.xsd
  http://grids.ucs.indiana.edu/okc/schema/cg/ver/1
  http://grids.ucs.indiana.edu/schemas/commgrids-v1.xsd" version="2"
>

<event>
 <definition ref="anydefinition" uri="gxos://someuri"/>
 <definition ref="anotherdefinition" uri="gxos://someuri/anotheruri"/>
 <definition ref="me" uri="gxos://someuri/users/$user"/>
 <definition ref="ideas" uri="$me/ideas/$today"/>
 <comment>comments here</comment>
 <sender>sender's name</sender>
 <distribution>destination of this message</distribution>
 <organization>name of the organization where this message will be
distributed</organization>

 <update createuri="$ideas/$next"/>

 <keywords>keywords go here</keywords>
 <message whitespace="preserve">
  You message goes here. your <keyword>keywords</keyword> can be
highlighted.
 </message>
 <filingdate>12/31/2000</filingdate>  <!-- MM/DD/YYYY -->
 <cg:category main="general"/>
 <cg:category main="facility" sub="okc"/>
 <cg:category main="research" sub="gxos"/>
 <cg:category main="other" other="a_category"/>
 <cg:category main="other" sub="other" other="a_category/a_sub_category"/>
 <cg:messageType>WeeklyReport</cg:messageType>
 <cg:messageType>Other</cg:messageType>
</event>

</okc>





COMPLETE SOURCE CODE
---------------------------------------

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.net.*;
import java.io.*;

import org.xml.sax.InputSource;

import org.apache.xerces.parsers.DOMParser;

public class validate implements ErrorHandler {
  private String instance = null;
  private URL instanceURL = null;
  private boolean noSchema = false;
  private String schema = null;
  private int warnings = 0;
  private int errors = 0;
  private int fatalErrors = 0;
  private org.apache.xerces.parsers.DOMParser parser
                = new org.apache.xerces.parsers.DOMParser();
  public boolean valid = false;

  public validate()                 {   parser.setErrorHandler(this);   }
  public void setInstance(String s) {   instance = s;  }
  public void setInstance(URL u)    {   instanceURL = u;  }
  public void setSchema(String s)   {   schema = s; }
    //  System.out.println("Currently you must specify the schema utilizing"
+
    //  " xsi:schemaLocation " + "or xsi:noNamespaceSchemaLocation - using
default");



  public boolean doCheckSourceDocumentURL()throws Exception {

    try {
      try {
        // Turn validation off
        parser.setFeature( "http://xml.org/sax/features/validation", false);
        // Get the document
        System.out.println("");
        System.out.println("Checking source document...");

        parser.parse(new org.xml.sax.InputSource(new InputStreamReader(
this.instanceURL.openStream())) );
//        parser.parse(instance);

        // Get the document
        Document doc = parser.getDocument();
        if (doc != null) {
          // Check if the schema is null
          if (schema != null) {

            // Check for xsi:schemaLocation
            // Check for xsi:noNamespaceSchemaLocation !! assuming no
namespace for now

doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation",
schema);
            doc.getDocumentElement().setAttribute("xmlns:xsi",

org.apache.xerces.validators.schema.SchemaSymbols.URI_XSI);


          // Check if they specified the location
          } else if
((!doc.getDocumentElement().hasAttribute("xsi:schemaLocation"))
            &&
(!doc.getDocumentElement().hasAttribute("xsi:noNamespaceSchemaLocation"))) {

            this.noSchema = true;

            // Check for a namespace decl for the root element
            // Slam it for RDDL
            // Slam it for a Schema
            // No location - except out
//            throw new Exception("[Error] You must specify the schema for "
+ instance +
  //            " by including the attribute xsi:schemaLocation " +
    //          "or xsi:noNamespaceSchemaLocation on the root element");
          }
        }
      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
        return false;
      }
      // Made it through with no exceptions...
      return true;
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }




  public boolean doCheckSourceDocument() throws Exception {
    try {
      try {
        // Turn validation off
        parser.setFeature( "http://xml.org/sax/features/validation", false);
        // Get the document
        System.out.println("");
        System.out.println("Checking source document...");

    //    parser.parse(instance);

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

        // Get the document
        Document doc = parser.getDocument();
        if (doc != null) {
          // Check if the schema is null
          if (schema != null) {
            // Check for xsi:schemaLocation
            // Check for xsi:noNamespaceSchemaLocation !! assuming no
namespace for now

doc.getDocumentElement().setAttribute("xsi:noNamespaceSchemaLocation",
schema);
            doc.getDocumentElement().setAttribute("xmlns:xsi",

org.apache.xerces.validators.schema.SchemaSymbols.URI_XSI);

          // Check if they specified the location
          } else if
((!doc.getDocumentElement().hasAttribute("xsi:schemaLocation")) &&

(!doc.getDocumentElement().hasAttribute("xsi:noNamespaceSchemaLocation"))) {

            this.noSchema = true;

            // Check for a namespace decl for the root element
            // Slam it for RDDL
            // Slam it for a Schema
            // No location - except out
//            throw new Exception("[Error] You must specify the schema for "
+ instance +
  //            " by including the attribute xsi:schemaLocation " +
    //          "or xsi:noNamespaceSchemaLocation on the root element");
          } // else if---
        }//else if---

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
        return false;
      }
      // Made it through with no exceptions...
      return true;
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }


  public boolean doValidate()throws Exception {

  if (this.noSchema) {
        System.out.println("There is no schema specified in the document, so
there will not be any validation.");
        this.noSchema = false;
        return false;
  }

    try {
      warnings = 0;
      errors = 0;
      fatalErrors = 0;
      try {
        // Now reparse with the validation turned on
        parser.setFeature( "http://xml.org/sax/features/validation", true);
        System.out.println("Validating source document...");

    //    parser.parse(instance);

        FileInputStream fis = new FileInputStream(instance);
        parser.parse(new InputSource(fis));

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
      }

      // Return true if we made it this far with no errors
      System.out.println("");
      System.out.println("Complete " + warnings + " warnings " +
                        errors + " errors " + fatalErrors + " fatal
errors");
      return ((errors == 0) && (fatalErrors == 0));
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }

  public boolean doValidateURL() throws Exception{

  if (this.noSchema) {
        this.noSchema = false;
        System.out.println("There is no schema specified in the document, so
there will not be any validation.");
        return false;
  }

    try {
      warnings = 0;
      errors = 0;
      fatalErrors = 0;
      try {
        // Now reparse with the validation turned on
        parser.setFeature( "http://xml.org/sax/features/validation", true);
        System.out.println("Validating source document...");

        parser.parse(new org.xml.sax.InputSource(new InputStreamReader(
this.instanceURL.openStream())) );
        // parser.parse(instance);

      } catch (SAXException e) {
        this.valid=false;
        System.out.println("");
        System.err.println("Could not activate validation feature");
      }

      // Return true if we made it this far with no errors
      System.out.println("");
      System.out.println("Complete " + warnings + " warnings " +
                        errors + " errors " + fatalErrors + " fatal
errors");
      return ((errors == 0) && (fatalErrors == 0));
    } catch (Exception e) {
      this.valid=false;
      System.out.println("");
      System.err.println(e.getMessage());
      return false;
    }
  }

  public void warning(SAXParseException ex) {
     System.out.println("");
     System.err.println("[Warning] " + ex.getMessage() +
       " line " + ex.getLineNumber() + " column " + ex.getColumnNumber() +
       " - " + instance);
     warnings++;
  }

  public void error(SAXParseException ex) {
     System.out.println("");
     System.err.println("[Error] " + ex.getMessage() +
       " line " + ex.getLineNumber() + " column " + ex.getColumnNumber() +
       " - " + instance);
     errors++;
  }

  public void fatalError(SAXParseException ex) throws SAXException {
     System.out.println("");
     System.err.println("[Fatal Error] " + ex.getMessage() + " line " +
ex.getLineNumber() +
       " column " + ex.getColumnNumber() + " - " + instance);
     fatalErrors++;
//     throw ex;
  }


  public static void main(String[] args) {
    validate validate1 = new validate();
    if (args.length == 0) {
      System.out.println("Usage : java validate <instance> <schema>");
      return;
    }
    if (args.length >= 1) {           // Set the instance
      validate1.setInstance(args[0]);
    }
    if (args.length >= 2) {               // Set the Schema
      validate1.setSchema(args[1]);
    }
    // Validate - currently doCheckSourceDocument does nothing to modify the
    // validating document... it can be removed to speed this up...
//    if (!validate1.doCheckSourceDocument()) return;
//    if (!validate1.doValidate()) return;
  }
}








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