📄 cmsxmlcontentdefinition.java
字号:
* @param root the node of the document where to append the generated XML to
* @param locale the locale to create the default element in the document with
*
* @return the default XML content for this content definition, and append it to the given root element
*/
public Element createDefaultXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) {
Iterator i = m_typeSequence.iterator();
while (i.hasNext()) {
I_CmsXmlSchemaType type = (I_CmsXmlSchemaType)i.next();
for (int j = 0; j < type.getMinOccurs(); j++) {
Element typeElement = type.generateXml(cms, document, root, locale);
// need to check for default value again because the of appinfo "mappings" node
I_CmsXmlContentValue value = type.createValue(document, typeElement, locale);
String defaultValue = document.getContentDefinition().getContentHandler().getDefault(cms, value, locale);
if (defaultValue != null) {
// only if there is a default value available use it to overwrite the initial default
value.setStringValue(cms, defaultValue);
}
}
}
return root;
}
/**
* Generates a valid XML document according to the XML schema of this content definition.<p>
*
* @param cms the current users OpenCms context
* @param document the OpenCms XML document the XML is created for
* @param locale the locale to create the default element in the document with
*
* @return a valid XML document according to the XML schema of this content definition
*/
public Document createDocument(CmsObject cms, I_CmsXmlDocument document, Locale locale) {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement(getOuterName());
root.add(I_CmsXmlSchemaType.XSI_NAMESPACE);
root.addAttribute(I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION, getSchemaLocation());
createLocale(cms, document, root, locale);
return doc;
}
/**
* Generates a valid locale (language) element for the XML schema of this content definition.<p>
*
* @param cms the current users OpenCms context
* @param document the OpenCms XML document the XML is created for
* @param root the root node of the document where to append the locale to
* @param locale the locale to create the default element in the document with
*
* @return a valid XML element for the locale according to the XML schema of this content definition
*/
public Element createLocale(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) {
// add an element with a "locale" attribute to the given root node
Element element = root.addElement(getInnerName());
element.addAttribute(XSD_ATTRIBUTE_VALUE_LANGUAGE, locale.toString());
// now generate the default XML for the element
return createDefaultXml(cms, document, element, locale);
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof CmsXmlContentDefinition)) {
return false;
}
CmsXmlContentDefinition other = (CmsXmlContentDefinition)obj;
if (!getInnerName().equals(other.getInnerName())) {
return false;
}
if (!getOuterName().equals(other.getOuterName())) {
return false;
}
return m_typeSequence.equals(other.m_typeSequence);
}
/**
* Freezes this content definition, making all internal data structures
* unmodifiable.<p>
*
* This is required to prevent modification of a cached content definition.<p>
*/
public void freeze() {
m_types = Collections.unmodifiableMap(m_types);
m_typeSequence = Collections.unmodifiableList(m_typeSequence);
}
/**
* Returns the selected XML content handler for this XML content definition.<p>
*
* If no specific XML content handler was provided in the "appinfo" node of the
* XML schema, the default XML content handler <code>{@link CmsDefaultXmlContentHandler}</code> is used.<p>
*
* @return the contentHandler
*/
public I_CmsXmlContentHandler getContentHandler() {
return m_contentHandler;
}
/**
* Returns the set of nested (included) XML content definitions.<p>
*
* @return the set of nested (included) XML content definitions
*/
public Set getIncludes() {
return m_includes;
}
/**
* Returns the inner element name of this content definiton.<p>
*
* @return the inner element name of this content definiton
*/
public String getInnerName() {
return m_innerName;
}
/**
* Returns the outer element name of this content definiton.<p>
*
* @return the outer element name of this content definiton
*/
public String getOuterName() {
return m_outerName;
}
/**
* Generates an XML schema for the content definition.<p>
*
* @return the generated XML schema
*/
public Document getSchema() {
Document schema = DocumentHelper.createDocument();
Element root = schema.addElement(XSD_NODE_SCHEMA);
root.addAttribute(XSD_ATTRIBUTE_ELEMENT_FORM_DEFAULT, XSD_ATTRIBUTE_VALUE_QUALIFIED);
Element include = root.addElement(XSD_NODE_INCLUDE);
include.addAttribute(XSD_ATTRIBUTE_SCHEMA_LOCATION, XSD_INCLUDE_OPENCMS);
if (m_includes.size() > 0) {
Iterator i = m_includes.iterator();
while (i.hasNext()) {
CmsXmlContentDefinition definition = (CmsXmlContentDefinition)i.next();
root.addElement(XSD_NODE_INCLUDE).addAttribute(
XSD_ATTRIBUTE_SCHEMA_LOCATION,
definition.m_schemaLocation);
}
}
String outerTypeName = createTypeName(getOuterName());
String innerTypeName = createTypeName(getInnerName());
Element content = root.addElement(XSD_NODE_ELEMENT);
content.addAttribute(XSD_ATTRIBUTE_NAME, getOuterName());
content.addAttribute(XSD_ATTRIBUTE_TYPE, outerTypeName);
Element list = root.addElement(XSD_NODE_COMPLEXTYPE);
list.addAttribute(XSD_ATTRIBUTE_NAME, outerTypeName);
Element listSequence = list.addElement(XSD_NODE_SEQUENCE);
Element listElement = listSequence.addElement(XSD_NODE_ELEMENT);
listElement.addAttribute(XSD_ATTRIBUTE_NAME, getInnerName());
listElement.addAttribute(XSD_ATTRIBUTE_TYPE, innerTypeName);
listElement.addAttribute(XSD_ATTRIBUTE_MIN_OCCURS, XSD_ATTRIBUTE_VALUE_ZERO);
listElement.addAttribute(XSD_ATTRIBUTE_MAX_OCCURS, XSD_ATTRIBUTE_VALUE_UNBOUNDED);
Element main = root.addElement(XSD_NODE_COMPLEXTYPE);
main.addAttribute(XSD_ATTRIBUTE_NAME, innerTypeName);
Element mainSequence = main.addElement(XSD_NODE_SEQUENCE);
Iterator i = m_typeSequence.iterator();
while (i.hasNext()) {
I_CmsXmlSchemaType schemaType = (I_CmsXmlSchemaType)i.next();
schemaType.appendXmlSchema(mainSequence);
}
Element language = main.addElement(XSD_NODE_ATTRIBUTE);
language.addAttribute(XSD_ATTRIBUTE_NAME, XSD_ATTRIBUTE_VALUE_LANGUAGE);
language.addAttribute(XSD_ATTRIBUTE_TYPE, CmsXmlLocaleValue.TYPE_NAME);
language.addAttribute(XSD_ATTRIBUTE_USE, XSD_ATTRIBUTE_VALUE_REQUIRED);
return schema;
}
/**
* Returns the location from which the XML schema was read (XML system id).<p>
*
* @return the location from which the XML schema was read (XML system id)
*/
public String getSchemaLocation() {
return m_schemaLocation;
}
/**
* Returns the scheme type for the given element name, or <code>null</code> if no
* node is defined with this name.<p>
*
* @param elementPath the element path to look up the type for
* @return the type for the given element name, or <code>null</code> if no
* node is defined with this name
*/
public I_CmsXmlSchemaType getSchemaType(String elementPath) {
String path = CmsXmlUtils.getFirstXpathElement(elementPath);
I_CmsXmlSchemaType type = (I_CmsXmlSchemaType)m_types.get(path);
if (type == null) {
// no node with the given path defined in schema
return null;
}
// check if recursion is required to get value from a nested schema
if (type.isSimpleType() || !CmsXmlUtils.isDeepXpath(elementPath)) {
// no recusion required
return type;
}
// recusion required since the path is an Xpath
CmsXmlNestedContentDefinition nestedDefinition = (CmsXmlNestedContentDefinition)type;
path = CmsXmlUtils.removeFirstXpathElement(elementPath);
return nestedDefinition.getNestedContentDefinition().getSchemaType(path);
}
/**
* Returns the main type name of this XML content definition.<p>
*
* @return the main type name of this XML content definition
*/
public String getTypeName() {
return m_typeName;
}
/**
* Returns the type sequence.<p>
*
* @return the type sequence
*/
public List getTypeSequence() {
return m_typeSequence;
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return getInnerName().hashCode();
}
/**
* Sets the inner element name to use for the content definiton.<p>
*
* @param innerName the inner element name to set
*/
protected void setInnerName(String innerName) {
m_innerName = innerName;
if (m_innerName != null) {
m_typeName = createTypeName(innerName);
}
}
/**
* Sets the outer element name to use for the content definiton.<p>
*
* @param outerName the outer element name to set
*/
protected void setOuterName(String outerName) {
m_outerName = outerName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -