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

📄 cmsexport.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        // initialize the dom4j writer object as member variable
        setSaxWriter(new SAXWriter(saxHandler, saxHandler));
        // the XML document to write the XMl to
        Document doc = DocumentHelper.createDocument();
        // start the document
        saxHandler.startDocument();

        // the node in the XML document where the file entries are appended to        
        String exportNodeName = getExportNodeName();
        // add main export node to XML document
        Element exportNode = doc.addElement(exportNodeName);
        getSaxWriter().writeOpen(exportNode);

        // add the info element. it contains all infos for this export
        Element info = exportNode.addElement(CmsImportExportManager.N_INFO);
        info.addElement(CmsImportExportManager.N_CREATOR).addText(getCms().getRequestContext().currentUser().getName());
        info.addElement(CmsImportExportManager.N_OC_VERSION).addText(OpenCms.getSystemInfo().getVersionName());
        info.addElement(CmsImportExportManager.N_DATE).addText(CmsDateUtil.getHeaderDate(System.currentTimeMillis()));
        info.addElement(CmsImportExportManager.N_PROJECT).addText(
            getCms().getRequestContext().currentProject().getName());
        info.addElement(CmsImportExportManager.N_VERSION).addText(CmsImportExportManager.EXPORT_VERSION);

        // write the XML
        digestElement(exportNode, info);

        return exportNode;
    }

    /**
     * Sets the OpenCms context object this export was initialized with.<p>
     * 
     * @param cms the OpenCms context object this export was initialized with
     */
    protected void setCms(CmsObject cms) {

        m_cms = cms;
    }

    /**
     * Sets the name of the export file.<p>
     * 
     * @param exportFileName the name of the export file
     */
    protected void setExportFileName(String exportFileName) {

        // ensure the export file name ends with ".zip"
        if (!exportFileName.toLowerCase().endsWith(".zip")) {
            m_exportFileName = exportFileName + ".zip";
        } else {
            m_exportFileName = exportFileName;
        }
    }

    /**
     * Sets the zip output stream to write to.<p>
     * 
     * @param exportZipStream the zip output stream to write to
     */
    protected void setExportZipStream(ZipOutputStream exportZipStream) {

        m_exportZipStream = exportZipStream;
    }

    /**
     * Sets the report to write progess messages to.<p>
     * 
     * @param report the report to write progess messages to
     */
    protected void setReport(I_CmsReport report) {

        m_report = report;
    }

    /**
     * Sets the SAX baesed xml writer to write the XML output to.<p>
     * 
     * @param saxWriter the SAX baesed xml writer to write the XML output to
     */
    protected void setSaxWriter(SAXWriter saxWriter) {

        m_saxWriter = saxWriter;
    }

    /**
     * Adds all files in fileNames to the manifest.xml file.<p>
     * 
     * @param fileNames list of path Strings, e.g. <code>/folder/index.html</code>
     * 
     * @throws CmsImportExportException if something goes wrong
     * @throws IOException if a file could not be exported
     * @throws SAXException if something goes wrong procesing the manifest.xml
     */
    private void addFiles(List fileNames) throws CmsImportExportException, IOException, SAXException {

        if (fileNames != null) {
            for (int i = 0; i < fileNames.size(); i++) {
                String fileName = (String)fileNames.get(i);

                try {
                    CmsFile file = getCms().readFile(fileName, CmsResourceFilter.IGNORE_EXPIRATION);
                    if ((file.getState() != CmsResource.STATE_DELETED) && (!file.getName().startsWith("~"))) {
                        if (checkExportResource(fileName)) {
                            if (m_recursive) {
                                addParentFolders(fileName);
                            }
                            exportFile(file);
                        }
                    }
                } catch (CmsImportExportException e) {

                    throw e;
                } catch (CmsException e) {
                    if (e instanceof CmsVfsException) { // file not found
                        CmsMessageContainer message = Messages.get().container(
                            Messages.ERR_IMPORTEXPORT_ERROR_ADDING_FILE_1,
                            fileName);
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(message.key(), e);
                        }

                        throw new CmsImportExportException(message, e);
                    }
                }
            }
        }
    }

    /**
     * Exports all page body files that have not explicityl been added by the user.<p>
     * 
     * @throws CmsImportExportException if something goes wrong
     * @throws IOException if a file could not be exported
     * @throws SAXException if something goes wrong procesing the manifest.xml
     */
    private void addPageBodyFiles() throws CmsImportExportException, IOException, SAXException {

        Iterator i;

        List bodyFileNames = new ArrayList();
        String bodyPath = CmsCompatibleCheck.VFS_PATH_BODIES.substring(
            0,
            CmsCompatibleCheck.VFS_PATH_BODIES.lastIndexOf("/"));

        // check all exported page files if their body has already been exported
        i = m_exportedPageFiles.iterator();
        while (i.hasNext()) {
            String filename = (String)i.next();
            // check if the site path is within the filename. If so,this export is
            // started from the root site and the path to the bodies must be modifed
            // this is not nice, but it works.
            if (filename.startsWith(CmsResource.VFS_FOLDER_SITES)) {
                filename = filename.substring(CmsResource.VFS_FOLDER_SITES.length() + 1, filename.length());
                filename = filename.substring(filename.indexOf("/"), filename.length());
            }
            String body = bodyPath + filename;
            bodyFileNames.add(body);
        }

        // now export the body files that have not already been exported
        addFiles(bodyFileNames);
    }

    /**
     * Adds the parent folders of the given resource to the config file, 
     * starting at the top, excluding the root folder.<p>
     * 
     * @param resourceName the name of a resource in the VFS
     * @throws CmsImportExportException if something goes wrong
     * @throws SAXException if something goes wrong procesing the manifest.xml
     */
    private void addParentFolders(String resourceName) throws CmsImportExportException, SAXException {

        try {
            // this is a resource in /system/ folder and option includeSystem is not true
            if (!checkExportResource(resourceName)) {
                return;
            }

            // Initialize the "previously added folder cache"
            if (m_superFolders == null) {
                m_superFolders = new ArrayList();
            }
            List superFolders = new ArrayList();

            // Check, if the path is really a folder
            if (resourceName.lastIndexOf("/") != (resourceName.length() - 1)) {
                resourceName = resourceName.substring(0, resourceName.lastIndexOf("/") + 1);
            }
            while (resourceName.length() > "/".length()) {
                superFolders.add(resourceName);
                resourceName = resourceName.substring(0, resourceName.length() - 1);
                resourceName = resourceName.substring(0, resourceName.lastIndexOf("/") + 1);
            }
            for (int i = superFolders.size() - 1; i >= 0; i--) {
                String addFolder = (String)superFolders.get(i);
                if (!m_superFolders.contains(addFolder)) {
                    // This super folder was NOT added previously. Add it now!
                    CmsFolder folder = getCms().readFolder(addFolder, CmsResourceFilter.IGNORE_EXPIRATION);
                    appendResourceToManifest(folder, false);
                    // Remember that this folder was added
                    m_superFolders.add(addFolder);
                }
            }
        } catch (CmsImportExportException e) {

            throw e;
        } catch (CmsException e) {

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

            throw new CmsImportExportException(message, e);
        }
    }

    /**
     * Writes the data for a resource (like access-rights) to the <code>manifest.xml</code> file.<p>
     * 
     * @param resource the resource to get the data from
     * @param source flag to show if the source information in the xml file must be written
     * @throws CmsImportExportException if something goes wrong
     * @throws SAXException if something goes wrong procesing the manifest.xml
     */
    private void appendResourceToManifest(CmsResource resource, boolean source)
    throws CmsImportExportException, SAXException {

        try {
            CmsProperty property = null;
            String key = null, value = null;
            Element propertyElement = null;

            // define the file node
            Element fileElement = m_resourceNode.addElement(CmsImportExportManager.N_FILE);

            // only write <source> if resource is a file
            String fileName = trimResourceName(getCms().getSitePath(resource));
            if (resource.isFile()) {
                if (source) {
                    fileElement.addElement(CmsImportExportManager.N_SOURCE).addText(fileName);
                }
            } else {
                m_exportCount++;
                I_CmsReport report = getReport();
                // output something to the report for the folder
                report.print(org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_SUCCESSION_1,
                    String.valueOf(m_exportCount)), I_CmsReport.FORMAT_NOTE);
                report.print(Messages.get().container(Messages.RPT_EXPORT_0), I_CmsReport.FORMAT_NOTE);
                report.print(org.opencms.report.Messages.get().container(
                    org.opencms.report.Messages.RPT_ARGUMENT_1,
                    getCms().getSitePath(resource)));
                report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
                report.println(
                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                    I_CmsReport.FORMAT_OK);

                if (LOG.isInfoEnabled()) {
                    LOG.info(Messages.get().getBundle().key(
                        Messages.LOG_EXPORTING_OK_2,
                        String.valueOf(m_exportCount),
                        getCms().getSitePath(resource)));
                }
            }

            // <destination>
            fileElement.addElement(CmsImportExportManager.N_DESTINATION).addText(fileName);
            // <type>
            fileElement.addElement(CmsImportExportManager.N_TYPE).addText(
                OpenCms.getResourceManager().getResourceType(resource.getTypeId()).getTypeName());

            if (resource.isFile()) {
                //  <uuidresource>
                fileElement.addElement(CmsImportExportManager.N_UUIDRESOURCE).addText(
                    resource.getResourceId().toString());
            }

            // <datelastmodified>
            fileElement.addElement(CmsImportExportManager.N_DATELASTMODIFIED).addText(
                CmsDateUtil.getHeaderDate(resource.getDateLastModified()));
            // <userlastmodified>
            String userNameLastModified = null;
            try {
                userNameLastModified = getCms().readUser(resource.getUserLastModified()).getName();
            } catch (CmsException e) {
                userNameLastModified = OpenCms.getDefaultUsers().getUserAdmin();
            }
            fileElement.addElement(CmsImportExportManager.N_USERLASTMODIFIED).addText(userNameLastModified);
            // <datecreated>
            fileElement.addElement(CmsImportExportManager.N_DATECREATED).addText(
                CmsDateUtil.getHeaderDate(resource.getDateCreated()));
            // <usercreated>
            String userNameCreated = null;
            try {
                userNameCreated = getCms().readUser(resource.getUserCreated()).getName();
            } catch (CmsException e) {
                userNameCreated = OpenCms.getDefaultUsers().getUserAdmin();
            }
            fileElement.addElement(CmsImportExportManager.N_USERCREATED).addText(userNameCreated);
            // <release>
            if (resource.getDateReleased() != CmsResource.DATE_RELEASED_DEFAULT) {
                fileElement.addElement(CmsImportExportManager.N_DATERELEASED).addText(
                    CmsDateUtil.getHeaderDate(resource.getDateReleased()));
            }
            // <expire>
            if (resource.getDateExpired() != CmsResource.DATE_EXPIRED_DEFAULT) {
                fileElement.addElement(CmsImportExportManager.N_DATEEXPIRED).addText(
                    CmsDateUtil.getHeaderDate(resource.getDateExpired()));
            }
            // <flags>
            int resFlags = resource.getFlags();
            resFlags &= ~CmsResource.FLAG_LABELED;
            fileElement.addElement(CmsImportExportManager.N_FLAGS).addText(Integer.toString(resFlags));

            // write the properties to the manifest
            Element propertiesElement = fileElement.addElement(CmsImportExportManager.N_PROPERTIES);
            List properties = getCms().readPropertyObjects(getCms().getSitePath(resource), false);
            for (int i = 0, n = properties.size(); i < n; i++) {
                property = (CmsProperty)properties.get(i);

                if (isIgnoredProperty(property)) {
                    continue;
                }

                key = property.getName();

                for (int j = 0; j < 2; j++) {
                    // iterations made here:
                    // 0) append individual/structure property value
                    // 1) append shared/resource property value
                    if ((j == 0 && (value = property.getStructureValue()) != null)
                        || (j == 1 && (value = property.getResourceValue()) != null)) {
                        propertyElement = propertiesElement.addElement(CmsImportExportManager.N_PROPERTY);

                        if (j == 1) {
                            // add a type attrib. to the property node in case of a shared/resource property value
                            propertyElement.addAttribute(
                                CmsImportExportManager.N_PROPERTY_ATTRIB_TYPE,
                                CmsImportExportManager.N_PROPERTY_ATTRIB_TYPE_SHARED);
                        }

                        propertyElement.addElement(CmsImportExportManager.N_NAME).addText(key);
                        propertyElement.addElement(CmsImportExportManager.N_VALUE).addCDATA(value);
                    }
                }
            }

⌨️ 快捷键说明

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