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

📄 cmsxmltemplate.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/template/CmsXmlTemplate.java,v $
* Date   : $Date: 2002/05/08 07:29:20 $
* Version: $Revision: 1.95 $
*
* 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 java.util.*;
import java.io.*;
import com.opencms.launcher.*;
import com.opencms.file.*;
import com.opencms.util.*;
import com.opencms.defaults.*;
import com.opencms.core.*;
import com.opencms.template.cache.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.servlet.http.*;

/**
 * Template class for displaying the processed contents of hierachical XML template files
 * that can include other subtemplates.
 *
 * @author Alexander Lucas
 * @version $Revision: 1.95 $ $Date: 2002/05/08 07:29:20 $
 */
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 = "body";

    /** 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!";

    /**
     * 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.
     * @exception 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 cms, String templateFile, String elementName, Hashtable parameters)
     * @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.
     * @exception 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.
     * @exception 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.
     * @exception 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.
     * @exception 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) {
                    cmsframe = query.substring(start + 9, end);
                }
                else {
                    cmsframe = query.substring(start + 9);
                }
                if(!cmsframe.equals("plain")) {
                    if(!frame.equals("")) {
                        if(end != -1) {
                            query = query.substring(0, start + 9) + frame + query.substring(end);
                        }
                        else {
                            query = query.substring(0, start + 9) + frame;
                        }
                    }
                    else {
                        if(end != -1) {
                            query = query.substring(0, start) + query.substring(end + 1);
                        }
                        else {
                            query = query.substring(0, start);

⌨️ 快捷键说明

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