📄 cmsdefaultxmlcontenthandler.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/xml/content/CmsDefaultXmlContentHandler.java,v $
* Date : $Date: 2006/03/27 14:52:36 $
* Version: $Revision: 1.46 $
*
* 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.content;
import org.opencms.configuration.CmsConfigurationManager;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsMessages;
import org.opencms.main.CmsException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsHtmlConverter;
import org.opencms.util.CmsMacroResolver;
import org.opencms.util.CmsStringUtil;
import org.opencms.widgets.I_CmsWidget;
import org.opencms.xml.CmsXmlContentDefinition;
import org.opencms.xml.CmsXmlEntityResolver;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.CmsXmlUtils;
import org.opencms.xml.types.I_CmsXmlContentValue;
import org.opencms.xml.types.I_CmsXmlSchemaType;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* Default implementation for the XML content handler, will be used by all XML contents that do not
* provide their own handler.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.46 $
*
* @since 6.0.0
*/
public class CmsDefaultXmlContentHandler implements I_CmsXmlContentHandler {
/** Constant for the "appinfo" element name itself. */
public static final String APPINFO_APPINFO = "appinfo";
/** Constant for the "configuration" appinfo attribute name. */
public static final String APPINFO_ATTR_CONFIGURATION = "configuration";
/** Constant for the "element" appinfo attribute name. */
public static final String APPINFO_ATTR_ELEMENT = "element";
/** Constant for the "mapto" appinfo attribute name. */
public static final String APPINFO_ATTR_MAPTO = "mapto";
/** Constant for the "message" appinfo attribute name. */
public static final String APPINFO_ATTR_MESSAGE = "message";
/** Constant for the "name" appinfo attribute name. */
public static final String APPINFO_ATTR_NAME = "name";
/** Constant for the "regex" appinfo attribute name. */
public static final String APPINFO_ATTR_REGEX = "regex";
/** Constant for the "type" appinfo attribute name. */
public static final String APPINFO_ATTR_TYPE = "type";
/** Constant for the "warning" appinfo attribute value. */
public static final String APPINFO_ATTR_TYPE_WARNING = "warning";
/** Constant for the "uri" appinfo attribute name. */
public static final String APPINFO_ATTR_URI = "uri";
/** Constant for the "value" appinfo attribute name. */
public static final String APPINFO_ATTR_VALUE = "value";
/** Constant for the "widget" appinfo attribute name. */
public static final String APPINFO_ATTR_WIDGET = "widget";
/** Constant for the "default" appinfo element name. */
public static final String APPINFO_DEFAULT = "default";
/** Constant for the "defaults" appinfo element name. */
public static final String APPINFO_DEFAULTS = "defaults";
/** Constant for the "layout" appinfo element name. */
public static final String APPINFO_LAYOUT = "layout";
/** Constant for the "layouts" appinfo element name. */
public static final String APPINFO_LAYOUTS = "layouts";
/** Constant for the "mapping" appinfo element name. */
public static final String APPINFO_MAPPING = "mapping";
/** Constant for the "mappings" appinfo element name. */
public static final String APPINFO_MAPPINGS = "mappings";
/** Constant for the "preview" appinfo element name. */
public static final String APPINFO_PREVIEW = "preview";
/** Constant for the "resourcebundle" appinfo element name. */
public static final String APPINFO_RESOURCEBUNDLE = "resourcebundle";
/** Constant for the "rule" appinfo element name. */
public static final String APPINFO_RULE = "rule";
/** The file where the appinfo schema is located. */
public static final String APPINFO_SCHEMA_FILE = "org/opencms/xml/content/DefaultAppinfo.xsd";
/** The XML system is for the appinfo schema. */
public static final String APPINFO_SCHEMA_SYSTEM_ID = CmsConfigurationManager.DEFAULT_DTD_PREFIX
+ APPINFO_SCHEMA_FILE;
/** Constant for the "validationrules" appinfo element name. */
public static final String APPINFO_VALIDATIONRULES = "validationrules";
/** Macro for resolving the preview URI. */
public static final String MACRO_PREVIEW_TEMPFILE = "previewtempfile";
/** Default message for validation errors. */
protected static final String MESSAGE_VALIDATION_DEFAULT_ERROR = "${validation.path}: "
+ "${key." + Messages.GUI_EDITOR_XMLCONTENT_VALIDATION_ERROR_2 + "|${validation.value}|[${validation.regex}]}";
/** Default message for validation warnings. */
protected static final String MESSAGE_VALIDATION_DEFAULT_WARNING = "${validation.path}: "
+ "${key." + Messages.GUI_EDITOR_XMLCONTENT_VALIDATION_WARNING_2 + "|${validation.value}|[${validation.regex}]}";
/** The configuration values for the element widgets (as defined in the annotations). */
protected Map m_configurationValues;
/** The default values for the elements (as defined in the annotations). */
protected Map m_defaultValues;
/** The element mappings (as defined in the annotations). */
protected Map m_elementMappings;
/** The widgets used for the elements (as defined in the annotations). */
protected Map m_elementWidgets;
/** The resource bundle name to be used for localization of this content handler. */
protected String m_messageBundleName;
/** The preview location (as defined in the annotations). */
protected String m_previewLocation;
/** The messages for the error rules. */
protected Map m_validationErrorMessages;
/** The validation rules that cause an error (as defined in the annotations). */
protected Map m_validationErrorRules;
/** The messages for the warning rules. */
protected Map m_validationWarningMessages;
/** The validation rules that cause a warning (as defined in the annotations). */
protected Map m_validationWarningRules;
/**
* Creates a new instance of the default XML content handler.<p>
*/
public CmsDefaultXmlContentHandler() {
init();
}
/**
* Static initializer for caching the default appinfo validation schema.<p>
*/
static {
// the schema definition is located in a separate file for easier editing
byte[] appinfoSchema;
try {
appinfoSchema = CmsFileUtil.readFile(APPINFO_SCHEMA_FILE);
} catch (Exception e) {
throw new CmsRuntimeException(Messages.get().container(
org.opencms.xml.types.Messages.ERR_XMLCONTENT_LOAD_SCHEMA_1,
APPINFO_SCHEMA_FILE), e);
}
CmsXmlEntityResolver.cacheSystemId(APPINFO_SCHEMA_SYSTEM_ID, appinfoSchema);
}
/**
* @see org.opencms.xml.content.I_CmsXmlContentHandler#getConfiguration(org.opencms.xml.types.I_CmsXmlSchemaType)
*/
public String getConfiguration(I_CmsXmlSchemaType type) {
String elementName = type.getName();
return (String)m_configurationValues.get(elementName);
}
/**
* @see org.opencms.xml.content.I_CmsXmlContentHandler#getDefault(org.opencms.file.CmsObject, I_CmsXmlContentValue, java.util.Locale)
*/
public String getDefault(CmsObject cms, I_CmsXmlContentValue value, Locale locale) {
String defaultValue;
if (value.getElement() == null) {
// use the "getDefault" method of the given value, will use value from standard XML schema
defaultValue = value.getDefault(locale);
} else {
String xpath = value.getPath();
// look up the default from the configured mappings
defaultValue = (String)m_defaultValues.get(xpath);
if (defaultValue == null) {
// no value found, try default xpath
xpath = CmsXmlUtils.removeXpath(xpath);
xpath = CmsXmlUtils.createXpath(xpath, 1);
// look up the default value again with default index of 1 in all path elements
defaultValue = (String)m_defaultValues.get(xpath);
}
}
if (defaultValue != null) {
// return the default value with processed macros
CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms).setMessages(
getMessages(locale));
return resolver.resolveMacros(defaultValue);
}
// no default value is available
return null;
}
/**
* Returns the mapping defined for the given element xpath.<p>
*
* @param elementName the element xpath to look up the mapping for
* @return the mapping defined for the given element xpath
*/
public String getMapping(String elementName) {
return (String)m_elementMappings.get(elementName);
}
/**
* @see org.opencms.xml.content.I_CmsXmlContentHandler#getMessages(java.util.Locale)
*/
public CmsMessages getMessages(Locale locale) {
if (m_messageBundleName == null) {
// no message bundle was initialized
return null;
}
return new CmsMessages(m_messageBundleName, locale);
}
/**
* @see org.opencms.xml.content.I_CmsXmlContentHandler#getPreview(org.opencms.file.CmsObject, org.opencms.xml.content.CmsXmlContent, java.lang.String)
*/
public String getPreview(CmsObject cms, CmsXmlContent content, String resourcename) {
CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms);
resolver.addMacro(MACRO_PREVIEW_TEMPFILE, resourcename);
return resolver.resolveMacros(m_previewLocation);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -