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

📄 cmsexport.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        Element newElement = docXml.createElement(name);
        element.appendChild(newElement);
        Text text = docXml.createTextNode(value);
        newElement.appendChild(text);
    }
    
    /**
     * Creates the export XML file and appends the initial tags to it.<p>
     * 
     * @param moduleNode a node with module informations
     * @throws CmsException if something goes wrong
     */
    private void getXmlConfigFile(Node moduleNode) throws CmsException {

        try {
            // creates the document
            if (m_exportingModuleData) {
                // module (COS) data is exported
                m_docXml = A_CmsXmlContent.getXmlParser().createEmptyDocument(C_EXPORT_TAG_MODULEXPORT);                
            } else {
                // standard export, only VFS resources are exported
                m_docXml = A_CmsXmlContent.getXmlParser().createEmptyDocument(C_EXPORT_TAG_EXPORT);
            }
            // abbends the initital tags
            Node exportNode = m_docXml.getFirstChild();

            // add the info element. it contains all infos for this export
            Element info = m_docXml.createElement(C_EXPORT_TAG_INFO);
            m_docXml.getDocumentElement().appendChild(info);
            addElement(m_docXml, info, C_EXPORT_TAG_CREATOR, m_cms.getRequestContext().currentUser().getName());
            addElement(m_docXml, info, C_EXPORT_TAG_OC_VERSION, A_OpenCms.getVersionName());
            addElement(m_docXml, info, C_EXPORT_TAG_DATE, Utils.getNiceDate(new Date().getTime()));
            addElement(m_docXml, info, C_EXPORT_TAG_PROJECT, m_cms.getRequestContext().currentProject().getName());
            addElement(m_docXml, info, C_EXPORT_TAG_VERSION, C_EXPORT_VERSION);

            if (moduleNode != null) {
                // this is a module export - import module informations here
                exportNode.appendChild(A_CmsXmlContent.getXmlParser().importNode(m_docXml, moduleNode));
                // now remove the unused file informations
                NodeList files = ((Element)exportNode).getElementsByTagName("files");
                if (files.getLength() == 1) {
                    files.item(0).getParentNode().removeChild(files.item(0));
                }
            }
            if (m_exportingModuleData) {
                // add the root element for the channels
                m_filesElement = m_docXml.createElement(C_EXPORT_TAG_CHANNELS);
                m_docXml.getDocumentElement().appendChild(m_filesElement);
                // add the root element for the masters
                m_mastersElement = m_docXml.createElement(C_EXPORT_TAG_MASTERS);
                m_docXml.getDocumentElement().appendChild(m_mastersElement);
            } else {
                // standard export, add not for the files
                m_filesElement = m_docXml.createElement(C_EXPORT_TAG_FILES);
                m_docXml.getDocumentElement().appendChild(m_filesElement);
                
            }
            if (m_exportUserdata) {
                // add userdata
                m_userdataElement = m_docXml.createElement(C_EXPORT_TAG_USERGROUPDATA);
                m_docXml.getDocumentElement().appendChild(m_userdataElement);
            }
        } catch (Exception exc) {
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }
    
    /**
     * Writes the <code>manifex.xml</code> configuration file to the esport zip file.<p>
     * 
     * @throws CmsException if something goes wrong
     */
    private void writeXmlConfigFile() throws CmsException {
        try {
            ZipEntry entry = new ZipEntry(C_EXPORT_XMLFILENAME);
            m_exportZipStream.putNextEntry(entry);
            A_CmsXmlContent.getXmlParser().getXmlText(m_docXml, m_exportZipStream, A_OpenCms.getDefaultEncoding());
            m_exportZipStream.closeEntry();
        } catch (Exception exc) {
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Gets the export resource and stores it in a member variable.<p>
     * 
     * @throws CmsException if something goes wrong
     */
    private void getExportResource() throws CmsException {
        try {
            // add zip-extension, if needed
            if (!m_exportFile.toLowerCase().endsWith(".zip")) {
                m_exportFile += ".zip";
            }
            // create the export-zipstream
            m_exportZipStream = new ZipOutputStream(new FileOutputStream(m_exportFile));
        } catch (Exception exc) {
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Exports all page body files that have not explicityl been added by the user.<p>
     * 
     * @throws CmsException if something goes wrong
     */
    private void addPageBodyFiles() throws CmsException {
        Iterator i;   
        
        Vector bodyFileNames = new Vector();
        String bodyPath = I_CmsWpConstants.C_VFS_PATH_BODIES.substring(0, I_CmsWpConstants.C_VFS_PATH_BODIES.lastIndexOf("/"));
        
        // check all exported page files if their body has already been exported
        i = m_exportedPageFiles.iterator();
        while (i.hasNext()) {
            String body = bodyPath + (String)i.next();
            if (! isRedundant(body)) {
                bodyFileNames.add(body);
            }
        }
        
        // now export the body files that have not already been exported
        addSingleFiles(bodyFileNames);
    }

    /**
     * Adds all files with names in fileNames to the xml-config file.<p>
     * 
     * @param fileNames Vector of path Strings, e.g. <code>/folder/index.html</code>
     * @throws CmsException if something goes wrong
     */
    private void addSingleFiles(Vector fileNames) throws CmsException {
        if (fileNames != null) {
            for (int i = 0; i < fileNames.size(); i++) {
                String fileName = (String) fileNames.elementAt(i);
                try {
                    CmsFile file = m_cms.readFile(fileName);
                    if((file.getState() != C_STATE_DELETED) && (!file.getName().startsWith("~"))) {
                        addSuperFolders(fileName);
                        exportResource(file);
                        m_exportedResources.add(fileName);
                    }
                } catch(CmsException exc) {
                    if(exc.getType() != CmsException.C_RESOURCE_DELETED) {
                        throw exc;
                    }
                }
            }
        }
    }

    /**
     * Adds the superfolders of path to the config file, starting at the top, 
     * excluding the root folder.<p>
     * 
     * @param path the path of the folder in the virtual files system (VFS) 
     * @throws CmsException if something goes wrong
     */
    private void addSuperFolders(String path) throws CmsException {
        // Initialize the "previously added folder cache"
        if (m_superFolders == null) {
            m_superFolders = new Vector();
        }
        Vector superFolders = new Vector();

        // Check, if the path is really a folder
        if (path.lastIndexOf(C_ROOT) != (path.length() - 1)) {
            path = path.substring(0, path.lastIndexOf(C_ROOT) + 1);
        }
        while (path.length() > C_ROOT.length()) {
            superFolders.addElement(path);
            path = path.substring(0, path.length() - 1);
            path = path.substring(0, path.lastIndexOf(C_ROOT) + 1);
        }
        for (int i = superFolders.size() - 1; i >= 0; i--) {
            String addFolder = (String)superFolders.elementAt(i);
            if (!m_superFolders.contains(addFolder)) {
                // This super folder was NOT added previously. Add it now!
                CmsFolder folder = m_cms.readFolder(addFolder);
                writeXmlEntrys(folder);
                // Remember that this folder was added
                m_superFolders.addElement(addFolder);
            }
        }
    }

    /**
     * Checks if a given resource is already included in the export
     * or not.<p>
     * 
     * @param resourcename the VFS resource name to check
     * @return <code>true</code> if the resource must not be exported again, <code>false</code> otherwise
     */
    private boolean isRedundant(String resourcename) {
        if (m_exportedResources == null)
            return false;
        Iterator i = m_exportedResources.iterator();
        while (i.hasNext()) {
            String s = (String)i.next();
            if (resourcename.startsWith(s))
                return true;
        }
        return false;
    }

    /** 
     * Checks whether some of the resources are redundant because a superfolder has also
     * been selected or a file is included in a folder.<p>
     * 
     * @param folderNames contains the full pathnames of all folders
     * @param fileNames contains the full pathnames of all files
     */
    private void checkRedundancies(Vector folderNames, Vector fileNames) {
        int i, j;
        if (folderNames == null) {
            return;
        }
        Vector redundant = new Vector();
        int n = folderNames.size();
        if (n > 1) {
            // otherwise no check needed, because there is only one resource

            for (i = 0; i < n; i++) {
                redundant.addElement(new Boolean(false));
            }
            for (i = 0; i < n - 1; i++) {
                for (j = i + 1; j < n; j++) {
                    if (((String)folderNames.elementAt(i)).length() < ((String)folderNames.elementAt(j)).length()) {
                        if (((String)folderNames.elementAt(j)).startsWith((String)folderNames.elementAt(i))) {
                            redundant.setElementAt(new Boolean(true), j);
                        }
                    } else {
                        if (((String)folderNames.elementAt(i)).startsWith((String)folderNames.elementAt(j))) {
                            redundant.setElementAt(new Boolean(true), i);
                        }
                    }
                }
            }
            for (i = n - 1; i >= 0; i--) {
                if (((Boolean)redundant.elementAt(i)).booleanValue()) {
                    folderNames.removeElementAt(i);
                }
            }
        }
        // now remove the files who are included automatically in a folder
        // otherwise there would be a zip exception

        for (i = fileNames.size() - 1; i >= 0; i--) {
            for (j = 0; j < folderNames.size(); j++) {
                if (((String)fileNames.elementAt(i)).startsWith((String)folderNames.elementAt(j))) {
                    fileNames.removeElementAt(i);
                }
            }
        }
    }
    
    /**
     * Exports one single file with all its data and content.<p>
     *
     * @param file the file to be exported
     * @throws CmsException if something goes wrong
     */
    private void exportResource(CmsFile file) throws CmsException {
        String source = getSourceFilename(file.getAbsolutePath());

        m_report.print(m_report.key("report.exporting"), I_CmsReport.C_FORMAT_NOTE);
        m_report.print(file.getAbsolutePath());
        m_report.print(m_report.key("report.dots"), I_CmsReport.C_FORMAT_NOTE);
        try {
            // create the manifest-entrys
            writeXmlEntrys((CmsResource)file);
            // store content in zip-file
            ZipEntry entry = new ZipEntry(source);
            m_exportZipStream.putNextEntry(entry);
            m_exportZipStream.write(file.getContents());
            m_exportZipStream.closeEntry();
        } catch (Exception exc) {
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }

        if (file.getType() == m_pageType) {
            m_exportedPageFiles.add(file.getAbsolutePath());
        }

        m_report.println(m_report.key("report.ok"), I_CmsReport.C_FORMAT_OK);
    }
    
    /**
     * Exports all needed sub-resources to the zip-file.<p>
     *
     * @param path to complete path to the resource to export
     * @throws CmsException if something goes wrong
     */
    private void exportResources(String path) throws CmsException {
        
        if (m_exportingModuleData) {
            // collect channel id information if required
            String channelId = m_cms.readProperty(path, I_CmsConstants.C_PROPERTY_CHANNELID);
            if (channelId != null) {
                if(! m_exportedChannelIds.contains(channelId)) {
                    m_exportedChannelIds.add(channelId);
                }
            }
        }
                
        // get all subFolders
        Vector subFolders = m_cms.getSubFolders(path);
        // get all files in folder
        Vector subFiles = m_cms.getFilesInFolder(path);

        // walk through all files and export them
        for (int i = 0; i < subFiles.size(); i++) {
            CmsResource file = (CmsResource)subFiles.elementAt(i);
            int state = file.getState();
            long age = file.getDateLastModified();

⌨️ 快捷键说明

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