<commit_before>/*
* File   : $Source: /alkacon/cvs/opencms/src/org/opencms/importexport/Attic/CmsImportModuledata.java,v $
* Date   : $Date: 2003/09/29 07:59:40 $
* Version: $Revision: 1.13 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2002 - 2003 Alkacon Software (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, 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.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.util.CmsUUID;

import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.defaults.master.CmsMasterContent;
import com.opencms.defaults.master.CmsMasterDataSet;
import com.opencms.defaults.master.CmsMasterMedia;
import com.opencms.file.CmsObject;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.util.Encoder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.zip.ZipEntry;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Holds the functionaility to import resources from the filesystem
 * or a zip file into the OpenCms COS.
 *
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (<EMAIL>) 
 * 
 * @version $Revision: 1.13 $ $Date: 2003/09/29 07:59:40 $
 */
public class CmsImportModuledata extends CmsImport implements Serializable {

    /**
     * Constructs a new import object which imports the module data 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 CmsException if something goes wrong
     */
    public CmsImportModuledata(CmsObject cms, String importFile, String importPath, I_CmsReport report) throws CmsException {
        // set member variables
        m_cms = cms;
        m_importFile = importFile;
        m_importPath = importPath;
        m_report = report;
        m_importingChannelData = true;
        // try to get all import implementations
         // This has only made once.
         if (m_ImportImplementations == null) {
             m_ImportImplementations=OpenCms.getRegistry().getImportClasses();
         }     
     }

    /**
     * Imports the moduledata and writes them to the cms even if there already exist 
     * conflicting files.<p>
     * @throws CmsException in case something goes wrong
     */
    public void importResources() throws CmsException {
        // initialize the import
        openImportFile();
        try {
            // first import the channels
            m_report.println(m_report.key("report.import_channels_begin"), I_CmsReport.C_FORMAT_HEADLINE);
            //importAllResources(null, null, null, null, null);
            // now find the correct import implementation    
            m_cms.getRequestContext().saveSiteRoot();
            m_cms.setContextToCos();     
            Iterator i=m_ImportImplementations.iterator();
                while (i.hasNext()) {
                    I_CmsImport imp=((I_CmsImport)i.next());
                    if (imp.getVersion()==m_importVersion) {
                        // this is the correct import version, so call it for the import process
                        imp.importResources(m_cms, m_importPath, m_report, 
                                        m_digest, m_importResource, m_importZip, m_docXml, null, null, null, null, null);
                        break;                    
                     }
                 }   
            m_cms.getRequestContext().restoreSiteRoot();
            m_report.println(m_report.key("report.import_channels_end"), I_CmsReport.C_FORMAT_HEADLINE);
 
            // now import the moduledata
            m_report.println(m_report.key("report.import_moduledata_begin"), I_CmsReport.C_FORMAT_HEADLINE);
            importModuleMasters();
            m_report.println(m_report.key("report.import_moduledata_end"), I_CmsReport.C_FORMAT_HEADLINE);
        } catch (CmsException e) {
            throw e;
        } finally {
            // close the import file
            closeImportFile();
        }
    }

