📄 cmsxmltemplate.java
字号:
return m_cache != null;
}
/**
* For debugging purposes only.
* Prints out all parameters.
* <P>
* May be called from the template file using
* <code><METHOD name="parameters"></code>.
*
* @param cms CmsObject Object for accessing system resources.
* @param tagcontent Unused in this special case of a user method. Can be ignored.
* @param doc Reference to the A_CmsXmlContent object the initiating XLM document.
* @param userObj Hashtable with parameters.
* @return Debugging information about all parameters.
*/
public String parameters(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject) {
Hashtable param = (Hashtable)userObject;
Enumeration keys = param.keys();
String s = "";
while(keys.hasMoreElements()) {
String key = (String)keys.nextElement();
s = s + "<B>" + key + "</B>: " + param.get(key) + "<BR>";
}
s = s + "<B>" + tagcontent + "</B><BR>";
return s;
}
/**
* Set the instance of template cache that should be used to store
* cacheable results of the subtemplates.
* If the template cache is not set, caching will be disabled.
* @param c Template cache to be used.
*/
public final void setTemplateCache(I_CmsTemplateCache c) {
m_cache = c;
}
/**
* Indicates if a previous cached result should be reloaded.
* <P>
* <em>not implemented.</em> Returns always <code>false</code>.
*
* @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 <code>false</code>
*/
public boolean shouldReload(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String templateSelector) {
return false;
}
/**
* Saves the dependencies for this elementvariante.
* We save the deps two ways.
* First we have an so called extern Hashtable where we use as key a resource
* (represented by a String) and as values an Vector with all ElementVariants
* that depend on this resource (again represented by a String in which we save
* the variant and the element it is in)
* The second saveplace is the elementvariant itselv. The intern way to save.
* There we store which resoucess affect this variant.
*
* @param cms The cms object.
* @param templateName.
* @param elementName only needed for getCachDirectives, if it is not used there it may be null
* @param templateSelector only needed for getCachDirectives, if it is not used there it may be null
* @param parameters.
* @param vfsDeps A vector (of CmsResource objects) with the resources that variant depends on.
* @param cosDeps A vector (of CmsContentDefinitions) with the cd-resources that variant depends on.
* @param cosClassDeps A vector (of Class objects) with the contentdefinitions that variant depends on.
*/
protected void registerVariantDeps(CmsObject cms, String templateName, String elementName,
String templateSelector, Hashtable parameters, Vector vfsDeps,
Vector cosDeps, Vector cosClassDeps) throws CmsException {
String cacheKey = getCacheDirectives(cms, templateName, elementName,
parameters, templateSelector).getCacheKey(cms, parameters);
if(cms.getRequestContext().isElementCacheEnabled() && (cacheKey != null) &&
(cms.getRequestContext().currentProject().isOnlineProject()) ) {
boolean exportmode = cms.getMode() == CmsObject.C_MODUS_EXPORT;
Hashtable externVarDeps = cms.getVariantDependencies();
long exTimeForVariant = Long.MAX_VALUE;
long now = System.currentTimeMillis();
// this will be the entry for the extern hashtable
String variantEntry = getClass().getName() + "|"+ templateName +"|"+ cacheKey;
// the vector for the intern variant store. it contains the keys for the extern Hashtable
Vector allDeps = new Vector();
// first the dependencies for the cos system
if(cosDeps != null){
for (int i = 0; i < cosDeps.size(); i++){
A_CmsContentDefinition contentDef = (A_CmsContentDefinition)cosDeps.elementAt(i);
String key = cms.getSiteName() + CmsObject.C_ROOTNAME_COS + "/"+contentDef.getClass().getName() +"/"+contentDef.getUniqueId(cms);
if(exportmode){
cms.getRequestContext().addDependency(key);
}
allDeps.add(key);
if(contentDef.isTimedContent()){
long time = ((I_CmsTimedContentDefinition)cosDeps.elementAt(i)).getPublicationDate();
if (time > now && time < exTimeForVariant){
exTimeForVariant = time;
}
time = ((I_CmsTimedContentDefinition)cosDeps.elementAt(i)).getPurgeDate();
if (time > now && time < exTimeForVariant){
exTimeForVariant = time;
}
time = ((I_CmsTimedContentDefinition)cosDeps.elementAt(i)).getAdditionalChangeDate();
if (time > now && time < exTimeForVariant){
exTimeForVariant = time;
}
}
}
}
// now for the Classes
if(cosClassDeps != null){
for(int i=0; i<cosClassDeps.size(); i++){
String key = cms.getSiteName() + CmsObject.C_ROOTNAME_COS + "/" + ((Class)cosClassDeps.elementAt(i)).getName() +"/";
allDeps.add(key);
if(exportmode){
cms.getRequestContext().addDependency(key);
}
}
}
// now for the vfs
if(vfsDeps != null){
for(int i = 0; i < vfsDeps.size(); i++){
allDeps.add(((CmsResource)vfsDeps.elementAt(i)).getResourceName());
if(exportmode){
cms.getRequestContext().addDependency(((CmsResource)vfsDeps.elementAt(i)).getResourceName());
}
}
}
// now put them all in the extern store
for(int i=0; i<allDeps.size(); i++){
String key = (String)allDeps.elementAt(i);
Vector variantsForDep = (Vector)externVarDeps.get(key);
if (variantsForDep == null){
variantsForDep = new Vector();
}
if(!variantsForDep.contains(variantEntry)){
variantsForDep.add(variantEntry);
}
externVarDeps.put(key, variantsForDep);
}
// at last we have to fill the intern store. that means we have to
// put the alldeps vector in our variant that will be created later
// in the startproccessing method.
// Get current element.
CmsElementCache elementCache = cms.getRequestContext().getElementCache();
CmsElementDescriptor elKey = new CmsElementDescriptor(getClass().getName(), templateName);
A_CmsElement currElem = elementCache.getElementLocator().get(cms, elKey, parameters);
// add an empty variant with the vector to the element
CmsElementVariant emptyVar = new CmsElementVariant();
emptyVar.addDependencies(allDeps);
if(exTimeForVariant < Long.MAX_VALUE ){
emptyVar.mergeNextTimeout(exTimeForVariant);
}
Vector removedVar = currElem.addVariant(cacheKey, emptyVar);
if((removedVar != null) ){
// adding a new variant deleted this variant so we have to update the extern store
String key = (String)removedVar.firstElement();
CmsElementVariant oldVar = (CmsElementVariant)removedVar.lastElement();
Vector oldVarDeps = oldVar.getDependencies();
if (oldVarDeps != null){
String oldVariantEntry = getClass().getName() + "|"+ templateName +"|"+ key;
for(int i=0; i<oldVarDeps.size(); i++){
Vector externEntrys = (Vector)externVarDeps.get(oldVarDeps.elementAt(i));
if(externEntrys != null){
externEntrys.removeElement(oldVariantEntry);
}
}
}
}
// mark this element so it wont be deleted without updating the extern store
currElem.thisElementHasDepVariants();
}
}
/**
* Starts the processing of the given template file by calling the
* <code>getProcessedTemplateContent()</code> method of the content defintition
* of the corresponding content type.
* <P>
* Any exceptions thrown while processing the template will be caught,
* printed and and thrown again.
* <P>
* If element cache is enabled, <code>generateElementCacheVariant()</code>
* will be called instead of <code>getProcessedTemplateContent()</code> for
* generating a new element cache variant instead of the completely
* processed output data.
* This new variant will be stored in the current element using the cache key
* given by the cache directives.
*
* @param cms CmsObject Object for accessing system resources.
* @param xmlTemplateDocument XML parsed document of the content type "XML template file" or
* any derived content type.
* @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 Content of the template and all subtemplates.
* @throws CmsException
*/
protected byte[] startProcessing(CmsObject cms, CmsXmlTemplateFile xmlTemplateDocument, String elementName, Hashtable parameters, String templateSelector) throws CmsException {
byte[] result = null;
if(cms.getRequestContext().isElementCacheEnabled()) {
CmsElementDefinitionCollection mergedElDefs = (CmsElementDefinitionCollection)parameters.get("_ELDEFS_");
// We are in element cache mode. Create a new variant instead of a completely processed subtemplate
CmsElementVariant variant = xmlTemplateDocument.generateElementCacheVariant(this, parameters, elementName, templateSelector);
// Get current element.
CmsElementCache elementCache = cms.getRequestContext().getElementCache();
CmsElementDescriptor elKey = new CmsElementDescriptor(getClass().getName(), xmlTemplateDocument.getAbsoluteFilename());
A_CmsElement currElem = elementCache.getElementLocator().get(cms, elKey, parameters);
// If this elemement is cacheable, store the new variant
if(currElem.getCacheDirectives().isInternalCacheable()) {
//currElem.addVariant(getKey(cms, xmlTemplateDocument.getAbsoluteFilename(), parameters, templateSelector), variant);
Vector removedVar = currElem.addVariant(currElem.getCacheDirectives().getCacheKey(cms, parameters), variant);
if((removedVar != null) && currElem.hasDependenciesVariants()){
// adding a new variant deleted this variant so we have to update the extern dependencies store
String key = (String)removedVar.firstElement();
CmsElementVariant oldVar = (CmsElementVariant)removedVar.lastElement();
Vector oldVarDeps = oldVar.getDependencies();
if (oldVarDeps != null){
String oldVariantEntry = getClass().getName() + "|"+ xmlTemplateDocument.getAbsoluteFilename() +"|"+ key;
for(int i=0; i<oldVarDeps.size(); i++){
Vector externEntrys = (Vector)cms.getVariantDependencies().get(oldVarDeps.elementAt(i));
if(externEntrys != null){
externEntrys.removeElement(oldVariantEntry);
}
}
}
}
}
result = ((CmsElementXml)currElem).resolveVariant(cms, variant, elementCache, mergedElDefs, parameters);
} else {
// Classic way. Element cache is not activated, so let's genereate the template as usual
// Try to process the template file
try {
//result = xmlTemplateDocument.getProcessedTemplateContent(this, parameters, templateSelector).getBytes();
result = xmlTemplateDocument.getProcessedTemplateContent(this, parameters, templateSelector).getBytes(
cms.getRequestContext().getEncoding());
}
catch(Throwable e) {
// There were errors while generating output for this template.
// Clear HTML cache and then throw exception again
xmlTemplateDocument.removeFromFileCache();
if(isCacheable(cms, xmlTemplateDocument.getAbsoluteFilename(), elementName, parameters, templateSelector)) {
m_cache.clearCache(getKey(cms, xmlTemplateDocument.getAbsoluteFilename(), parameters, templateSelector));
}
if(e instanceof CmsException) {
throw (CmsException)e;
}
else {
// under normal cirumstances, this should not happen.
// any exception should be caught earlier and replaced by
// corresponding CmsExceptions.
String errorMessage = "Exception while getting content for (sub)template " + elementName + ". " + e;
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
A_OpenCms.log(C_OPENCMS_CRITICAL, getClassName() + errorMessage);
}
throw new CmsException(errorMessage);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -