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

📄 cmsimport.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/importexport/CmsImport.java,v $
 * Date   : $Date: 2007-08-13 16:30:11 $
 * Version: $Revision: 1.45 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 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.importexport;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.i18n.CmsMessageContainer;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.security.CmsRole;
import org.opencms.security.CmsRoleViolationException;
import org.opencms.util.CmsFileUtil;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.CmsXmlUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.logging.Log;

import org.dom4j.Document;
import org.dom4j.Element;

/**
 * Holds the functionaility to import resources from the filesystem
 * or a zip file into the OpenCms VFS.<p>
 *
 * @author Andreas Zahner 
 * @author Alexander Kandzior 
 * @author Michael Emmerich 
 * @author Thomas Weckert  
 * 
 * @version $Revision: 1.45 $ 
 * 
 * @since 6.0.0 
 */
public class CmsImport {

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

    /** The cms context to do the operations with. */
    protected CmsObject m_cms;

    /** The xml manifest-file. */
    protected Document m_docXml;

    /** The import-file to load resources from. */
    protected String m_importFile;

    /** Stores all import interface implementations .*/
    protected List m_importImplementations;

    /** The import-path to write resources into the cms. */
    protected String m_importPath;

    /** The import-resource (folder) to load resources from. */
    protected File m_importResource;

    /**
     * The version of this import, noted in the info tag of the manifest.xml.<p>
     * 
     * 0 indicates an export file without a version number, that is before version 4.3.23 of OpenCms.<br>
     * 1 indicates an export file of OpenCms with a version before 5.0.0
     * 2 indicates an export file of OpenCms with a version before 5.1.2
     * 3 indicates an export file of OpenCms with a version before 5.1.6
     * 4 indicates an export file of OpenCms with a version after 5.1.6
     */
    protected int m_importVersion;

    /**  The import-resource (zip) to load resources from. */
    protected ZipFile m_importZip;

    /** The object to report the log messages. */
    protected I_CmsReport m_report;

    /**
     * Constructs a new uninitialized import, required for special subclass data import.<p>
     */
    public CmsImport() {

        // empty
        super();
    }

    /**
     * Constructs a new import object which imports the resources from an OpenCms 
     * export zip file or a folder in the "real" file system.<p>
     *
     * @param cms the current cms object
     * @param importFile the file or folder to import from
     * @param importPath the path in the cms VFS to import into
     * @param report a report object to output the progress information to
     * 
     * @throws CmsRoleViolationException if the current user dies not have role permissions to import the database
     */
    public CmsImport(CmsObject cms, String importFile, String importPath, I_CmsReport report)
    throws CmsRoleViolationException {

        // check the role permissions
        OpenCms.getRoleManager().checkRole(cms, CmsRole.DATABASE_MANAGER);

        // set member variables
        m_cms = cms;
        m_importFile = importFile;
        m_importPath = importPath;
        m_report = report;
        m_importImplementations = OpenCms.getImportExportManager().getImportVersionClasses();
    }

    /**
     * Returns the value of a child element with a specified name for a given parent element.<p>
     *
     * @param parentElement the parent element
     * @param elementName the child element name
     * @return the value of the child node, or null if something went wrong
     */
    public static String getChildElementTextValue(Element parentElement, String elementName) {

        try {
            // get the first child element matching the specified name
            Element childElement = (Element)parentElement.selectNodes("./" + elementName).get(0);
            // return the value of the child element
            return childElement.getTextTrim();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Returns a list of files which are both in the import and in the virtual file system.<p>
     * 
     * @return a list of Strings, complete path of the files
     * @throws CmsImportExportException if the import file could not be opened
     * @throws CmsXmlException if the manifest of the import could not be unmarshalled
     */
    public List getConflictingFilenames() throws CmsXmlException, CmsImportExportException {

        List fileNodes;
        Element currentElement;
        String source, destination;
        List conflictNames = new ArrayList();
        //String xpathExpr = null;

        if (m_docXml == null) {
            openImportFile();
        }

        // get all file-nodes
        fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE);

        // walk through all files in manifest
        for (int i = 0; i < fileNodes.size(); i++) {
            currentElement = (Element)fileNodes.get(i);
            source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE);
            destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION);
            if (source != null) {
                // only consider files
                boolean exists = true;
                try {
                    CmsResource res = m_cms.readResource(m_importPath + destination);
                    if (res.getState().isDeleted()) {
                        exists = false;
                    }
                } catch (CmsException e) {
                    exists = false;
                }
                if (exists) {
                    conflictNames.add(m_importPath + destination);
                }
            }
        }

        if (m_importZip != null) {
            try {
                m_importZip.close();

⌨️ 快捷键说明

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