Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

axis and schema validation

Thread view: 
chonkme@gmail.com - 23 May 2005 11:27 GMT
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
iksrazal@terra.com.br - 23 May 2005 18:27 GMT
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
chonkme@gmail.com - 24 May 2005 05:14 GMT
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
iksrazal@terra.com.br - 24 May 2005 15:27 GMT
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/
chonkme@gmail.com - 24 May 2005 15:45 GMT
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.
iksrazal@terra.com.br - 24 May 2005 16:38 GMT
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/
chonkme@gmail.com - 25 May 2005 02:20 GMT
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?
chonkme@gmail.com - 25 May 2005 07:35 GMT
it looks like the problem was having the class that extends the default
handler in the same file as the function that calls it. i removed the
class, put it in a separate file and then imported it and it now works
fine. a bit picky eh? anyway thanks for your help along the way it's
greatly appreciated


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.