Custom XML Content Validation

From OpenCms Wiki
Revision as of 15:32, 23 July 2008 by 195.226.108.254 (Talk)
Jump to: navigation, search

Alexander Kandzior, Alkacon member/founder wrote on the OpenCms Developers List (opencms-dev@opencms.org):

The default regex based validation for XML contents can be replaced by a custom validation since OpenCms 6.0. In order to do that, you need to specify your own XML content handler class in the XSD like this:

  <xsd:annotation>
    <xsd:appinfo>
       <handler class="my.own.XmlContentHandlerImplementationClassNameGoesHere" />
       ... Other nodes as required ...
     </xsd:appinfo>
  </xsd:annotation>

A handler needs to implement the org.opencms.xml.content.I_CmsXmlContentHandler interface.

In case you need only special validation, it's a good idea to extend the default content handler and override only the required method, like this:

 public class XmlContentHandlerImplementationClassNameGoesHere extends org.opencms.xml.content.CmsDefaultXmlContentHandler {
 
    public XmlContentHandlerImplementationClassNameGoesHere() {
        super();
    }
 
    public CmsXmlContentErrorHandler resolveValidation(
        CmsObject cms,
        I_CmsXmlContentValue value,
        CmsXmlContentErrorHandler errorHandler) {
 
        if (errorHandler == null) {
            // init a new error handler if required
            errorHandler = new CmsXmlContentErrorHandler();
        }
 
        if (value.getName().equals("NameOfTheXmlNodeIAmInterestedIn")) {
            if ("BadValue".equals(value.getStringValue(cms))) {
                errorHandler.addError(value, "The interesting node has a bad value!");
            }
        } else {
            errorHandler = super.resolveValidation(cms, value, errorHandler);
        }
 
        // return the result
        return errorHandler;
    }
 }
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox