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

📄 cmsxmltemplatefile.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * Check if there is the template filename of a given subelement defined.
     * @param elementName Name of the subelement.
     * @return <code>true</code>, if defined, <code>false</code> otherwise
     */
    public boolean hasSubtemplateFilename(String name) throws CmsException {
        return hasData("ELEMENTDEF." + name + ".TEMPLATE");
    }

    /**
     * Checks if there is a template selector defined in the subelement definition of
     * this template file.
     * @param elementName Name of the subelement.
     * @return <code>true</code>, if there exists a template selector, <code>false</code> otherwise.
     */
    public boolean hasSubtemplateSelector(String name) throws CmsException {
        return hasData("ELEMENTDEF." + name + ".TEMPLATESELECTOR");
    }
    private int min(int a, int b) {
        if(a == -1) {
            return b;
        }
        if(b == -1) {
            return a;
        }
        return a < b ? a : b;
    }
    private String replaceBack(String s) {
        StringBuffer tempContent = new StringBuffer();

        //int index = s.indexOf(search);
        int index = min(s.indexOf("<"), s.indexOf(">"));
        index = min(index, s.indexOf("\""));
        int lastindex = 0;
        while(index != -1) {
            String sub = s.substring(lastindex, index);
            tempContent.append(sub);
            if(s.charAt(index) == '>') {

                //tempContent.append("]</CODE>");
                tempContent.append("]]");
                lastindex = index + 1;
            }
            else {
                if(s.charAt(index) == '<') {

                    //tempContent.append("<CODE>[");
                    tempContent.append("[[");
                    lastindex = index + 1;
                }
                else {
                    tempContent.append("&quot;");
                    lastindex = index + 1;
                }
            }

            //index = s.indexOf(search, index+1);

            //index = min(s.indexOf("<", index+1), s.indexOf(">", index+1));
            index = min(s.indexOf("<", index + 1), min(s.indexOf(">", index + 1), s.indexOf("\"", index + 1)));
        }
        tempContent.append(s.substring(lastindex));
        return new String(tempContent);
    }

    /**
     * Registers the special tag <CODE>&lt;ELEMENT&gt;</CODE> for processing with
     * processNode().
     */
    private void registerMyTags() {
        registerTag("ELEMENT", CmsXmlTemplateFile.class, "handleElementTag", C_REGISTER_MAIN_RUN);
    }

    /**
     * Remove a datablock from the internal hashtable and
     * from the XML document
     * @param tag Key of the datablock to delete.
     */
    public void removeData(String tag) {
        super.removeData(tag);
    }
    public void renameSection(String oldName, String newName) throws CmsException {
        if(!hasData("template." + newName)) {
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(C_OPENCMS_INFO, getClassName() + "datablock TEMPLATE." + newName + " not found. creating.");
            }
            Element newData = (Element)getData("template." + oldName).cloneNode(true);
            newData.setAttribute("name", newName);
            setData("template." + newName, newData);
            removeData("template." + oldName);
            // now for the editor copy
            if(hasData(C_EDIT_TEMPLATE +"."+oldName)){
                Element newEditData = (Element)getData(C_EDIT_TEMPLATE +"."+oldName).cloneNode(true);
                newEditData.setAttribute("name", newName);
                setData(C_EDIT_TEMPLATE +"."+newName, newEditData);
                removeData(C_EDIT_TEMPLATE +"."+oldName);
            }
        }else {
            throw new CmsException("Section already exists: " + newName, CmsException.C_BAD_NAME);
        }
    }
    /* parameters search and replace are ignored.*/
    private String replace(String s, String search, String replace) {
        StringBuffer tempContent = new StringBuffer();

        //int index = s.indexOf(search);
        int index = min(s.indexOf("[["), s.indexOf("]]"));
        index = min(index, s.indexOf("&quot;"));
        int lastindex = 0;
        while(index != -1) {
            String sub = s.substring(lastindex, index);
            tempContent.append(sub);
            if(s.charAt(index) == ']') {
                tempContent.append("><![CDATA[");
                lastindex = index + 2;
            }
            else {
                if(s.charAt(index) == '[') {
                    tempContent.append("]]><");
                    lastindex = index + 2;
                }
                else {
                    tempContent.append("\"");
                    lastindex = index + 6;
                }
            }

            //index = s.indexOf(search, index+1);
            index = min(s.indexOf("[[", index + 1), min(s.indexOf("]]", index + 1), s.indexOf("&quot;", index + 1)));
        }
        tempContent.append(s.substring(lastindex));
        return new String(tempContent);
    }
    public void setBodyTag(Element data) throws CmsException {
        setData("bodytag", data);
    }

    /**
     * Creates a datablock consisting of a single TextNode containing
     * data and stores this block into the datablock-hashtable.
     *
     * @param tag Key for this datablock.
     * @param data String to be put in the datablock.
     */
    public void setData(String tag, String data) {
        super.setData(tag, data);
    }

    /**
     * Stores a given datablock element in the datablock hashtable.
     *
     * @param tag Key for this datablock.
     * @param data DOM element node for this datablock.
     */
    public void setData(String tag, Element data) {
        super.setData(tag, data);
    }

    /**
     * Creates a datablock element by parsing the data string
     * and stores this block into the datablock-hashtable.
     *
     * @param tag Key for this datablock.
     * @param data String to be put in the datablock.
     */
    public void setParsedData(String tag, String data) throws CmsException {
        super.setParsedData(tag, data);
    }

    public void setEditedTemplateContent(CmsObject cms, String content, String templateSelector, boolean html, String filePath, String relativeRoot) throws CmsException {
        //first the original only used by the editor
        String editDatablockName = getEditTemplateDatablockName(templateSelector);
        String copyOfContent = content;
        if(html) {
            
            int startIndex = content.indexOf("<body");
            if (startIndex < 0) startIndex = content.indexOf("<BODY");
            startIndex = content.indexOf(">", startIndex + 1) + 1;
            int endIndex = content.lastIndexOf("</body>");
            if (endIndex < 0) endIndex = content.lastIndexOf("</BODY>");
            if(startIndex > 0) {
                content = content.substring(startIndex, endIndex);
            }
        }
        
		// substitute contextpath with variable 		
        content = CmsStringSubstitution.substituteContextPath(content, A_OpenCms.getOpenCmsContext());                
                
        StringBuffer tempXmlString = new StringBuffer();
        tempXmlString.append("<?xml version=\"1.0\"?>\n");
        tempXmlString.append("<" + getXmlDocumentTagName() + ">");
        tempXmlString.append("<"+C_EDIT_TEMPLATE +">\n");
        if(html) {
            tempXmlString.append("<![CDATA[");
            content = replace(content, "[", "]]><");
            tempXmlString.append(content.trim());
            tempXmlString.append("]]>");
        }else {
            tempXmlString.append(content);
        }
        tempXmlString.append("</"+C_EDIT_TEMPLATE +">\n");
        tempXmlString.append("</" + getXmlDocumentTagName() + ">\n");
        I_CmsXmlParser parser = getXmlParser();
        StringReader parserReader = new StringReader(tempXmlString.toString());
        Document tempDoc = null;
        try {
            tempDoc = parser.parse(parserReader);
        }catch(Exception e) {
            throwException("PARSING ERROR!", CmsException.C_XML_PARSING_ERROR);
        }
        Element templateNode = (Element)tempDoc.getDocumentElement().getFirstChild();
        setData(editDatablockName, templateNode);

        // now the parsed content for the templatemechanism
        String datablockName = this.getTemplateDatablockName(templateSelector);
        if(!html){
            // we have to prepare the content for the tidy
            copyOfContent = "<HTML><HEAD></HEAD><body>" + copyOfContent.substring(9, copyOfContent.lastIndexOf("]]>")) + "</body></HTML>";
        }else{
            copyOfContent = replace(copyOfContent, "", "");
        }
        // now we have something for the tidy
        try{
            LinkSubstitution sub = new LinkSubstitution();
            copyOfContent = sub.substituteEditorContent(cms, copyOfContent, filePath, relativeRoot);
        }catch(CmsException e){
            throw new CmsException("["+this.getClass().getName()+"] cant parse the content:", e);
        }
        int startIndex = copyOfContent.indexOf("<body");
        startIndex = copyOfContent.indexOf(">", startIndex + 1) + 1;
        int endIndex = copyOfContent.lastIndexOf("</body>");
        if(startIndex > 0) {
            copyOfContent = copyOfContent.substring(startIndex, endIndex);
        }
        tempXmlString = new StringBuffer();
        tempXmlString.append("<?xml version=\"1.0\"?>\n");
        tempXmlString.append("<" + getXmlDocumentTagName() + ">");
        tempXmlString.append("<template>\n");
        tempXmlString.append("<![CDATA[");
        tempXmlString.append(copyOfContent.trim());
        tempXmlString.append("]]>");
        tempXmlString.append("</template>\n");
        tempXmlString.append("</" + getXmlDocumentTagName() + ">\n");
        I_CmsXmlParser parser2 = getXmlParser();
        StringReader parserReader2 = new StringReader(tempXmlString.toString());
        Document tempDoc2 = null;
        try {
            tempDoc2 = parser2.parse(parserReader2);
        }
        catch(Exception e) {
            throwException("PARSING ERROR!", CmsException.C_XML_PARSING_ERROR);
        }
        Element templateNode2 = (Element)tempDoc2.getDocumentElement().getFirstChild();
        setData(datablockName, templateNode2);

    }

    public void setSectionTitle(String sectionName, String title) throws CmsException {
        String datablockName = getTemplateDatablockName(sectionName);
        Element data = null;
        try {
            data = getData(datablockName);
        }
        catch(Exception e) {

            // The given section doesn't exist. Ignore.
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(C_OPENCMS_INFO, "Cannot set title for template section \"" + sectionName + "\" in file " + getAbsoluteFilename() + ". Section doesn't exist.");
            }
            return ;
        }
        data.setAttribute("title", title);
    }
}

⌨️ 快捷键说明

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