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

📄 cmsxmltemplate.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/template/CmsXmlTemplate.java,v $
* Date   : $Date: 2003/03/02 18:43:55 $
* Version: $Revision: 1.110 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* 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 OpenCms, please see the
* OpenCms 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.template;

import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.defaults.A_CmsContentDefinition;
import com.opencms.defaults.I_CmsTimedContentDefinition;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsRequestContext;
import com.opencms.file.CmsResource;
import com.opencms.launcher.I_CmsTemplateCache;
import com.opencms.template.cache.A_CmsElement;
import com.opencms.template.cache.CmsElementCache;
import com.opencms.template.cache.CmsElementDefinition;
import com.opencms.template.cache.CmsElementDefinitionCollection;
import com.opencms.template.cache.CmsElementDescriptor;
import com.opencms.template.cache.CmsElementVariant;
import com.opencms.template.cache.CmsElementXml;
import com.opencms.util.Encoder;
import com.opencms.util.Utils;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;

/**
 * Template class for displaying the processed contents of hierachical XML template files
 * that can include other subtemplates.
 *
 * @author Alexander Lucas
 * @version $Revision: 1.110 $ $Date: 2003/03/02 18:43:55 $
 */
public class CmsXmlTemplate extends A_CmsTemplate implements I_CmsXmlTemplate {
    public static final String C_FRAME_SELECTOR = "cmsframe";

    /** name of the special body element */
    public final static String C_BODY_ELEMENT = I_CmsConstants.C_XML_BODY_ELEMENT;

    /** Boolean for additional debug output control */
    public final static boolean C_DEBUG = false;

    /** Error string to be inserted for corrupt subtemplates for guest user requests. */
    private final static String C_ERRORTEXT = "ERROR!";
    
    /** Element descriptior */
    private static final String C_ELEMENT = "_ELEMENT_";

    /**
     * Template cache for storing cacheable results of the subtemplates.
     */
    protected static com.opencms.launcher.I_CmsTemplateCache m_cache = null;

    /**
     * For debugging purposes only.
     * Counts the number of re-uses od the instance of this class.
     */
    private int counter = 0;

    /**
     * For debugging purposes only.
     * Increments the class variable <code>counter</code> and
     * prints out its new value..
     * <P>
     * May be called from the template file using
     * <code>&lt;METHOD name="counter"&gt;</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 Actual value of <code>counter</code>.
     */
    public Integer counter(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject) throws CmsException {
        counter++;
        return new Integer(counter);
    }

    /**
     * Help method to print nice classnames in error messages
     * @return class name in [ClassName] format
     */
    protected String getClassName() {
        String name = getClass().getName();
        return "[" + name.substring(name.lastIndexOf(".") + 1) + "] ";
    }

    /**
     * Gets the content of a given template file and its subtemplates
     * with the given parameters. The default section in the template file
     * will be used.
     * <P>
     * Parameters are stored in a hashtable and can derive from
     * <UL>
     * <LI>Template file of the parent template</LI>
     * <LI>Body file clicked by the user</LI>
     * <LI>URL parameters</LI>
     * </UL>
     * Paramter names must be in "elementName.parameterName" format.
     *
     * @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.
     * @return Content of the template and all subtemplates.
     * @throws CmsException
     */
    public byte[] getContent(CmsObject cms, String templateFile, String elementName, Hashtable parameters) throws CmsException {
        return getContent(cms, templateFile, elementName, parameters, null);
    }

