[XOM-interest] Schema validation using XOM

Marks, Chris chris.marks at landg.com
Fri Apr 25 04:48:57 EDT 2008


Hi,

I've used XOM successfully using a test Websphere workspace (using JRE
5) to schema validate and build a document using XOM. 

However, I'm having trouble doing this using a Websphere workspace using
the default runtime environment.

Using JRE5 the code works fine :

		SchemaFactory sf =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		System.out.println("retrieved SF : " + sf.getClass());
		Schema s = sf.newSchema(ss);
		System.out.println("Schema type : " + s.getClass());
	
		SAXParserFactory spf = SAXParserFactory.newInstance();
		System.out.println("SPF : " + spf.getClass());
		
		spf.setValidating(true);
		spf.setNamespaceAware(true);
		spf.setSchema(s);
	
		SAXParser sp = spf.newSAXParser();
		System.out.println("SAXParser : " + sp.getClass());
		XMLReader reader = sp.getXMLReader();
		System.out.println("XML Reader : " + reader.getClass());
	
		reader.setErrorHandler(new XOMErrorHandler());
		// Overridden by XOM Builder true/false flag
	
reader.setFeature("http://xml.org/sax/features/validation",true);
		
	
reader.setFeature("http://apache.org/xml/features/validation/dynamic",tr
ue);
	
reader.setFeature("http://apache.org/xml/features/validation/schema",tru
e);
		
		// True by default
	
reader.setFeature("http://xml.org/sax/features/namespaces",true);
			
		is =
XOMtester.class.getResourceAsStream("ExampleProduct.xml");
		
		Builder b = new Builder(reader, true);
		Document d = b.build(is);

And gives the following output :

		retrieved SF : class
com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl
		Schema type : class
com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaImpl
		SPF : class org.apache.xerces.jaxp.SAXParserFactoryImpl
		SAXParser : class org.apache.xerces.jaxp.SAXParserImpl
		XML Reader : class
org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser
		Validity Exception : cvc-enumeration-valid: Value
'UglyFemale' is not facet-valid with respect to enumeration '[Male,
Female, Unknown]'. It 		must be a value from the enumeration.

However, when I'm running under a different runtime environment the XOM
builder does not seem to perform schema validation. I've noticed that
the SchemaFactory that is returned is different from the test workspace,
and I cannot seem to perform schema validation. 

This is the code :

		Log.debug(this, "Validating flag is......" +
validateAgainstSchema);
		SAXParserFactory saxParserFactory =
