Custom XML Content Validation

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
m (category added)
 
(One intermediate revision by one user not shown)
Line 4: Line 4:
 
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:
 
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:
  
<code>
+
<source lang="java">
   &lt;xsd:annotation&gt;
+
   <xsd:annotation>
     &lt;xsd:appinfo&gt;
+
     <xsd:appinfo>
       &lt;handler class="my.own.XmlContentHandlerImplementationClassNameGoesHere" /&gt;
+
       <handler class="my.own.XmlContentHandlerImplementationClassNameGoesHere" />
 
       ... Other nodes as required ...
 
       ... Other nodes as required ...
     &lt;/xsd:appinfo&gt;
+
     </xsd:appinfo>
   &lt;/xsd:annotation&gt;
+
   </xsd:annotation>
</code>
+
</source>
  
 
A handler needs to implement the org.opencms.xml.content.I_CmsXmlContentHandler interface.
 
A handler needs to implement the org.opencms.xml.content.I_CmsXmlContentHandler interface.
Line 17: Line 17:
 
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:
 
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:
  
<code>
+
<source lang="java">
 
  public class XmlContentHandlerImplementationClassNameGoesHere extends org.opencms.xml.content.CmsDefaultXmlContentHandler {
 
  public class XmlContentHandlerImplementationClassNameGoesHere extends org.opencms.xml.content.CmsDefaultXmlContentHandler {
 
   
 
   
Line 46: Line 46:
 
     }
 
     }
 
  }
 
  }
</code>
+
</source>

Latest revision as of 15:33, 23 July 2008

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