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

📄 cmsexport.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/Attic/CmsExport.java,v $
 * Date   : $Date: 2003/03/25 08:52:21 $
 * Version: $Revision: 1.53 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2001  The OpenCms Group
 *
 * 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 OpenCms, please see the
 * OpenCms 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 com.opencms.file;

import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.report.CmsShellReport;
import com.opencms.report.I_CmsReport;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.util.Utils;
import com.opencms.workplace.I_CmsWpConstants;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * Provides the functionality to export files from the OpenCms VFS to a ZIP file.<p>
 * 
 * The ZIP file written will contain a copy of all exported files with their contents.
 * It will also contain a <code>manifest.xml</code> file in wich all meta-information 
 * about this files are stored, like permissions etc.
 *
 * @author Andreas Schouten
 * @author Alexander Kandzior (a.kandzior@alkacon.com)
 * 
 * @version $Revision: 1.53 $ $Date: 2003/03/25 08:52:21 $
 */
public class CmsExport implements I_CmsConstants, Serializable {

    // the tags for the manifest
    public static String C_EXPORT_TAG_FILES = "files";    
    public static String C_EXPORT_TAG_CHANNELS = "channels";
    public static String C_EXPORT_TAG_MASTERS = "masters";
    
    /** The xml elemtent to store user data and further information in */
    private Element m_userdataElement;

    /** Indicates if the system should be included to the export */
    private boolean m_excludeSystem;

    /** Indicated if the unchanged resources should be included to the export */
    private boolean m_excludeUnchanged;

    /** Indicates if the user data and group data should be included to the export */
    private boolean m_exportUserdata;
    
    /** Max file age of contents to export */
    private long m_contentAge = 0; 

    /** Indicates if the current project it the online project */
    private boolean m_isOnlineProject;

    /** Cache for previously added super folders */
    private Vector m_superFolders;
    
    /** Set to store the names of page files in, required for later page body file export */
    private Set m_exportedPageFiles = null;
    
    /** Set of all exported files, required for later page body file export */
    private Set m_exportedResources = null;
    
    /** Stores the id of the "page" resource type to save lookup time */
    private int m_pageType;
        
    /** Indicates if module data is exported */
    protected boolean m_exportingModuleData = false;   

    /** The export ZIP file to store resources in */
    protected String m_exportFile;

    /** The export ZIP stream to write resources to */
    protected ZipOutputStream m_exportZipStream = null;

    /** The CmsObject to do the operations */
    protected CmsObject m_cms;

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

    /** The xml element to store file information in */
    protected Element m_filesElement;

    /** The xml-element to store masters information in */
    protected Element m_mastersElement;
    
    /** The report for the log messages */
    protected I_CmsReport m_report = null;
     
    /** The channelid and the resourceobject of the exported channels */
    protected Set m_exportedChannelIds = new HashSet();
    
    /**
     * Constructs a new uninitialized export, required for the module data export.<p>
     * 
     * @see CmsExportModuledata
     */
    public CmsExport() {
        // empty constructor
    }    
    
    /**
     * Constructs a new export.<p>
     *
     * @param cms the cmsObject to work with
     * @param exportFile the file or folder to export to
     * @param resourcesToExport the paths of folders and files to export
     * @param excludeSystem if true, the system folder is excluded, if false all the resources in
     *        resourcesToExport are included
     * @param excludeUnchanged <code>true</code>, if unchanged files should be excluded
     * 
     * @throws CmsException if something goes wrong
     */
    public CmsExport(
        CmsObject cms, 
        String exportFile, 
        String[] resourcesToExport, 
        boolean excludeSystem, 
        boolean excludeUnchanged
    ) throws CmsException {
        this(cms, exportFile, resourcesToExport, excludeSystem, excludeUnchanged, null, false, 0, new CmsShellReport());
    }

