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