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

📄 cmsxmltemplateloader.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            if (DEBUG > 1) {
                System.err.println("CmsXmlTemplateLoader.service(): Encodig set to "
                    + cms.getRequestContext().getEncoding());
                System.err.println("CmsXmlTemplateLoader.service(): Uri set to " + cms.getRequestContext().getUri());
            }
            // process the included XMLTemplate
            result = generateOutput(cms, fx, cms_req);
            // append the result to the output stream
            if (result != null) {
                if (DEBUG > 1) {
                    System.err.println("CmsXmlTemplateLoader.service(): encoding="
                        + enc
                        + " requestEncoding="
                        + rnc
                        + " defaultEncoding="
                        + dnc);
                }
                res.getOutputStream().write(result);
            }
        } finally {
            // restore the context settings
            cms_req.setOriginalRequest(originalreq);
            cms.getRequestContext().setEncoding(rnc);
            // cms.getRequestContext().setUri(oldUri);
            if (DEBUG > 1) {
                System.err.println("CmsXmlTemplateLoader.service(): Encodig reset to "
                    + cms.getRequestContext().getEncoding());
                System.err.println("CmsXmlTemplateLoader.service(): Uri reset to " + cms.getRequestContext().getUri());
            }
        }

        if (DEBUG > 0) {
            long timer2 = System.currentTimeMillis() - timer1;
            System.err.println("============ CmsXmlTemplateLoader time delivering XmlTemplate for "
                + cms.getSitePath(file)
                + ": "
                + timer2
                + "ms");
        }
    }

    /**
     * Starts generating the output.
     * Calls the canonical root with the appropriate template class.
     *
     * @param cms CmsObject Object for accessing system resources
     * @param file CmsFile Object with the selected resource to be shown
     * @param req the CmsRequest
     * @return the generated output for the file
     * @throws CmsException if something goes wrong
     */
    protected byte[] generateOutput(CmsObject cms, CmsFile file, I_CmsRequest req) throws CmsException {

        byte[] output = null;

        // hashtable for collecting all parameters.
        Hashtable newParameters = new Hashtable();
        String uri = cms.getRequestContext().getUri();

        // collect xml template information
        String absolutePath = cms.getSitePath(file);
        if (CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("absolutePath=" + absolutePath);
        }
        String templateProp = cms.readPropertyObject(absolutePath, CmsPropertyDefinition.PROPERTY_TEMPLATE, false).getValue();
        if (CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("templateProp=" + templateProp);
        }
        String templateClassProp = cms.readPropertyObject(
            absolutePath,
            org.opencms.file.CmsPropertyDefinition.PROPERTY_BODY_CLASS,
            false).getValue(org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS);
        if (CmsLog.getLog(this).isDebugEnabled()) {
            CmsLog.getLog(this).debug("templateClassProp=" + templateClassProp);
        }

        // ladies and gentelman: and now for something completly different 
        String xmlTemplateContent = null;
        if (templateProp != null) {
            // i got a black magic template,
            StringBuffer buf = new StringBuffer(256);
            buf.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
            buf.append("<PAGE>\n<class>");
            buf.append(org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS);
            buf.append("</class>\n<masterTemplate>");
            // i got a black magic template,
            buf.append(templateProp);
            buf.append("</masterTemplate>\n<ELEMENTDEF name=\"body\">\n<CLASS>");
            buf.append(templateClassProp);
            buf.append("</CLASS>\n<TEMPLATE>");
            // i got a black magic template got me so blind I can't see,
            buf.append(uri);
            buf.append("</TEMPLATE>\n</ELEMENTDEF>\n</PAGE>\n");
            // i got a black magic template it's try'in to make a devil out of me...
            xmlTemplateContent = buf.toString();
            uri += com.opencms.core.I_CmsConstants.C_XML_CONTROL_FILE_SUFFIX;
        }

        // Parameters used for element cache
        boolean elementCacheEnabled = CmsXmlTemplateLoader.isElementCacheEnabled();
        CmsElementCache elementCache = null;
        CmsUriDescriptor uriDesc = null;
        CmsUri cmsUri = null;

        String templateClass = null;
        String templateName = null;
        CmsXmlControlFile doc = null;

        if (elementCacheEnabled) {
            // Get the global element cache object
            elementCache = CmsXmlTemplateLoader.getElementCache();

            // Prepare URI Locator
            uriDesc = new CmsUriDescriptor(uri);
            cmsUri = elementCache.getUriLocator().get(uriDesc);
            // check if cached
            if (CmsLog.getLog(this).isDebugEnabled()) {
                CmsLog.getLog(this).debug("found cmsUri=" + cmsUri);
            }
            if ((cmsUri != null) && !cmsUri.getElementDescriptor().getTemplateName().equalsIgnoreCase(templateProp)) {
                if (CmsLog.getLog(this).isDebugEnabled()) {
                    CmsLog.getLog(this).debug(
                        "cmsUri has different template: "
                            + cmsUri.getElementDescriptor().getTemplateName()
                            + " than current template: "
                            + templateProp
                            + ", not using cmsUri from cache");
                }
                cmsUri = null;
            }
        }

        // check if printversion is requested
        String replace = req.getParameter(C_ELEMENT_REPLACE);
        boolean elementreplace = false;
        CmsElementDefinition replaceDef = null;
        if (replace != null) {
            int index = replace.indexOf(":");
            if (index != -1) {
                elementreplace = true;
                cmsUri = null;
                String elementName = replace.substring(0, index);
                String replaceUri = replace.substring(index + 1);
                replaceDef = new CmsElementDefinition(
                    elementName,
                    org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS,
                    replaceUri,
                    null,
                    new Hashtable());
                newParameters.put(C_ELEMENT_REPLACE + "_VFS_" + elementName, cms.getRequestContext().addSiteRoot(
                    replaceUri));
            }
        }

        if ((cmsUri == null) || !elementCacheEnabled) {
            // Entry point to page file analysis.
            // For performance reasons this should only be done if the element
            // cache is not activated or if it's activated but no URI object could be found.

            // Parse the page file
            try {
                if (xmlTemplateContent == null) {
                    doc = new CmsXmlControlFile(cms, file);
                } else {
                    doc = new CmsXmlControlFile(cms, uri, xmlTemplateContent);
                }
            } catch (Exception e) {
                // there was an error while parsing the document.
                // No chance to go on here.
                handleException(cms, e, "There was an error while parsing XML page file " + cms.getSitePath(file));
                return "".getBytes();
            }

            if (!elementCacheEnabled && (replaceDef != null)) {
                // Required to enable element replacement if element cache is disabled
                doc.setElementClass(replaceDef.getName(), replaceDef.getClassName());
                doc.setElementTemplate(replaceDef.getName(), replaceDef.getTemplateName());
            }

            // Get the names of the master template and the template class from
            // the parsed page file. Fall back to default value, if template class
            // is not defined
            templateClass = doc.getTemplateClass();
            if (templateClass == null || "".equals(templateClass)) {
                templateClass = this.getClass().getName();
            }
            if (templateClass == null || "".equals(templateClass)) {
                templateClass = org.opencms.importexport.CmsCompatibleCheck.XML_CONTROL_DEFAULT_CLASS;
            }
            templateName = doc.getMasterTemplate();
            if (templateName != null && !"".equals(templateName)) {
                templateName = CmsLinkManager.getAbsoluteUri(templateName, cms.getSitePath(file));
            }

            // Previously, the template class was loaded here.
            // We avoid doing this so early, since in element cache mode the template
            // class is not needed here.

            // Now look for parameters in the page file...
            // ... first the params of the master template...
            Enumeration masterTemplateParams = doc.getParameterNames();
            while (masterTemplateParams.hasMoreElements()) {
                String paramName = (String)masterTemplateParams.nextElement();
                String paramValue = doc.getParameter(paramName);
                newParameters.put(com.opencms.core.I_CmsConstants.C_ROOT_TEMPLATE_NAME + "." + paramName, paramValue);
            }

            // ... and now the params of all subtemplates
            Enumeration elementDefinitions = doc.getElementDefinitions();
            while (elementDefinitions.hasMoreElements()) {
                String elementName = (String)elementDefinitions.nextElement();
                if (doc.isElementClassDefined(elementName)) {
                    newParameters.put(elementName + "._CLASS_", doc.getElementClass(elementName));
                }
                if (doc.isElementTemplateDefined(elementName)) {
                    // need to check for the body template here so that non-XMLTemplate templates 
                    // like JSPs know where to find the body defined in the XMLTemplate
                    String template = doc.getElementTemplate(elementName);
                    if (xmlTemplateContent == null) {
                        template = doc.validateBodyPath(cms, template, file);
                    }
                    if (CmsDefaultPageEditor.XML_BODY_ELEMENT.equalsIgnoreCase(elementName)) {
                        // found body element
                        if (template != null) {
                            cms.getRequestContext().setAttribute(CmsDefaultPageEditor.XML_BODY_ELEMENT, template);
                        }
                    }
                    newParameters.put(elementName + "._TEMPLATE_", template);
                }
                if (doc.isElementTemplSelectorDefined(elementName)) {
                    newParameters.put(elementName + "._TEMPLATESELECTOR_", doc.getElementTemplSelector(elementName));
                }
                Enumeration parameters = doc.getElementParameterNames(elementName);
                while (parameters.hasMoreElements()) {
                    String paramName = (String)parameters.nextElement();
                    String paramValue = doc.getElementParameter(elementName, paramName);
                    if (paramValue != null) {
                        newParameters.put(elementName + "." + paramName, paramValue);
                    } else {
                        if (CmsLog.getLog(this).isInfoEnabled()) {
                            CmsLog.getLog(this).info("Empty parameter \"" + paramName + "\" found.");
                        }
                    }
                }
            }
        }

        // URL parameters ary really dynamic.
        // We cannot store them in an element cache.
        // Therefore these parameters must be collected in ANY case!

        String datafor = req.getParameter("datafor");
        if (datafor == null) {
            datafor = "";
        } else {
            if (!"".equals(datafor)) {
                datafor = datafor + ".";
            }
        }
        Enumeration urlParameterNames = req.getParameterNames();
        while (urlParameterNames.hasMoreElements()) {
            String pname = (String)urlParameterNames.nextElement();
            String paramValue = req.getParameter(pname);
            if (paramValue != null) {
                if ((!"datafor".equals(pname)) && (!"_clearcache".equals(pname))) {
                    newParameters.put(datafor + pname, paramValue);
                }
            } else {
                if (CmsLog.getLog(this).isInfoEnabled()) {
                    CmsLog.getLog(this).info("Empty URL parameter \"" + pname + "\" found.");
                }
            }
        }

⌨️ 快捷键说明

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