Custom XML Content Mapping

From OpenCms Wiki
Jump to: navigation, search

Mappings that cannot be created using the standard mapping approach can be accomplished using a custom content handler.

A custom content handler is a Java class that implements the org.opencms.xml.content.I_CmsXmlContentHandler interface.

An easy approach is to extend org.opencms.xml.content.CmsDefaultXmlContentHandler and override its resolveMapping method. (It may be a good idea to use the original method as a blueprint and modify it to fit your needs.)

package com.mydomain.opencms.whatever;
import [whatever you need];
 
/**
 * Overrides resolveMappings(). Otherwise the same as CmsXmlDefaultContentHandler.
 */
public class MyContentHandler extends CmsDefaultXmlContentHandler {
 
    public MyContentHandler() {
        super();
    }
 
    /**
     * @see org.opencms.xml.content.I_CmsXmlContentHandler#resolveMapping(org.opencms.file.CmsObject, org.opencms.xml.content.CmsXmlContent, org.opencms.xml.types.I_CmsXmlContentValue)
     */
    @Override
    public void resolveMapping(CmsObject cms, CmsXmlContent content, I_CmsXmlContentValue value) throws CmsException {
        //
        // Your custom mapping code here
        //
    }
}


Connecting the content handler to the content type

When your content handler is ready, bind it to your content type by adding it in the "appinfo" node of your content’s XSD, like this:

...
<xsd:annotation>
    <xsd:appinfo>
        <handler class="com.yourdomain.opencms.YourResourceHandler" /><!-- Note: No .java postfix on the class name -->
        ...
    </xsd:appinfo>
</xsd:annotation>
...
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox