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

📄 cmsxmltemplateeditor.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            title = (String)session.getValue(C_PARA_TITLE);
            session.removeValue(C_PARA_TITLE);
        }
        String bodytitle = (String)parameters.get("bodytitle");
        if(bodytitle == null){
            // try to get the value from the session because we might come from errorpage
            bodytitle = (String)session.getValue("bodytitle");
            session.removeValue("bodytitle");
        }
        String layoutTemplateFilename = (String)parameters.get("template");
        if(layoutTemplateFilename == null){
            // try to get the value from the session because we might come from errorpage
            layoutTemplateFilename = (String)session.getValue("template");
            session.removeValue("template");
        }
        String layoutTemplatFilenameRelative = layoutTemplateFilename;
        layoutTemplateFilename = Utils.mergeAbsolutePath(file, layoutTemplateFilename);
        String bodyElementClassName = (String)parameters.get("bodyclass");
        if(bodyElementClassName == null){
            // try to get the value from the session because we might come from errorpage
            bodyElementClassName = (String)session.getValue("bodyclass");
            session.removeValue("bodyclass");
        }
        String bodyElementFilename = (String)parameters.get("bodyfile");
        if(bodyElementFilename == null){
            // try to get the value from the session because we might come from errorpage
            bodyElementFilename = (String)session.getValue("bodyfile");
            session.removeValue("bodyfile");
        }
        String action = (String)parameters.get(C_PARA_ACTION);

        String startView = (String)parameters.get("startview");
        if((startView == null) || ("".equals(startView))){
            // try to get the value from the session because we might come from errorpage
            startView = (String)session.getValue("startview");
            session.removeValue("startview");
        }

        // Get all session parameters
        String oldEdit = (String)session.getValue("te_oldedit");
        String oldLayoutFilename = (String)session.getValue("te_oldlayout");
        String oldTitle = (String)session.getValue("te_title");
        String oldBody = (String)session.getValue("te_oldbody");
        String oldBodytitle = (String)session.getValue("te_oldbodytitle");
        String layoutTemplateClassName = (String)session.getValue("te_templateclass");
        String tempPageFilename = (String)session.getValue("te_temppagefile");
        String tempBodyFilename = (String)session.getValue("te_tempbodyfile");
        String style = (String)session.getValue("te_stylesheet");

        boolean existsContentParam = content != null;
        boolean existsFileParam = (file != null && (!"".equals(file)));
        boolean saveRequested = ((action != null) && (C_EDIT_ACTION_SAVE.equals(action) || C_EDIT_ACTION_SAVEEXIT.equals(action)));
        boolean exitRequested = ((action != null) && (C_EDIT_ACTION_EXIT.equals(action) || C_EDIT_ACTION_SAVEEXIT.equals(action)));
        boolean bodychangeRequested = ((oldBody != null) && (body != null) && (!(oldBody.equals(body))));
        boolean templatechangeRequested = (oldLayoutFilename != null && layoutTemplateFilename != null
               && (!(oldLayoutFilename.equals(layoutTemplateFilename))));
        boolean titlechangeRequested = (oldTitle != null && title != null && (!(oldTitle.equals(title))));
        boolean newbodyRequested = ((action != null) && "newbody".equals(action));
        boolean previewRequested = ((action != null) && "preview".equals(action));
        boolean bodytitlechangeRequested = (oldBodytitle != null && bodytitle != null && (!(oldBodytitle.equals(bodytitle))));

        String saveerror = "";

        // Check if there is a file parameter in the request
        if(!existsFileParam) {
            throwException("No \"file\" parameter given. Don't know which file should be edited.");
        }

        // If there is no content parameter this seems to be
        // a new request of the page editor.
        // So we have to read all files and set some initial values.
        parameters.put("root.pagetype", cms.getResourceType(cms.readFileHeader(file).getType()).getResourceTypeName());
        parameters.put("filename_for_relative_template", file);
        if(!existsContentParam) {
            CmsXmlControlFile originalControlFile = new CmsXmlControlFile(cms, file);
            if(originalControlFile.isElementClassDefined(C_BODY_ELEMENT)) {
                bodyElementClassName = originalControlFile.getElementClass(C_BODY_ELEMENT);
            }
            if(originalControlFile.isElementTemplateDefined(C_BODY_ELEMENT)) {
                bodyElementFilename = originalControlFile.getElementTemplate(C_BODY_ELEMENT);
            }
            if((bodyElementClassName == null) || (bodyElementFilename == null)) {
                // Either the template class or the template file
                // for the body element could not be determined.
                // BUG: Send error here
            }
            // Check, if the selected page file is locked
            CmsResource pageFileResource = cms.readFileHeader(file);
            if(!pageFileResource.isLocked()) {
                // BUG: Check only, dont't lock here!
                cms.lockResource(file);
            }
            // The content file must be locked before editing
            CmsResource contentFileResource = cms.readFileHeader(bodyElementFilename);
            if(!contentFileResource.isLocked()) {
                cms.lockResource(bodyElementFilename);
            }
            // Now get the currently selected master template file
            layoutTemplateFilename = originalControlFile.getMasterTemplate();
            layoutTemplatFilenameRelative = layoutTemplateFilename;
            layoutTemplateFilename = Utils.mergeAbsolutePath(originalControlFile.getAbsoluteFilename()
                                                            , layoutTemplateFilename);
            layoutTemplateClassName = originalControlFile.getTemplateClass();
            int browserId;
            if(browser.indexOf("MSIE") > -1) {
                browserId = 0;
            }else {
                browserId = 1;
            }

            if(editor == null || "".equals(editor)) {
                if(startView == null || "".equals(startView)){
                    editor = C_SELECTBOX_EDITORVIEWS[C_SELECTBOX_EDITORVIEWS_DEFAULT[browserId]];
                } else {
                    editor = startView;
                }
                session.putValue("te_pageeditor", editor);
                parameters.put("editor", editor);
            }

            // And finally the document title
            title = cms.readProperty(file, C_PROPERTY_TITLE);
            if(title == null) {
                title = "";
            }

            // We don't want the user to go on and create any temporary
            // files, if he has insufficient rights. Check this now.
            if(!cms.accessWrite(file)) {
                throw new CmsException(getClassName() + "Insufficient rights for editing the file " + file, CmsException.C_NO_ACCESS);
            }
            if(!cms.accessWrite(bodyElementFilename)) {
                throw new CmsException(getClassName() + "Insufficient rights for editing the file " + bodyElementFilename, CmsException.C_NO_ACCESS);
            }

            // Okay. All values are initialized. Now we can create
            // the temporary files.
            // if the parameter noactivex is set the temp file was already created,
            // so read the filename from the session
            String noactivex = (String)parameters.get("noactivex");
            if(noactivex == null || "".equals(noactivex.trim())){
                tempPageFilename = createTemporaryFile(cms, pageFileResource, tempProject, curProject);
            } else {
                tempPageFilename = (String)session.getValue("te_temppagefile");
            }
            cms.getRequestContext().setCurrentProject(curProject);
            tempBodyFilename = C_VFS_PATH_BODIES.substring(0, C_VFS_PATH_BODIES.length()-1) + tempPageFilename;

            session.putValue("te_temppagefile", tempPageFilename);
            session.putValue("te_tempbodyfile", tempBodyFilename);
        }
        // Get the XML parsed content of the layout file.
        // This can be done by calling the getOwnTemplateFile() method of the
        // layout's template class.
        // The content is needed to determine the HTML style of the body element.
        Object tempObj = CmsTemplateClassManager.getClassInstance(cms, layoutTemplateClassName);
        CmsXmlTemplate layoutTemplateClassObject = (CmsXmlTemplate)tempObj;
        CmsXmlTemplateFile layoutTemplateFile = layoutTemplateClassObject.getOwnTemplateFile(cms,
                layoutTemplateFilename, null, parameters, null);

        // Get the XML parsed content of the body file.
        // This can be done by calling the getOwnTemplateFile() method of the
        // body's template class.
        tempObj = CmsTemplateClassManager.getClassInstance(cms, bodyElementClassName);
        CmsXmlTemplate bodyElementClassObject = (CmsXmlTemplate)tempObj;
        CmsXmlTemplateFile bodyTemplateFile = bodyElementClassObject.getOwnTemplateFile(cms,
                tempBodyFilename, C_BODY_ELEMENT, parameters, null);

        // Get the temporary page file object
        CmsXmlControlFile temporaryControlFile = new CmsXmlControlFile(cms, tempPageFilename);
        if(!existsContentParam) {
            Vector allBodys = bodyTemplateFile.getAllSections();
            if(allBodys == null || allBodys.size() == 0) {
                body = "";
            }else {
                body = (String)allBodys.elementAt(0);
            }
            bodytitle = body.equals("(default)") ? "" : body;
            temporaryControlFile.setElementTemplSelector(C_BODY_ELEMENT, body);
            temporaryControlFile.setElementTemplate(C_BODY_ELEMENT, tempBodyFilename);
            // change the current project to temp file project
            cms.getRequestContext().setCurrentProject(tempProject);
            temporaryControlFile.write();
            cms.getRequestContext().setCurrentProject(curProject);
            try {
                style = getStylesheet(cms, null, layoutTemplateFile, null);
                if(style != null && !"".equals(style)) {
                    style = hostName + style;
                }
            }catch(Exception e) {
                style = "";
            }
            session.putValue("te_stylesheet", style);
        }else {
            // There exists a content parameter.
            // We have to check all possible changes requested by the user.
            if(titlechangeRequested) {
                // The user entered a new document title
                try {
                    cms.getRequestContext().setCurrentProject(tempProject);
                    cms.writeProperty(tempPageFilename, C_PROPERTY_TITLE, title);
                    cms.getRequestContext().setCurrentProject(curProject);
                }catch(CmsException e) {
                    cms.getRequestContext().setCurrentProject(curProject);
                    if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                        A_OpenCms.log(C_OPENCMS_INFO, getClassName()
                                + "Could not write property " + C_PROPERTY_TITLE + " for file " + file + ".");
                        A_OpenCms.log(C_OPENCMS_INFO, getClassName() + e);
                    }
                }
            }
            if(templatechangeRequested) {
                // The user requested a change of the layout template
                temporaryControlFile.setMasterTemplate(layoutTemplatFilenameRelative );
                try {
                    style = getStylesheet(cms, null, layoutTemplateFile, null);
                    if(style != null && !"".equals(style)) {
                        style = hostName + style;
                    }
                }catch(Exception e) {
                    style = "";
                }
                session.putValue("te_stylesheet", style);
            }
            if(bodytitlechangeRequested) {
                // The user entered a new title for the current body
                if((!oldBody.equals("(default)")) && (!oldBody.equals("script"))) {
                    if(bodytitle.toLowerCase().equals("script")) {
                        bodytitle = "script";
                    }
                    try {
                        bodyTemplateFile.renameSection(oldBody, bodytitle);
                        oldBody = bodytitle;
                        if(!bodychangeRequested) {
                            body = bodytitle;
                        }
                    }catch(Exception e) {
                        bodytitle = oldBodytitle;
                    }
                    if(bodytitle.equals("script")) {
                        session.putValue("te_pageeditor", editor);
                        editor = C_SELECTBOX_EDITORVIEWS[1];
                        parameters.put("editor", editor);
                    }
                }else {
                    bodytitle = oldBodytitle;
                }
            }
            if(bodychangeRequested) {
                temporaryControlFile.setElementTemplSelector(C_BODY_ELEMENT, body);
                bodytitle = body.equals("(default)") ? "" : body;
                if(body.equals("script")) {
                    // User wants to edit javascript code
                    // Select text editor
                    session.putValue("te_pageeditor", editor);
                    editor = C_SELECTBOX_EDITORVIEWS[1];
                    parameters.put("editor", editor);
                }else {
                    if(oldBody.equals("script")) {
                        // User wants to switch back from javascript mode
                        // Select old editor
                        editor = (String)session.getValue("te_pageeditor");
                        parameters.put("editor", editor);
                    }
                }
            }
            if(newbodyRequested) {
                body = C_BODY_ELEMENT + bodyTemplateFile.createNewSection(C_BODY_ELEMENT);
                bodytitle = body;
                temporaryControlFile.setElementTemplSelector(C_BODY_ELEMENT, body);
                temporaryControlFile.setElementTemplate(C_BODY_ELEMENT, tempBodyFilename);
            }
            
            // check for C_PROPERTY_RELATIVEROOT property (with directory search)
            String relativeRoot = cms.readProperty(file, C_PROPERTY_RELATIVEROOT, true);
            
            // save file contents to our temporary file.
            content = Encoder.unescape(content, Encoder.C_URI_ENCODING);
            if (! Encoder.C_URI_ENCODING.equalsIgnoreCase(cms.getRequestContext().getEncoding())) {			
                content = Encoder.escapeNonAscii(content);
            }          
			
            if((!exitRequested) || saveRequested) {
                bodyTemplateFile.setEditedTemplateContent(cms, content, oldBody, oldEdit.equals(C_SELECTBOX_EDITORVIEWS[0]), file, relativeRoot);
            }
            cms.getRequestContext().setCurrentProject(tempProject);
            bodyTemplateFile.write();
            temporaryControlFile.write();
            cms.getRequestContext().setCurrentProject(curProject);
        }

        // If the user requested a preview then send a redirect
        // to the temporary page file.
        if(previewRequested) {
            preview(tempPageFilename, reqCont);
            return "".getBytes();
        }

        // If the user requested a "save" expilitly by pressing one of
        // the "save" buttons, copy all informations of the temporary
        // files to the original files.
        if(saveRequested) {

⌨️ 快捷键说明

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