Class CmsXmlContentDefinition

java.lang.Object
org.opencms.xml.CmsXmlContentDefinition
All Implemented Interfaces:
Cloneable

public class CmsXmlContentDefinition extends Object implements Cloneable
Describes the structure definition of an XML content object.

Since:
6.0.0
  • Field Details

  • Constructor Details

    • CmsXmlContentDefinition

      public CmsXmlContentDefinition(String innerName, String schemaLocation)
      Creates a new XML content definition.

      Parameters:
      innerName - the inner element name to use for the content definiton
      schemaLocation - the location from which the XML schema was read (system id)
    • CmsXmlContentDefinition

      public CmsXmlContentDefinition(String outerName, String innerName, String schemaLocation)
      Creates a new XML content definition.

      Parameters:
      outerName - the outer element name to use for the content definition
      innerName - the inner element name to use for the content definition
      schemaLocation - the location from which the XML schema was read (system id)
    • CmsXmlContentDefinition

      Required empty constructor for clone operation.

  • Method Details

    • getContentDefinitionForResource

      Factory method that returns the XML content definition instance for a given resource.

      Parameters:
      cms - the cms-object
      resource - the resource
      Returns:
      the XML content definition
      Throws:
      CmsException - if something goes wrong
    • getContentDefinitionForType

      Reads the content definition which is configured for a resource type.

      Parameters:
      cms - the current CMS context
      typeName - the type name
      Returns:
      the content definition
      Throws:
      CmsException - if something goes wrong
    • getContentHandlerForResource

      Returns a content handler instance for the given resource.

      Parameters:
      cms - the cms-object
      resource - the resource
      Returns:
      the content handler
      Throws:
      CmsException - if something goes wrong
    • unmarshal

      public static CmsXmlContentDefinition unmarshal(byte[] xmlData, String schemaLocation, EntityResolver resolver) throws CmsXmlException
      Factory method to unmarshal (read) a XML content definition instance from a byte array that contains XML data.

      Parameters:
      xmlData - the XML data in a byte array
      schemaLocation - the location from which the XML schema was read (system id)
      resolver - the XML entity resolver to use
      Returns:
      a XML content definition instance unmarshalled from the byte array
      Throws:
      CmsXmlException - if something goes wrong
    • unmarshal

      public static CmsXmlContentDefinition unmarshal(CmsObject cms, String resourcename) throws CmsXmlException
      Factory method to unmarshal (read) a XML content definition instance from the OpenCms VFS resource name.

      Parameters:
      cms - the current users CmsObject
      resourcename - the resource name to unmarshal the XML content definition from
      Returns:
      a XML content definition instance unmarshalled from the VFS resource
      Throws:
      CmsXmlException - if something goes wrong
    • unmarshal

      public static CmsXmlContentDefinition unmarshal(org.dom4j.Document document, String schemaLocation) throws CmsXmlException
      Factory method to unmarshal (read) a XML content definition instance from a XML document.

      This method does additional validation to ensure the document has the required XML structure for a OpenCms content definition schema.

      Parameters:
      document - the XML document to generate a XML content definition from
      schemaLocation - the location from which the XML schema was read (system id)
      Returns:
      a XML content definition instance unmarshalled from the XML document
      Throws:
      CmsXmlException - if something goes wrong
    • unmarshal

      public static CmsXmlContentDefinition unmarshal(InputSource source, String schemaLocation, EntityResolver resolver) throws CmsXmlException
      Factory method to unmarshal (read) a XML content definition instance from a XML InputSource.

      Parameters:
      source - the XML InputSource to use
      schemaLocation - the location from which the XML schema was read (system id)
      resolver - the XML entity resolver to use
      Returns:
      a XML content definition instance unmarshalled from the InputSource
      Throws:
      CmsXmlException - if something goes wrong
    • unmarshal

      Factory method to unmarshal (read) a XML content definition instance from a given XML schema location.

      The XML content definition data to unmarshal will be read from the provided schema location using an XML InputSource.

      Parameters:
      schemaLocation - the location from which to read the XML schema (system id)
      resolver - the XML entity resolver to use
      Returns:
      a XML content definition instance unmarshalled from the InputSource
      Throws:
      CmsXmlException - if something goes wrong
      SAXException - if the XML schema location could not be converted to an XML InputSource
      IOException - if the XML schema location could not be converted to an XML InputSource
    • unmarshal

      public static CmsXmlContentDefinition unmarshal(String xmlData, String schemaLocation, EntityResolver resolver) throws CmsXmlException
      Factory method to unmarshal (read) a XML content definition instance from a String that contains XML data.

      Parameters:
      xmlData - the XML data in a String
      schemaLocation - the location from which the XML schema was read (system id)
      resolver - the XML entity resolver to use
      Returns:
      a XML content definition instance unmarshalled from the byte array
      Throws:
      CmsXmlException - if something goes wrong
    • createTypeName

      protected static String createTypeName(String name)
      Creates the name of the type attribute from the given content name.

      Parameters:
      name - the name to use
      Returns:
      the name of the type attribute
    • validateAttribute

      protected static String validateAttribute(org.dom4j.Element element, String attributeName, String requiredValue) throws CmsXmlException
      Validates if a given attribute exists at the given element with an (optional) specified value.

      If the required value is not null, the attribute must have exactly this value set.

      If no value is required, some simple validation is performed on the attribute value, like a check that the value does not have leading or trailing white spaces.

      Parameters:
      element - the element to validate
      attributeName - the attribute to check for
      requiredValue - the required value of the attribute, or null if any value is allowed
      Returns:
      the value of the attribute
      Throws:
      CmsXmlException - if the element does not have the required attribute set, or if the validation fails
    • validateAttributesExists

      protected static void validateAttributesExists(org.dom4j.Element element, String[] requiredAttributes, String[] optionalAttributes) throws CmsXmlException
      Validates if a given element has exactly the required attributes set.

      Parameters:
      element - the element to validate
      requiredAttributes - the list of required attributes
      optionalAttributes - the list of optional attributes
      Throws:
      CmsXmlException - if the validation fails
    • validateComplexTypeSequence

      protected static org.opencms.xml.CmsXmlComplexTypeSequence validateComplexTypeSequence(org.dom4j.Element element, Set<CmsXmlContentDefinition> includes) throws CmsXmlException
      Validates the given element as a complex type sequence.

      Parameters:
      element - the element to validate
      includes - the XML schema includes
      Returns:
      a data structure containing the validated complex type sequence data
      Throws:
      CmsXmlException - if the validation fails
    • addDefaultXml

      public org.dom4j.Element addDefaultXml(CmsObject cms, I_CmsXmlDocument document, org.dom4j.Element root, Locale locale)
      Adds the missing default XML according to this content definition to the given document element.

      In case the root element already contains sub nodes, only missing sub nodes are added.

      Parameters:
      cms - the current users OpenCms context
      document - the document where the XML is added in (required for default XML generation)
      root - the root node to add the missing XML for
      locale - the locale to add the XML for
      Returns:
      the given root element with the missing content added
    • addInclude

      public void addInclude(CmsXmlContentDefinition nestedSchema)
      Adds a nested (included) XML content definition.

      Parameters:
      nestedSchema - the nested (included) XML content definition to add
    • addType

      public void addType(I_CmsXmlSchemaType type) throws CmsXmlException
      Adds the given content type.

      Parameters:
      type - the content type to add
      Throws:
      CmsXmlException - in case an unregistered type is added
    • clone

      public Object clone()
      Creates a clone of this XML content definition.

      Overrides:
      clone in class Object
      Returns:
      a clone of this XML content definition
    • createDefaultXml

      public org.dom4j.Element createDefaultXml(CmsObject cms, I_CmsXmlDocument document, org.dom4j.Element root, Locale locale)
      Generates the default XML content for this content definition, and append it to the given root element.

      Please note: The default values for the annotations are read from the content definition of the given document. For a nested content definitions, this means that all defaults are set in the annotations of the "outer" or "main" content definition.

      Parameters:
      cms - the current users OpenCms context
      document - the OpenCms XML document the XML is created for
      root - the node of the document where to append the generated XML to
      locale - the locale to create the default element in the document with
      Returns:
      the default XML content for this content definition, and append it to the given root element
    • createDocument

      public org.dom4j.Document createDocument(CmsObject cms, I_CmsXmlDocument document, Locale locale)
      Generates a valid XML document according to the XML schema of this content definition.

      Parameters:
      cms - the current users OpenCms context
      document - the OpenCms XML document the XML is created for
      locale - the locale to create the default element in the document with
      Returns:
      a valid XML document according to the XML schema of this content definition
    • createLocale

      public org.dom4j.Element createLocale(CmsObject cms, I_CmsXmlDocument document, org.dom4j.Element root, Locale locale)
      Generates a valid locale (language) element for the XML schema of this content definition.

      Parameters:
      cms - the current users OpenCms context
      document - the OpenCms XML document the XML is created for
      root - the root node of the document where to append the locale to
      locale - the locale to create the default element in the document with
      Returns:
      a valid XML element for the locale according to the XML schema of this content definition
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      See Also:
    • findSchemaTypesForPath

      Iterates over all schema types along a given xpath, starting from a root content definition.

      Parameters:
      path - the path
      consumer - a handler that consumes both the schema type and the remaining suffix of the path, relative to the schema type
      Returns:
      true if for all path components a schema type could be found
    • freeze

      public void freeze()
      Freezes this content definition, making all internal data structures unmodifiable.

      This is required to prevent modification of a cached content definition.

    • getChoiceMaxOccurs

      public int getChoiceMaxOccurs()
      Returns the maxOccurs value for the choice in case this is a xsd:choice content definition.

      This content definition is a xsd:choice sequence if the returned value is larger then 0.

      Returns:
      the maxOccurs value for the choice in case this is a xsd:choice content definition
    • getContentHandler

      Returns the selected XML content handler for this XML content definition.

      If no specific XML content handler was provided in the "appinfo" node of the XML schema, the default XML content handler CmsDefaultXmlContentHandler is used.

      Returns:
      the contentHandler
    • getIncludes

      Returns the set of nested (included) XML content definitions.

      Returns:
      the set of nested (included) XML content definitions
    • getInnerName

      public String getInnerName()
      Returns the inner element name of this content definition.

      Returns:
      the inner element name of this content definition
    • getOuterName

      public String getOuterName()
      Returns the outer element name of this content definition.

      Returns:
      the outer element name of this content definition
    • getSchema

      public org.dom4j.Document getSchema()
      Generates an XML schema for the content definition.

      Returns:
      the generated XML schema
    • getSchemaLocation

      Returns the location from which the XML schema was read (XML system id).

      Returns:
      the location from which the XML schema was read (XML system id)
    • getSchemaType

      public I_CmsXmlSchemaType getSchemaType(String elementPath)
      Returns the schema type for the given element name, or null if no node is defined with this name.

      Parameters:
      elementPath - the element xpath to look up the type for
      Returns:
      the type for the given element name, or null if no node is defined with this name
    • getSchemaTypes

      Returns the internal set of schema type names.

      Returns:
      the internal set of schema type names
    • getSequenceType

      Returns the sequence type of this content definition.

      Returns:
      the sequence type of this content definition
    • getTypeName

      public String getTypeName()
      Returns the main type name of this XML content definition.

      Returns:
      the main type name of this XML content definition
    • getTypeSequence

      Returns the type sequence, contains instances of I_CmsXmlSchemaType.

      Returns:
      the type sequence, contains instances of I_CmsXmlSchemaType
    • getVersion

      public int getVersion()
      Gets the version.
      Returns:
      the version number
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
      See Also:
    • setInnerName

      protected void setInnerName(String innerName)
      Sets the inner element name to use for the content definition.

      Parameters:
      innerName - the inner element name to set
    • setOuterName

      protected void setOuterName(String outerName)
      Sets the outer element name to use for the content definition.

      Parameters:
      outerName - the outer element name to set