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 "Shakya Wijerama (JIRA)" <xe...@xml.apache.org> on 2012/08/19 18:14:38 UTC

[jira] [Commented] (XERCESJ-1130) Cannot validate against multiple XML schemas within the same namespace

    [ https://issues.apache.org/jira/browse/XERCESJ-1130?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13437527#comment-13437527 ] 

Shakya Wijerama commented on XERCESJ-1130:
------------------------------------------

Hi,

Hereby I attach the source code changes and the classes added during the project.
Still there are some modifications to do in the code such exception handling, etc.

This is the list of files changed:

1. XMLSchemaFactory.java

    private String getNameSpace(XMLInputSource is) - added this method
    private XMLInputSource generateDummySchema(XMLInputSource[] xmlInputSources) - added this method
    public Schema newSchema( Source[] schemas ) throws SAXException - modified this method

2. SAXContentHandler (newly added class file)

3. MyEntityResolver (newly added class file - the class name should be refactored)

I will update with the final code soon.

Thanks.
                
> Cannot validate against multiple XML schemas within the same namespace
> ----------------------------------------------------------------------
>
>                 Key: XERCESJ-1130
>                 URL: https://issues.apache.org/jira/browse/XERCESJ-1130
>             Project: Xerces2-J
>          Issue Type: Bug
>          Components: XML Schema 1.0 Structures
>    Affects Versions: 2.7.1
>         Environment: All platforms
>            Reporter: Sunitha
>            Assignee: Shakya Wijerama
>              Labels: gsoc, gsoc2012, mentor
>         Attachments: MyEntityResolver.java, SAXContentHandler.java, XMLSchemaFactory.java
>
>
> The javax.xml.validation.SchemaFactory.newSchema method can take an array of schema sources and construct a composite schema. Unfortunately, this does not work if the schema are in the same namespace. When the schema are in the same namespace, only the first schema is used.
> The bug is caused by the computation of the hashcode for a grammar in the grammar cache. The hashcode is computed based only on the namespace of a grammar (i.e. schema file). Thus if multiple schemas are specified from the same namespace, they will all hash to the same code and only the first will be cached and returned for all subsequent caching attempts.
> If each schema is in a separate namespace, the newSchema method works properly. Effectively, the assumption that was made in the code was that there will only be a single schema per namespace. This is an incorrect assumption.
> One of the more major implications of this bug is that the XML Schema "include" element can never work since by definition, included schemas must be in the same namespace. Because of this bug, inclusion can only be done using the "import" element since it supports different namespaces.
> STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
> 1. Create a sample XML file called test.xml
> 2. Create a schema for the test file but split it between two schema files: schema1.xsd and schema2.xsd
> 3. Create a java class that uses SAX (DOM shows the same problem) to parse the file and extends defaultHandler.
> 4. Call the SchemaFactory.newSchema method with a source array containing schema1.xsd and schema2.xsd.
> 5. Set the resulting schema on the parser
> 6. Implement an error method in the class
> 7. Run the program and observe that there is a validation failure. This is becuase the second schema in the array was never processed and so the validator does not accept the test XML file.
> The source code, test XML file, and schemas are included in this bug report.
> EXPECTED VERSUS ACTUAL BEHAVIOR :
> EXPECTED -
> The test XML file should have validated properly against the composite schema.
> ACTUAL -
> There was a validation failure. This is becuase the second schema in the array was never processed and so the validator does not accept the test XML file.
> ERROR MESSAGES/STACK TRACES THAT OCCUR :
> SAX Error: http://www.w3.org/TR/xml-schema-1#cvc-elt.1?root
> REPRODUCIBILITY :
> This bug can be reproduced always.
> ---------- BEGIN SOURCE ----------
> ------------- schemaTest.java ----------------------------
> import org.xml.sax.Attributes;
> import org.xml.sax.SAXException;
> import org.xml.sax.SAXParseException;
> import org.xml.sax.helpers.DefaultHandler;
> public class SchemaTest extends DefaultHandler {
> public static void main(String[] args)
> {
>     try {
>         FileInputStream is = new FileInputStream("test.xml");
>         
>         StreamSource[] sources = new StreamSource[2];
>         FileInputStream ss = new FileInputStream("schema2.xsd");
>         sources[0] = new StreamSource(ss);
>         ss = new FileInputStream("schema1.xsd");
>         sources[1] = new StreamSource(ss);
>         
>         SchemaFactory schemaFactory =
>             SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
>         Schema schema = schemaFactory.newSchema(sources);
>         
>         SAXParserFactory saxFactory = SAXParserFactory.newInstance();
>         saxFactory.setNamespaceAware(true);
>         saxFactory.setValidating(false);
>         saxFactory.setXIncludeAware(true);
>         saxFactory.setSchema(schema);
>         
>         SAXParser parser = saxFactory.newSAXParser();
>         parser.parse(is, new SchemaTest());
>     }
>     catch (FileNotFoundException e) {
>         e.printStackTrace();
>     }
>     catch (SAXException e) {
>         e.printStackTrace();
>     }
>     catch (ParserConfigurationException e) {
>         e.printStackTrace();
>     }
>     catch (IOException e) {
>         e.printStackTrace();
>     }
> }
> /**
>  * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
>  */
> public void warning(SAXParseException e) throws SAXException
> {
>     System.err.println("SAX Warning: " + e.getLocalizedMessage());
> }
> /**
>  * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
>  */
> public void error(SAXParseException e) throws SAXException
> {
>     System.err.println("SAX Error: " + e.getLocalizedMessage());
> }
> /**
>  * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
>  */
> public void fatalError(SAXParseException e) throws SAXException
> {
>     System.err.println("SAX Fatal Error: " + e.getLocalizedMessage());
> }
> /**
>  * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
>  */
> public void startElement(String uri, String localName, String qName,
>         Attributes attributes) throws SAXException
> {
>     System.out.println("Start element: " + localName);
> }
> /**
>  * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
>  */
> public void endElement(String uri, String localName, String qName)
>         throws SAXException
> {
>     System.out.println("End element: " + localName);
> }
> } // End of class SchemaTest
> -------------------- test.xml -------------------------
> <?xml version="1.0"?>
> <root>
>     <elem1 value="abc"/>
> </root>
> ---------------- schema1.xsd ------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema elementFormDefault="qualified" xml:lang="EN" xmlns:xs="http://www.w3.org/2001/XMLSchema">
>     
>     <xs:include schemaLocation="schema2.xsd"/>
> 	
>     <xs:element name="root">
>         <xs:complexType>
>             <xs:all>
>                 <xs:element ref="elem1" minOccurs="0"/>
>             </xs:all>
>         </xs:complexType>
>     </xs:element>
>     <xs:element name="elem1">
>         <xs:complexType>
>             <xs:attribute name="value" type="ValueType" use="required"/>
>         </xs:complexType>
>     </xs:element>
> </xs:schema>
> --------------------- schema2.xsd ------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <xs:schema elementFormDefault="qualified" xml:lang="EN" xmlns:xs="http://www.w3.org/2001/XMLSchema">
>     <xs:simpleType name="ValueType">
>       <xs:restriction base="xs:string">
>         <xs:enumeration value="abc"/>
>         <xs:enumeration value="def"/>
>       </xs:restriction>
>     </xs:simpleType>
> </xs:schema>
> ---------- END SOURCE ----------

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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