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

📄 cmsexportmoduledata.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                         
        // the name of the XML-file where the dataset is stored
        String dataSetFile = "dataset_"+subId+"_"+masterNr+".xml";
        // create new mastercontent for getting channels and media
        CmsMasterContent content = getContentDefinition(classname, new Class[]{CmsObject.class, CmsMasterDataSet.class}, new Object[]{m_cms, dataset});
        // write these informations to the xml-manifest
        Element master = m_docXml.createElement(C_EXPORT_TAG_MASTER);
        m_mastersElement.appendChild(master);

        addElement(m_docXml, master, C_EXPORT_TAG_MASTER_SUBID, ""+subId);
        // add the name of the datasetfile and create the datasetfile
        // with the information from the dataset
        addElement(m_docXml, master, C_EXPORT_TAG_MASTER_DATASET, dataSetFile);
        writeExportDataset(dataset, dataSetFile, masterNr, subId);
        // add the channel relation of this master
        Element channelrel = m_docXml.createElement(C_EXPORT_TAG_MASTER_CHANNELREL);
        master.appendChild(channelrel);
        Vector moduleChannels = content.getChannels();
        for(int i=0; i<moduleChannels.size(); i++){
            String channelname = (String)moduleChannels.elementAt(i);
            addElement(m_docXml, channelrel, C_EXPORT_TAG_MASTER_CHANNELNAME, channelname);
        }
        // add the mediaset
        Element mediaset = m_docXml.createElement(C_EXPORT_TAG_MASTER_MEDIASET);
        master.appendChild(mediaset);
        Vector moduleMedia = content.getMedia();
        for(int i=0; i<moduleMedia.size(); i++){
            // for each media add the name of the xml-file for the mediadata to the manifest
            // and create the files for the media information
            String mediaFile = "media_"+subId+"_"+masterNr+"_"+i+".xml";
            addElement(m_docXml, mediaset, C_EXPORT_TAG_MASTER_MEDIA, mediaFile);
            writeExportMediaset((CmsMasterMedia)moduleMedia.elementAt(i), mediaFile, masterNr, subId, i);
        }
        
        m_report.print(m_report.key("report.dots"), I_CmsReport.C_FORMAT_NOTE);
        m_report.println(m_report.key("report.ok"), I_CmsReport.C_FORMAT_OK);        
    }

    /**
     * Returns a master content definition object instance created with the reflection API.<p>
     * 
     * @param classname name of the content definition class
     * @param classes required for constructor generation 
     * @param objects instances to be used as parameters for class instance generation
     * 
     * @return a master content definition object instance created with the reflection API
     */
    private CmsMasterContent getContentDefinition(
        String classname, 
        Class[] classes, 
        Object[] objects
    ) {
        CmsMasterContent cd = null;
        try {
            Class cdClass = Class.forName(classname);
            Constructor co = cdClass.getConstructor(classes);
            cd = (CmsMasterContent)co.newInstance(objects);
        } catch (InvocationTargetException ite) {
            m_report.println(ite);
            if (I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO, "[CmsExportModuledata] "+classname + " contentDefinitionConstructor: Invocation target exception!");
            }
        } catch (NoSuchMethodException nsm) {
            m_report.println(nsm);
            if (I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO, "[CmsExportModuledata] "+classname + " contentDefinitionConstructor: Requested method was not found!");
            }
        } catch (InstantiationException ie) {
            m_report.println(ie);
            if (I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO, "[CmsExportModuledata] "+classname + " contentDefinitionConstructor: the reflected class is abstract!");
            }
        } catch (Exception e) {
            m_report.println(e);
            if (I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO, "[CmsExportModuledata] "+classname + " contentDefinitionConstructor: Other exception! "+e);
            }
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
                A_OpenCms.log(A_OpenCms.C_OPENCMS_INFO, e.getMessage() );
            }
        }
        return cd;
    }
    
    /**
     * Exports a content definition content in a "dataset_xxx.xml" file and a number of 
     * "datayyy_xxx.dat" files.<p>
     * 
     * @param dataset data for the content definition object instance
     * @param filename name of the zip file for the module data export
     * @param masterNr id of master
     * @param subId id of content definition
     * 
     * @throws CmsException if something goes wrong
     */
    private void writeExportDataset(
        CmsMasterDataSet dataset, 
        String filename, 
        int masterNr, 
        int subId
    ) throws CmsException{
        // creates the XML-document
        Document xmlDoc = null;
        try{
            xmlDoc = A_CmsXmlContent.getXmlParser().createEmptyDocument(C_EXPORT_TAG_MODULEXPORT);
        } catch(Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
        // add the data element
        Element elementDataset = xmlDoc.createElement(C_EXPORT_TAG_MASTER_DATASET);
        xmlDoc.getDocumentElement().appendChild(elementDataset);
        // add the data of the contentdefinition
        // get the name of the owner
        String ownerName = "";
        try{
            ownerName = m_cms.readUser(dataset.m_userId).getName();
        } catch (CmsException e){
        }
        // get the name of the group
        String groupName = "";
        try{
            groupName = m_cms.readGroup(dataset.m_groupId).getName();
        } catch (CmsException e){
        }
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_USER, ownerName);
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_GROUP, groupName);
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_ACCESSFLAGS, ""+dataset.m_accessFlags);
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_PUBLICATIONDATE, Utils.getNiceDate(dataset.m_publicationDate));
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_PURGEDATE, Utils.getNiceDate(dataset.m_purgeDate));
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_FLAGS, ""+dataset.m_flags);
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_FEEDID, ""+dataset.m_feedId);
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_FEEDREFERENCE, ""+dataset.m_feedReference);
        addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_FEEDFILENAME, dataset.m_feedFilename);
        addCdataElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_TITLE, dataset.m_title);
        // get the values of data_big from the string array
        for(int i=0; i< dataset.m_dataBig.length; i++){
            String value = dataset.m_dataBig[i];
            String dataFile = new String();
            if(value != null && !"".equals(value)){
                // the name of the file where the value of the field is stored
                dataFile = "databig_"+subId+"_"+masterNr+"_"+i+".dat";
                writeExportContentFile(dataFile, value.getBytes());
            }
            addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_DATABIG+i, dataFile);
        }
        // get the values of data_medium from the string array
        for(int i=0; i< dataset.m_dataMedium.length; i++){
            String value = dataset.m_dataMedium[i];
            String dataFile = new String();
            if(value != null && !"".equals(value)){
                // the name of the file where the value of the field is stored
                dataFile = "datamedium_"+subId+"_"+masterNr+"_"+i+".dat";
                writeExportContentFile(dataFile, value.getBytes());
            }
            addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_DATAMEDIUM+i, dataFile);
        }
        // get the values of data_small from the string array
        for(int i=0; i< dataset.m_dataSmall.length; i++){
            String value = dataset.m_dataSmall[i];
            String dataFile = new String();
            if(value != null && !"".equals(value)){
                // the name of the file where the value of the field is stored
                dataFile = "datasmall_"+subId+"_"+masterNr+"_"+i+".dat";
                writeExportContentFile(dataFile, value.getBytes());
            }
            addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_DATASMALL+i, dataFile);
        }
        // get the values of data_int from the int array
        for(int i=0; i< dataset.m_dataInt.length; i++){
            String value = ""+dataset.m_dataInt[i];
            addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_DATAINT+i, value);
        }
        // get the values of data_reference from the int array
        for(int i=0; i< dataset.m_dataReference.length; i++){
            String value = ""+dataset.m_dataReference[i];
            addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_DATAREFERENCE+i, value);
        }
        // get the values of data_reference from the int array
        for(int i=0; i< dataset.m_dataDate.length; i++){
            String value = Utils.getNiceDate(dataset.m_dataDate[i]);
            addElement(xmlDoc, elementDataset, C_EXPORT_TAG_MASTER_DATADATE+i, value);
        }

        try {
            ZipEntry entry = new ZipEntry(filename);
            m_exportZipStream.putNextEntry(entry);
            A_CmsXmlContent.getXmlParser().getXmlText(xmlDoc, m_exportZipStream, null);
            m_exportZipStream.closeEntry();
        } catch(Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Exports a media object, creates a "media_xxx.xml" and a "mediacontent_xxx.dat" data file.<p> 
     * 
     * @param media data for the media object instance
     * @param filename name of the xml file for the media data export
     * @param masterNr id of master
     * @param subId id of content definition
     * @param mediaId if od media object
     * 
     * @throws CmsException if something goes wrong
     */
    private void writeExportMediaset(
        CmsMasterMedia media, 
        String filename, 
        int masterNr, 
        int subId, 
        int mediaId
    ) throws CmsException{
        // creates the XML-document
        Document xmlDoc = null;
        try{
            xmlDoc = A_CmsXmlContent.getXmlParser().createEmptyDocument(C_EXPORT_TAG_MODULEXPORT);
        } catch(Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
        // add the data element
        Element elementMedia = xmlDoc.createElement("media");
        xmlDoc.getDocumentElement().appendChild(elementMedia);
        // add the data of the contentdefinition
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_POSITION, ""+media.getPosition());
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_WIDTH, ""+media.getWidth());
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_HEIGHT, ""+media.getHeight());
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_SIZE, ""+media.getSize());
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_MIMETYPE, media.getMimetype());
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_TYPE, ""+media.getType());
        addCdataElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_TITLE, media.getTitle());
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_NAME, media.getName());
        addCdataElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_DESCRIPTION, media.getDescription());
        // now add the name of the file where the media content is stored and write this file
        String contentFilename = "mediacontent_"+subId+"_"+masterNr+"_"+mediaId+".dat";
        addElement(xmlDoc, elementMedia, C_EXPORT_TAG_MEDIA_CONTENT, contentFilename);
        writeExportContentFile(contentFilename, media.getMedia());
        // write the media xml-file
        try {
            ZipEntry entry = new ZipEntry(filename);
            m_exportZipStream.putNextEntry(entry);
            A_CmsXmlContent.getXmlParser().getXmlText(xmlDoc, m_exportZipStream, null);
            m_exportZipStream.closeEntry();
        } catch(Exception exc) {
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Writes a binary content to a "*.dat" file in the export zip.<p>
     * 
     * @param filename name of the file, usually ends with .dat
     * @param content contents to write to the file
     */
    private void writeExportContentFile(
        String filename, 
        byte[] content
    ) {
        try{
            // store the userinfo in zip-file
            ZipEntry entry = new ZipEntry(filename);
            m_exportZipStream.putNextEntry(entry);
            m_exportZipStream.write(content);
            m_exportZipStream.closeEntry();
        } catch (IOException ioex){
            m_report.println(ioex);
            System.err.println("IOException: "+ioex.getMessage());
        }
    }
}

⌨️ 快捷键说明

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