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

📄 cmsxmltemplateloader.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/legacy/CmsXmlTemplateLoader.java,v $
 * Date   : $Date: 2005/06/27 23:27:46 $
 * Version: $Revision: 1.15 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2002  Alkacon Software (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.opencms.legacy;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsRequestContext;
import org.opencms.file.CmsResource;
import org.opencms.flex.CmsFlexController;
import org.opencms.importexport.CmsCompatibleCheck;
import org.opencms.jsp.CmsJspTagInclude;
import org.opencms.loader.I_CmsLoaderIncludeExtension;
import org.opencms.loader.I_CmsResourceLoader;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.staticexport.CmsLinkManager;
import org.opencms.workplace.CmsWorkplace;
import org.opencms.workplace.editors.CmsDefaultPageEditor;

import com.opencms.core.*;
import com.opencms.template.*;
import com.opencms.template.cache.*;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.ExtendedProperties;

/**
 * Implementation of the {@link I_CmsResourceLoader} for XMLTemplates.<p>
 * 
 * Parameters supported by this loader:<dl>
 * 
 * <dt>elementcache.enabled</dt>
 * <dd>(Optional) Controls if the legacy XMLTemplate element cache is disabled 
 * (the default) or enabled. Enable the element cache only to support old
 * XMLTemplate based code that depend on specific element cache behaviour.</dd>
 * 
 * <dt>elementcache.uri</dt>
 * <dd>(Optional) Element cache URI size. The default is 10000.</dd>
 * 
 * <dt>elementcache.elements</dt>
 * <dd>(Optional) Element cache element size. The default is 50000.</dd>
 * 
 * <dt>elementcache.variants</dt>
 * <dd>(Optional) Element cache variant size. The default is 100.</dd></dl>
 * 
 * @author  Alexander Kandzior (a.kandzior@alkacon.com)
 *
 * @version $Revision: 1.15 $
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */
public class CmsXmlTemplateLoader implements I_CmsResourceLoader, I_CmsLoaderIncludeExtension {
    
    /** Magic element replace name. */
    public static final String C_ELEMENT_REPLACE = "_CMS_ELEMENTREPLACE";
    
    /** URI of the bodyloader XML file in the OpenCms VFS. */    
    public static final String C_BODYLOADER_URI = CmsWorkplace.VFS_PATH_SYSTEM + "shared/bodyloader.html";
            
    /** The id of this loader. */
    public static final int C_RESOURCE_LOADER_ID = 3;

    /** Flag for debugging output. Set to 9 for maximum verbosity. */ 
    private static final int DEBUG = 0;

    /** The element cache used for the online project. */
    private static CmsElementCache m_elementCache;

    /** The template cache that holds all cached templates. */
    private static I_CmsTemplateCache m_templateCache;
    
    /** The variant dependencies for the element cache. */
    private static Hashtable m_variantDeps;
    
    /** The resource loader configuration. */
    private Map m_configuration;

    /**
     * The constructor of the class is empty and does nothing.
     */
    public CmsXmlTemplateLoader() {
        m_templateCache = new CmsTemplateCache();
        m_configuration = new TreeMap();
    }
    
    /**
     * Compatibility method to ensure the legacy cache command line parameters
     * are still supported.<p>
     * 
     * @param clearFiles if true, A_CmsXmlContent cache is cleared
     * @param clearTemplates if true, internal template cache is cleared
     */
    private static void clearLoaderCache(boolean clearFiles, boolean clearTemplates) {
        if (clearFiles) {
            A_CmsXmlContent.clearFileCache();
        }
        if (clearTemplates) {
            m_templateCache.clearCache();
        }        
    }

    /**
     * Returns the element cache,
     * or null if the element cache is not initialized.<p>
     * 
     * @return the element cache that belongs to the given cms context
     */        
    public static final CmsElementCache getElementCache() {
        return m_elementCache;
    }

    /**
     * Returns the variant dependencies of the online element cache.<p>
     * 
     * @return the variant dependencies of the online element cache
     */
    public static final CmsElementCache getOnlineElementCache() {
        return m_elementCache;
    }
    
    /**
     * Returns the hashtable with the variant dependencies used for the elementcache.<p>
     * 
     * @return the hashtable with the variant dependencies used for the elementcache
     */
    public static final Hashtable getVariantDependencies() {
        return m_variantDeps;
    }    
    
    /**
     * Returns true if the element cache is enabled.<p>
     * 
     * @return true if the element cache is enabled
     */
    public static final boolean isElementCacheEnabled() {
        return m_elementCache != null;
    }

    /**
     * Utility method used by the loader implementation to give control
     * to the CanonicalRoot.<p>
     * 
     * The CanonicalRoot will call the master template and return a byte array of the
     * generated output.<p>
     *
     * @param cms the cms context object
     * @param templateClass to generate the output of the master template
     * @param masterTemplate masterTemplate for the output
     * @param parameters contains all parameters for the template class
     * @return the generated output or null if there were errors
     * @throws CmsException if something goes wrong
     */
    private byte[] callCanonicalRoot(CmsObject cms, I_CmsTemplate templateClass, CmsFile masterTemplate, Hashtable parameters) throws CmsException {
        try {
            CmsRootTemplate root = new CmsRootTemplate();
            return root.getMasterTemplate(cms, templateClass, masterTemplate, m_templateCache, parameters);
        } catch (Exception e) {
            // no document we could show...
            handleException(cms, e, "Received error while calling canonical root for requested file " + masterTemplate.getName() + ". ");
        }
        return null;
    }
        
    /** 
     * Destroy this ResourceLoder, this is a NOOP so far.  
     */
    public void destroy() {
        // NOOP
    }

    /**
     * @see org.opencms.loader.I_CmsResourceLoader#export(org.opencms.file.CmsObject, org.opencms.file.CmsResource, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public byte[] export(CmsObject cms, CmsResource resource, HttpServletRequest req, HttpServletResponse res) 
    throws IOException, CmsException {
        
        CmsFile file = CmsFile.upgrade(resource, cms);
        initLegacyRequest(cms, req, res);
        CmsRequestHttpServlet cmsReq = new CmsRequestHttpServlet(req, cms.getRequestContext().getFileTranslator());
        return generateOutput(cms, file, cmsReq);
    }        
    
    /**
     * Starts generating the output.
     * Calls the canonical root with the appropriate template class.
     *
     * @param cms CmsObject Object for accessing system resources
     * @param file CmsFile Object with the selected resource to be shown
     * @param req the CmsRequest
     * @return the generated output for the file
     * @throws CmsException if something goes wrong
     */
    protected byte[] generateOutput(CmsObject cms, CmsFile file, I_CmsRequest req) throws CmsException {
        byte[] output = null;

        // hashtable for collecting all parameters.
        Hashtable newParameters = new Hashtable();
        String uri = cms.getRequestContext().getUri();

        // collect xml template information
        String absolutePath = cms.getSitePath(file);
        if (CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("absolutePath=" + absolutePath);
        }
        String templateProp = cms.readPropertyObject(absolutePath, CmsPropertyDefinition.PROPERTY_TEMPLATE, false).getValue();
        if (CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("templateProp=" + templateProp);
        }
        String templateClassProp = cms.readPropertyObject(absolutePath, org.opencms.file.CmsPropertyDefinition.PROPERTY_BODY_CLASS, false).getValue(org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS);
        if (CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("templateClassProp=" + templateClassProp);
        }
        
        // ladies and gentelman: and now for something completly different 
        String xmlTemplateContent = null;
        if (templateProp != null) {
            // i got a black magic template,
            StringBuffer buf = new StringBuffer(256);
            buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
            buf.append("<PAGE>\n<class>");
            buf.append(org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS);
            buf.append("</class>\n<masterTemplate>");
            // i got a black magic template,
            buf.append(templateProp);
            buf.append("</masterTemplate>\n<ELEMENTDEF name=\"body\">\n<CLASS>");
            buf.append(templateClassProp);
            buf.append("</CLASS>\n<TEMPLATE>");
            // i got a black magic template got me so blind I can't see,
            buf.append(uri);
            buf.append("</TEMPLATE>\n</ELEMENTDEF>\n</PAGE>\n");              
            // i got a black magic template it's try'in to make a devil out of me...
            xmlTemplateContent = buf.toString();
            uri += com.opencms.core.I_CmsConstants.C_XML_CONTROL_FILE_SUFFIX; 
        }
        
        // Parameters used for element cache
        boolean elementCacheEnabled = CmsXmlTemplateLoader.isElementCacheEnabled();
        CmsElementCache elementCache = null;
        CmsUriDescriptor uriDesc = null;
        CmsUri cmsUri = null;

        String templateClass = null;
        String templateName = null;
        CmsXmlControlFile doc = null;

        if (elementCacheEnabled) {
            // Get the global element cache object
            elementCache = CmsXmlTemplateLoader.getElementCache();

            // Prepare URI Locator
            uriDesc = new CmsUriDescriptor(uri);
            cmsUri = elementCache.getUriLocator().get(uriDesc);
            // check if cached
            if (CmsLog.getLog(this).isDebugEnabled()) {
                CmsLog.getLog(this).debug("found cmsUri=" + cmsUri);
        }
            if ((cmsUri != null) && !cmsUri.getElementDescriptor().getTemplateName().equalsIgnoreCase(templateProp)) {
                if (CmsLog.getLog(this).isDebugEnabled()) {
                    CmsLog.getLog(this).debug("cmsUri has different template: " + cmsUri.getElementDescriptor().getTemplateName()
                            + " than current template: " + templateProp + ", not using cmsUri from cache");
                }
                cmsUri = null;
            }            
        }

        // check if printversion is requested
        String replace = req.getParameter(C_ELEMENT_REPLACE);
        boolean elementreplace = false;
        CmsElementDefinition replaceDef = null;
        if (replace != null) {
            int index = replace.indexOf(":");
            if (index != -1) {
                elementreplace = true;
                cmsUri = null;
                String elementName = replace.substring(0, index);
                String replaceUri = replace.substring(index+1);               
                replaceDef = new CmsElementDefinition(elementName,
                    org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS,
                                        replaceUri, null, new Hashtable());
                newParameters.put(C_ELEMENT_REPLACE + "_VFS_" + elementName, cms.getRequestContext().addSiteRoot(replaceUri));
            }

⌨️ 快捷键说明

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