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

📄 cmsxmltemplateloader.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        if (elementCacheEnabled && (cmsUri == null)) {
            // ---- element cache stuff --------
            // No URI could be found in cache.
            // So create a new URI object with a start element and store it using the UriLocator
            CmsElementDescriptor elemDesc = new CmsElementDescriptor(templateClass, templateName);
            CmsElementDefinitionCollection eldefs = doc.getElementDefinitionCollection();
            if (elementreplace) {
                // we cant cach this
                eldefs.add(replaceDef);
                cmsUri = new CmsUri(elemDesc, eldefs);
            } else {
                cmsUri = new CmsUri(elemDesc, eldefs);
                elementCache.getUriLocator().put(uriDesc, cmsUri);
            }
        }

        if (elementCacheEnabled) {
            // TODO: Make cache more efficient
            clearLoaderCache(true, true);

            // now lets get the output
            if (elementreplace) {
                output = cmsUri.callCanonicalRoot(elementCache, cms, newParameters);
            } else {
                output = elementCache.callCanonicalRoot(cms, newParameters, uri);
            }
        } else {
            // ----- traditional stuff ------
            // Element cache is deactivated. So let's go on as usual.
            try {
                CmsFile masterTemplate = loadMasterTemplateFile(cms, templateName, doc);
                I_CmsTemplate tmpl = getTemplateClass(templateClass);
                if (!(tmpl instanceof I_CmsXmlTemplate)) {
                    String errorMessage = "Error in "
                        + cms.getSitePath(file)
                        + ": "
                        + templateClass
                        + " is not a XML template class.";
                    if (CmsLog.getLog(this).isErrorEnabled()) {
                        CmsLog.getLog(this).error(errorMessage);
                    }
                    throw new CmsLegacyException(errorMessage, CmsLegacyException.C_XML_WRONG_TEMPLATE_CLASS);
                }
                // TODO: Make cache more efficient
                clearLoaderCache(true, true);
                output = callCanonicalRoot(cms, tmpl, masterTemplate, newParameters);
            } catch (CmsException e) {
                if (CmsLog.getLog(this).isWarnEnabled()) {
                    CmsLog.getLog(this);
                }
                doc.removeFromFileCache();
                throw e;
            }
        }
        return output;
    }

    /**
     * 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;
    }

    /**
     * Calls the CmsClassManager to get an instance of the given template class.<p>
     * 
     * The returned object is checked to be an implementing class of the interface
     * I_CmsTemplate.
     * If the template cache of the template class is not yet set up, 
     * this will be done, too.<p>
     * 
     * @param classname name of the requested template class
     * @return instance of the template class
     * @throws CmsException if something goes wrong
     */
    private I_CmsTemplate getTemplateClass(String classname) throws CmsException {

        if (CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("Getting start template class: " + classname);
        }
        Object o = CmsTemplateClassManager.getClassInstance(classname);

        // Check, if the loaded class really is a OpenCms template class.

        // This is done be checking the implemented interface.
        if (!(o instanceof I_CmsTemplate)) {
            String errorMessage = "Class " + classname + " is not an OpenCms template class.";
            if (CmsLog.getLog(this).isErrorEnabled()) {
                CmsLog.getLog(this).error(errorMessage);
            }
            throw new CmsLegacyException(errorMessage, CmsLegacyException.C_XML_NO_TEMPLATE_CLASS);
        }
        I_CmsTemplate cmsTemplate = (I_CmsTemplate)o;
        if (!cmsTemplate.isTemplateCacheSet()) {
            cmsTemplate.setTemplateCache(m_templateCache);
        }
        return cmsTemplate;
    }

    /**
     * Utility method to handle any occurence of an execption.<p>
     * 
     * If the Exception is NO CmsException (i.e. it was not detected previously)
     * it will be written to the logfile.<p>
     * 
     * If the current user is the anonymous user, no further exception will
     * be thrown, but a server error will be sent
     * (we want to prevent the user from seeing any exeptions).
     * Otherwise a new Exception will be thrown.
     * This will trigger the OpenCms error message box.<p>
     *
     * @param cms the cms context object
     * @param e Exception that should be handled
     * @param errorText error message that should be shown
     * @throws CmsException if 
     */
    private void handleException(CmsObject cms, Exception e, String errorText) throws CmsException {

        // log the error if it is no CmsException
        if (!(e instanceof CmsException)) {
            if (CmsLog.getLog(this).isErrorEnabled()) {
                CmsLog.getLog(this).error(errorText, e);
            }
        }

        // if the user is a guest (and it's not a login exception) we send an servlet error,
        // otherwise we try to throw an exception.
        CmsRequestContext reqContext = cms.getRequestContext();
        if ((DEBUG == 0)
            && reqContext.currentUser().isGuestUser()
            && (!(e instanceof CmsLegacyException && ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() == CmsLegacyException.C_NO_USER))))) {
            throw new CmsLegacyException(errorText, CmsLegacyException.C_SERVICE_UNAVAILABLE, e);
        } else {
            if (e instanceof CmsException) {
                throw (CmsException)e;
            } else {
                throw new CmsLegacyException(errorText, CmsLegacyException.C_LOADER_GENERIC_ERROR, e);
            }
        }
    }

    /**
     * Internal utility method for checking and loading a given template file.
     * @param cms CmsObject for accessing system resources.
     * @param templateName Name of the requestet template file.
     * @param doc CmsXmlControlFile object containig the parsed body file.
     * @return CmsFile object of the requested template file.
     * @throws CmsException if something goes wrong
     */
    private CmsFile loadMasterTemplateFile(
        CmsObject cms,
        String templateName,
        com.opencms.template.CmsXmlControlFile doc) throws CmsException {

        CmsFile masterTemplate = null;
        try {
            masterTemplate = cms.readFile(templateName);
        } catch (Exception e) {
            handleException(cms, e, "Cannot load master template " + templateName + ". ");
            doc.removeFromFileCache();
        }
        return masterTemplate;
    }

    /**
     * Processes the XmlTemplates and writes the result to 
     * the apropriate output stream, which is obtained from the request 
     * context of the cms object.<p>
     *
     * @param cms the cms context object
     * @param file the selected resource to be shown
     * @throws CmsException if something goes wrong
     */
    private void processXmlTemplate(CmsObject cms, CmsFile file) throws CmsException {

        // first some debugging output.
        if ((DEBUG > 0) && CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("Loader started for " + file.getName());
        }

        // check all values to be valid
        String errorMessage = null;
        if (file == null) {
            errorMessage = "CmsFile missing";
        }
        if (cms == null) {
            errorMessage = "CmsObject missing";
        }
        if (errorMessage != null) {
            if (CmsLog.getLog(this).isErrorEnabled()) {
                CmsLog.getLog(this).error(errorMessage);
            }
            throw new CmsLegacyException(errorMessage, CmsLegacyException.C_LOADER_GENERIC_ERROR);
        }

        // Check the clearcache parameter
        String clearcache = getRequest(cms.getRequestContext()).getParameter("_clearcache");

        // Clear loader caches if this is required
        clearLoaderCache(
            ((clearcache != null) && ("all".equals(clearcache) || "file".equals(clearcache))),
            ((clearcache != null) && ("all".equals(clearcache) || "template".equals(clearcache))));

        // get the CmsRequest
        I_CmsRequest req = getRequest(cms.getRequestContext());
        byte[] result = generateOutput(cms, file, req);
        if (result != null) {
            writeBytesToResponse(cms, result);
        }
    }

    /**
     * Writes a given byte array to the HttpServletRespose output stream.<p>
     * 
     * @param cms an initialized CmsObject
     * @param result byte array that should be written.
     * @throws CmsException if something goes wrong
     */
    private void writeBytesToResponse(CmsObject cms, byte[] result) throws CmsException {

        try {
            I_CmsResponse resp = getResponse(cms.getRequestContext());
            if ((result != null) && !resp.isRedirected()) {
                // Only write any output to the response output stream if
                // the current request is neither redirected nor streamed.
                OutputStream out = resp.getOutputStream();

                resp.setContentLength(result.length);
                resp.setHeader("Connection", "keep-alive");
                out.write(result);
                out.close();
            }
        } catch (IOException ioe) {
            if (CmsLog.getLog(this).isDebugEnabled()) {
                CmsLog.getLog(this).debug(
                    "IO error while writing to response stream for " + cms.getRequestContext().getUri(),
                    ioe);
            }
        } catch (Exception e) {
            String errorMessage = "Cannot write output to HTTP response stream";
            handleException(cms, e, errorMessage);
        }
    }
}

⌨️ 快捷键说明

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