📄 cmsselectwidgetxmlcontenttype.java
字号:
m_macroCmsObjectInner.getRequestContext().setSiteRoot("/");
m_macroResolverInner = new CmsMacroResolver();
m_macroResolverInner.setCmsObject(m_macroCmsObjectInner);
}
/**
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {
CmsResourceSelectWidgetOption option1 = (CmsResourceSelectWidgetOption)o1;
CmsResourceSelectWidgetOption option2 = (CmsResourceSelectWidgetOption)o2;
CmsResource resource1 = option1.getResource();
CmsResource resource2 = option2.getResource();
String sort1, sort2;
// fool the macro resolver:
CmsRequestContext requestContext = m_macroCmsObjectInner.getRequestContext();
requestContext.setUri(resource1.getRootPath());
// implant the resource name for macro "${opencms.filename}:
m_macroResolverInner.setResourceName(resource1.getName());
sort1 = m_macroResolverInner.resolveMacros(m_comparatorMacro);
requestContext.setUri(resource2.getRootPath());
m_macroResolverInner.setResourceName(resource2.getName());
sort2 = m_macroResolverInner.resolveMacros(m_comparatorMacro);
return sort1.compareTo(sort2);
}
}
/**
* Configuration parameter for construction of the option display value by a macro containing
* xpath macros for the xmlcontent.
*/
public static final String CONFIGURATION_OPTION_DISPLAY_MACRO = "displayOptionMacro";
/**
* Configuration parameter for choosing the macro to sort the display options by.
*/
public static final String CONFIGURATION_OPTION_SORT_MACRO = "sortMacro";
/** Configuration parameter to set the name of the resource types to accept. */
public static final String CONFIGURATION_RESOURCETYPENAME = "resourcetypeName";
/** Configuration parameter to set the top folder in the VFS to search for xmlcontent resources. */
public static final String CONFIGURATION_TOPFOLDER = "folder";
/** The log object for this class. */
private static final Log LOG = CmsLog.getLog(CmsSelectWidgetXmlcontentType.class);
/** Only used for the macro resolver to resolve macros for the collected XML contents. */
protected CmsObject m_macroCmsObject;
/** The macro resolver to use. */
protected CmsMacroResolver m_macroResolver;
/**
* The macro to search for the display String of the options in xmlcontent files found below the
* folder to search in.
*/
private String m_displayOptionMacro;
/** A map filled with properties and their values that have to exist on values to display. */
private Map m_filterProperties;
/** The resource folder under which the xmlcontent resources will be searched. */
private CmsResource m_resourceFolder;
/** The type id of xmlcontent resources to use. */
private int m_resourceTypeID;
/**
* The macro that describes the {@link CmsResource} - related value to use for sorting of the
* select widget options.
*/
private String m_sortMacro;
/**
* Creates an unconfigured widget that has to be configured by
* {@link org.opencms.widgets.A_CmsWidget#setConfiguration(String)} before any html output API
* call is triggered.
* <p>
*
*/
public CmsSelectWidgetXmlcontentType() {
this("");
}
/**
* Creates an instance with the given configuration.
* <p>
*
* @param configuration see the class description for the format.
*/
public CmsSelectWidgetXmlcontentType(String configuration) {
super(configuration);
m_filterProperties = new HashMap();
}
/**
* Returns the displayOptionXpathMacro.
* <p>
*
* @return the displayOptionXpathMacro
*/
public String getDisplayOptionMacro() {
return m_displayOptionMacro;
}
/**
* Returns the resourceFolder under which xmlcontent resources will be investigated recursively.
* <p>
*
* @return the resourceFolder
*/
public CmsResource getResourceFolder() {
return m_resourceFolder;
}
/**
* Returns the resource type id.
* <p>
*
* @return the resourceTypeID
*/
public int getResourceTypeID() {
return m_resourceTypeID;
}
/**
* @see org.opencms.widgets.CmsSelectWidget#newInstance()
*/
public I_CmsWidget newInstance() {
return new CmsSelectWidgetXmlcontentType(getConfiguration());
}
/**
* Returns the list of configured select options, parsing the configuration String if required.
* <p>
*
* @param cms the current users OpenCms context.
*
* @param widgetDialog the dialog of this widget.
*
* @param param the widget parameter of this dialog.
*
* @see org.opencms.widgets.A_CmsSelectWidget#parseSelectOptions(org.opencms.file.CmsObject,
* org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
*
* @return the list of configured select options.
*
* @throws CmsIllegalArgumentException if the "folder" property of the configuration does not
* denote a folder within the VFS.
*/
protected List parseSelectOptions(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param)
throws CmsIllegalArgumentException {
if (m_macroCmsObject == null) {
try {
m_macroCmsObject = OpenCms.initCmsObject(cms);
m_macroCmsObject.getRequestContext().setSiteRoot("/");
} catch (CmsException e) {
// should never happen
if (LOG.isErrorEnabled()) {
LOG.error(Messages.get().getBundle().key(
Messages.ERR_SELECTWIDGET_INTERNAL_CONFIGURATION_2,
new Object[] {getClass().getName(), getConfiguration()}));
}
return Collections.EMPTY_LIST;
}
}
if (m_macroResolver == null) {
m_macroResolver = new CmsMacroResolver();
m_macroResolver.setCmsObject(m_macroCmsObject);
m_macroResolver.setKeepEmptyMacros(true);
}
List selectOptions = getSelectOptions();
if (selectOptions == null) {
String configuration = getConfiguration();
if (configuration == null) {
// workaround: use the default value to parse the options
configuration = param.getDefault(cms);
}
try {
// parse configuration to members
parseConfigurationInternal(configuration, cms, param);
// build the set of sorted options
SortedSet sortOptions = new TreeSet(new CmsResourceSelectWidgetOptionComparator(
m_macroCmsObject,
m_sortMacro));
CmsSelectWidgetOption option;
List resources;
// collect all subresources of resource folder
CmsResourceFilter filter = CmsResourceFilter.ALL.addRequireType(getResourceTypeID());
CmsRequestContext context = cms.getRequestContext();
String oldSiteroot = context.getSiteRoot();
context.setSiteRoot("/");
resources = cms.readResources(m_resourceFolder.getRootPath(), filter, true);
context.setSiteRoot(oldSiteroot);
if (resources.size() == 0) {
if (LOG.isErrorEnabled()) {
LOG.error(Messages.get().getBundle().key(
Messages.LOG_ERR_SELECTWIDGET_NO_RESOURCES_FOUND_3,
configuration,
m_resourceFolder.getRootPath(),
OpenCms.getResourceManager().getResourceType(getResourceTypeID()).getTypeName()));
}
}
Iterator itResources = resources.iterator();
CmsResource resource;
String displayName;
// inner loop vars :
while (itResources.hasNext()) {
resource = (CmsResource)itResources.next();
// macro resolvation within hasFilterProperty will resolve values to the current
// request
if (hasFilterProperty(resource, cms)) {
// implant the uri to the special cms object for resolving macros from the
// collected xml contents:
m_macroCmsObject.getRequestContext().setUri(resource.getRootPath());
// implant the resource for macro "${opencms.filename}"
m_macroResolver.setResourceName(resource.getName());
// implant the messages
m_macroResolver.setMessages(widgetDialog.getMessages());
// filter out unwanted resources - if no filter properties are defined,
// every
// resource collected here is ok:
displayName = m_macroResolver.resolveMacros(getDisplayOptionMacro());
// deal with a bug of the macro resolver: it will return "" if it gets
// "${unknown.thing}":
if (CmsStringUtil.isEmptyOrWhitespaceOnly(displayName)) {
// it was a "${xpath.field}" expression only and swallowed by macro
// resolver:
displayName = resolveXpathMacros(cms, resource, getDisplayOptionMacro());
} else {
// there was more than one xpath macro: allow further replacements
// within partly resolved macro:
displayName = resolveXpathMacros(cms, resource, displayName);
}
// final check:
if (CmsStringUtil.isEmpty(displayName)) {
displayName = resource.getName();
}
displayName = resolveXpathMacros(cms, resource, displayName);
if (!CmsStringUtil.isEmpty(displayName)) {
// now everything required is there:
option = new CmsResourceSelectWidgetOption(resource, false, displayName);
sortOptions.add(option);
}
}
}
selectOptions = new LinkedList(sortOptions);
} catch (Exception e) {
if (LOG.isErrorEnabled()) {
LOG.error(Messages.get().getBundle().key(
Messages.ERR_SELECTWIDGET_CONFIGURATION_2,
getClass(),
configuration), e);
}
}
if (selectOptions == Collections.EMPTY_LIST) {
selectOptions = new ArrayList();
}
// no method to add the parsed option list....
// Caution: if it is decided to return a copy of the list we are doomed unless
// setSelectOptions is set to protected!
List pOptions = getSelectOptions();
if (pOptions != null) {
pOptions.clear();
}
Iterator it = selectOptions.iterator();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -