📄 a_cmselement.java
字号:
export = true;
}
}
}
} catch (Exception e) {
// do nothing, set everything to false and log the error
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn(toString()
+ " could not find out if the element is proxy cacheable", e);
}
}
}
if (!m_cacheDirectives.userSetProxyPrivate()) {
((CmsCacheDirectives)m_cacheDirectives).setProxyPrivateCacheable(proxyPrivate);
}
if (!m_cacheDirectives.userSetProxyPublic()) {
((CmsCacheDirectives)m_cacheDirectives).setProxyPublicCacheable(proxyPublic);
}
if (!m_cacheDirectives.userSetExport()) {
((CmsCacheDirectives)m_cacheDirectives).setExport(export);
}
}
proxySettings.merge(m_cacheDirectives);
// now for the subelements
Enumeration elementNames = m_elementDefinitions.getAllElementNames();
while (elementNames.hasMoreElements()) {
String name = (String)elementNames.nextElement();
CmsElementDefinition currentDef = m_elementDefinitions.get(name);
A_CmsElement currentEle = CmsXmlTemplateLoader.getElementCache().getElementLocator().get(cms, new CmsElementDescriptor(
currentDef.getClassName(), currentDef.getTemplateName()), parameters);
currentEle.checkProxySettings(cms, proxySettings, parameters);
}
}
/**
* Abstract method for getting the content of this element.
* Element classes may have different implementations for getting
* the contents. Common tasks of all implementations are checking
* the variant cache and creating the variant if required.
* @param elementCache Entry point for the element cache
* @param cms CmsObject for accessing system resources
* @param elDefs Definitions of this element's subelements
* @param parameters All parameters of this request
* @param methodparameter used only for methode elements. Contains the parameter for the methode.
* @return Byte array with the processed content of this element.
* @throws CmsException
*/
public abstract byte[] getContent(CmsElementCache elementCache, CmsObject cms,
CmsElementDefinitionCollection efDefs, String elementName, Hashtable parameters,
String methodParameter) throws CmsException;
/**
* Get a template class from the template class manager.
* @param cms CmsObject for accessing system resources.
* @param classname Name of the requested class.
* @throws CmsException if the loaded class is no OpenCms template class
*/
protected I_CmsTemplate getTemplateClass(CmsObject cms, String classname) throws CmsException {
Object o = CmsTemplateClassManager.getClassInstance(classname);
// Check, if the loaded class really is a OpenCms template class.
if (o instanceof I_CmsTemplate) {
return (I_CmsTemplate)o;
} else {
throw new CmsLegacyException(classname + " is no OpenCms template class.",
CmsLegacyException.C_XML_NO_TEMPLATE_CLASS);
}
}
/**
* Resolve given variant of this element and get content of all sub elements.<p>
*
* @param cms CmsObject for accessing system resources
* @param variant Variant that should be resolved
* @param elementCache Entry point for element cache
* @param elDefs Definitions for all subelements
* @param parameters All parameters of this request
* @return Byte array with processed element content
* @throws CmsException if resolving fails.
*/
public byte[] resolveVariant(CmsObject cms, CmsElementVariant variant,
CmsElementCache elementCache, CmsElementDefinitionCollection elDefs, Hashtable parameters)
throws CmsException {
boolean resolveDebug = false;
if (resolveDebug)
System.err.println("= Start resolving variant " + variant);
int len = variant.size();
// Try to get the corresponding element using the element locator
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (int i = 0; i < len; i++) {
// put the name of the element into the params
if (resolveDebug)
System.err.print("= Part " + i + " is a ");
Object o = variant.get(i);
// Decide what to do with the current part.
// If it's a String or byte array, just print it out.
// If it's a link to a sub element, get this element and call its getContent() method
if (o instanceof String) {
if (resolveDebug)
System.err.println("String");
baos.write(((String)o).getBytes(cms.getRequestContext().getEncoding()));
} else if (o instanceof byte[]) {
if (resolveDebug)
System.err.println("byte array");
baos.write((byte[])o);
} else if (o instanceof CmsElementLink) {
if (resolveDebug)
System.err.println("Element Link");
// we have to resolve the element link right NOW!
// Look for the element definition
String lookupName = ((CmsElementLink)o).getElementName();
if (resolveDebug)
System.err.println("= Trying to resolve link \"" + lookupName + "\".");
CmsElementDefinition elDef = elDefs.get(lookupName);
if (elDef != null) {
// We have successfully found an element definition.
// first add the parameter from the elementdefinition to the parameters
elDef.joinParameters(parameters);
// put the name of the element into the params
parameters.put("_ELEMENT_", elDef.getName());
if (elDef.getTemplateName() != null) {
parameters.put(elDef.getName() + "._TEMPLATE_", elDef.getTemplateName());
}
parameters.put(elDef.getName() + "._CLASS_", elDef.getClassName());
if (elDef.getTemplateSelector() != null) {
parameters.put(elDef.getName() + "._TEMPLATESELECTOR_", elDef.getTemplateSelector());
} else {
parameters.put(elDef.getName() + "._TEMPLATESELECTOR_", "default");
}
// Try to get the corresponding element using the element locator
A_CmsElement subEl = elementCache.getElementLocator().get(cms, elDef.getDescriptor(), parameters);
if (resolveDebug)
System.err.println("= Element defintion for \"" + lookupName
+ "\" says: ");
if (resolveDebug)
System.err.println("= -> Class : " + elDef.getClassName());
if (resolveDebug)
System.err.println("= -> Template : " + elDef.getTemplateName());
String errorMessage = "";
if (subEl != null) {
// An element could be found. Very fine.
// So we can go on and call its getContent() method
if (resolveDebug)
System.err.println("= Element object found for \"" + lookupName
+ "\". Calling getContent on this object. ");
byte[] buffer = null;
try {
buffer = subEl.getContent(elementCache, cms, elDefs, lookupName, parameters, null);
} catch (Exception e) {
// An error occured while getting the element's content.
// Do some error handling here.
if (CmsUser.USER_TYPE_SYSTEMUSER == cms.getRequestContext().currentUser().getType()
&& !OpenCms.getDefaultUsers().getUserGuest().equals(cms.getRequestContext().currentUser().getName())) {
// a systemuser gets the real message.(except guests)
errorMessage = e.toString();
}
subEl = null;
buffer = null;
if (e instanceof CmsException) {
CmsException ce = (CmsException)e;
if (ce instanceof CmsSecurityException) {
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Access denied in element "
+ lookupName, ce);
}
throw ce;
} else {
// Any other CmsException. This may be critical
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error in element "
+ lookupName, e);
}
}
} else {
// Any other Non-CmsException.
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Non-CmsException in element "
+ lookupName, e);
}
}
}
// If we have some results print them out.
if (buffer != null) {
baos.write(buffer);
}
} else {
// The subelement object is null, i.e. the element could not be found.
// Do nothing but a little bit logging here.
if (resolveDebug)
System.err.println("= Cannot find Element object for \""
+ lookupName + "\". Ignoring this link. ");
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn("Cannot find Element object for \""
+ lookupName + "\", ignoring this link");
}
}
// If the element could not be generated properly, print a little error
// message instead of the element's results.
if (subEl == null) {
baos.write(("[" + lookupName + "] ??? ").getBytes());
baos.write(errorMessage.getBytes());
}
} else {
// No element definition could be found.
// Do some logging only and ignore this element
baos.write(("[" + lookupName + "] Element not defined.").getBytes());
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn("No element definition found for \""
+ lookupName + "\", ignoring this link");
}
if (resolveDebug) {
System.err.println("= No element definition found for \"" + lookupName
+ "\". Ignoring this link. ");
System.err.println(elDefs.toString());
}
}
} else if (o instanceof CmsMethodLink) {
if (resolveDebug)
System.err.println("Method Link");
// get the methodelement
String methodName = ((CmsMethodLink)o).getMethodeName();
String methodParameter = ((CmsMethodLink)o).getMethodParameter();
A_CmsElement metEle = elementCache.getElementLocator().get(cms, new CmsElementDescriptor(
m_className + "." + methodName, "METHOD"), parameters);
byte[] buffer = null;
if (metEle != null) {
try {
buffer = metEle.getContent(elementCache, cms, elDefs, null, parameters, methodParameter);
} catch (Exception e) {
if (e instanceof CmsException) {
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Error in method " + methodName, e);
}
} else {
// Any other Non-CmsException.
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("Non-CmsException in method "
+ methodName, e);
}
}
}
} else {
// The subelement object is null, i.e. the element could not be found.
// Do nothing but a little bit logging here.
if (resolveDebug)
System.err.println("= Cannot find methodElemtn object for \""
+ methodName + "\". Ignoring this link. ");
if (CmsLog.getLog(this).isWarnEnabled()) {
CmsLog.getLog(this).warn("Cannot find method Element object for \""
+ methodName + "\", ignoring this link");
}
}
// If we have some results print them out.
if (buffer != null) {
baos.write(buffer);
}
}
}
return baos.toByteArray();
} catch (IOException e) {
// Something went wrong while writing to the OutputStream
if (CmsLog.getLog(this).isErrorEnabled()) {
CmsLog.getLog(this).error("IOException while writing to XMl template OutputStream", e);
}
throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
}
}
/**
* Get a string representation of this element.
* @return String representation.
*/
public String toString() {
String part1 = getClass().getName();
part1 = part1.substring(part1.lastIndexOf(".") + 1);
String part2 = m_className.substring(m_className.lastIndexOf(".") + 1);
String part3 = "";
if (m_templateName != null) {
part3 = m_templateName.substring(m_templateName.lastIndexOf("/") + 1);
}
return "[" + part1 + " (" + part2 + "/" + part3 + ")]";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -