📄 cmsselectwidgetxmlcontenttype.java
字号:
while (it.hasNext()) {
addSelectOption((CmsSelectWidgetOption)it.next());
}
}
return selectOptions;
}
private boolean hasFilterProperty(CmsResource resource, CmsObject cms) throws CmsException {
boolean result = false;
Iterator itFilterProperties;
Map.Entry entry;
CmsProperty property;
// filter out unwanted resources - if no filter properties are defined, every
// resource collected here is ok:
if (m_filterProperties.size() > 0) {
itFilterProperties = m_filterProperties.entrySet().iterator();
while (itFilterProperties.hasNext()) {
entry = (Map.Entry)itFilterProperties.next();
property = cms.readPropertyObject(resource, (String)entry.getKey(), true);
if (property == CmsProperty.getNullProperty()) {
continue;
} else {
// check if value is ok:
if (property.getValue().equals(entry.getValue())) {
// Ok, resource granted:
result = true;
break;
} else {
// Failed, try further filter properties for match:
}
}
}
} else {
// don't filter if now filter props configured
result = true;
}
return result;
}
/**
* Parses the configuration and puts it to the member variables.
* <p>
*
* Only invoked if options were not parsed before in this instance.
* <p>
*
* @param configuration the configuration (with resolved macros).
*
* @param cms needed to read the resource folder to use.
*
* @param param allows to access the resource currently being rendered.
*
*
* @throws CmsIllegalArgumentException if the configuration is invalid.
*
*/
private void parseConfigurationInternal(String configuration, CmsObject cms, I_CmsWidgetParameter param) {
// prepare for macro resolvation of property value against the resource currently
// rendered
// implant the uri to the special cms object for resolving macros from the
// collected xml contents:
CmsFile file = ((I_CmsXmlContentValue)param).getDocument().getFile();
m_macroCmsObject.getRequestContext().setUri(file.getRootPath());
List mappings = CmsStringUtil.splitAsList(configuration, '|');
Iterator itMappings = mappings.iterator();
String mapping;
String[] keyValue;
String key;
String value;
boolean displayMacroFound = false, sortMacroFound = false, folderFound = false, typeFound = false;
while (itMappings.hasNext()) {
mapping = (String)itMappings.next();
keyValue = CmsStringUtil.splitAsArray(mapping, '=');
if (keyValue.length != 2) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEYVALUE_LENGTH_1,
mapping));
}
key = keyValue[0].trim();
value = keyValue[1].trim();
// implant the resource for macro "${opencms.filename}"
m_macroResolver.setResourceName(file.getName());
// check key
if (CONFIGURATION_OPTION_DISPLAY_MACRO.equals(key)) {
if (displayMacroFound) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_DUPLICATE_2,
key,
configuration));
}
m_displayOptionMacro = value;
displayMacroFound = true;
} else if (CONFIGURATION_OPTION_SORT_MACRO.equals(key)) {
if (sortMacroFound) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_DUPLICATE_2,
key,
configuration));
}
m_sortMacro = value;
sortMacroFound = true;
} else if (CONFIGURATION_RESOURCETYPENAME.equals(key)) {
if (typeFound) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_DUPLICATE_2,
key,
configuration));
}
// check if resource type name is OK
// if setResourceType will be implemented copy here and invoke that one
try {
m_resourceTypeID = OpenCms.getResourceManager().getResourceType(value).getTypeId();
} catch (CmsLoaderException e) {
throw new CmsIllegalArgumentException(org.opencms.file.Messages.get().container(
org.opencms.file.Messages.ERR_UNKNOWN_RESOURCE_TYPE_1,
value), e);
}
typeFound = true;
} else if (CONFIGURATION_TOPFOLDER.equals(key)) {
if (folderFound) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_DUPLICATE_2,
key,
configuration));
}
// allow collector path to contain macros relative to the current resource:
value = m_macroResolver.resolveMacros(value);
try {
CmsRequestContext context = cms.getRequestContext();
String oldSiteRoot = context.getSiteRoot();
context.setSiteRoot("/");
CmsResource resource = cms.readResource(value);
context.setSiteRoot(oldSiteRoot);
if (resource.isFile()) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_RESOURCE_NOFOLDER_2,
value,
configuration));
}
m_resourceFolder = resource;
} catch (CmsException e) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_RESOURCE_INVALID_2,
value,
configuration), e);
}
folderFound = true;
} else {
// a property=value definition???
CmsPropertyDefinition propDef;
try {
propDef = cms.readPropertyDefinition(key);
} catch (CmsException e) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_UNKNOWN_2,
key,
getClass().getName()), e);
}
if (propDef != null) {
// a valid property - value combination to filter resources for:
// value is potentially a macro that will be compared to the current xml content
// resource!
value = m_macroResolver.resolveMacros(value);
m_filterProperties.put(key, value);
} else {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_UNKNOWN_2,
key,
getClass().getName()));
}
}
}
// final check wether all has been set
if (!displayMacroFound) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_MISSING_3,
CONFIGURATION_OPTION_DISPLAY_MACRO,
configuration,
getClass().getName()));
}
if (!folderFound) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_MISSING_3,
CONFIGURATION_TOPFOLDER,
configuration,
getClass().getName()));
}
if (!typeFound) {
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_SELECTWIDGET_CONFIGURATION_KEY_MISSING_3,
CONFIGURATION_RESOURCETYPENAME,
configuration,
getClass().getName()));
}
}
/**
*
* Resolves xpath macros of the form <code>"${xpath.XPATHEXPRESSION}"</code> by the field
* value of the XML content denoted by the given resource.
* <p>
*
* File laoding and unmarshalling is only done if the given String contains xpath macros.
* <p>
*
* @param cms to access values in the cmsobject.
*
* @param content the resource pointing to an xmlcontent containing the macro values to resolve.
*
* @param value the unresolved macro string.
*
* @return a String with resolved xpath macros that have been read from the xmlcontent.
*
*/
private String resolveXpathMacros(CmsObject cms, CmsResource resource, String value) throws CmsException {
StringBuffer result = new StringBuffer();
int startmacro = value.indexOf("${xpath.");
int stopmacro = 0;
String xpath;
if (startmacro != -1) {
// for the option value we have to unmarshal...
CmsXmlContent xmlcontent = CmsXmlContentFactory.unmarshal(cms, CmsFile.upgrade(resource, cms));
// we read the locale node of the xmlcontent instance matching the resources
// locale property (or top level locale).
Locale locale = CmsLocaleManager.getLocale(cms.readPropertyObject(
xmlcontent.getFile(),
CmsPropertyDefinition.PROPERTY_LOCALE,
true).getValue());
while (startmacro != -1) {
stopmacro = value.indexOf('}');
if (stopmacro == 0) {
// TODO: complain about missing closing macro bracket!
}
// first cut the prefix of the macro to put it to the result:
result.append(value.substring(0, startmacro));
// now replace the macro:
xpath = value.substring(startmacro + 8, stopmacro);
// Foreign languages will be invisible!!!
// List locales = content.getLocales();
// if (!locales.contains(locale)) {
// locale = (Locale)locales.get(0);
// }
try {
result.append(xmlcontent.getValue(xpath, locale).getPlainText(cms));
} catch (Exception ex) {
if (LOG.isErrorEnabled()) {
LOG.error(Messages.get().getBundle().key(
Messages.LOG_ERR_SELECTWIDGET_XPATH_INVALID_4,
new Object[] {
xpath,
locale.toString(),
xmlcontent.getFile().getRootPath(),
ex.getLocalizedMessage()}));
}
}
// skip over the consumed String of value:
value = value.substring(stopmacro + 1);
// take a new start for macro:
startmacro = value.indexOf("${xpath.");
}
}
// append trailing value
result.append(value);
return result.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -