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

📄 cmseditor.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     */
    public void setParamModified(String modified) {

        m_paramModified = modified;
    }

    /**
     * Sets the old element language.<p>
     * 
     * @param oldElementLanguage the old element language
     */
    public void setParamOldelementlanguage(String oldElementLanguage) {

        m_paramOldelementlanguage = oldElementLanguage;
    }

    /**
     * Sets the name of the temporary file.<p>
     * 
     * @param fileName the name of the temporary file
     */
    public void setParamTempfile(String fileName) {

        m_paramTempFile = fileName;
    }

    /**
     * Closes the editor and forwards to the workplace or the resource depending on the editor mode.<p>
     * 
     * @throws IOException if forwarding fails 
     * @throws ServletException if forwarding fails
     * @throws JspException if including a JSP fails
     */
    protected void actionClose() throws IOException, JspException, ServletException {

        if (Boolean.valueOf(getParamDirectedit()).booleanValue()) {
            // editor is in direct edit mode
            if (CmsStringUtil.isNotEmpty(getParamBacklink())) {
                // set link to the specified back link target
                setParamCloseLink(getJsp().link(getParamBacklink()));
            } else {
                // set link to the edited resource
                setParamCloseLink(getJsp().link(getParamResource()));
            }
            // save initialized instance of this class in request attribute for included sub-elements
            getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
            // load the common JSP close dialog
            getJsp().include(FILE_DIALOG_CLOSE);
        } else {
            // forward to the workplace explorer view
            sendForward(CmsFrameset.JSP_WORKPLACE_URI, new HashMap());
        }
    }

    /**
     * Writes the content of a temporary file back to the original file.<p>
     * 
     * @throws CmsException if something goes wrong
     */
    protected void commitTempFile() throws CmsException {

        switchToTempProject();
        CmsFile tempFile;
        List properties;
        try {
            tempFile = getCms().readFile(getParamTempfile(), CmsResourceFilter.ALL);
            properties = getCms().readPropertyObjects(getParamTempfile(), false);
        } finally {
            // make sure the project is reset in case of any exception
            switchToCurrentProject();
        }
        if (getCms().existsResource(getParamResource(), CmsResourceFilter.ALL)) {
            // update properties of original file first (required if change in encoding occured)
            getCms().writePropertyObjects(getParamResource(), properties);
            // now replace the content of the original file
            CmsFile orgFile = getCms().readFile(getParamResource(), CmsResourceFilter.ALL);
            orgFile.setContents(tempFile.getContents());
            getCms().writeFile(orgFile);
        } else {
            // original file does not exist, remove visibility permission entries and copy temporary file
            if (getCms().hasPermissions(tempFile, CmsPermissionSet.ACCESS_CONTROL)) {
                getCms().rmacc(
                    getParamTempfile(),
                    I_CmsPrincipal.PRINCIPAL_GROUP,
                    OpenCms.getDefaultUsers().getGroupUsers());
                getCms().rmacc(
                    getParamTempfile(),
                    I_CmsPrincipal.PRINCIPAL_GROUP,
                    OpenCms.getDefaultUsers().getGroupProjectmanagers());
            }
            getCms().copyResource(getParamTempfile(), getParamResource(), CmsResource.COPY_AS_NEW);
        }
        // remove the temporary file flag
        int flags = getCms().readResource(getParamResource(), CmsResourceFilter.ALL).getFlags();
        if ((flags & CmsResource.FLAG_TEMPFILE) == CmsResource.FLAG_TEMPFILE) {
            flags ^= CmsResource.FLAG_TEMPFILE;
            getCms().chflags(getParamResource(), flags);
        }
    }

    /**
     * Creates a temporary file which is needed while working in an editor with preview option.<p>
     * 
     * @return the file name of the temporary file
     * @throws CmsException if something goes wrong
     */
    protected String createTempFile() throws CmsException {

        // read the selected file
        CmsResource file = getCms().readResource(getParamResource(), CmsResourceFilter.ALL);

        // create the filename of the temporary file
        String temporaryFilename = CmsResource.getFolderPath(getCms().getSitePath(file))
            + CmsWorkplace.TEMP_FILE_PREFIX
            + file.getName();

        // check if the temporary file is already present
        if (getCms().existsResource(temporaryFilename, CmsResourceFilter.ALL)) {
            // delete old temporary file
            if (!getCms().getLock(temporaryFilename).equals(CmsLock.getNullLock())) {
                // steal lock
                getCms().changeLock(temporaryFilename);
            } else {
                // lock resource to current user
                getCms().lockResource(temporaryFilename);
            }
            getCms().deleteResource(temporaryFilename, CmsResource.DELETE_PRESERVE_SIBLINGS);
        }

        // switch to the temporary file project
        switchToTempProject();

        // copy the file to edit to a temporary file
        try {
            getCms().copyResource(getCms().getSitePath(file), temporaryFilename, CmsResource.COPY_AS_NEW);
            getCms().setDateLastModified(temporaryFilename, System.currentTimeMillis(), false);
            // set the temporary file flag
            CmsResource tempFile = getCms().readResource(temporaryFilename, CmsResourceFilter.ALL);
            int flags = tempFile.getFlags();
            if ((flags & CmsResource.FLAG_TEMPFILE) == 0) {
                flags += CmsResource.FLAG_TEMPFILE;
            }
            getCms().chflags(temporaryFilename, flags);
            // remove eventual release & expiration date from temporary file to make preview in editor work
            getCms().setDateReleased(temporaryFilename, CmsResource.DATE_RELEASED_DEFAULT, false);
            getCms().setDateExpired(temporaryFilename, CmsResource.DATE_EXPIRED_DEFAULT, false);
            // remove visibility permissions for users and projectmanagers on temporary file
            if (getCms().hasPermissions(tempFile, CmsPermissionSet.ACCESS_CONTROL)) {
                getCms().chacc(
                    temporaryFilename,
                    I_CmsPrincipal.PRINCIPAL_GROUP,
                    OpenCms.getDefaultUsers().getGroupUsers(),
                    "-v");
                getCms().chacc(
                    temporaryFilename,
                    I_CmsPrincipal.PRINCIPAL_GROUP,
                    OpenCms.getDefaultUsers().getGroupProjectmanagers(),
                    "-v");
            }
        } catch (CmsException e) {
            switchToCurrentProject();
            throw e;
        }

        // switch back to current project
        switchToCurrentProject();

        return temporaryFilename;
    }

    /**
     * Decodes the given content the same way the client would do it.<p>
     * 
     * Content is decoded as if it was encoded using the JavaScript
     * "encodeURIComponent()" function.<p>
     * 
     * @param content the content to decode
     * @return the decoded content
     */
    protected String decodeContent(String content) {

        return CmsEncoder.unescape(content, CmsEncoder.ENCODING_UTF_8);
    }

    /**
     * Decodes an individual parameter value, ensuring the content is always decoded in UTF-8.<p>
     * 
     * For editors the content is always encoded using the 
     * JavaScript encodeURIComponent() method on the client,
     * which always encodes in UTF-8.<p> 
     * 
     * @param paramName the name of the parameter 
     * @param paramValue the unencoded value of the parameter
     * @return the encoded value of the parameter
     */
    protected String decodeParamValue(String paramName, String paramValue) {

        if ((paramName != null) && (paramValue != null)) {
            if (PARAM_CONTENT.equals(paramName)) {
                // content will be always encoded in UTF-8 unicode by the editor client
                return CmsEncoder.decode(paramValue, CmsEncoder.ENCODING_UTF_8);
            } else if (PARAM_RESOURCE.equals(paramName) || PARAM_TEMPFILE.equals(paramName)) {
                String filename = CmsEncoder.decode(paramValue, getCms().getRequestContext().getEncoding());
                if (PARAM_TEMPFILE.equals(paramName) || CmsStringUtil.isEmpty(getParamTempfile())) {
                    // always use value from temp file if it is available
                    setFileEncoding(getFileEncoding(getCms(), filename));
                }
                return filename;
            } else {
                return CmsEncoder.decode(paramValue, getCms().getRequestContext().getEncoding());
            }
        } else {
            return null;
        }
    }

    /**
     * Deletes a temporary file from the OpenCms VFS, needed when exiting an editor.<p> 
     */
    protected void deleteTempFile() {

        try {
            // switch to the temporary file project
            switchToTempProject();
            // delete the temporary file
            getCms().deleteResource(getParamTempfile(), CmsResource.DELETE_PRESERVE_SIBLINGS);
            // switch back to the current project
            switchToCurrentProject();
        } catch (CmsException e) {
            // should usually never happen
            if (LOG.isInfoEnabled()) {
                LOG.info(e);
            }
        }
    }

    /**
     * Encodes the given content so that it can be transfered to the client.<p>
     * 
     * Content is encoded so that it is compatible with the JavaScript
     * "decodeURIComponent()" function.<p>
     * 
     * @param content the content to encode
     * @return the encoded content
     */
    protected String encodeContent(String content) {

        return CmsEncoder.escapeWBlanks(content, CmsEncoder.ENCODING_UTF_8);
    }

    /**
     * Returns the encoding parameter.<p>
     *
     * @return the encoding parameter
     */
    protected String getFileEncoding() {

        return m_fileEncoding;
    }

    /**
     * Helper method to determine the encoding of the given file in the VFS,
     * which must be set using the "content-encoding" property.<p>
     * 
     * @param cms the CmsObject
     * @param filename the name of the file which is to be checked
     * @return the encoding for the file
     */
    protected String getFileEncoding(CmsObject cms, String filename) {

        try {
            return cms.readPropertyObject(filename, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, false).getValue(
                OpenCms.getSystemInfo().getDefaultEncoding());
        } catch (CmsException e) {
            return OpenCms.getSystemInfo().getDefaultEncoding();
        }
    }

    /**
     * Initializes the editor content when openening the editor for the first time.<p>
     */
    protected abstract void initContent();

    /**
     * Sets the encoding parameter.<p>
     *
     * @param value the encoding value to set
     */
    protected void setFileEncoding(String value) {

        m_fileEncoding = CmsEncoder.lookupEncoding(value, value);
    }

    /**
     * Shows the selected error page in case of an exception.<p>
     * 
     * @param exception the current exception
     * @throws JspException if inclusion of the error page fails
     */
    protected void showErrorPage(Exception exception) throws JspException {

        // reset the action parameter            
        setParamAction("");
        showErrorPage(this, exception);
        // save not successful, set cancel action 
        setAction(ACTION_CANCEL);
        return;
    }

    /**
     * Shows the selected error page in case of an exception.<p>
     * 
     * @param editor initialized instance of the editor class
     * @param exception the current exception
     * @throws JspException if inclusion of the error page fails
     */
    protected void showErrorPage(Object editor, Exception exception) throws JspException {

        // save initialized instance of the editor class in request attribute for included sub-elements
        getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, editor);

        // reading of file contents failed, show error dialog
        setAction(ACTION_SHOW_ERRORMESSAGE);
        setParamTitle(key("title.edit") + ": " + CmsResource.getName(getParamResource()));
        if (exception != null) {
            getJsp().getRequest().setAttribute(ATTRIBUTE_THROWABLE, exception);
            if (CmsLog.getLog(editor).isWarnEnabled()) {
                CmsLog.getLog(editor).warn(exception.getLocalizedMessage(), exception);
            }
        }
        // include the common error dialog
        getJsp().include(FILE_DIALOG_SCREEN_ERRORPAGE);
    }
}

⌨️ 快捷键说明

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