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

📄 cmsshowcontent.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
* 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.defaults;

import org.opencms.file.CmsObject;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;

import com.opencms.legacy.CmsLegacyException;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.template.CmsCacheDirectives;
import com.opencms.template.CmsXmlTemplate;
import com.opencms.template.CmsXmlTemplateFile;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * Generic class for showing information encapsulated by contentdefinition objects.
 * There are mainly two user methods in this template class for this purpose:
 * getEntry and getList. The getEntry method accepts an id or read it from
 * the url parameters (it has to be named id to be found) if given
 * and set datablocks inside the template with
 * values returned by all public get-methods of the contentdefinition that
 * returns a String and have no parameters. This method must be called in the
 * template before acccessing any of this data with process tags.
 * The getList method builds a list of contentdefinition entries. It invokes
 * a filtermethod defined inside a datablock named <filtermethod>.
 * The user parameter of type String passed to the filtermethod is taken from
 * the url parameters or from a default value. The getList method accepts a name
 * and a defaut value in a comma seperated String in this way : name, defaultvalue.
 * The entrys for the list will be filled by appending a processed datablock
 * with the name listentry. Every get-method will be invoked for every entry
 * unless a tag named <method> is given where a commaseperated list of
 * exactly typed get-method names can be given that results in invoking only
 * this methods (for performance improvement if not all values are needed and
 * many get-methods are given). The filtermethod is assumed to have the following
 * signature: filtermehodname(CmsObject, String). If one wants to use filtermethods
 * with other signatures he has to derive from this class and override the
 * method invokeFilterMethod where the filtermethod is actually invoked.
 * For both methods the fully qualified classname of the contentdefinition
 * has to be stated in a datablock with the name <contentdefinition_class>.
 * Note: the method getUniqueId(CmsObject) will be invoked for the contentdefinition
 * class in any case, and the datablock uniqueid can be used in the template to refer
 * to the id of an contentdefinition object
 * @author Michael Dernen
 *
 * changed: the tagcontent parameter for getList may only use filterparameter0 to
 *          filterparameter9.
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */
public class CmsShowContent extends CmsXmlTemplate {

    /**
     *  The name of the datablock containing the contentdefinition's fully qualified classname.
     */
     protected static final String C_CONTENTDEFINITION_CLASS_DATABLOCK =
        "contentdefinition_class";

    /**
     *  The name of the datablock containing the name of the filtermethod to use.
     */
     protected static final String C_FILTERMETHOD_DATABLOCK =
        "filtermethod";

    /**
     * replaces the tagcontent for usermethod "getList"
     */
    protected static final String C_FILTER_PARAMETERS_START = "filterparameter";

    /**
     *  The name of the datablock to define which get-methods to invoke.
     *  If a datablock with this name is not given all get-methods will be invoked.
     */
     protected static final String C_METHODS_TO_USE_DATABLOCK =
        "methods";

    /**
     * The name of the datablock to define a listentry.
     */
     protected static final String C_LISTENTRY_DATABLOCK = "listentry";

     /**
     * Error message for a missing id parameter.
     */
     protected static final String C_MISSING_ID_PARAMETER =
        "Missing id parameter: "
        + "You have to provide a parameter id (as tagcontent or url parameter) to specify the "
        + "dataentry you want to show";

    /**
     * Error message if the constructor in case the contentdefinition class throws an exception.
     */
     protected static final String C_CONSTRUCTOR_THROWED_EXCEPTION =
        "Failed to create contentdefinition object: "
        + "The constructor of the contentdefinition class throwed an exception";

    /**
     * Error message in case the constructor is not a subclass of A_CmsContentDefinition.
     */
     protected static final String C_CONSTRUCTOR_IS_NOT_SUBCLASS_OF_A_CMSCONTENTDEFINITION =
        "Your contentdefinition class is not a subclass of the abstract A_CmsContentDefinition class";

    /**
     * Error message in case a non-numerical id parameter is given.
     */
     protected static final String C_NON_NUMERICAL_ID_PARAMETER =
        "The parameter id is not a numerical value";

    /**
    * Error text that will be set inside the template if a get-method throws an exception.
    */
    protected static final String C_ERROR_TEXT = "ERROR";

    /**
     * The name of the id parameter.
     */
     protected static final String C_ID_PARAMETER = "id";

    /**
     * Usermethod to fill template datablocks with values of a contentdefinition's get-methods.
     * @param cms CmsObject Object for accessing system resources.
     * @param tagcontent the tagcontent passed to the method. This can be an id
     * value which will be used if the parameter id is not found parameters passed
     * with the request (url parameters).
     * @param doc reference to the A_CmsXmlContent object of the initiating XLM document
     * @param userObject normally the Hashtable with the url parameters
     * @return String or byte[] with the content of this subelement
     * @throws org.opencms.main.CmsException in case of unrecoverable errors
     */
    public Object getEntry(CmsObject cms, String tagcontent,
            A_CmsXmlContent doc, Object userObject) throws CmsException {

        CmsXmlTemplateFile template = (CmsXmlTemplateFile)doc;

        String paramId = null;
        if (tagcontent != null && !tagcontent.trim().equals("")) {
            // if a tagcontent is given take it as the id of the entry to be shown
            paramId = tagcontent;
        } else {
            // get the id of the entry to be shown from the url parameters
            paramId = (String)((Hashtable)userObject).get(C_ID_PARAMETER);
        }
        Integer id = null;
        if (paramId != null) {
            // try to convert the id to an Integer object
            try {
                id = new Integer(paramId);
            } catch (NumberFormatException e) {
                // throw exception if the id is not numeric
                throw new CmsLegacyException(C_NON_NUMERICAL_ID_PARAMETER, CmsLegacyException.C_UNKNOWN_EXCEPTION);
            }
        } else {
            // throw exception if id is not given (not as a tagcontent or url parameter)
            throw new CmsLegacyException(C_MISSING_ID_PARAMETER);
        }
        // first get the contentdefinition's classname from a datablock
        String cdClassname = template.getDataValue(C_CONTENTDEFINITION_CLASS_DATABLOCK);
        try {
            ArrayList getMethods = null;
            // try to get class object might throws ClassNotFoundException
            Class cdClass = Class.forName(cdClassname);
            // might throws NoSuchMethodException
            Constructor constructor = cdClass.getConstructor(new Class[] {CmsObject.class, Integer.class});
            // might throws InvocationTargetException, ClassCastException
            A_CmsContentDefinition cdObject = (A_CmsContentDefinition)constructor.newInstance(new Object[]{cms, id});
            // register the CD for the variantdependencies
            Vector cdVec = new Vector();
            cdVec.add(cdObject);
            registerVariantDeps(cms, doc.getAbsoluteFilename(), null, null,
                                (Hashtable)userObject, null, cdVec, null);
            boolean showIt = true;
            if (cdObject.isTimedContent()) {
                I_CmsTimedContentDefinition curTimed = (I_CmsTimedContentDefinition)cdObject;
                long currentTime = System.currentTimeMillis();
                if (((curTimed.getPublicationDate() != 0) && (currentTime < curTimed.getPublicationDate()))
                        || ((curTimed.getPurgeDate() != 0) && (currentTime > curTimed.getPurgeDate()))) {
                    showIt = false;
                }
            }
            if (!showIt) {
                //  TODO: read an datablock from the template and set all the proccesstags with it then remove this exception
                throw new CmsLegacyException("requested content is not valid.");
            }
            if (template.hasData(C_METHODS_TO_USE_DATABLOCK)) {
                // if the datablock methods is set inside the template
                // only take the methods that are listed in this datablock
                // (the methods should be listed as a comma seperated list of names)
                String datablockContent = template.getDataValue(C_METHODS_TO_USE_DATABLOCK);
                StringTokenizer tokenizer = new StringTokenizer(datablockContent, ",");
                int tokens = tokenizer.countTokens();
                String[] names = new String[tokens];
                for (int i=0; i < tokens; i++) {
                    names[i] = tokenizer.nextToken().trim();
                }
                getMethods = getGetMethodsByName(cdClass, names);
            } else {
                // get all public getMethods that return a String and have no parameters
                getMethods = getGetMethods(cdClass);
            }
            try {
                Method getUniqueIdMethod = cdClass.getMethod("getUniqueId", new Class[] {CmsObject.class});
                template.setData("uniqueid", (String)getUniqueIdMethod.invoke(cdObject, new Object[] {cms}));
            } catch (Exception e) { }
            setDatablocks(template, cdObject, getMethods);
        } catch (InvocationTargetException e) {
            // the constructor has throwed an exception, InvocationTargetExceptions of the egt-methods
            // will be catched inside the setDatablocks method and cannot propagate to this point
            throw new CmsLegacyException(C_CONSTRUCTOR_THROWED_EXCEPTION, e.getTargetException());
        } catch (ClassCastException e) {
            // in this case the cast to A_CmsContentDefinition failed
            throw new CmsLegacyException(C_CONSTRUCTOR_IS_NOT_SUBCLASS_OF_A_CMSCONTENTDEFINITION, e);
        } catch (Exception e) {
            // rethrow any other exception
            if (e instanceof CmsException) {
                throw (CmsException)e;
            } else {
                // encapsulate in CmsException
                throw new CmsLegacyException (e.getMessage(), e);
            }
        }
        return "";
    }

    /**
     * User-method to build a list of contentdefinition entries.
     * @param cms CmsObject Object for accessing system resources.
     * @param tagcontent comma separated name, default value pair. The name

⌨️ 快捷键说明

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