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

📄 cmsimportversion1.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/legacy/CmsImportVersion1.java,v $
 * Date   : $Date: 2005/06/27 23:27:46 $
 * Version: $Revision: 1.8 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (C) 2002  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 com.opencms.legacy;

import org.opencms.importexport.CmsCompatibleCheck;
import org.opencms.importexport.CmsImportVersion2;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.util.CmsStringUtil;
import org.opencms.xml.CmsXmlException;
import org.opencms.xml.CmsXmlUtils;

import com.opencms.template.*;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.xml.sax.InputSource;

/**
 * Implementation of the OpenCms Import Interface ({@link org.opencms.importexport.I_CmsImport}) for 
 * the import version 1.<p>
 * 
 * This import format was used in OpenCms 4.3.23 - 5.0.0.<p>
 * 
 * This import class has similar funktions to CmsImportVersion2, but because of the need for a
 * single import class for each import version, a new, inherited class must be used, returning 
 * the correct import version.<p>
 *
 * @author Michael Emmerich (m.emmerich@alkacon.com)
 * @author Thomas Weckert (t.weckert@alkacon.com)
 * 
 * @see org.opencms.importexport.A_CmsImport
 * 
 * @deprecated Will not be supported past the OpenCms 6 release.
 */
public class CmsImportVersion1 extends CmsImportVersion2 {

    /** The version number of this import implementation. */
    private static final int C_IMPORT_VERSION = 1;

    /** The path to the bodies in OpenCms 4.x. */
    private static final String C_VFS_PATH_OLD_BODIES = "/content/bodys/";

    /**
     * @see org.opencms.importexport.I_CmsImport#getVersion()
     */
    public int getVersion() {

        return CmsImportVersion1.C_IMPORT_VERSION;
    }

    /**
     * Converts the content of a file from OpenCms 4.x versions.<p>
     * 
     * @param filename the name of the file to convert
     * @param byteContent the content of the file
     * @return the converted filecontent
     */
    private byte[] convertFile(String filename, byte[] byteContent) {

        byte[] returnValue = byteContent;
        if (!filename.startsWith("/")) {
            filename = "/" + filename;
        }

        String fileContent = new String(byteContent);
        String encoding = getEncoding(fileContent);
        if (!"".equals(encoding)) {
            // encoding found, ensure that the String is correct
            try {
                // get content of the file and store it in String with the correct encoding
                fileContent = new String(byteContent, encoding);
            } catch (UnsupportedEncodingException e) {
                // encoding not supported, we use the default and hope we are lucky
                if (DEBUG > 0) {
                    System.err.println("["
                        + this.getClass().getName()
                        + ".convertFile()]: Encoding not supported, using default encoding.");
                }
            }
        } else {
            // encoding not found, set encoding of xml files to default
            if (DEBUG > 0) {
                System.err.println("["
                    + this.getClass().getName()
                    + ".convertFile()]: Encoding not set, using default encoding and setting it in <?xml...?>.");
            }
            encoding = OpenCms.getSystemInfo().getDefaultEncoding();
            fileContent = setEncoding(fileContent, encoding);
        }
        // check the frametemplates
        if (filename.indexOf("frametemplates") != -1) {
            fileContent = scanFrameTemplate(fileContent);
        }
        // scan content/bodys
        if (filename.indexOf(C_VFS_PATH_OLD_BODIES) != -1 || filename.indexOf(CmsCompatibleCheck.VFS_PATH_BODIES) != -1) {
            if (DEBUG > 0) {
                System.err.println("[" + this.getClass().getName() + ".convertFile()]: Starting scan of body page.");
            }
            fileContent = convertPageBody(fileContent, filename);
        }
        // translate OpenCms 4.x paths to the new directory structure 
        fileContent = setDirectories(fileContent, m_cms.getRequestContext().getDirectoryTranslator().getTranslations());

        // create output ByteArray
        try {
            returnValue = fileContent.getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            // encoding not supported, we use the default and hope we are lucky
            returnValue = fileContent.getBytes();
        }
        return returnValue;
    }

    /**
     * Searches for the webapps String and replaces it with a macro which is needed for the WYSIWYG editor,
     * also creates missing &lt;edittemplate&gt; tags for exports of older OpenCms 4.x versions.<p>
     * 
     * @param content the filecontent 
     * @param fileName the name of the file 
     * @return String the modified filecontent
     */

    private String convertPageBody(String content, String fileName) {

        String nodeName = null;

        // variables needed for the creation of <template> elements
        boolean createTemplateTags = false;
        Hashtable templateElements = new Hashtable();

        // first check if any contextpaths are in the content String
        boolean found = false;
        for (int i = 0; i < m_webAppNames.size(); i++) {
            if (content.indexOf((String)m_webAppNames.get(i)) != -1) {
                found = true;
            }
        }
        // check if edittemplates are in the content string
        if (content.indexOf("<edittemplate>") != -1 || content.indexOf("<EDITTEMPLATE>") != -1) {
            found = true;
        }

        // only build document when some paths were found or <edittemplate> is missing!
        if (found) {
            InputStream in = new ByteArrayInputStream(content.getBytes());
            String editString, templateString;
            try {
                // create DOM document
                InputSource source = new InputSource(in);
                Document doc = CmsXmlUtils.unmarshalHelper(source, null);

                // get all <edittemplate> nodes to check their content
                nodeName = "edittemplate";
                Element root = doc.getRootElement();
                List editNodes = root.elements(nodeName.toLowerCase());
                editNodes.addAll(root.elements(nodeName.toUpperCase()));
                // no <edittemplate> tags present, create them!
                if (editNodes.size() < 1) {
                    if (DEBUG > 0) {
                        System.err.println("["

⌨️ 快捷键说明

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