Custom XML Content Validation
(Difference between revisions)
m (category added) |
|||
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: | ||
− | < | + | <source lang="java"> |
<xsd:annotation> | <xsd:annotation> | ||
<xsd:appinfo> | <xsd:appinfo> | ||
Line 11: | Line 11: | ||
</xsd:appinfo> | </xsd:appinfo> | ||
</xsd:annotation> | </xsd:annotation> | ||
− | </ | + | </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: | ||
− | < | + | <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: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
Revision as of 15:32, 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; } }