    /**
     * Constructs a new export.<p>
     *
     * @param cms the cmsObject to work with
     * @param exportFile the file or folder to export to
     * @param resourcesToExport the paths of folders and files to export
     * @param excludeSystem if true, the system folder is excluded, if false all the resources in
     *        resourcesToExport are included
     * @param excludeUnchanged <code>true</code>, if unchanged files should be excluded
     * @param Node moduleNode module informations in a Node for module export
     * @param exportUserdata if true, the user and grou pdata will also be exported
     * @param report to handle the log messages
     * 
     * @throws CmsException if something goes wrong
     */
    public CmsExport(
        CmsObject cms, 
        String exportFile, 
        String[] resourcesToExport, 
        boolean excludeSystem, 
        boolean excludeUnchanged, 
        Node moduleNode, 
        boolean exportUserdata, 
        long contentAge, 
        I_CmsReport report
    ) throws CmsException {                       

        m_exportFile = exportFile;
        m_cms = cms;
        m_excludeSystem = excludeSystem;
        m_excludeUnchanged = excludeUnchanged;
        m_exportUserdata = exportUserdata;
        m_isOnlineProject = cms.getRequestContext().currentProject().isOnlineProject();
        m_contentAge = contentAge;
        m_report = report;
        m_exportingModuleData = false;

        openExportFile(moduleNode);

        // export all the resources
        exportAllResources(resourcesToExport);

        // export userdata and groupdata if desired
        if(m_exportUserdata){
            exportGroups();
            exportUsers();
        }
                
        closeExportFile();
    }
        
    /**
     * Opens the export ZIP file and initializes the internal XML document for the manifest.<p>
     * 
     * @param moduleNode optional modul node if a module is to be exported
     * @throws CmsException if something goes wrong
     */
    protected void openExportFile(Node moduleNode) throws CmsException {
        // open the export resource
        getExportResource();

        // create the xml-config file
        getXmlConfigFile(moduleNode);        
    }
    
    /**
     * Closes the export ZIP file and saves the internal XML document for the manifest.<p>
     * 
     * @throws CmsException if something goes wrong
     */    
    protected void closeExportFile() throws CmsException {
        // write the document to the zip-file
        writeXmlConfigFile();

        try {
            m_exportZipStream.close();
        } catch(IOException exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }        
    }
        
    /**
     * Exports all resources and possible sub-folders form the provided list of resources.
     * 
     * @param resourcesToExport the list of resources to export
     * @throws CmsException if something goes wrong
     */
    protected void exportAllResources(String[] resourcesToExport) throws CmsException {
                 
        // distinguish folder and file names   
        Vector folderNames = new Vector();
        Vector fileNames = new Vector();
        for (int i=0; i<resourcesToExport.length; i++) {
            if (resourcesToExport[i].endsWith(C_FOLDER_SEPARATOR)) {
                folderNames.addElement(resourcesToExport[i]);
            } else {
                fileNames.addElement(resourcesToExport[i]);
            }
        }
                
        // remove the possible redundancies in the list of resources
        checkRedundancies(folderNames, fileNames);
        
        // init sets required for the body file exports 
        m_exportedPageFiles = new HashSet();
        m_exportedResources = new HashSet();
        
        // set the "page" file resource type id
        m_pageType = m_cms.getResourceType("page").getResourceType();
                
        // export the folders
        for (int i=0; i<folderNames.size(); i++) {
            String path = (String) folderNames.elementAt(i);
            // first add superfolders to the xml-config file
            addSuperFolders(path);
            exportResources(path);
            m_exportedResources.add(path);
        }
        
        // export the single files
        addSingleFiles(fileNames);

        // export all body files that have not already been exported
        addPageBodyFiles();        
    }        
        
    /**
     * Adds a CDATA element to the XML document.<p>
     * 
     * @param docXml Document to create the new element in
     * @param element the element to add the subelement to
     * @param name the name of the new subelement
     * @param value the value of the element
     */
    protected void addCdataElement(Document docXml, Element element, String name, String value) {
        Element newElement = docXml.createElement(name);
        element.appendChild(newElement);
        CDATASection text = docXml.createCDATASection(value);
        newElement.appendChild(text);
    }
    
    /**
     * Adds a text element to the XML document.<p>
     * 
     * @param docXml Document to create the new element in
     * @param element the element to add the subelement to
     * @param name the name of the new subelement
     * @param value the value of the element
     */
    protected void addElement(Document docXml, Element element, String name, String value) {

⌨️ 快捷键说明

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