⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmsxmltemplate.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    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() + cms.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.
     * @exception 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, elementName, 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();
            }
            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);
                }
            }
        }
        // update the template selector if nescessary
        if (templateSelector!=null) {
          parameters.put(elementName+"._TEMPLATESELECTOR_",templateSelector);
        }
        return result;
    }

    /**
     * Handles any occurence of an <code>&lt;ELEMENT&gt;</code> tag.
     * <P>
     * Every XML template class should use CmsXmlTemplateFile as
     * the interface to the XML file. Since CmsXmlTemplateFile is
     * an extension of A_CmsXmlContent by the additional tag
     * <code>&lt;ELEMENT&gt;</code> this user method ist mandatory.
     *
     * @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 of the initiating XLM document.
     * @param userObj Hashtable with parameters.
     * @return String or byte[] with the content of this subelement.
     * @exception CmsException
     */
    public Object templateElement(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject) throws CmsException {

        // Our own template file that wants to include a subelement
        CmsXmlTemplateFile templateFile = (CmsXmlTemplateFile)doc;

        // Indicates, if this is a request of a guest user. Needed for error outputs.
        boolean isAnonymousUser = cms.anonymousUser().equals(cms.getRequestContext().currentUser());

        // First create a copy of the parameter hashtable
        Hashtable parameterHashtable = (Hashtable)((Hashtable)userObject).clone();

        // Name of the template class that should be used to handle the subtemplate
        String templateClass = getTemplateClassName(tagcontent, templateFile, parameterHashtable);

        // Name of the subtemplate file.
        String templateFilename = getTemplateFileName(tagcontent, templateFile, parameterHashtable);

        // Name of the subtemplate template selector
        String templateSelector = getTemplateSelector(tagcontent, templateFile, parameterHashtable);

        // Results returned by the subtemplate class
        byte[] result = null;

        // Temporary object for loading the subtemplate class
        Object loadedObject = null;

        // subtemplate class to be used for the include
        I_CmsTemplate subTemplate = null;

        // Key for the cache
        Object subTemplateKey = null;

        // try to load the subtemplate class
        try {
            loadedObject = CmsTemplateClassManager.getClassInstance(cms, templateClass);
        }
        catch(CmsException e) {

            // There was an error. First remove the template file from the file cache
            templateFile.removeFromFileCache();
            if(isAnonymousUser) {

                // The current user is the anonymous user
                return C_ERRORTEXT;
            }
            else {
                // The current user is a system user, so we throw the exception again.
                throw e;
            }
        }

        // Check if the loaded object is really an instance of an OpenCms template class
        if(!(loadedObject instanceof I_CmsTemplate)) {
            String errorMessage = "Class " + templateClass + " is no OpenCms template class.";
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(C_OPENCMS_CRITICAL, "[CmsXmlTemplate] " + errorMessage);
            }
            throw new CmsException(errorMessage, CmsException.C_XML_NO_TEMPLATE_CLASS);
        }
        subTemplate = (I_CmsTemplate)loadedObject;

        // Template class is now loaded. Next try to read the parameters
        Vector parameterTags = null;
        parameterTags = templateFile.getParameterNames(tagcontent);
        if(parameterTags != null) {
            int numParameterTags = parameterTags.size();
            for(int i = 0;i < numParameterTags;i++) {
                String paramName = (String)parameterTags.elementAt(i);
                String paramValue = templateFile.getParameter(tagcontent, paramName);
                if(!parameterHashtable.containsKey(paramName)) {
                    parameterHashtable.put(tagcontent + "." + paramName, paramValue);
                }
            }
        }

        // all parameters are now parsed. Finally give the own subelement name
        // as parameter
        // TODO: replace _ELEMENT_ by a constant
        parameterHashtable.put("_ELEMENT_", tagcontent);

        // Try to get the result from the cache
        //if(subTemplate.isCacheable(cms, templateFilename, tagcontent, parameterHashtable, null)) 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -