📄 cmsshowcontent.java
字号:
* is the name of an url parameter which value will be passed to the
* filtermethod if given. If the url parameter is not found the default
* value will be used as a userparameter for the filtermethod.
* @param doc reference to the A_CmsXmlContent object of the initiating XLM document.
* @param userObject normally the Hashtable with url parameters.
* @return String or byte[] with the content of this subelement.
* @throws org.opencms.main.CmsException in case of unrecoverable errors
*/
public Object getList(CmsObject cms, String tagcontent,
A_CmsXmlContent doc, Object userObject) throws CmsException {
StringBuffer list = new StringBuffer();
ArrayList getMethods = null;
Hashtable parameters = (Hashtable) userObject;
CmsXmlTemplateFile template = (CmsXmlTemplateFile) doc;
String contentDefinitionName = template.getDataValue(C_CONTENTDEFINITION_CLASS_DATABLOCK);
String filterMethodName = template.getDataValue(C_FILTERMETHOD_DATABLOCK);
try {
// get contentdefinition class might throws ClassNotFoundException
Class cdClass = Class.forName(contentDefinitionName);
//register the class for the dependencies
Vector theClass = new Vector();
theClass.add(cdClass);
Vector allCdClasses = new Vector();
// get getFilterMethods method might throws NoSuchMethodException, IllegalAccessException
String userParameter = getUserParameter(parameters, tagcontent);
Vector cdObjects = invokeFilterMethod(cms, cdClass, filterMethodName, userParameter);
if (template.hasData(C_METHODS_TO_USE_DATABLOCK)) {
// if the datablock methods is set inside the template
// only take the methods that are listed in this datablock
// (the methods should be listed as a comma seperated list of names)
String datablockContent = template.getDataValue(C_METHODS_TO_USE_DATABLOCK);
StringTokenizer tokenizer = new StringTokenizer(datablockContent, ",");
int tokens = tokenizer.countTokens();
String[] names = new String[tokens];
for (int i=0; i < tokens; i++) {
names[i] = tokenizer.nextToken().trim();
}
getMethods = getGetMethodsByName(cdClass, names);
} else {
// get all public getMethods that return a String and have no parameters
getMethods = getGetMethods(cdClass);
}
// walk through Vector and fill content
int size = cdObjects.size();
long currentTime = System.currentTimeMillis();
for (int i=0; i < size; i++) {
boolean showIt = true;
A_CmsContentDefinition curCont = (A_CmsContentDefinition)cdObjects.elementAt(i);
if (curCont.isTimedContent()) {
allCdClasses.add(curCont);
I_CmsTimedContentDefinition curTimed = (I_CmsTimedContentDefinition)curCont;
if (((curTimed.getPublicationDate() != 0) && (currentTime < curTimed.getPublicationDate()))
|| ((curTimed.getPurgeDate() != 0) && (currentTime > curTimed.getPurgeDate()))) {
showIt = false;
}
}
if (showIt) {
try {
Method getUniqueIdMethod = cdClass.getMethod("getUniqueId", new Class[] {CmsObject.class});
template.setData("uniqueid", (String)getUniqueIdMethod.invoke(curCont, new Object[] {cms}));
} catch (Exception e) {
}
setDatablocks(template, curCont, getMethods);
list.append(template.getProcessedDataValue(C_LISTENTRY_DATABLOCK, this));
}
}
//register the classes for the dependencies
registerVariantDeps(cms, doc.getAbsoluteFilename(), null, null, parameters, null, allCdClasses, theClass);
} catch (Exception e) {
if (e instanceof CmsException) {
throw (CmsException) e;
} else {
throw new CmsLegacyException (e.getMessage(), CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
}
}
return list.toString();
}
/**
* Gets the userparameter from tagcontent of the getList method-tag or from url parameters.
* The tagcontent should contain the name of an url parameter and a default
* value for this parameter. For example: name,B.
* If the url parameter can be fetched from the parameters Hashtable
* this value will be returned otherwise the default value
* (in this example B). If the tagcontent contains a String without
* any commas this value will be taken as the userparameter.
* @param parameters Hashtable with the url parameters
* @param tagcontent String with the content of the method tag
* @return String with the value of the userparameter
* @throws CmsException if something goes wrong
*/
protected String getUserParameter (Hashtable parameters, String tagcontent) throws CmsException {
String userparameter = "";
String parameterName = null;
String parameterValue = null;
if (tagcontent != null) {
int index = tagcontent.indexOf(",");
if (index != -1) {
parameterName = tagcontent.substring(0, index);
// todo: check also if the length is ok and if the last char is in {0,1,..,9}
if (!(parameterName.startsWith(C_FILTER_PARAMETERS_START))) {
throw new CmsLegacyException("The filterparameter has to be \""
+C_FILTER_PARAMETERS_START+"N\" where 0 <= N <= 9.");
}
parameterValue = (String)parameters.get(parameterName);
if (parameterValue != null) {
userparameter = parameterValue;
} else if (tagcontent.length() > index+1) {
userparameter = tagcontent.substring(index+1);
}
} else {
return tagcontent;
}
}
return userparameter;
}
/**
* This methods collects all "getXYZ" methods of the contentdefinition.
* @param cdClass the class object of the contentdefinition class
* @return ArrayList of java.lang.reflect.Method objects
*/
protected ArrayList getGetMethods (Class cdClass) {
// the Vector of methods to return
ArrayList getMethods = new ArrayList();
// get an array of all public member methods
Method[] methods = cdClass.getMethods();
Method m = null;
String name = null;
//get all public get-methods which return a String
for (int i=0; i < methods.length; i++) {
m = methods[i];
name = m.getName().toLowerCase();
//now extract all methods whose name starts with a "get"
if (name.startsWith("get")) {
//only take methods that have no parameter and return a String
if (m.getReturnType().equals(String.class) && m.getParameterTypes().length == 0) {
getMethods.add(m);
}
}
}
return getMethods;
}
/**
* This methods collects all get-methods with the names specified in the
* String array names. All these methods has to be without parameters and
* should return a String (if they don't return a String there might
* be thrown an exception, when the return value will be casted to a String
* in the method setDatablocks which will result in setting an error text
* instead of the output of the method inside the template).<p>
*
* @param cdClass the class object of the contentdefinition class
* @param names array of method names
* @return ArrayList of java.lang.reflect.Method objects
* @throws NoSuchMethodException in case of unrecoverable errors
*/
protected ArrayList getGetMethodsByName (Class cdClass, String[] names)
throws NoSuchMethodException {
// the list of methods to return
ArrayList getMethods = new ArrayList();
Class[] argTypes = new Class[0];
for (int i=0; i < names.length; i++) {
getMethods.add(cdClass.getMethod(names[i], argTypes));
}
return getMethods;
}
/**
* This method automatically fills all datablocks in the template that fit
* to a special name scheme.
* A datablock named "xyz" is filled with the value of a "getXYZ" method
* form the content defintion.
* @param template The template file of this template
* @param contentDefinition The actual content defintion object
* @param methods A vector of java.lang.reflect.Method objects with all "getXYZ" methods to be used
* @throws org.opencms.main.CmsException in case of unrecoverable erros
*/
protected void setDatablocks(CmsXmlTemplateFile template,
A_CmsContentDefinition contentDefinition,
ArrayList methods) throws CmsException {
String datablockName= null;
Method method = null;
int size = methods.size();
Object[] args = new Object[0];
for (int i=0; i < size; i++) {
// get the method name
method = (Method)methods.get(i);
//get the datablock name - the methodname without the leading "get"
datablockName = (method.getName().substring(3)).toLowerCase();
//check if the datablock name ends with a "string" if so, remove it from the datablockname
if (datablockName.endsWith("string")) {
datablockName = datablockName.substring(0, datablockName.lastIndexOf("string"));
}
// now call the method to get the value
try {
template.setData(datablockName, (String)method.invoke(contentDefinition, args));
} catch (Exception e) {
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error during automatic call method '" + method.getName(), e);
}
// set datablock with error text to indicate that calling the get-method failed
template.setData(datablockName, C_ERROR_TEXT);
}
} // for
}
/**
* Invokes the filtermethod and returns a Vector of contentdefinition
* objects returned by the filtermethod.
* If you want to use filtermethods with other sinatures you have to override
* this method in your own derived subclass.<p>
*
* @param cms the cms object
* @param cdClass Class object of the ContentDefinition class
* @param name the name of the filtermethod to invoke
* @param userparameter the value of the userparameetr which will be passed to the filtermethod
* @return Vector of contentdefinition objects
* @throws NoSuchMethodException if the filtermethod doesn't exist
* @throws InvocationTargetException if the invoked filtermethod throws an exception itself
* @throws IllegalAccessException if the filtermethod is inaccessible
*/
protected Vector invokeFilterMethod (CmsObject cms, Class cdClass, String name, String userparameter)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = cdClass.getMethod(name, new Class[] {CmsObject.class, String.class});
return (Vector)method.invoke(cdClass, new Object[] {cms, userparameter});
}
/**
* Gets the caching information from the current template class.
*
* @param cms CmsObject Object for accessing system resources
* @param templateFile Filename of the template file
* @param elementName Element name of this template in our parent template
* @param parameters Hashtable with all template class parameters
* @param templateSelector template section that should be processed
* @return object with caching information
*/
public CmsCacheDirectives getCacheDirectives(CmsObject cms, String templateFile,
String elementName, Hashtable parameters, String templateSelector) {
CmsCacheDirectives result = new CmsCacheDirectives(true, false, false, false, false);
result.setCacheUri(true);
result.noAutoRenewAfterPublish();
Vector para = new Vector();
para.add(C_ID_PARAMETER);
for (int i=0; i < 10; i++) {
para.add(C_FILTER_PARAMETERS_START + i);
}
result.setCacheParameters(para);
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -