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

📄 cmsxmlcontenteditor.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsXmlContentEditor.java,v $
 * Date   : $Date: 2006/03/28 07:53:23 $
 * Version: $Revision: 1.68 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.workplace.editors;

import org.opencms.file.CmsFile;
import org.opencms.file.CmsRequestContext;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.collectors.I_CmsResourceCollector;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.util.CmsRequestUtil;
import org.opencms.util.CmsStringUtil;
import org.opencms.widgets.A_CmsWidget;
import org.opencms.widgets.I_CmsWidget;
import org.opencms.widgets.I_CmsWidgetDialog;
import org.opencms.widgets.I_CmsWidgetParameter;
import org.opencms.workplace.CmsWorkplaceSettings;
import org.opencms.xml.CmsXmlContentDefinition;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.CmsXmlUtils;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.content.CmsXmlContentErrorHandler;
import org.opencms.xml.content.CmsXmlContentFactory;
import org.opencms.xml.content.CmsXmlContentValueSequence;
import org.opencms.xml.types.CmsXmlNestedContentDefinition;
import org.opencms.xml.types.I_CmsXmlContentValue;
import org.opencms.xml.types.I_CmsXmlSchemaType;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.apache.commons.logging.Log;

/**
 * Creates the editor for XML content definitions.<p> 
 *
 * @author Alexander Kandzior 
 * @author Andreas Zahner 
 * 
 * @version $Revision: 1.68 $ 
 * 
 * @since 6.0.0 
 */
public class CmsXmlContentEditor extends CmsEditor implements I_CmsWidgetDialog {

    /** Action for checking content before executing the direct edit action. */
    public static final int ACTION_CHECK = 151;

    /** Action for optional element creation. */
    public static final int ACTION_ELEMENT_ADD = 152;

    /** Action for element move down operation. */
    public static final int ACTION_ELEMENT_MOVE_DOWN = 154;

    /** Action for element move up operation. */
    public static final int ACTION_ELEMENT_MOVE_UP = 153;

    /** Action for optional element removal. */
    public static final int ACTION_ELEMENT_REMOVE = 155;

    /** Action for new file creation. */
    public static final int ACTION_NEW = 156;

    /** Indicates that the content should be checked before executing the direct edit action. */
    public static final String EDITOR_ACTION_CHECK = "check";

    /** Indicates an optional element should be created. */
    public static final String EDITOR_ACTION_ELEMENT_ADD = "addelement";

    /** Indicates an element should be moved down. */
    public static final String EDITOR_ACTION_ELEMENT_MOVE_DOWN = "elementdown";

    /** Indicates an element should be moved up. */
    public static final String EDITOR_ACTION_ELEMENT_MOVE_UP = "elementup";

    /** Indicates an optional element should be removed. */
    public static final String EDITOR_ACTION_ELEMENT_REMOVE = "removeelement";

    /** Indicates a new file should be created. */
    public static final String EDITOR_ACTION_NEW = I_CmsEditorActionHandler.DIRECT_EDIT_OPTION_NEW;

    /** Parameter name for the request parameter "elementindex". */
    public static final String PARAM_ELEMENTINDEX = "elementindex";

    /** Parameter name for the request parameter "elementname". */
    public static final String PARAM_ELEMENTNAME = "elementname";

    /** Constant for the editor type, must be the same as the editors subfolder name in the VFS. */
    private static final String EDITOR_TYPE = "xmlcontent";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsXmlContentEditor.class);

    /** The content object to edit. */
    private CmsXmlContent m_content;

    /** The element locale. */
    private Locale m_elementLocale;

    /** The error handler for the xml content. */
    private CmsXmlContentErrorHandler m_errorHandler;

    /** File object used to read and write contents. */
    private CmsFile m_file;

    /** The set of help message ids that have already been used. */
    private Set m_helpMessageIds;

    /** Indicates if an optional element is included in the form. */
    private boolean m_optionalElementPresent;

    /** Parameter stores the index of the element to add or remove. */
    private String m_paramElementIndex;

    /** Parameter stores the name of the element to add or remove. */
    private String m_paramElementName;

    /** Parameter to indicate if a new XML content resource should be created. */
    private String m_paramNewLink;

    /** Visitor implementation that stored the widgets for the content.  */
    private CmsXmlContentWidgetVisitor m_widgetCollector;

    /**
     * Public constructor.<p>
     * 
     * @param jsp an initialized JSP action element
     */
    public CmsXmlContentEditor(CmsJspActionElement jsp) {

        super(jsp);
    }

    /**
     * Performs the change element language action of the editor.<p>
     */
    public void actionChangeElementLanguage() {

        if (!m_content.hasLocale(getElementLocale())) {
            // create new element if selected language element is not present
            try {
                m_content.addLocale(getCms(), getElementLocale());
            } catch (CmsXmlException e) {
                if (LOG.isErrorEnabled()) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
            }
        }
        // save eventually changed content of the editor
        Locale oldLocale = CmsLocaleManager.getLocale(getParamOldelementlanguage());
        try {
            setEditorValues(oldLocale);
            if (!getErrorHandler().hasErrors()) {
                // no errors found in content, save to temporary file              
                writeContent();
            } else {
                // errors found, switch back to old language to show errors
                setParamElementlanguage(getParamOldelementlanguage());
                // set stored locale to null to reinitialize it
                m_elementLocale = null;
            }
        } catch (Exception e) {
            // should usually never happen
            if (LOG.isInfoEnabled()) {
                LOG.info(e.getLocalizedMessage(), e);
            }
        }
    }

    /**
     * Deletes the temporary file and unlocks the edited resource when in direct edit mode.<p>
     * 
     * @param forceUnlock if true, the resource will be unlocked anyway
     */
    public void actionClear(boolean forceUnlock) {

        // delete the temporary file        
        deleteTempFile();
        boolean directEditMode = Boolean.valueOf(getParamDirectedit()).booleanValue();
        boolean modified = Boolean.valueOf(getParamModified()).booleanValue();
        if (directEditMode || forceUnlock || !modified) {
            // unlock the resource when in direct edit mode, force unlock is true or resource was not modified
            try {
                getCms().unlockResource(getParamResource());
            } catch (CmsException e) {
                // should usually never happen
                if (LOG.isInfoEnabled()) {
                    LOG.info(e.getLocalizedMessage(), e);
                }
            }
        }
    }

    /**
     * Performs a configurable action performed by the editor.<p>
     * 
     * The default action is: save resource, clear temporary files and publish the resource directly.<p>
     * 
     * @throws IOException if a forward fails
     * @throws ServletException of a forward fails
     * @throws JspException if including a JSP fails
     */
    public void actionDirectEdit() throws IOException, JspException, ServletException {

        // get the action class from the OpenCms runtime property
        I_CmsEditorActionHandler actionClass = OpenCms.getWorkplaceManager().getEditorActionHandler();
        if (actionClass == null) {
            // error getting the action class, save content and exit the editor
            actionSave();
            actionExit();
        } else {
            actionClass.editorAction(this, getJsp());
        }
    }

    /**
     * Performs the exit editor action.<p>
     * 
     * @see org.opencms.workplace.editors.CmsEditor#actionExit()
     */
    public void actionExit() throws IOException, JspException, ServletException {

        if (getAction() == ACTION_CANCEL) {
            // save and exit was cancelled
            return;
        }
        // unlock resource if we are in direct edit mode
        actionClear(false);
        // close the editor
        actionClose();
    }

    /**
     * Moves an element in the xml content either up or down.<p>
     * 
     * Depends on the given action value.<p>
     * 
     * @throws JspException if including the error page fails
     */
    public void actionMoveElement() throws JspException {

        // set editor values from request
        try {
            setEditorValues(getElementLocale());
        } catch (CmsXmlException e) {
            // an error occured while trying to set the values, stop action
            showErrorPage(e);
            return;
        }

        // validate the content values
        if (!getErrorHandler().hasErrors()) {
            // get the necessary parameters to move the element
            int index = 0;
            try {
                index = Integer.parseInt(getParamElementIndex());
            } catch (Exception e) {
                // ignore, should not happen
            }

            // get the value to move
            I_CmsXmlContentValue value = m_content.getValue(getParamElementName(), getElementLocale(), index);

            if (getAction() == ACTION_ELEMENT_MOVE_DOWN) {
                // move down the value
                value.moveDown();
            } else {
                // move up the value
                value.moveUp();
            }

            if (getErrorHandler().hasWarnings(getElementLocale())) {
                // there were warnings for the edited content, reset error handler to avoid display issues
                resetErrorHandler();
            }

            try {
                // write the modified content to the temporary file
                writeContent();
            } catch (CmsException e) {
                // an error occured while trying to save
                showErrorPage(e);
            }
        }
    }

    /**
     * Creates a new XML content item for editing.<p>
     * 
     * @throws JspException in case something goes wrong
     */
    public void actionNew() throws JspException {

        // get the collector used to create the new content
        int pos = m_paramNewLink.indexOf('|');
        String collectorName = m_paramNewLink.substring(0, pos);
        String param = m_paramNewLink.substring(pos + 1);

        // get the collector used for calculating the next file name
        I_CmsResourceCollector collector = OpenCms.getResourceManager().getContentCollector(collectorName);
        String newFileName = "";
        try {

            // one resource serves as a "template" for the new resource
            CmsFile templateFile = getCms().readFile(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION);
            CmsXmlContent template = CmsXmlContentFactory.unmarshal(getCms(), templateFile);
            Locale locale = (Locale)OpenCms.getLocaleManager().getDefaultLocales(getCms(), getParamResource()).get(0);

            // now create a new XML content based on the templates content definition            
            CmsXmlContent newContent = CmsXmlContentFactory.createDocument(
                getCms(),
                locale,
                template.getEncoding(),
                template.getContentDefinition());

            // IMPORTANT: calculation of the name MUST be done here so the file name is ensured to be valid
            newFileName = collector.getCreateLink(getCms(), collectorName, param);

⌨️ 快捷键说明

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