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

📄 cmsimport.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsImport.java,v $
* Date   : $Date: 2001/11/20 11:04:38 $
* Version: $Revision: 1.52 $
*
* 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 java.io.*;
import java.util.*;
import java.util.zip.*;
import java.lang.reflect.*;
import java.security.*;
import com.opencms.boot.*;
import com.opencms.core.*;
import com.opencms.file.*;
import com.opencms.template.*;
import org.w3c.dom.*;
import source.org.apache.java.util.*;

/**
 * This class holds the functionaility to import resources from the filesystem
 * into the cms.
 *
 * @author Andreas Schouten
 * @version $Revision: 1.52 $ $Date: 2001/11/20 11:04:38 $
 */
public class CmsImport implements I_CmsConstants, Serializable {

    /**
     * The algorithm for the message digest
     */
    public static final String C_IMPORT_DIGEST="MD5";

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

    /**
     * The import-resource (folder) to load resources from
     */
    private File m_importResource = null;

    /**
     * The version of this import, noted in the info tag of the manifest.xml.
     * 0 if the import file dosent have a version nummber (that is befor version
     * 4.3.23 of OpenCms).
     */
    private int m_importVersion = 0;

    /**
     * The import-resource (zip) to load resources from
     */
    private ZipFile m_importZip = null;

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

    /**
     * The cms-object to do the operations.
     */
    private CmsObject m_cms;

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

    /**
     * Digest for taking a fingerprint of the files
     */
    private MessageDigest m_digest = null;

    /**
     *
     */
    private Stack m_groupsToCreate = new Stack();

    /**
     * This constructs a new CmsImport-object which imports the resources.
     *
     * @param importFile the file or folder to import from.
     * @param importPath the path to the cms to import into.
     * @exception CmsException the CmsException is thrown if something goes wrong.
     */
    public CmsImport(String importFile, String importPath, CmsObject cms)
        throws CmsException {

        m_importFile = importFile;
        m_importPath = importPath;
        m_cms = cms;

        // create the digest
        createDigest();

        // open the import resource
        getImportResource();

        // read the xml-config file
        getXmlConfigFile();

        // try to read the export version nummber
        try{
            m_importVersion = Integer.parseInt(
                getTextNodeValue((Element)m_docXml.getElementsByTagName(
                    C_EXPORT_TAG_INFO).item(0) , C_EXPORT_TAG_VERSION));
        }catch(Exception e){
            //ignore the exception, the export file has no version nummber (version 0).
        }
    }
/**
 * Read infos from the properties and create a MessageDigest
 * Creation date: (29.08.00 15:45:35)
 */
private void createDigest() throws CmsException {
    // Configurations config = m_cms.getConfigurations();

    String digest = C_IMPORT_DIGEST;
    // create the digest
    try {
        m_digest = MessageDigest.getInstance(digest);
    } catch (NoSuchAlgorithmException e) {
        throw new CmsException("Could'nt create MessageDigest with algorithm " + digest);
    }
}
    /**
     * Creates missing property definitions if needed.
     *
     * @param name the name of the property.
     * @param propertyType the type of the property.
     * @param resourceType the type of the resource.
     *
     * @exception throws CmsException if something goes wrong.
     */
    private void createPropertydefinition(String name, String resourceType)
        throws CmsException {
        // does the propertydefinition exists already?
        try {
            m_cms.readPropertydefinition(name, resourceType);
        } catch(CmsException exc) {
            // no: create it
            m_cms.createPropertydefinition(name, resourceType);
        }
    }

    /**
     * checks if the file sticks to the rules for files in the conten path.
     * If not, it sets the type of the file to compatible_plain.
     * This is for exports of older versions of OpenCms. The imported files
     * will work as befor, but they cant be edited.
     *
     * @param path the path the resource will be imported to.
     * @param name The name of the resource.
     * @param content the content of the resource.
     * @param type the type of the resourse, is set to compatible_plain if nessesary.
     * @param properties the properties, not yet used here.
     * @return the new type of the resouce
     */
    private String fitFileType(String path, String name, byte[] content, String type, Hashtable properties){

        // only check the file if the version of the export is 0
        if(m_importVersion == 0){
            // ok, an old system exported this, check if the file is ok
            if(!(new CmsCompatibleCheck()).isTemplateCompatible(path+name, content, type)){
                type = C_TYPE_COMPATIBLEPLAIN_NAME;
                System.out.print(" must set to "+C_TYPE_COMPATIBLEPLAIN_NAME+" ");
            }
        }
        return type;
    }

/**
 * Returns a list of files which are both in the import and in the virtual file system
 * Creation date: (24.08.00 16:18:23)
 * @return java.util.Vector of Strings, complete path of the files
 */
public Vector getConflictingFilenames() throws CmsException {
    NodeList fileNodes;
    Element currentElement, currentProperty;
    String source, destination, path;
    Vector conflictNames = new Vector();
    try {
        // get all file-nodes
        fileNodes = m_docXml.getElementsByTagName(C_EXPORT_TAG_FILE);

        // walk through all files in manifest
        for (int i = 0; i < fileNodes.getLength(); i++) {
            currentElement = (Element) fileNodes.item(i);
            source = getTextNodeValue(currentElement, C_EXPORT_TAG_SOURCE);
            destination = getTextNodeValue(currentElement, C_EXPORT_TAG_DESTINATION);
            path = m_importPath + destination;
            if (source != null) {
                // only consider files
                boolean exists = true;
                try {
                    CmsResource res=m_cms.readFileHeader(m_importPath + destination);
                    if (res.getState()==C_STATE_DELETED) {
                        exists=false;
                    }
                } catch (CmsException e) {
                    exists = false;
                }
                if (exists) {
                    conflictNames.addElement(m_importPath + destination);
                }
            }
        }
    } catch (Exception exc) {
        throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
    }
    if (m_importZip != null)
    {
      try
      {
          m_importZip.close();
      } catch (IOException exc) {
          throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
      }
    }
    return conflictNames;
}
    /**
     * Returns a byte-array containing the content of the file.
     *
     * @param filename The name of the file to read.
     * @return bytes[] The content of the file.
     */
    private byte[] getFileBytes(String filename)
        throws Exception{
        // is this a zip-file?
        if(m_importZip != null) {

⌨️ 快捷键说明

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