📄 cmsxmlcontentdefinition.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/CmsXmlContentDefinition.java,v $
* Date : $Date: 2006/07/19 12:38:16 $
* Version: $Revision: 1.37 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.xml;
import org.opencms.file.CmsObject;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsStringUtil;
import org.opencms.xml.content.CmsDefaultXmlContentHandler;
import org.opencms.xml.content.I_CmsXmlContentHandler;
import org.opencms.xml.types.CmsXmlLocaleValue;
import org.opencms.xml.types.CmsXmlNestedContentDefinition;
import org.opencms.xml.types.I_CmsXmlContentValue;
import org.opencms.xml.types.I_CmsXmlSchemaType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Describes the structure definition of an XML content object.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.37 $
*
* @since 6.0.0
*/
public class CmsXmlContentDefinition implements Cloneable {
/**
* Simple data structure to describe a type seqnence in an XML schema.<p>
*/
private final class CmsXmlComplexTypeSequence {
/** Indicates if this type sequence has a language attribute. */
protected boolean m_hasLanguageAttribute;
/** The name of the complex type seqnence. */
protected String m_name;
/** The type sequence elements. */
protected List m_sequence;
/**
* Creates a new complex type sequence data structure.<p>
*
* @param name the name of the sequence
* @param sequence the type sequence element list
* @param hasLanguageAttribute indicates if a "language" attribute is present
*/
protected CmsXmlComplexTypeSequence(String name, List sequence, boolean hasLanguageAttribute) {
m_name = name;
m_sequence = sequence;
m_hasLanguageAttribute = hasLanguageAttribute;
}
}
/** Constant for the XML schema attribute "mapto". */
public static final String XSD_ATTRIBUTE_DEFAULT = "default";
/** Constant for the XML schema attribute "elementFormDefault". */
public static final String XSD_ATTRIBUTE_ELEMENT_FORM_DEFAULT = "elementFormDefault";
/** Constant for the XML schema attribute "maxOccurs". */
public static final String XSD_ATTRIBUTE_MAX_OCCURS = "maxOccurs";
/** Constant for the XML schema attribute "minOccurs". */
public static final String XSD_ATTRIBUTE_MIN_OCCURS = "minOccurs";
/** Constant for the XML schema attribute "name". */
public static final String XSD_ATTRIBUTE_NAME = "name";
/** Constant for the XML schema attribute "schemaLocation". */
public static final String XSD_ATTRIBUTE_SCHEMA_LOCATION = "schemaLocation";
/** Constant for the XML schema attribute "type". */
public static final String XSD_ATTRIBUTE_TYPE = "type";
/** Constant for the XML schema attribute "use". */
public static final String XSD_ATTRIBUTE_USE = "use";
/** Constant for the XML schema attribute value "language". */
public static final String XSD_ATTRIBUTE_VALUE_LANGUAGE = "language";
/** Constant for the XML schema attribute value "optional". */
public static final String XSD_ATTRIBUTE_VALUE_OPTIONAL = "optional";
/** Constant for the XML schema attribute value "qualified". */
public static final String XSD_ATTRIBUTE_VALUE_QUALIFIED = "qualified";
/** Constant for the XML schema attribute value "required". */
public static final String XSD_ATTRIBUTE_VALUE_REQUIRED = "required";
/** Constant for the XML schema attribute value "unbounded". */
public static final String XSD_ATTRIBUTE_VALUE_UNBOUNDED = "unbounded";
/** Constant for the XML schema attribute value "0". */
public static final String XSD_ATTRIBUTE_VALUE_ZERO = "0";
/** The opencms default type definition include. */
public static final String XSD_INCLUDE_OPENCMS = CmsXmlEntityResolver.OPENCMS_SCHEME + "opencms-xmlcontent.xsd";
/** The schema definition namespace. */
public static final Namespace XSD_NAMESPACE = Namespace.get("xsd", "http://www.w3.org/2001/XMLSchema");
/** Constant for the "annotation" node in the XML schema namespace. */
public static final QName XSD_NODE_ANNOTATION = QName.get("annotation", XSD_NAMESPACE);
/** Constant for the "appinfo" node in the XML schema namespace. */
public static final QName XSD_NODE_APPINFO = QName.get("appinfo", XSD_NAMESPACE);
/** Constant for the "attribute" node in the XML schema namespace. */
public static final QName XSD_NODE_ATTRIBUTE = QName.get("attribute", XSD_NAMESPACE);
/** Constant for the "complexType" node in the XML schema namespace. */
public static final QName XSD_NODE_COMPLEXTYPE = QName.get("complexType", XSD_NAMESPACE);
/** Constant for the "element" node in the XML schema namespace. */
public static final QName XSD_NODE_ELEMENT = QName.get("element", XSD_NAMESPACE);
/** Constant for the "include" node in the XML schema namespace. */
public static final QName XSD_NODE_INCLUDE = QName.get("include", XSD_NAMESPACE);
/** Constant for the "schema" node in the XML schema namespace. */
public static final QName XSD_NODE_SCHEMA = QName.get("schema", XSD_NAMESPACE);
/** Constant for the "sequence" node in the XML schema namespace. */
public static final QName XSD_NODE_SEQUENCE = QName.get("sequence", XSD_NAMESPACE);
/** The XML content handler. */
private I_CmsXmlContentHandler m_contentHandler;
/** The set of included additional XML content definitions. */
private Set m_includes;
/** The inner element name of the content definition (type sequence). */
private String m_innerName;
/** The outer element name of the content definition (languange sequence). */
private String m_outerName;
/** The location from which the XML schema was read (XML system id). */
private String m_schemaLocation;
/** The main type name of this XML content definition. */
private String m_typeName;
/** The Map of configured types. */
private Map m_types;
/** The type sequence. */
private List m_typeSequence;
/**
* Creates a new XML content definition.<p>
*
* @param innerName the inner element name to use for the content definiton
* @param schemaLocation the location from which the XML schema was read (system id)
*/
public CmsXmlContentDefinition(String innerName, String schemaLocation) {
this(innerName + "s", innerName, schemaLocation);
}
/**
* Creates a new XML content definition.<p>
*
* @param outerName the outer element name to use for the content definiton
* @param innerName the inner element name to use for the content definiton
* @param schemaLocation the location from which the XML schema was read (system id)
*/
public CmsXmlContentDefinition(String outerName, String innerName, String schemaLocation) {
m_outerName = outerName;
m_innerName = innerName;
setInnerName(innerName);
m_typeSequence = new ArrayList();
m_types = new HashMap();
m_includes = new HashSet();
m_schemaLocation = schemaLocation;
m_contentHandler = new CmsDefaultXmlContentHandler();
}
/**
* Required empty constructor for clone operation.<p>
*/
protected CmsXmlContentDefinition() {
// noop, required for clone operation
}
/**
* Factory method to unmarshal (read) a XML content definition instance from a byte array
* that contains XML data.<p>
*
* @param xmlData the XML data in a byte array
* @param schemaLocation the location from which the XML schema was read (system id)
* @param resolver the XML entitiy resolver to use
*
* @return a XML content definition instance unmarshalled from the byte array
*
* @throws CmsXmlException if something goes wrong
*/
public static CmsXmlContentDefinition unmarshal(byte[] xmlData, String schemaLocation, EntityResolver resolver)
throws CmsXmlException {
CmsXmlContentDefinition result = getCachedContentDefinition(schemaLocation, resolver);
if (result == null) {
// content definition was not found in the cache, unmarshal the XML document
result = unmarshalInternal(CmsXmlUtils.unmarshalHelper(xmlData, resolver), schemaLocation, resolver);
}
return result;
}
/**
* Factory method to unmarshal (read) a XML content definition instance from the OpenCms VFS resource name.<p>
*
* @param cms the current users CmsObject
* @param resourcename the resource name to unmarshal the XML content definition from
*
* @return a XML content definition instance unmarshalled from the VFS resource
*
* @throws CmsXmlException if something goes wrong
*/
public static CmsXmlContentDefinition unmarshal(CmsObject cms, String resourcename) throws CmsXmlException {
CmsXmlEntityResolver resolver = new CmsXmlEntityResolver(cms);
String schemaLocation = CmsXmlEntityResolver.OPENCMS_SCHEME.concat(resourcename.substring(1));
CmsXmlContentDefinition result = getCachedContentDefinition(schemaLocation, resolver);
if (result == null) {
// content definition was not found in the cache, unmarshal the XML document
InputSource source = resolver.resolveEntity(null, schemaLocation);
result = unmarshalInternal(CmsXmlUtils.unmarshalHelper(source, resolver), schemaLocation, resolver);
}
return result;
}
/**
* Factory method to unmarshal (read) a XML content definition instance from a XML document.<p>
*
* This method does additional validation to ensure the document has the required
* XML structure for a OpenCms content definition schema.<p>
*
* @param document the XML document to generate a XML content definition from
* @param schemaLocation the location from which the XML schema was read (system id)
*
* @return a XML content definition instance unmarshalled from the XML document
*
* @throws CmsXmlException if something goes wrong
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -