axis and schema validation

C

chonkme

hi,
i have a web-service that accepts an xml string and needs to validate
it against the schema that is referenced in the incoming string. on the
client side, i use the same classes and methods and it works fine but
on the server side, with the file saved as a ".jws" file and sitting in
my "axis" directory, the same function (validateSchema) keeps throwing
a "throwable". the message provided with the throwable is simply
"Validator" which is the name of my class that extends the default
handler to schema validation. the relevant parts of the code are as
follows ...

public String thesisEndPoint(String requestMsg) {
....
schema = validateSchema(requestMsg);
.....
}
......
public static String validateSchema(String XmlDocument) {
SAXParser parser = new SAXParser();
String returnVal="";
try{
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
parser.setFeature("http://xml.org/sax/features/namespaces",true);
parser.setFeature("http://xml.org/sax/features/namespace-prefixes",true);

Validator handler=new Validator();
StringReader resultString = new StringReader(XmlDocument);

parser.setErrorHandler(handler);
parser.parse(new InputSource(resultString));

if(handler.validationError==true) {
returnVal = "0";
}
else{
returnVal = "1";
}
}
catch(java.io.IOException ioe){
return ioe.getMessage();
}
catch (SAXException e) {
return e.getMessage();
}
catch (Exception f) {
return "in f + " + f.getMessage();

}
catch(Throwable t) {
return "in throwable: " + t.getMessage();
}
return returnVal;
}

/* end of class containing the service end point functions */
........

/* class to help with validating schema */
class Validator extends DefaultHandler {
public boolean validationError = false;

public SAXParseException saxParseException=null;

public void error(SAXParseException exception) throws SAXException {
validationError=true;
saxParseException=exception;
}

public void fatalError(SAXParseException exception) throws SAXException
{
validationError = true;
saxParseException=exception;
}

public void warning(SAXParseException exception) throws SAXException {}

}

the same problem happens if i make the "Validator" class private and
static and within the class that contains the service end point
functions. again, this validating method works fine on the client side
but just not on the service side. also, xerces is correctly installed
etc and on the classpath. any helps greatly appreciated, thanks
 
I

iksrazal

Could you try and clarify your question a bit? What exactly are you
doing with this jws file - is it the webservices alternative to wsdl
that I'm thinking of? Where is the schema being referenced, are you
using entityResolver? Classpath, for a webservice? Should be in the
servlet container ... place your xerces jar in WEB-INF/lib . Or use
JAXP.

iksrazal
 
C

chonkme

the jws file is an alternative to the wsdl file (it means i can just
drop the jws file in the servlet container and not go throught the
deploying the web-service). the schema is on my computer and there are
no problems with the reference i think as schemas on the client side
are referenced in the same way, its located at
"http://127.0.0.1/~user_name/schemas_html/the_schema.xsd". the function
is returning a throwable not an exception so i assume the problem isn't
schema related. xerces is in that directory and i only added it to the
classpath to test if that may be the problem. what is the entity
resolver method you talk about? also i dont think the problem lies with
xerces, maybe something to do with axis, so i'm not too sure if jaxp
would fix the problem
 
I

iksrazal

Sure about the way you responded, ie, "http://127.0.0.1" ? Perhaps you
purposely changed the ip - I do too- but you should clarify that.

Also, you don't even need jws if you don't want - I prefer to skip both
jws and wsdl.

With entity resolver, the schema can reside on the local machine. This
might be a good test for you:

//Constants when using XML Schema for SAX parsing.
static final String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";

Fwlog.debug(this, Fwlog.WI, "starting schema validation ...");
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
DocumentBuilder db = dbf.newDocumentBuilder();
//Add a custom error handler
MyDefaultHandler dh = new MyDefaultHandler();
db.setErrorHandler(dh);
db.setEntityResolver(new SchemaLoader());
// assumming var xml is already recieved as a String
InputSource isXml = new InputSource (new StringReader(xml));
Document doc_in = db.parse(isXml);
if (false == dh.isSchemaValidated)
{
throw new java.lang.IllegalStateException("ERR: Invalid XML
received - schema validation failed");
}

//Custom error handler to print errors and return isValid
class MyDefaultHandler extends DefaultHandler
{
//flag to check if the xml document was valid
public boolean isSchemaValidated = true;

//Receive notification of a recoverable error.
public void error(SAXParseException se)
{
setValidity(se);
}

//Receive notification of a non-recoverable error.
public void fatalError(SAXParseException se)
{
setValidity(se);
}

//Receive notification of a warning.
public void warning(SAXParseException se)
{
setValidity(se);
}

private void setValidity(SAXParseException se)
{
isSchemaValidated = false;
Fwlog.error(this, Fwlog.DB, se);
}
}

package com.infoseg.mr.atualiza.controller;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import gov.sefaz.fw.fwlog.Fwlog;

public class SchemaLoader implements EntityResolver
{
/Schema file needs to be at the root of classpath - WEB-INF/classes
private static final String TAG = "file:///at.xsd";
private static final String XSD = "at.xsd";

public InputSource resolveEntity(String publicId, String systemId)
throws IOException, SAXException
{
Fwlog.debug(this, Fwlog.WI, "resolveEntity started...");
if (systemId.equals(TAG))
{
Fwlog.debug(this, Fwlog.WI, "sysid equal tag: " + XSD);
Fwlog.debug(this, Fwlog.WI, "publicId: " + publicId);
//file 'XSD' must be at the root of the classpath - in a servlet
//container that would be WEB-INF/classes
//InputStream is = getClass().getResourceAsStream(XSD);
InputStream is =
getClass().getClassLoader().getResourceAsStream(XSD);

//InputStream is = this.getClass().getResourceAsStream(XSD);
if (null == is)
{
throw new IOException("Cannot load xsd file for schema
validation: " + XSD);
}
Fwlog.debug(this, Fwlog.WI, "Loaded schema: " + XSD);
InputSource inputsource = new InputSource(is);
if (null == inputsource)
{
throw new IOException("Cannot create InputSource from
InputStream");
}

Fwlog.debug(this, Fwlog.WI, "BELEZA!!! Returning InputSource");
return inputsource;
}
else
{
Fwlog.error(this, Fwlog.WI, "Cannot load schema: " + XSD);
Fwlog.error(this, Fwlog.WI, "publicId: " + publicId);
Fwlog.error(this, Fwlog.WI, "Cannot match: systemId: " +systemId+
", with: " + TAG);
return null;
}
}
}

HTH,
iksrazal
http://www.braziloutsource.com/
 
C

chonkme

hi, thanks for the reply, do i just dump that code into a file in my
axis directory to replace the old file and send it an xml string from a
client to see if it validates? the "http://127.0.0.1" is just the
localhost and as i am using macosx i can access stuff in my "Sites"
directory on that ip address.
 
I

iksrazal

Try replacing the schema validation code with what I gave you.
MyDefaultHandler could be an inner class, it is as I gave it to you, in
the same class that does the validation. Put SchemaLoader in a seperate
file.

Put some print statements or logging in your code - like the first line
- to see what is happening.

You might also try JAXP if you get stuck, my imports are:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.*;

HTH,
iksrazal
http://www.braziloutsource.com/
 
C

chonkme

hi,
i tried to the new default handler class and get a throwable again
with just the name of the class - > "MyDefaultHandler". the line of
code that throws the throwable is
MyDefaultHandler dh = new MyDefaultHandler();

is there something in axis that says you can't have classes in classes
or anything like that because with the SAX implementation it would also
throw when trying to create a new instance of the class that implements
the default handler?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top