📄 cmsdefaultxmlcontenthandler.java
字号:
addMapping(contentDefinition, elementName, maptoName);
}
}
}
/**
* Initializes the preview location for this content handler.<p>
*
* @param root the "preview" element from the appinfo node of the XML content definition
* @param contentDefinition the content definition the validation rules belong to
* @throws CmsXmlException if something goes wrong
*/
protected void initPreview(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException {
String preview = root.attributeValue(APPINFO_ATTR_URI);
if (preview == null) {
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_MISSING_PREVIEW_URI_2,
root.getName(),
contentDefinition.getSchemaLocation()));
}
m_previewLocation = preview;
}
/**
* Initializes the resource bundle to use for localized messages in this content handler.<p>
*
* @param root the "resourcebundle" element from the appinfo node of the XML content definition
* @param contentDefinition the content definition the validation rules belong to
*
* @throws CmsXmlException if something goes wrong
*/
protected void initResourceBundle(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException {
String name = root.attributeValue(APPINFO_ATTR_NAME);
if (name == null) {
throw new CmsXmlException(Messages.get().container(
Messages.ERR_XMLCONTENT_MISSING_RESOURCE_BUNDLE_NAME_2,
root.getName(),
contentDefinition.getSchemaLocation()));
}
m_messageBundleName = name;
}
/**
* Initializes the validation rules this content handler.<p>
*
* OpenCms always performs XML schema validation for all XML contents. However,
* for most projects in the real world a more fine-grained control over the validation process is
* required. For these cases, individual validation rules can be defined for the appinfo node.<p>
*
* @param root the "validationrules" element from the appinfo node of the XML content definition
* @param contentDefinition the content definition the validation rules belong to
* @throws CmsXmlException if something goes wrong
*/
protected void initValidationRules(Element root, CmsXmlContentDefinition contentDefinition) throws CmsXmlException {
Iterator i = root.elementIterator(APPINFO_RULE);
while (i.hasNext()) {
// iterate all "layout" elements in the "layouts" node
Element element = (Element)i.next();
String elementName = element.attributeValue(APPINFO_ATTR_ELEMENT);
String regex = element.attributeValue(APPINFO_ATTR_REGEX);
String type = element.attributeValue(APPINFO_ATTR_TYPE);
String message = element.attributeValue(APPINFO_ATTR_MESSAGE);
if ((elementName != null) && (regex != null)) {
// add a validation ruls for the element
addValidationRule(
contentDefinition,
elementName,
regex,
message,
APPINFO_ATTR_TYPE_WARNING.equals(type));
}
}
}
/**
* Returns the localized resource string for a given message key according to the configured resource bundle
* of this content handler.<p>
*
* If the key was not found in the configuredd bundle, or no bundle is configured for this
* content handler, the return value is
* <code>"??? " + keyName + " ???"</code>.<p>
*
* @param keyName the key for the desired string
* @param locale the locale to get the key from
*
* @return the resource string for the given key
*
* @see CmsMessages#formatUnknownKey(String)
* @see CmsMessages#isUnknownKey(String)
*/
protected String key(String keyName, Locale locale) {
CmsMessages messages = getMessages(locale);
if (messages != null) {
return messages.key(keyName);
}
return CmsMessages.formatUnknownKey(keyName);
}
/**
* Removes property values on resources for non-existing, optional elements.<p>
*
* @param cms the current users OpenCms context
* @param content the XML content to remove the property values for
*
* @throws CmsException in case of read/write errors accessing the OpenCms VFS
*/
protected void removeEmptyMappings(CmsObject cms, CmsXmlContent content) throws CmsException {
Iterator mappings = m_elementMappings.keySet().iterator();
String rootPath = null;
List siblings = null;
while (mappings.hasNext()) {
String path = (String)mappings.next();
String mapping = (String)m_elementMappings.get(path);
if (mapping.startsWith(MAPTO_PROPERTY_LIST) || mapping.startsWith(MAPTO_PROPERTY)) {
// get root path of the file
if (rootPath == null) {
rootPath = content.getFile().getRootPath();
}
try {
// try / catch to ensure site root is always restored
cms.getRequestContext().saveSiteRoot();
cms.getRequestContext().setSiteRoot("/");
// read all siblings of the file
if (siblings == null) {
siblings = cms.readSiblings(rootPath, CmsResourceFilter.IGNORE_EXPIRATION);
}
for (int i = 0; i < siblings.size(); i++) {
// get sibline filename and locale
String filename = ((CmsResource)siblings.get(i)).getRootPath();
Locale locale = OpenCms.getLocaleManager().getDefaultLocale(cms, filename);
if (!content.hasLocale(locale)) {
// only remove property if the locale fits
continue;
}
if (content.hasValue(path, locale)) {
// value is available, property must be kept
continue;
}
String property;
if (mapping.startsWith(MAPTO_PROPERTY_LIST)) {
// this is a property list mapping
property = mapping.substring(MAPTO_PROPERTY_LIST.length());
} else {
// this is a property mapping
property = mapping.substring(MAPTO_PROPERTY.length());
}
// delete the property value for the not existing node
cms.writePropertyObject(filename, new CmsProperty(property, CmsProperty.DELETE_VALUE, null));
}
} finally {
// restore the saved site root
cms.getRequestContext().restoreSiteRoot();
}
}
}
}
/**
* Validates if the given <code>appinfo</code> element node from the XML content definition schema
* is valid according the the capabilities of this content handler.<p>
*
* @param appinfoElement the <code>appinfo</code> element node to validate
*
* @throws CmsXmlException in case the element validation fails
*/
protected void validateAppinfoElement(Element appinfoElement) throws CmsXmlException {
// create a document to validate
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement(APPINFO_APPINFO);
// attach the default appinfo schema
root.add(I_CmsXmlSchemaType.XSI_NAMESPACE);
root.addAttribute(I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION, APPINFO_SCHEMA_SYSTEM_ID);
// append the content from the appinfo node in the content definition
root.appendContent(appinfoElement);
// now validate the document with the default appinfo schema
CmsXmlUtils.validateXmlStructure(doc, CmsEncoder.ENCODING_UTF_8, new CmsXmlEntityResolver(null));
}
/**
* Validates the given rules against the given value.<p>
*
* @param cms the current users OpenCms context
* @param value the value to validate
* @param errorHandler the error handler to use in case errors or warnings are detected
* @param rules the rules to validate the value against
* @param isWarning if true, this validation should be stored as a warning, otherwise as an error
*
* @return the updated error handler
*/
protected CmsXmlContentErrorHandler validateValue(
CmsObject cms,
I_CmsXmlContentValue value,
CmsXmlContentErrorHandler errorHandler,
Map rules,
boolean isWarning) {
String valueStr;
try {
valueStr = value.getStringValue(cms);
} catch (Exception e) {
// if the value can not be accessed it's useless to continue
errorHandler.addError(value, e.getMessage());
return errorHandler;
}
String regex = (String)rules.get(value.getName());
if (regex == null) {
// no customized rule, check default XML schema validation rules
return validateValue(cms, value, valueStr, errorHandler, isWarning);
}
boolean matchResult = true;
if (regex.charAt(0) == '!') {
// negate the pattern
matchResult = false;
regex = regex.substring(1);
}
String matchValue = valueStr;
if (matchValue == null) {
// set match value to empty String to avoid exceptions in pattern matcher
matchValue = "";
}
// use the custom validation pattern
if (matchResult != Pattern.matches(regex, matchValue)) {
// generate the message
String message = getValidationMessage(cms, value, regex, valueStr, matchResult, isWarning);
if (isWarning) {
errorHandler.addWarning(value, message);
} else {
errorHandler.addError(value, message);
// if an error was found, the default XML schema validation is not applied
return errorHandler;
}
}
// no error found, check default XML schema validation rules
return validateValue(cms, value, valueStr, errorHandler, isWarning);
}
/**
* Checks the default XML schema vaildation rules.<p>
*
* These rules should only be tested if this is not a test for warnings.<p>
*
* @param cms the current users OpenCms context
* @param value the value to validate
* @param valueStr the string value of the given value
* @param errorHandler the error handler to use in case errors or warnings are detected
* @param isWarning if true, this validation should be stored as a warning, otherwise as an error
*
* @return the updated error handler
*/
protected CmsXmlContentErrorHandler validateValue(
CmsObject cms,
I_CmsXmlContentValue value,
String valueStr,
CmsXmlContentErrorHandler errorHandler,
boolean isWarning) {
if (isWarning) {
// default schema validation only applies to errors
return errorHandler;
}
if (!value.validateValue(valueStr)) {
// value is not valid, add an error to the handler
String message = getValidationMessage(cms, value, value.getTypeName(), valueStr, true, false);
errorHandler.addError(value, message);
}
return errorHandler;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -