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

📄 cmsxmltemplate.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

    /**
     * @param cms CmsObject Object for accessing system resources.
     * @param tagcontent May contain the parameter for framesets to work in the static export.
     * @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 getUri(CmsObject cms, String tagcontent, A_CmsXmlContent doc, Object userObject)
    throws CmsException {

        String res = cms.getRequestContext().getUri();
        if (tagcontent == null || "".equals(tagcontent)) {
            return OpenCms.getLinkManager().substituteLink(cms, res).getBytes();
        } else {
            return OpenCms.getLinkManager().substituteLink(cms, res + "?" + tagcontent).getBytes();
        }
    }

    /**
     * @param cms CmsObject Object for accessing system resources.
     * @param tagcontent Contains the parameter for framesets.
     * @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 getUriWithParameter(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)CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).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 parameters in the tagcontent
        if ((tagcontent != null) && (!"".equals(tagcontent))) {
            if (tagcontent.startsWith("?")) {
                tagcontent = tagcontent.substring(1);
            }
            query = tagcontent + "&" + query;
        }
        return getUri(cms, query, doc, userObject);
    }

    /**
     * Indicates if the current template class is able to stream it's results
     * directly to the response oputput stream.
     * <P>
     * Classes must not set this feature, if they might throw special
     * exception that cause HTTP errors (e.g. 404/Not Found), or if they
     * might send HTTP redirects.
     * <p>
     * If a class sets this feature, it has to check the
     * isStreaming() property of the RequestContext. If this is set
     * to <code>true</code> the results must be streamed directly
     * to the output stream. If it is <code>false</code> the results
     * must not be streamed.
     * <P>
     * Complex classes that are able top include other subtemplates
     * have to check the streaming ability of their subclasses here!
     *
     * @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.
     * @return <EM>true</EM> if this class may stream it's results, <EM>false</EM> otherwise.
     */
    public boolean isStreamable(CmsObject cms, String templateFile, String elementName,
        Hashtable parameters, String templateSelector) {

        return true;
    }

    /**
     * Tests, if the template cache is setted.
     * @return <code>true</code> if setted, <code>false</code> otherwise.
     */
    public final boolean isTemplateCacheSet() {

        return m_cache != null;
    }

    /**
     * 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 userObject Hashtable with parameters.
     * @return String or byte[] with the content of this subelement.
     * @throws CmsException if something goes wrong
     */
    public Object mergeAbsolutePath(CmsObject cms, String tagcontent, A_CmsXmlContent doc,
        Object userObject) throws CmsException {

        return CmsLinkManager.getAbsoluteUri(tagcontent, doc.getAbsoluteFilename()).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 userObject Hashtable with parameters.
     * @return String or byte[] with the content of this subelement.
     * @throws CmsException if something goes wrong
     */
    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 = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl();
        return (servletPath + ocPath).getBytes();
    }

    /**
     * For debugging purposes only.
     * Prints out all parameters.
     * <P>
     * May be called from the template file using
     * <code>&lt;METHOD name="parameters"&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 Debugging information about all parameters.
     */
    public String parameters(CmsObject cms, String tagcontent, A_CmsXmlContent doc,
        Object userObject) {

        Hashtable param = (Hashtable)userObject;
        Enumeration keys = param.keys();
        String s = "";
        while (keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            s = s + "<B>" + key + "</B>: " + param.get(key) + "<BR>";
        }
        s = s + "<B>" + tagcontent + "</B><BR>";
        return s;
    }

    public Object setEncoding(CmsObject cms, String tagcontent, A_CmsXmlContent doc,
        Object userObject) throws CmsException {

        if ((tagcontent != null) && !"".equals(tagcontent)) {
            cms.getRequestContext().setEncoding(tagcontent.trim());
        }
        return "";
    }

    /**
     * Set the instance of template cache that should be used to store
     * cacheable results of the subtemplates.
     * If the template cache is not set, caching will be disabled.
     * @param c Template cache to be used.
     */
    public final void setTemplateCache(I_CmsTemplateCache c) {

        m_cache = c;
    }

    /**
     * Indicates if a previous cached result should be reloaded.
     * <P>
     * <em>not implemented.</em> Returns always <code>false</code>.
     *
     * @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.
     * @return <code>false</code>
     */
    public boolean shouldReload(CmsObject cms, String templateFile, String elementName,
        Hashtable parameters, String templateSelector) {

        return false;
    }

    /**
     * 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.
     * @throws 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.getRequestContext().currentUser().isGuestUser();

        // 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 = CmsLinkManager.getAbsoluteUri(getTemplateFileName(tagcontent, templateFile, parameterHashtable), doc.getAbsoluteFilename());

        // 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(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 (CmsLog.getLog(this).isErrorEnabled()) {
                CmsLog.getLog(this).error(errorMessage);
            }
            throw new CmsLegacyException(errorMessage, CmsLegacyException.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);

⌨️ 快捷键说明

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