📄 cmsdefaultxmlcontenthandler.java
字号:
Messages.ERR_XMLCONTENT_CONFIG_ELEM_UNKNOWN_1,
elementName));
}
m_configurationValues.put(elementName, configurationValue);
}
/**
* Adds a default value for an element.<p>
*
* @param contentDefinition the XML content definition this XML content handler belongs to
* @param elementName the element name to map
* @param defaultValue the default value to use
*
* @throws CmsXmlException in case an unknown element name is used
*/
protected void addDefault(CmsXmlContentDefinition contentDefinition, String elementName, String defaultValue)
throws CmsXmlException {
if (contentDefinition.getSchemaType(elementName) == null) {
throw new CmsXmlException(org.opencms.xml.types.Messages.get().container(
org.opencms.xml.types.Messages.ERR_XMLCONTENT_INVALID_ELEM_DEFAULT_1,
elementName));
}
// store mappings as Xpath to allow better control about what is mapped
String xpath = CmsXmlUtils.createXpath(elementName, 1);
m_defaultValues.put(xpath, defaultValue);
}
/**
* Adds an element mapping.<p>
*
* @param contentDefinition the XML content definition this XML content handler belongs to
* @param elementName the element name to map
* @param mapping the mapping to use
*
* @throws CmsXmlException in case an unknown element name is used
*/
protected void addMapping(CmsXmlContentDefinition contentDefinition, String elementName, String mapping)
throws CmsXmlException {
if (contentDefinition.getSchemaType(elementName) == null) {
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_INVALID_ELEM_MAPPING_1,
elementName));
}
// store mappings as Xpath to allow better control about what is mapped
String xpath = CmsXmlUtils.createXpath(elementName, 1);
m_elementMappings.put(xpath, mapping);
}
/**
* Adds a validation rule for a specified element.<p>
*
* @param contentDefinition the XML content definition this XML content handler belongs to
* @param elementName the element name to add the rule to
* @param regex the validation rule regular expression
* @param message the message in case validation fails (may be null)
* @param isWarning if true, this rule is used for warnings, otherwise it's an error
*
* @throws CmsXmlException in case an unknown element name is used
*/
protected void addValidationRule(
CmsXmlContentDefinition contentDefinition,
String elementName,
String regex,
String message,
boolean isWarning) throws CmsXmlException {
if (contentDefinition.getSchemaType(elementName) == null) {
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_INVALID_ELEM_VALIDATION_1,
elementName));
}
if (isWarning) {
m_validationWarningRules.put(elementName, regex);
if (message != null) {
m_validationWarningMessages.put(elementName, message);
}
} else {
m_validationErrorRules.put(elementName, regex);
if (message != null) {
m_validationErrorMessages.put(elementName, message);
}
}
}
/**
* Adds a GUI widget for a specified element.<p>
*
* @param contentDefinition the XML content definition this XML content handler belongs to
* @param elementName the element name to map
* @param widgetClassOrAlias the widget to use as GUI for the element (registered alias or class name)
*
* @throws CmsXmlException in case an unknown element name is used
*/
protected void addWidget(CmsXmlContentDefinition contentDefinition, String elementName, String widgetClassOrAlias)
throws CmsXmlException {
if (contentDefinition.getSchemaType(elementName) == null) {
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_INVALID_ELEM_LAYOUTWIDGET_1,
elementName));
}
// get the base widget from the XML content type manager
I_CmsWidget widget = OpenCms.getXmlContentTypeManager().getWidget(widgetClassOrAlias);
if (widget == null) {
// no registered widget class found
if (CmsStringUtil.isValidJavaClassName(widgetClassOrAlias)) {
// java class name given, try to create new instance of the class and cast to widget
try {
Class specialWidgetClass = Class.forName(widgetClassOrAlias);
widget = (I_CmsWidget)specialWidgetClass.newInstance();
} catch (Exception e) {
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_INVALID_CUSTOM_CLASS_3,
widgetClassOrAlias,
elementName,
contentDefinition.getSchemaLocation()), e);
}
}
if (widget == null) {
// no valid widget found
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_INVALID_WIDGET_3,
widgetClassOrAlias,
elementName,
contentDefinition.getSchemaLocation()));
}
}
m_elementWidgets.put(elementName, widget);
}
/**
* Returns the validation message to be displayed if a certain rule was violated.<p>
*
* @param cms the current users OpenCms context
* @param value the value to validate
* @param regex the rule that was vialoted
* @param valueStr the string value of the given value
* @param matchResult if false, the rule was negated
* @param isWarning if true, this validation indicate a warning, otherwise an error
*
* @return the validation message to be displayed
*/
protected String getValidationMessage(
CmsObject cms,
I_CmsXmlContentValue value,
String regex,
String valueStr,
boolean matchResult,
boolean isWarning) {
String message = null;
if (isWarning) {
message = (String)m_validationWarningMessages.get(value.getName());
} else {
message = (String)m_validationErrorMessages.get(value.getName());
}
if (message == null) {
if (isWarning) {
message = MESSAGE_VALIDATION_DEFAULT_WARNING;
} else {
message = MESSAGE_VALIDATION_DEFAULT_ERROR;
}
}
// create additional macro values
Map additionalValues = new HashMap();
additionalValues.put(CmsMacroResolver.KEY_VALIDATION_VALUE, valueStr);
additionalValues.put(CmsMacroResolver.KEY_VALIDATION_REGEX, ((!matchResult) ? "!" : "") + regex);
additionalValues.put(CmsMacroResolver.KEY_VALIDATION_PATH, value.getPath());
CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms).setMessages(
getMessages(cms.getRequestContext().getLocale())).setAdditionalMacros(additionalValues);
return resolver.resolveMacros(message);
}
/**
* Called when this content handler is initialized.<p>
*/
protected void init() {
m_elementMappings = new HashMap();
m_elementWidgets = new HashMap();
m_validationErrorRules = new HashMap();
m_validationErrorMessages = new HashMap();
m_validationWarningRules = new HashMap();
m_validationWarningMessages = new HashMap();
m_defaultValues = new HashMap();
m_configurationValues = new HashMap();
m_previewLocation = null;
}
/**
* Initializes the default values for this content handler.<p>
*
* Using the default values from the appinfo node, it's possible to have more
* sophisticated logic for generating the defaults then just using the XML schema "default"
* attribute.<p>
*
* @param root the "defaults" element from the appinfo node of the XML content definition
* @param contentDefinition the content definition the default values belong to
* @throws CmsXmlException if something goes wrong
*/
protected void initDefaultValues(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException {
Iterator i = root.elementIterator(APPINFO_DEFAULT);
while (i.hasNext()) {
// iterate all "default" elements in the "defaults" node
Element element = (Element)i.next();
String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT);
String defaultValue = element.attributeValue(APPINFO_ATTR_VALUE);
if ((elementName != null) && (defaultValue != null)) {
// add a default value mapping for the element
addDefault(contentDefinition, elementName, defaultValue);
}
}
}
/**
* Initializes the layout for this content handler.<p>
*
* Unless otherwise instructed, the editor uses one specific GUI widget for each
* XML value schema type. For example, for a {@link org.opencms.xml.types.CmsXmlStringValue}
* the default widget is the {@link org.opencms.widgets.CmsInputWidget}.
* However, certain values can also use more then one widget, for example you may
* also use a {@link org.opencms.widgets.CmsCheckboxWidget} for a String value,
* and as a result the Strings possible values would be eithe <code>"false"</code> or <code>"true"</code>,
* but nevertheless be a String.<p>
*
* The widget to use can further be controlled using the <code>widget</code> attribute.
* You can specifiy either a valid widget alias such as <code>StringWidget</code>,
* or the name of a Java class that implements <code>{@link I_CmsWidget}</code>.<p>
*
* Configuration options to the widget can be passed using the <code>configuration</code>
* attribute. You can specify any String as configuration. This String is then passed
* to the widget during initialization. It's up to the individual widget implementation
* to interpret this configuration String.<p>
*
* @param root the "layouts" element from the appinfo node of the XML content definition
* @param contentDefinition the content definition the layout belongs to
*
* @throws CmsXmlException if something goes wrong
*/
protected void initLayouts(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException {
Iterator i = root.elementIterator(APPINFO_LAYOUT);
while (i.hasNext()) {
// iterate all "layout" elements in the "layouts" node
Element element = (Element)i.next();
String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT);
String widgetClassOrAlias = element.attributeValue(APPINFO_ATTR_WIDGET);
String configuration = element.attributeValue(APPINFO_ATTR_CONFIGURATION);
if ((elementName != null) && (widgetClassOrAlias != null)) {
// add a widget mapping for the element
addWidget(contentDefinition, elementName, widgetClassOrAlias);
if (configuration != null) {
addConfiguration(contentDefinition, elementName, configuration);
}
}
}
}
/**
* Initializes the element mappings for this content handler.<p>
*
* Element mappings allow storing values from the XML content in other locations.
* For example, if you have an elemenet called "Title", it's likley a good idea to
* store the value of this element also in the "Title" property of a XML content resource.<p>
*
* @param root the "mappings" element from the appinfo node of the XML content definition
* @param contentDefinition the content definition the mappings belong to
* @throws CmsXmlException if something goes wrong
*/
protected void initMappings(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException {
Iterator i = root.elementIterator(APPINFO_MAPPING);
while (i.hasNext()) {
// iterate all "mapping" elements in the "mappings" node
Element element = (Element)i.next();
// this is a mapping node
String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT);
String maptoName = element.attributeValue(APPINFO_ATTR_MAPTO);
if ((elementName != null) && (maptoName != null)) {
// add the element mapping
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -