You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Michael Glavassevich <mr...@ca.ibm.com> on 2007/03/01 19:50:30 UTC
Re: Parse without schema location
Hi Pau,
There are many ways you can do this.
For example...
You could use the standard JAXP methods [1][2]. There are Xerces specific
properties [3][4] you can set. You could supply a grammar pool [5].
[1] http://xerces.apache.org/xerces2-j/faq-pcfp.html#faq-4
[2] http://xerces.apache.org/xerces2-j/samples-jaxp.html#SourceValidator
[3]
http://xerces.apache.org/xerces2-j/properties.html#schema.external-schemaLocation
[4]
http://xerces.apache.org/xerces2-j/properties.html#schema.external-noNamespaceSchemaLocation
[5] http://xerces.apache.org/xerces2-j/faq-grammars.html
Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org
"pau carre" <pa...@gmail.com> wrote on 02/26/2007 05:24:06 AM:
> Hi, I am developing a simple generic (wsdl/schema-independent) SOAP
> message validatior class that uses Xerces.
> The problem I have is that I use an string based schema (not an URI
> nor a file) because I want to use WSDL's schema. The validation only
> works when an schema location is specified in the XML instance:
>
> <fi:fish
> xmlns:fi="http://globalip:8080/fish"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://globalip:8080/fish
> http://192.168.82.29:8080/fisherWS/fish.xsd">
>
> But I would like it to work without schema location:
> <fi:fish
> xmlns:fi="http://globalip:8080/fish">
>
> I have been reading the Xerces doc (properties and features) and I
> coded the config in such a way:
> private String VALIDATION = "http://xml.org/sax/features/validation";
> private String SCHEMA =
"http://apache.org/xml/features/validation/schema";
> private String SCHEMA_FULL_CHECKING =
> "http://apache.org/xml/features/validation/schema-full-checking";
> private String WARN_ON_DUPLICATE_ATTDEF =
> "http://apache.org/xml/features/validation/warn-on-duplicate-attdef";
>
> parser.setFeature(LOAD_EXTERNAL_DTD, true);
> parser.setFeature(VALIDATION, true);
> parser.setFeature(SCHEMA, true);
> parser.setFeature(SCHEMA_FULL_CHECKING, true);
> parser.setFeature(WARN_ON_DUPLICATE_ATTDEF, true);
>
> Thanks in advance,
> Pau
>
> The full source code is:
>
> import org.w3c.dom.*;
> import java.io.FileNotFoundException;
> import java.net.UnknownHostException;
> import org.w3c.dom.Element;
> import org.xml.sax.SAXParseException;
> import org.xml.sax.SAXException;
> import org.xml.sax.ErrorHandler;
> import org.xml.sax.helpers.XMLReaderFactory;
> import org.xml.sax.InputSource;
> import java.io.StringReader;
> import javax.xml.soap.SOAPBody;
> import org.xml.sax.XMLReader;
> import javax.xml.soap.SOAPEnvelope;
> import org.apache.axis.MessageContext;
> import org.apache.catalina.util.SchemaResolver;
> import org.xml.sax.helpers.DefaultHandler;
> import java.io.IOException;
> import org.apache.tomcat.util.digester.Digester;
>
> public class SOAPvalidator {
> private String VALIDATION = "http://xml.org/sax/features/validation";
>
> private String SCHEMA =
"http://apache.org/xml/features/validation/schema";
>
> /**
> * String constant used for the schema full checking feature of the
Xerces
> * parser.
> *
> * @build 10
> */
> private String SCHEMA_FULL_CHECKING =
> "http://apache.org/xml/features/validation/schema-full-checking";
>
> /**
> * String constant used for the warn on duplicate attribute
definition
> * feature of the Xerces parser.
> *
> * @build 10
> */
> private String WARN_ON_DUPLICATE_ATTDEF =
> "http://apache.org/xml/features/validation/warn-on-duplicate-attdef";
>
> /**
> * String constant used for the warn on undeclared element definition
> * feature of the Xerces parser.
> *
> * @build 10
> */
> private String LOAD_EXTERNAL_DTD =
> "http://apache.org/xml/features/nonvalidating/load-external-dtd";
>
> // Name of the parser to use to create the xmlReader.
> String parserName = "org.apache.xerces.parsers.SAXParser";
>
> XMLReader parser = null;
>
> DefaultHandler defaultHandler = null;
>
> public SOAPvalidator() {
> try {
> parser = XMLReaderFactory.createXMLReader(parserName);
> // load schema\dtd
> parser.setFeature(LOAD_EXTERNAL_DTD, true);
> // turn on validation
> parser.setFeature(VALIDATION, true);
> parser.setFeature(SCHEMA, true);
> parser.setFeature(SCHEMA_FULL_CHECKING, true);
> parser.setFeature(WARN_ON_DUPLICATE_ATTDEF, true);
> defaultHandler = new SAXErrorHandler();
> } catch (Exception e) {
>
> }
> }
>
> public void validateSOAPmessage() {
> try {
> MessageContext context = org.apache.axis.MessageContext
> .getCurrentContext();
> SOAPEnvelope env =
context.getRequestMessage().getSOAPEnvelope();
> SOAPBody sb = env.getBody();
> System.out.println("SB fc: " + sb.getLastChild());
> String XMLinstance = sb.getLastChild().toString();
> SAXErrorHandler errorHandler = new SAXErrorHandler();
> validateFile(new InputSource(new
> StringReader(XMLinstance)), errorHandler);
> } catch (Exception e) {
>
> }
> }
>
> public void validateFile(InputSource source, DefaultHandler handler)
> throws SAXException, IOException {
> Digester digester = new Digester(parser);
>
> // make schema resolver so the parser knows where to find the
schema
> SchemaResolver resolver = new SchemaResolver(digester);
>
> parser.setEntityResolver(resolver);
> parser.setErrorHandler(handler);
>
> // parsing the file with validation turned on no content handler
needed
> parser.parse(source);
>
> }
>
> }
>
> /**
> * This class is used to handle any errors that occur durring parsing.
> *
> * @build 10
> */
> class SAXErrorHandler extends DefaultHandler {
> /**
> * Called by parser when an error is encountered while parsing
> the XML file.
> *
> * @build 10
> * @param ex
> * is th exception that describes the error
> */
> public void error(SAXParseException ex) {
> System.out.println("SOAP message error: " + ex.getMessage());
> // parseError.append("\n");
> // parseError.append(ex.getMessage());
> // errorCount++;
> }
>
> /**
> * Called by parser when an fatalError is encountered while parsing
the XML
> * file.
> *
> * @build 10
> * @param ex
> * is th exception that describes the error
> */
> public void fatalError(SAXParseException ex) {
> System.out.println("SOAP message fatal error: " +
ex.getMessage());
>
> // parseError.append("\n");
> // parseError.append(ex.getMessage());
> // errorCount++;
> }
>
> /**
> * Called by parser when an warning is encountered while parsing the
XML
> * file.
> *
> * @build 10
> * @param ex
> * is th exception that describes the error
> */
> public void warning(SAXParseException ex) {
> System.out.println("SOAP message warning: " + ex.getMessage());
>
> // parseError.append("\n");
> // parseError.append(ex.getMessage());
> // errorCount++;
> }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org