    /**
     * Gets the content of a defined section in a given template file and its subtemplates
     * with the given parameters.
     *
     * @see #getContent(CmsObject, String, String, Hashtable, String)
     * @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.
     */
    public byte[] getContent(CmsObject cms, String templateFile, String elementName, Hashtable parameters, String templateSelector) throws CmsException {
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() && C_DEBUG ) {
            A_OpenCms.log(C_OPENCMS_DEBUG, "[CmsXmlTemplate] getting content of element " + ((elementName == null) ? "<root>" : elementName));
            A_OpenCms.log(C_OPENCMS_DEBUG, "[CmsXmlTemplate] template file is: " + templateFile);
            A_OpenCms.log(C_OPENCMS_DEBUG, "[CmsXmlTemplate] selected template section is: " + ((templateSelector == null) ? "<default>" : templateSelector));
        }
        CmsXmlTemplateFile xmlTemplateDocument = getOwnTemplateFile(cms, templateFile, elementName, parameters, templateSelector);
        if(templateSelector == null || "".equals(templateSelector)) {
            templateSelector = (String)parameters.get(C_FRAME_SELECTOR);
        }
        return startProcessing(cms, xmlTemplateDocument, elementName, parameters, templateSelector);
    }

    /**
     * @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.
     * @throws CmsException
     */
    public Object getFileUri(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject) throws CmsException {
        return cms.getRequestContext().getFileUri().getBytes();
    }

    /**
     * Returns the absolute path of a resource merged with the absolute path of the file and
     * the relative path in the tagcontent. This path is a intern OpenCms path (i.e. it starts
     * with a "/" ).
     *
     * @param cms CmsObject Object for accessing system resources.
     * @param tagcontent The relative path of the resource incl. name of the resource.
     * @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.
     * @throws CmsException
     */
    public Object mergeAbsolutePath(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject) throws CmsException {
        return Utils.mergeAbsolutePath(doc.getAbsoluteFilename(), tagcontent).getBytes();
    }

    /**
     * Returns the absolute path of a resource merged with the absolute path of the file and
     * the relative path in the tagcontent. This method adds the servlet path at the beginning
     * of the path.
     *
     * @param cms CmsObject Object for accessing system resources.
     * @param tagcontent The relative path of the resource incl. name of the resource.
     * @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.
     * @throws CmsException
     */
    public Object mergeAbsoluteUrl(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject) throws CmsException {
        String ocPath = new String((byte[])mergeAbsolutePath(cms,tagcontent, doc, userObject));
        String servletPath = cms.getRequestContext().getRequest().getServletUrl();
        return (servletPath + ocPath).getBytes();
    }
    /**
     * Gets the QueryString for CmsFrameTemplates.
     * <P>
     * This method can be called using <code>&lt;METHOD name="getCmsQueryString"&gt;</code>
     * in the template file.
     *
     * @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.
     * @throws CmsException
     */
    public Object getFrameQueryString(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject) throws CmsException {

        String query = new String();
        // get the parameternames of the original request and get the values from the userObject
        try{
            Enumeration parameters = ((HttpServletRequest)cms.getRequestContext().getRequest().getOriginalRequest()).getParameterNames();
            StringBuffer paramQuery = new StringBuffer();
            while(parameters.hasMoreElements()){
                String name = (String)parameters.nextElement();
                String value = (String)((Hashtable)userObject).get(name);
                if(value != null && !"".equals(value)){
                    paramQuery.append(name+"="+value+"&");
                }
            }
            if(paramQuery.length() > 0){
                // add the parameters to the query string
                query = paramQuery.substring(0,paramQuery.length()-1).toString();
            }
        } catch (Exception exc){
            exc.printStackTrace();
        }

        // get the name of the frame and parameters
        String frame = "", param = "";
        if(!tagcontent.equals("")) {
            if(!tagcontent.startsWith("&")) {
                if(tagcontent.indexOf(",") != -1) {
                    frame = tagcontent.substring(0, tagcontent.indexOf(","));
                    param = tagcontent.substring(tagcontent.indexOf(",") + 1);
                }
                else {
                    frame = tagcontent;
                }
            }
            else {
                param = tagcontent;
            }
        }
        query = (query == null ? "" : query);
        if(!query.equals("")) {
            if(query.indexOf("cmsframe=") != -1) {
                int start = query.indexOf("cmsframe=");
                int end = query.indexOf("&", start);
                String cmsframe = "";
                if(end != -1) {

⌨️ 快捷键说明

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