📄 cmsjsptagcontentload.java
字号:
* Returns <code>"true"</code> if this content load tag should only preload the values from the collector.<p>
*
* @return <code>"true"</code> if this content load tag should only preload the values from the collector
*/
public String getPreload() {
return String.valueOf(m_preload);
}
/**
* Returns the property.<p>
*
* @return the property
*/
public String getProperty() {
return m_property;
}
/**
* @see org.opencms.jsp.I_CmsXmlContentContainer#getResourceName()
*/
public String getResourceName() {
return m_resourceName;
}
/**
* @see org.opencms.jsp.I_CmsXmlContentContainer#getXmlDocument()
*/
public I_CmsXmlDocument getXmlDocument() {
return m_content;
}
/**
* @see org.opencms.jsp.I_CmsXmlContentContainer#getXmlDocumentElement()
*/
public String getXmlDocumentElement() {
// value must be set in "loop" or "show" class
return null;
}
/**
* @see org.opencms.jsp.I_CmsXmlContentContainer#getXmlDocumentLocale()
*/
public Locale getXmlDocumentLocale() {
return m_contentLocale;
}
/**
* @see org.opencms.jsp.I_CmsXmlContentContainer#hasMoreContent()
*/
public boolean hasMoreContent() throws JspException {
if (m_isFirstLoop) {
m_isFirstLoop = false;
} else {
if (m_directEditOpen) {
// last element was direct editable, close it
CmsJspTagEditable.endDirectEdit(pageContext);
m_directEditOpen = false;
}
}
if (m_preload) {
// if in preload mode, no result is required
return false;
}
// check if there are more files to iterate
boolean hasMoreContent = m_collectorResult.size() > 0;
if (hasMoreContent) {
// there are more results available...
try {
doLoadNextFile();
} catch (CmsException e) {
m_controller.setThrowable(e, m_resourceName);
throw new JspException(e);
}
// check "direct edit" support
if (m_directEditMode.isEnabled() && (m_resourceName != null)) {
// check options for first element
CmsDirectEditButtonSelection directEditButtons;
if (m_directEditFollowButtons == null) {
// this is the first call, calculate the options
if (m_directEditLinkForNew == null) {
// if create link is null, show only "edit" button for first element
directEditButtons = CmsDirectEditButtonSelection.EDIT;
// also show only the "edit" button for 2nd to last element
m_directEditFollowButtons = directEditButtons;
} else {
// if create link is not null, show "edit", "delete" and "new" button for first element
directEditButtons = CmsDirectEditButtonSelection.EDIT_DELETE_NEW;
// show "edit" and "delete" button for 2nd to last element
m_directEditFollowButtons = CmsDirectEditButtonSelection.EDIT_DELETE;
}
} else {
// re-use pre calculated options
directEditButtons = m_directEditFollowButtons;
}
m_directEditOpen = CmsJspTagEditable.startDirectEdit(pageContext, new CmsDirectEditParams(
m_resourceName,
directEditButtons,
m_directEditMode,
m_directEditLinkForNew));
}
} else {
// no more results in the collector, reset locale (just to make sure...)
m_locale = null;
}
return hasMoreContent;
}
/**
* @see org.opencms.jsp.I_CmsXmlContentContainer#isPreloader()
*/
public boolean isPreloader() {
return m_preload;
}
/**
* @see javax.servlet.jsp.tagext.Tag#release()
*/
public void release() {
m_cms = null;
m_collector = null;
m_collectorName = null;
m_collectorParam = null;
m_collectorResult = null;
m_content = null;
m_contentInfoBean = null;
m_contentLocale = null;
m_controller = null;
m_directEditLinkForNew = null;
m_directEditFollowButtons = null;
m_directEditOpen = false;
m_directEditMode = null;
m_isFirstLoop = false;
m_locale = null;
m_pageIndex = null;
m_pageNavLength = null;
m_pageSize = null;
m_param = null;
m_preload = false;
m_property = null;
m_resourceName = null;
super.release();
}
/**
* Sets the collector.<p>
*
* @param collector the collector to set
*/
public void setCollector(String collector) {
m_collector = collector;
}
/**
* Sets the editable mode.<p>
*
* @param mode the mode to set
*/
public void setEditable(String mode) {
m_directEditMode = CmsDirectEditMode.valueOf(mode);
}
/**
* Sets the locale.<p>
*
* @param locale the locale to set
*/
public void setLocale(String locale) {
if (CmsStringUtil.isEmpty(locale)) {
m_locale = null;
m_contentLocale = null;
} else {
m_locale = CmsLocaleManager.getLocale(locale);
m_contentLocale = m_locale;
}
}
/**
* Sets the index of the page to be displayed.<p>
*
* @param pageIndex the index of the page to be displayed
*/
public void setPageIndex(String pageIndex) {
m_pageIndex = pageIndex;
}
/**
* Sets the number of page links in the Google-like page navigation.<p>
*
* @param pageNavLength the number of page links in the Google-like page navigation
*/
public void setPageNavLength(String pageNavLength) {
m_pageNavLength = pageNavLength;
}
/**
* Sets the size of a single page to be displayed.<p>
*
* @param pageSize the size of a single page to be displayed
*/
public void setPageSize(String pageSize) {
m_pageSize = pageSize;
}
/**
* Sets the collector parameter.<p>
*
* @param param the collector parameter to set
*/
public void setParam(String param) {
m_param = param;
}
/**
* Sets the preload flag for this content load tag.<p>
*
* If this is set to <code>true</code>, then the collector result will only
* be preloaded, but not iterated.<p>
*
* @param preload the preload flag to set
*/
public void setPreload(String preload) {
m_preload = Boolean.valueOf(preload).booleanValue();
}
/**
* Sets the property.<p>
*
* @param property the property to set
*/
public void setProperty(String property) {
m_property = property;
}
/**
* Load the next file name from the initialized list of file names.<p>
*
* @throws CmsException if something goes wrong
*/
protected void doLoadNextFile() throws CmsException {
// get the next resource from the collector
CmsResource resource = getNextResource();
if (resource == null) {
m_resourceName = null;
m_content = null;
return;
}
// set the resource name
m_resourceName = m_cms.getSitePath(resource);
// upgrade the resource to a file
// the static method CmsFile.upgrade(...) is not used for performance reasons
CmsFile file = null;
if (resource instanceof CmsFile) {
// check the resource contents
file = (CmsFile)resource;
if ((file.getContents() == null) || (file.getContents().length <= 0)) {
// file has no contents available, force re-read
file = null;
}
}
if (file == null) {
// use ALL filter since the list itself should have filtered out all unwanted resources already
file = m_cms.readFile(m_resourceName, CmsResourceFilter.ALL);
}
// unmarshal the XML content from the resource
m_content = CmsXmlContentFactory.unmarshal(m_cms, file);
// check if locale is available
m_contentLocale = m_locale;
if (!m_content.hasLocale(m_contentLocale)) {
Iterator it = OpenCms.getLocaleManager().getDefaultLocales().iterator();
while (it.hasNext()) {
Locale locale = (Locale)it.next();
if (m_content.hasLocale(locale)) {
// found a matching locale
m_contentLocale = locale;
break;
}
}
}
}
/**
* Returns the content info bean.<p>
*
* @return the content info bean
*/
protected CmsContentInfoBean getContentInfoBean() {
return m_contentInfoBean;
}
/**
* Initializes this content load tag.<p>
*
* @param container the parent container (could be a preloader)
*
* @throws JspException in case something goes wrong
*/
protected void init(I_CmsXmlContentContainer container) throws JspException {
// check if the tag contains a pageSize, pageIndex and pageNavLength attribute, or none of them
int pageAttribCount = 0;
pageAttribCount += CmsStringUtil.isNotEmpty(m_pageSize) ? 1 : 0;
pageAttribCount += CmsStringUtil.isNotEmpty(m_pageIndex) ? 1 : 0;
if ((pageAttribCount > 0) && (pageAttribCount < 2)) {
throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_TAG_CONTENTLOAD_INDEX_SIZE_0));
}
I_CmsXmlContentContainer usedContainer;
if (container == null) {
// no preloading ancestor has been found
usedContainer = this;
if (CmsStringUtil.isEmpty(m_collector)) {
// check if the tag contains a collector attribute
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_TAG_CONTENTLOAD_MISSING_COLLECTOR_0));
}
if (CmsStringUtil.isEmpty(m_param)) {
// check if the tag contains a param attribute
throw new CmsIllegalArgumentException(Messages.get().container(
Messages.ERR_TAG_CONTENTLOAD_MISSING_PARAM_0));
}
} else {
// use provided container (preloading ancestor)
usedContainer = container;
}
if (m_preload) {
// always deactivate direct edit for prelaod
m_directEditMode = CmsDirectEditMode.FALSE;
} else if (m_directEditMode == null) {
// direct edit mode must not be null
m_directEditMode = CmsDirectEditMode.FALSE;
}
// initialize OpenCms access objects
m_controller = CmsFlexController.getController(pageContext.getRequest());
m_cms = m_controller.getCmsObject();
// get the resource name from the selected container
String resourcename = getResourceName(m_cms, usedContainer);
// initialize a string mapper to resolve EL like strings in tag attributes
CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(m_cms).setJspPageContext(pageContext).setResourceName(
resourcename).setKeepEmptyMacros(true);
// resolve the collector name
if (container == null) {
// no preload parent container, initialize new values
m_collectorName = resolver.resolveMacros(getCollector());
// resolve the parameter
m_collectorParam = resolver.resolveMacros(getParam());
m_collectorResult = null;
} else {
// preload parent content container available, use values from this container
m_collectorName = usedContainer.getCollectorName();
m_collectorParam = usedContainer.getCollectorParam();
m_collectorResult = usedContainer.getCollectorResult();
if (m_locale == null) {
// use locale from ancestor if available
m_locale = usedContainer.getXmlDocumentLocale();
}
}
if (m_locale == null) {
// no locale set, use locale from users request context
m_locale = m_cms.getRequestContext().getLocale();
}
try {
// now collect the resources
I_CmsResourceCollector collector = OpenCms.getResourceManager().getContentCollector(m_collectorName);
if (collector == null) {
throw new CmsException(Messages.get().container(Messages.ERR_COLLECTOR_NOT_FOUND_1, m_collectorName));
}
// execute the collector if not already done in parent tag
if (m_collectorResult == null) {
m_collectorResult = collector.getResults(m_cms, m_collectorName, m_collectorParam);
}
m_contentInfoBean = new CmsContentInfoBean();
m_contentInfoBean.setPageSizeAsString(resolver.resolveMacros(m_pageSize));
m_contentInfoBean.setPageIndexAsString(resolver.resolveMacros(m_pageIndex));
m_contentInfoBean.setPageNavLengthAsString(resolver.resolveMacros(m_pageNavLength));
m_contentInfoBean.setResultSize(m_collectorResult.size());
m_contentInfoBean.setLocale(m_locale.toString());
m_contentInfoBean.initResultIndex();
if (!m_preload) {
// not required when only preloading
m_collectorResult = CmsJspTagContentLoad.limitCollectorResult(m_contentInfoBean, m_collectorResult);
m_contentInfoBean.initPageNavIndexes();
String createParam = collector.getCreateParam(m_cms, m_collectorName, m_collectorParam);
if (createParam != null) {
// use "create link" only if collector supports it
m_directEditLinkForNew = CmsEncoder.encode(m_collectorName + "|" + createParam);
}
}
} catch (CmsException e) {
m_controller.setThrowable(e, m_cms.getRequestContext().getUri());
throw new JspException(e);
}
// reset the direct edit options (required becaue of re-used tags)
m_directEditOpen = false;
m_directEditFollowButtons = null;
// the next loop is the first loop
m_isFirstLoop = true;
}
/**
* Returns the next resource from the collector.<p>
*
* @return the next resource from the collector
*/
private CmsResource getNextResource() {
if ((m_collectorResult != null) && (m_collectorResult.size() > 0)) {
m_contentInfoBean.incResultIndex();
return (CmsResource)m_collectorResult.remove(0);
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -