Hi,
I load an XML file with Apache digester and I have a strange behaviour
with XML schema validation. I have enabled XML schema validation and
provided a XML input file with a syntax error in it => The syntax error is
well detected by Apache Digester:
########################################################################
2554 [main] ERROR org.apache.commons.digester.Digester - Parse Error at
line 5 column 36: cvc-enumeration-valid: Value 'GLOBA' is not facet-valid
with respect to enumeration '[GLOBAL, PERL5]'.
org.xml.sax.SAXParseException: cvc-enumeration-valid: Value 'GLOBA' is not
facet-valid with respect to enumeration '[GLOBAL, PERL5]'
at
org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown
Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
########################################################################
But no exception SAXParseException is raised as I thought it would be. I
was expecting to catch a SAXParseException and actually it doesn't happen.
The stack trace seems to confirm to a SAXParseException should be raised
but it not the case.
I have enabled XML schema validation with the following lines:
digester.setFeature("http://xml.org/sax/features/validation", true);
digester.setFeature("http://apache.org/xml/features/validation/schema",true);
digester.setFeature("http://xml.org/sax/features/namespaces", true);
The XML parser is Xerces-j 2.4.0 with JAXP 1.2.3
Here's the source code:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.apache.commons.digester.Digester;
import org.xml.sax.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.xml.parsers.ParserConfigurationException;
public class XMLLogInputSource implements LogInputSource {
private File xmlfile;
private List loginputs = new ArrayList();
private Stack LogInputsStack=new Stack();
protected final Log logger = LogFactory.getLog(getClass());
public XMLLogInputSource(File file) {
xmlfile = file;
}
public LogInput[] getLogInputs() throws LogInputSourceException {
if (xmlfile == null) {
logger.error("Can not process a null xml file");
throw new LogInputSourceException("Error during xml input
source
processing");
}
feedLogInputs();
return (LogInput[]) loginputs.toArray(new LogInput[0]);
}
private void feedLogInputs() throws LogInputSourceException {
try {
logger.info("Beginning processing of the xml input file");
Digester digester = new Digester();
// We load the DTD by ourself, cause the DTD is included in
our jar
EntityResolver xmlentityresolver = new XMLEntityResolver();
digester.setEntityResolver(xmlentityresolver);
// Set DTD validation on
digester.setValidating(true);
// turn on XML schema validation
digester.setFeature("http://xml.org/sax/features/validation",
true);
digester.setFeature("http://apache.org/xml/features/validation/schema",true);
digester.setFeature("http://xml.org/sax/features/namespaces",
true);
digester.push(this);
digester.addCallMethod("loginputs/dir", "setLogInputDir", 1);
digester.addCallParam("loginputs/dir", 0, "name");
digester.addCallMethod("loginputs/dir/logpattern",
"addLogInput", 3);
digester.addCallParam("loginputs/dir/logpattern", 0, "num");
digester.addCallParam("loginputs/dir/logpattern", 1, "type");
digester.addCallParam("loginputs/dir/logpattern", 2);
digester.parse(xmlfile);
}
catch (SAXParseException saxex) {
logger.error(
"Sax parsing error processing file " +
xmlfile.getName());
throw new LogInputSourceException("Error during xml input
source processing");
}
catch(ParserConfigurationException pce){
logger.error("Xerces parser not properly configured
"+pce.toString());
throw new LogInputSourceException("Error during xml input
source processing");
} catch (IOException ioe) {
logger.error("IO error processing file " + xmlfile.getName());
throw new LogInputSourceException("Error during xml input
source processing");
} catch (SAXNotSupportedException e) {
e.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
} catch (SAXNotRecognizedException e) {
e.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
} catch (SAXException e) {
e.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
}
}
// Method called by Apache digester
// Must be public
public void setLogInputDir(String dirname){
// The XML file is constructed in a way that the directory name
// is shared by log inputs. This method is called by digester after
// the addLogInput method
while (! LogInputsStack.empty()) {
LogInput lgin=(LogInput)LogInputsStack.pop();
if ( dirname !=null ) {
lgin.setDir(dirname);
loginputs.add(lgin);
}
}
}
// Method called by Apache digester
// Must be public
public void addLogInput(String number,String type,String pattern) {
// Create a stack of log inputs. The directory name is missing
// so it can not be added to the loginputs array list now
LogInput lgin = new LogInput();
LogPattern lgp=null;
try {
if (type !=null && type.equalsIgnoreCase("perl5")) {
lgp=new LogPattern(pattern,LogPattern.PERL5TYPE);
}
else {
lgp=new LogPattern(pattern);
}
} catch (MalformedLogPatternException e) {
logger.error("Malformed pattern: "+pattern);
return;
}
lgin.setHistnum(Integer.parseInt(number));
lgin.setPattern(lgp);
this.LogInputsStack.push(lgin);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]