SAXParserFactory.newInstance();
		Log.debug(this, "retrieved SPF : " +
saxParserFactory.getClass());
		saxParserFactory.setNamespaceAware(true);
		
		if (validateAgainstSchema == true) {
		
			StreamSource[] ss = new StreamSource[2];
				
			is1 =
this.getClass().getResourceAsStream("/Qnbprotectionquoterequestmcontent.
xsd");
			if (is1 == null) {
				Log.debug(this, "Cannot locate schema
definition file");
				throw new IOException("Unable to read
schema definition file for schema validation");
			}
			else {
				Log.debug(this, "Found schema definition
file");
				ss[1] = new StreamSource(is1);
			}

			is2 =
this.getClass().getResourceAsStream("/Origodatatypelibrary.xsd");
			if (is2 == null) {
				Log.debug(this, "Cannot locate origo
type definition file");
				throw new IOException("Unable to read
origo type definition file for schema validation");
			}
			else {
				Log.debug(this, "Found origo type
definition file");
				ss[0] = new StreamSource(is2);
			}	
		
			Log.debug(this, "system property : " +
System.getProperty("javax.xml.validation.SchemaFactory:http://www.w3.org
/2001/XMLSchema"));
			SchemaFactory schemaFactory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
			Log.debug(this, "retrieved SF : " +
schemaFactory.getClass());
			
			try {
				schemaFactory.setErrorHandler(new
XOMErrorHandler());
				Log.debug(this, "Setting schema
object(s)");
				schema = schemaFactory.newSchema(ss);
				Log.debug(this, "Schema type : " +
schema.getClass());
			}
			catch (SAXException se) {
				Log.debug(this, "SE1 " +
se.getMessage());
			}
			
			saxParserFactory.setValidating(true);
			
			Log.debug(this, "Setting schema in SPF");
			saxParserFactory.setSchema(schema);
		
		//	is1.close();
		//	is2.close();
			
		}
		else {
			saxParserFactory.setValidating(false);
		}
	
		SAXParser saxParser = saxParserFactory.newSAXParser();
		Log.debug(this, "SAXParser : " + saxParser.getClass());
		XMLReader xmlReader = saxParser.getXMLReader();
		Log.debug(this, "XML reader : " + xmlReader.getClass());
			
		xmlReader.setErrorHandler(new XOMErrorHandler());
					// Overridden by XOM Builder
true/false flag
	
xmlReader.setFeature("http://xml.org/sax/features/validation",true);
					// These 2 are false by default.
Altering schema validation seems to make no difference
	
xmlReader.setFeature("http://apache.org/xml/features/validation/dynamic"
,true);
	
xmlReader.setFeature("http://apache.org/xml/features/validation/schema",
true);
	
xmlReader.setFeature("http://apache.org/xml/features/validation/schema-f
ull-checking",true);
		
		// True by default
	
xmlReader.setFeature("http://xml.org/sax/features/namespaces",true);
		
		BufferedReader bufferedReader = new BufferedReader(new
StringReader(origoRequestXML));
		Builder b = new Builder(xmlReader,
validateAgainstSchema);
		doc = b.build(bufferedReader);

Which gives the output :


Validating flag is......true
2008-04-25 09:24:16,516, com.landg.common.logging.Log4JAdapter DEBUG
retrieved SPF : class org.apache.xerces.jaxp.SAXParserFactoryImpl
2008-04-25 09:24:16,531, com.landg.common.logging.Log4JAdapter DEBUG
Found schema definition file
2008-04-25 09:24:16,547, com.landg.common.logging.Log4JAdapter DEBUG
Found origo type definition file
2008-04-25 09:24:16,547, com.landg.common.logging.Log4JAdapter DEBUG
system property : null
2008-04-25 09:24:16,562, com.landg.common.logging.Log4JAdapter DEBUG
retrieved SF : class org.apache.xerces.jaxp.validation.XMLSchemaFactory
2008-04-25 09:24:16,578, com.landg.common.logging.Log4JAdapter DEBUG
Setting schema object(s)
2008-04-25 09:24:16,750, com.landg.common.logging.Log4JAdapter DEBUG
Schema type : class org.apache.xerces.jaxp.validation.SimpleXMLSchema
2008-04-25 09:24:16,750, com.landg.common.logging.Log4JAdapter DEBUG
Setting schema in SPF
2008-04-25 09:24:16,750, com.landg.common.logging.Log4JAdapter DEBUG
SAXParser : class org.apache.xerces.jaxp.SAXParserImpl
2008-04-25 09:24:16,750, com.landg.common.logging.Log4JAdapter DEBUG XML
reader : class org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser
2008-04-25 09:24:16,828, com.landg.common.logging.Log4JAdapter DEBUG
Type : Element. Data : <m_content> which has 7 children and a parent of
[nu.xom.Document: m_content]
2008-04-25 09:24:16,828, com.landg.common.logging.Log4JAdapter DEBUG
Type : Element. Data : <b_control> which has 13 children and a parent of
[nu.xom.Element: m_content]

I've had to define the schema object slightly differently to my test
workspace, by passing in an array of streamsource objects instead of
just one (which contains an xsd:include) since this is the only way I
can construct a schema object that doesn't give some kind of SAX
Exception. 

Apart from the SchemaFactory implementation being used to construct the
schema object being different
(org.apache.xerces.jaxp.validation.XMLSchemaFactory instead of class
com.sun.org.apache.xerces.internal.jaxp.validation.xs.SchemaFactoryImpl)
there is no significant difference in the code. 

Could you please tell me if you think this is likely to be a problem
with the schema object not having been contructed properly, or if you
think that the problem is likely to be something to do with an xmlReader
property (or similar) that isn't being set properly? I have registered
an error handler to the xmlReader but no Log.debug's are being output so
it doesn't appear to be called. 

Many thanks for any assistance you can give.

Chris Marks.

**********************************************************************
This email (and any attachments) may contain privileged and/or confidential information. If you are not the intended recipient please do not disclose, copy, distribute, disseminate or take any action in reliance on it. If you have received this message in error please reply and tell us and then delete it. Should you wish to communicate with us by email we cannot guarantee the security of any data outside our own computer systems. For the protection of Legal & General's systems and staff, incoming emails will be automatically scanned. Any information contained in this message may be subject to applicable terms and conditions and must not be construed as giving investment advice within or outside the United Kingdom. 

Legal & General Group plc is registered in England under company number 1417162 and is a holding company.

The registered office for all companies in the Legal & General group is One Coleman Street London EC2R 5AA.

The following subsidiary companies of Legal & General Group Plc are authorised and regulated by the Financial Services Authority: Legal & General Partnership Services Limited, Legal & General Insurance Limited, Legal & General Assurance Society Limited, Legal & General (Unit Trust Managers) Limited and Legal & General (Portfolio Management Services) Limited.

Legal & General International (Ireland) is incorporated in Ireland under company number 440141 with its registered office at Alexandra House, The Sweepstakes, Ballsbridge, Dublin 4 and is authorised by the Financial Regulator in Ireland and by the Financial Services Authority for the conduct of insurance business in the UK. 

Full details can be found at http://www.legalandgeneralgroup.com/ 

**********************************************************************



More information about the XOM-interest mailing list