cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lionel Crine <cr...@4dconcept.fr>
Subject [A SOLUTION] Validating a document with an action.
Date Thu, 25 Dec 2003 12:43:02 GMT
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();
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: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message