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

📄 cmsexport.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        boolean exportUserdata,
        long contentAge,
        I_CmsReport report,
        boolean recursive)
    throws CmsImportExportException, CmsRoleViolationException {

        this(
            cms,
            exportFile,
            resourcesToExport,
            includeSystem,
            includeUnchanged,
            moduleElement,
            exportUserdata,
            false,
            contentAge,
            report,
            recursive);
    }

    /**
     * Exports the given folder and all child resources.<p>
     *
     * @param folderName to complete path to the resource to export
     * @throws CmsImportExportException if something goes wrong
     * @throws SAXException if something goes wrong procesing the manifest.xml
     * @throws IOException if not all resources could be appended to the ZIP archive
     */
    protected void addChildResources(String folderName) throws CmsImportExportException, IOException, SAXException {

        try {
            // get all subFolders
            List subFolders = getCms().getSubFolders(folderName, CmsResourceFilter.IGNORE_EXPIRATION);
            // get all files in folder
            List subFiles = getCms().getFilesInFolder(folderName, CmsResourceFilter.IGNORE_EXPIRATION);

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

                if (getCms().getRequestContext().currentProject().isOnlineProject()
                    || (m_includeUnchanged)
                    || state == CmsResource.STATE_NEW
                    || state == CmsResource.STATE_CHANGED) {
                    if ((state != CmsResource.STATE_DELETED)
                        && (!file.getName().startsWith("~"))
                        && (age >= m_contentAge)) {
                        String export = getCms().getSitePath(file);
                        if (checkExportResource(export)) {
                            exportFile(getCms().readFile(export, CmsResourceFilter.IGNORE_EXPIRATION));
                        }
                    }
                }
                // release file header memory
                subFiles.set(i, null);
            }
            // all files are exported, release memory
            subFiles = null;

            // walk through all subfolders and export them
            for (int i = 0; i < subFolders.size(); i++) {
                CmsResource folder = (CmsResource)subFolders.get(i);
                if (folder.getState() != CmsResource.STATE_DELETED) {
                    // check if this is a system-folder and if it should be included.
                    String export = getCms().getSitePath(folder);
                    if (checkExportResource(export)) {

                        long age = folder.getDateLastModified() < folder.getDateCreated() ? folder.getDateCreated()
                        : folder.getDateLastModified();
                        // export this folder only if age is above selected age
                        // default for selected age (if not set by user) is <code>long 0</code> (i.e. 1970)
                        if (age >= m_contentAge) {
                            // only export folder data to manifest.xml if it has changed
                            appendResourceToManifest(folder, false);
                        }

                        // export all sub-resources in this folder
                        addChildResources(getCms().getSitePath(folder));
                    }
                }
                // release folder memory
                subFolders.set(i, null);
            }
        } catch (CmsImportExportException e) {

            throw e;
        } catch (CmsException e) {

            CmsMessageContainer message = Messages.get().container(
                Messages.ERR_IMPORTEXPORT_ERROR_ADDING_CHILD_RESOURCES_1,
                folderName);
            if (LOG.isDebugEnabled()) {
                LOG.debug(message.key(), e);
            }

            throw new CmsImportExportException(message, e);
        }
    }

    /**
     * Closes the export ZIP file and saves the XML document for the manifest.<p>
     * 
     * @param exportNode the export root node
     * @throws SAXException if something goes wrong procesing the manifest.xml
     * @throws IOException if something goes wrong while closing the export file
     */
    protected void closeExportFile(Element exportNode) throws IOException, SAXException {

        // close the <export> Tag
        getSaxWriter().writeClose(exportNode);

        // close the XML document 
        CmsXmlSaxWriter xmlSaxWriter = (CmsXmlSaxWriter)getSaxWriter().getContentHandler();
        xmlSaxWriter.endDocument();

        // create zip entry for the manifest XML document
        ZipEntry entry = new ZipEntry(CmsImportExportManager.EXPORT_MANIFEST);
        getExportZipStream().putNextEntry(entry);

        // complex substring operation is required to ensure handling for very large export manifest files
        StringBuffer result = ((StringWriter)xmlSaxWriter.getWriter()).getBuffer();
        int steps = result.length() / SUB_LENGTH;
        int rest = result.length() % SUB_LENGTH;
        int pos = 0;
        for (int i = 0; i < steps; i++) {
            String sub = result.substring(pos, pos + SUB_LENGTH);
            getExportZipStream().write(sub.getBytes(OpenCms.getSystemInfo().getDefaultEncoding()));
            pos += SUB_LENGTH;
        }
        if (rest > 0) {
            String sub = result.substring(pos, pos + rest);
            getExportZipStream().write(sub.getBytes(OpenCms.getSystemInfo().getDefaultEncoding()));
        }

        // close the zip entry for the manifest XML document
        getExportZipStream().closeEntry();

        // finally close the zip stream
        getExportZipStream().close();
    }

    /**
     * Writes the output element to the XML output writer and detaches it 
     * from it's parent element.<p> 
     * 
     * @param parent the parent element
     * @param output the output element 
     * @throws SAXException if something goes wrong procesing the manifest.xml
     */
    protected void digestElement(Element parent, Element output) throws SAXException {

        m_saxWriter.write(output);
        parent.remove(output);
    }

    /**
     * Exports all resources and possible sub-folders form the provided list of resources.
     * 
     * @param parent the parent node to add the resources to
     * @param resourcesToExport the list of resources to export
     * @throws CmsImportExportException if something goes wrong
     * @throws SAXException if something goes wrong procesing the manifest.xml
     * @throws IOException if not all resources could be appended to the ZIP archive
     */
    protected void exportAllResources(Element parent, List resourcesToExport)
    throws CmsImportExportException, IOException, SAXException {

        // export all the resources
        String resourceNodeName = getResourceNodeName();
        m_resourceNode = parent.addElement(resourceNodeName);
        getSaxWriter().writeOpen(m_resourceNode);

        if (m_recursive) {
            // remove the possible redundancies in the list of resources
            resourcesToExport = CmsFileUtil.removeRedundancies(resourcesToExport);
        }

        // distinguish folder and file names   
        List folderNames = new ArrayList();
        List fileNames = new ArrayList();
        Iterator it = resourcesToExport.iterator();
        while (it.hasNext()) {
            String resource = (String)it.next();
            if (CmsResource.isFolder(resource)) {
                folderNames.add(resource);
            } else {
                fileNames.add(resource);
            }
        }

        // init sets required for the body file exports 
        m_exportedResources = new HashSet();
        m_exportedPageFiles = new HashSet();

        // export the folders
        for (int i = 0; i < folderNames.size(); i++) {
            String path = (String)folderNames.get(i);
            if (m_recursive) {
                // first add superfolders to the xml-config file
                addParentFolders(path);
                addChildResources(path);
            } else {
                CmsFolder folder;
                try {
                    folder = getCms().readFolder(path, CmsResourceFilter.IGNORE_EXPIRATION);
                } catch (CmsException e) {
                    CmsMessageContainer message = Messages.get().container(
                        Messages.ERR_IMPORTEXPORT_ERROR_ADDING_PARENT_FOLDERS_1,
                        path);
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(message.key(), e);
                    }
                    throw new CmsImportExportException(message, e);
                }
                int state = folder.getState();
                long age = folder.getDateLastModified() < folder.getDateCreated() ? folder.getDateCreated()
                : folder.getDateLastModified();

                if (getCms().getRequestContext().currentProject().isOnlineProject()
                    || (m_includeUnchanged)
                    || state == CmsResource.STATE_NEW
                    || state == CmsResource.STATE_CHANGED) {
                    if ((state != CmsResource.STATE_DELETED) && (age >= m_contentAge)) {
                        // check if this is a system-folder and if it should be included.
                        String export = getCms().getSitePath(folder);
                        if (checkExportResource(export)) {
                            appendResourceToManifest(folder, false);
                        }
                    }
                }
            }
            m_exportedResources.add(path);
        }
        // export the files
        addFiles(fileNames);
        // export all body files that have not already been exported
        addPageBodyFiles();

        // write the XML
        getSaxWriter().writeClose(m_resourceNode);
        parent.remove(m_resourceNode);
        m_resourceNode = null;
    }

    /**
     * Returns the OpenCms context object this export was initialized with.<p>
     * 
     * @return the OpenCms context object this export was initialized with
     */
    protected CmsObject getCms() {

        return m_cms;
    }

    /**
     * Returns the name of the export file.<p>
     * 
     * @return the name of the export file
     */
    protected String getExportFileName() {

        return m_exportFileName;
    }

    /**
     * Returns the name of the main export node.<p>
     * 
     * @return the name of the main export node
     */
    protected String getExportNodeName() {

        return CmsImportExportManager.N_EXPORT;
    }

    /**
     * Returns the zip output stream to write to.<p>
     * 
     * @return the zip output stream to write to
     */
    protected ZipOutputStream getExportZipStream() {

        return m_exportZipStream;
    }

    /**
     * Returns the report to write progess messages to.<p>
     * 
     * @return the report to write progess messages to
     */
    protected I_CmsReport getReport() {

        return m_report;
    }

    /**
     * Returns the name for the main resource node.<p>
     * 
     * @return the name for the main resource node
     */
    protected String getResourceNodeName() {

        return "files";
    }

    /**
     * Returns the SAX baesed xml writer to write the XML output to.<p>
     * 
     * @return the SAX baesed xml writer to write the XML output to
     */
    protected SAXWriter getSaxWriter() {

        return m_saxWriter;
    }

    /**
     * Checks if a property should be written to the export or not.<p>
     * 
     * @param property the property to check
     * @return if true, the property is to be ignored, otherwise it should be exported
     */
    protected boolean isIgnoredProperty(CmsProperty property) {

        if (property == null) {
            return true;
        }
        // default implementation is to export all properties not null
        return false;
    }

    /**
     * Opens the export ZIP file and initializes the internal XML document for the manifest.<p>
     * 
     * @return the node in the XML document where all files are appended to
     * @throws SAXException if something goes wrong procesing the manifest.xml
     * @throws IOException if something goes wrong while closing the export file
     */
    protected Element openExportFile() throws IOException, SAXException {

        // create the export-zipstream
        setExportZipStream(new ZipOutputStream(new FileOutputStream(getExportFileName())));
        // generate the SAX XML writer
        CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(
            new StringWriter(4096),
            OpenCms.getSystemInfo().getDefaultEncoding());
        saxHandler.setEscapeXml(true);
        saxHandler.setEscapeUnknownChars(true);

⌨️ 快捷键说明

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