I wrote an action which can validate an xml document according a DTD.
Here an example :
The ACTION : In the act method you can write :
StrDocument = source; --> representing the * of the sitemap.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder();
This line should be before the try.
dbf.setValidating(true); .
ErrorHandler handler = (ErrorHandler) new ValidationException();
db.setErrorHandler(handler);
db.parse(new InputSource(new ByteArrayInputStream(strDocument.getBytes("UTF-8"))));
return EMPTY_MAP;
}
catch (ParserConfigurationException pce) {
Debugger.println("ValidatingDTD.endElement : Validation Exception");
pce.printStackTrace();
return null;
}
catch (SAXParseException spe) { //This Exception is thrown from the ValidationException class. Debugger.println("ValidatingDTD.endElement : Validation Exception"); spe.printStackTrace(); return null; }
the SITEMAP :
<!--*** Finish the declaration ***--> <map:match pattern="validate/*.xml"> <map:act type="validateModif" src="{1}.xml"> <map:parameter name="url_dtd" value="modification.dtd" /> --> be sure the DTD will be find. <map:generate type="GetDocument" src="{../1}.xml" /> <map:serialize type="xml" /> </map:act> <map:redirect-to uri="cocoon:/validationFailed"/> </map:match>
The ValidationException class
it should implements ErrorHandler and you should set three methods :
warning, error, fatalError. Here an example of one :
/**
* @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
*/
public void fatalError(SAXParseException exception) throws SAXException {
Debugger.println("SAXParseException : Fatal Error");
Debugger.println("Fatal Error: " + exception.getMessage());
Debugger.println(" at line " + exception.getLineNumber() + ", column " + exception.getColumnNumber());
Debugger.println(" in entity " + exception.getSystemId());
exception.printStackTrace();
throw exception;
Hope that help..
Lionel
Lionel CRINE Ing�nieur Syst�mes documentaires Soci�t� : 4DConcept 22 rue Etienne de Jouy 78353 JOUY EN JOSAS Tel : 01.34.58.70.70 Fax : 01.39.58.70.70
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Lionel CRINE Ing�nieur Syst�mes documentaires Soci�t� : 4DConcept 22 rue Etienne de Jouy 78353 JOUY EN JOSAS Tel : 01.34.58.70.70 Fax : 01.39.58.70.70
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