    /**
     * Gets the available modules in the current system
     * and imports the data for existing modules.<p>
     * @throws CmsException in case something goes wrong
     */
    public void importModuleMasters() throws CmsException {
        // get all available modules in this system
        Hashtable moduleExportables = new Hashtable();
        m_cms.getRegistry().getModuleExportables(moduleExportables);
        // now get the subIds of each module
        Hashtable availableModules = new Hashtable();
        Enumeration modElements = moduleExportables.elements();
        while (modElements.hasMoreElements()) {
            String classname = (String)modElements.nextElement();
            // get the subId of the module
            try {
                int subId = getContentDefinition(classname, new Class[] {CmsObject.class }, new Object[] {m_cms }).getSubId();
                // put the subid and the classname into the hashtable of available modules
                availableModules.put("" + subId, classname);
            } catch (Exception e) {
                // do nothing
            }

        }
        // now get the moduledata for import
        NodeList masterNodes;
        Element currentElement;
        String subid;

        try {
            // get all master-nodes
            masterNodes = m_docXml.getElementsByTagName(CmsExportModuledata.C_EXPORT_TAG_MASTER);
            int length = masterNodes.getLength();

            // walk through all files in manifest
            for (int i = 0; i < length; i++) {
                currentElement = (Element)masterNodes.item(i);
                // get the subid of the modulemaster
                subid = getTextNodeValue(currentElement, CmsExportModuledata.C_EXPORT_TAG_MASTER_SUBID);
                // check if there exists a module with this subid
                String classname = (String)availableModules.get(subid);
                if ((classname != null) && !("".equals(classname.trim()))) {
                    // import the dataset, the channelrelation and the media
                    m_report.print(" ( " + (i + 1) + " / " + length + " ) ");
                    importMaster(subid, classname, currentElement);
                }
            }
        } catch (Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Imports a single master.<p>
     * 
     * @param subId the subid of the module
     * @param classname the name of the module class
     * @param currentElement the current element of the xml file
     * @throws CmsException in case something goes wrong
     */
    private void importMaster(String subId, String classname, Element currentElement) throws CmsException {
        // print out some information to the report
        m_report.print(m_report.key("report.importing"), I_CmsReport.C_FORMAT_NOTE);

        CmsMasterDataSet newDataset = new CmsMasterDataSet();
        Vector channelRelations = new Vector();<commit_msg>Changed report output color of counter<commit_after>/*
* File   : $Source: /alkacon/cvs/opencms/src/org/opencms/importexport/Attic/CmsImportModuledata.java,v $
* Date   : $Date: 2003/10/15 07:38:37 $
* Version: $Revision: 1.14 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2002 - 2003 Alkacon Software (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, 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.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.util.CmsUUID;

import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.defaults.master.CmsMasterContent;
import com.opencms.defaults.master.CmsMasterDataSet;
import com.opencms.defaults.master.CmsMasterMedia;
import com.opencms.file.CmsObject;
import com.opencms.template.A_CmsXmlContent;
import com.opencms.util.Encoder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.zip.ZipEntry;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Holds the functionaility to import resources from the filesystem
 * or a zip file into the OpenCms COS.
 *
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (<EMAIL>) 
 * 
 * @version $Revision: 1.14 $ $Date: 2003/10/15 07:38:37 $
 */
public class CmsImportModuledata extends CmsImport implements Serializable {

    /**
     * Constructs a new import object which imports the module data 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 CmsException if something goes wrong
     */
    public CmsImportModuledata(CmsObject cms, String importFile, String importPath, I_CmsReport report) throws CmsException {
        // set member variables
        m_cms = cms;
        m_importFile = importFile;
        m_importPath = importPath;
        m_report = report;
        m_importingChannelData = true;
        // try to get all import implementations
         // This has only made once.
         if (m_ImportImplementations == null) {
             m_ImportImplementations=OpenCms.getRegistry().getImportClasses();
         }     
     }

    /**
     * Imports the moduledata and writes them to the cms even if there already exist 
     * conflicting files.<p>
     * @throws CmsException in case something goes wrong
     */
    public void importResources() throws CmsException {
        // initialize the import
        openImportFile();
        try {
            // first import the channels
            m_report.println(m_report.key("report.import_channels_begin"), I_CmsReport.C_FORMAT_HEADLINE);
            //importAllResources(null, null, null, null, null);
            // now find the correct import implementation    
            m_cms.getRequestContext().saveSiteRoot();
            m_cms.setContextToCos();     
            Iterator i=m_ImportImplementations.iterator();
                while (i.hasNext()) {
                    I_CmsImport imp=((I_CmsImport)i.next());
                    if (imp.getVersion()==m_importVersion) {
                        // this is the correct import version, so call it for the import process
                        imp.importResources(m_cms, m_importPath, m_report, 
                                        m_digest, m_importResource, m_importZip, m_docXml, null, null, null, null, null);
                        break;                    
                     }
                 }   
            m_cms.getRequestContext().restoreSiteRoot();
            m_report.println(m_report.key("report.import_channels_end"), I_CmsReport.C_FORMAT_HEADLINE);
 
            // now import the moduledata
            m_report.println(m_report.key("report.import_moduledata_begin"), I_CmsReport.C_FORMAT_HEADLINE);
            importModuleMasters();
            m_report.println(m_report.key("report.import_moduledata_end"), I_CmsReport.C_FORMAT_HEADLINE);
        } catch (CmsException e) {
            throw e;
        } finally {
            // close the import file
            closeImportFile();
        }
    }

    /**
     * Gets the available modules in the current system
     * and imports the data for existing modules.<p>
     * @throws CmsException in case something goes wrong
     */
    public void importModuleMasters() throws CmsException {
        // get all available modules in this system
        Hashtable moduleExportables = new Hashtable();
        m_cms.getRegistry().getModuleExportables(moduleExportables);
        // now get the subIds of each module
        Hashtable availableModules = new Hashtable();
        Enumeration modElements = moduleExportables.elements();
        while (modElements.hasMoreElements()) {
            String classname = (String)modElements.nextElement();
            // get the subId of the module
            try {
                int subId = getContentDefinition(classname, new Class[] {CmsObject.class }, new Object[] {m_cms }).getSubId();
                // put the subid and the classname into the hashtable of available modules
                availableModules.put("" + subId, classname);
            } catch (Exception e) {
                // do nothing
            }

        }
        // now get the moduledata for import
        NodeList masterNodes;
        Element currentElement;
        String subid;

        try {
            // get all master-nodes
            masterNodes = m_docXml.getElementsByTagName(CmsExportModuledata.C_EXPORT_TAG_MASTER);
            int length = masterNodes.getLength();

            // walk through all files in manifest
            for (int i = 0; i < length; i++) {
                currentElement = (Element)masterNodes.item(i);
                // get the subid of the modulemaster
                subid = getTextNodeValue(currentElement, CmsExportModuledata.C_EXPORT_TAG_MASTER_SUBID);
                // check if there exists a module with this subid
                String classname = (String)availableModules.get(subid);
                if ((classname != null) && !("".equals(classname.trim()))) {
                    // import the dataset, the channelrelation and the media
                    m_report.print(" ( " + (i + 1) + " / " + length + " ) ", I_CmsReport.C_FORMAT_NOTE);
                    importMaster(subid, classname, currentElement);
                }
            }
        } catch (Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Imports a single master.<p>
     * 
     * @param subId the subid of the module
     * @param classname the name of the module class
     * @param currentElement the current element of the xml file
     * @throws CmsException in case something goes wrong
     */
    private void importMaster(String subId, String classname, Element currentElement) throws CmsException {
        // print out some information to the report
        m_report.print(m_report.key("report.importing"), I_CmsReport.C_FORMAT_NOTE);

        CmsMasterDataSet newDataset = new CmsMasterDataSet();
        Vector channelRelations = new Vector();
{'fraction_non_alphanumeric': 0.08535747021081576, 'fraction_numerical': 0.007218148487626031, 'mean_word_length': 2.525242326332795, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6715805', 'n_tokens_mistral': 4933, 'n_tokens_neox': 4725, 'n_words': 1469}