Custom XML Content Validation

From OpenCms Wiki
(Difference between revisions)
Jump to: navigation, search
 
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
[[Category:Extending OpenCms]]
 
''Alexander Kandzior'', ''Alkacon'' member/founder wrote on the ''OpenCms Developers List'' (opencms-dev@opencms.org):
 
''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:
 
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">
 
   <xsd:annotation>
 
   <xsd:annotation>
 
     <xsd:appinfo>
 
     <xsd:appinfo>
Line 10: Line 11:
 
     </xsd:appinfo>
 
     </xsd:appinfo>
 
   </xsd:annotation>
 
   </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 16: 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 45: Line 46:
 
     }
 
     }
 
  }
 
  }
</code>
+
</source>

Latest revision as of 16: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