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

📄 cmsimport.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    ) throws CmsException {
        NodeList fileNodes, propertyNodes;
        Element currentElement, currentProperty;
        String source, destination, type, user, group, access, launcherStartClass, timestamp;
        long lastmodified = 0;
        Map properties;
        
        Vector types = new Vector(); // stores the file types for which the property already exists
        if (excludeList == null) {
            excludeList = new Vector();
        }
        
        m_webAppNames = (List)A_OpenCms.getRuntimeProperty("compatibility.support.webAppNames");
        if (m_webAppNames == null)
            m_webAppNames = new ArrayList();

        // get the old webapp url from the OpenCms properties
        m_webappUrl = (String)A_OpenCms.getRuntimeProperty("compatibility.support.import.old.webappurl");
        if (m_webappUrl == null) {
            // use a default value
            m_webappUrl = "http://localhost:8080/opencms/opencms";
        }
        // cut last "/" from webappUrl if present
        if (m_webappUrl.endsWith("/")) {
            m_webappUrl = m_webappUrl.substring(0, m_webappUrl.lastIndexOf("/"));
        }

        // get list of unwanted properties
        List deleteProperties = (List) A_OpenCms.getRuntimeProperty("compatibility.support.import.remove.propertytags");
        if (deleteProperties == null) deleteProperties = new ArrayList();
        
        // get list of immutable resources
        List immutableResources = (List) A_OpenCms.getRuntimeProperty("import.immutable.resources");
        if (immutableResources == null) immutableResources = new ArrayList();
        if (DEBUG > 0) System.err.println("Import: Immutable resources size is " + immutableResources.size()); 
            
        try {
            // get all file-nodes
            fileNodes = m_docXml.getElementsByTagName(C_EXPORT_TAG_FILE);
            int importSize = fileNodes.getLength();
    
            String root = I_CmsConstants.C_DEFAULT_SITE + I_CmsConstants.C_ROOTNAME_VFS;
    
            // walk through all files in manifest
            for (int i = 0; i < fileNodes.getLength(); i++) {
    
                m_report.print(" ( " + (i+1) + " / " + importSize + " ) ");
                currentElement = (Element) fileNodes.item(i);
    
                // get all information for a file-import
                source = getTextNodeValue(currentElement, C_EXPORT_TAG_SOURCE);
                destination = getTextNodeValue(currentElement, C_EXPORT_TAG_DESTINATION);
                type = getTextNodeValue(currentElement, C_EXPORT_TAG_TYPE);
                user = getTextNodeValue(currentElement, C_EXPORT_TAG_USER);
                group = getTextNodeValue(currentElement, C_EXPORT_TAG_GROUP);
                access = getTextNodeValue(currentElement, C_EXPORT_TAG_ACCESS);
                launcherStartClass = getTextNodeValue(currentElement, C_EXPORT_TAG_LAUNCHER_START_CLASS);
                
                if ((timestamp=getTextNodeValue(currentElement,C_EXPORT_TAG_LASTMODIFIED))!=null) {
                    lastmodified = Long.parseLong(timestamp);
                }
                else {
                    lastmodified = System.currentTimeMillis();
                }                
                
                // if the type is "script" set it to plain
                if("script".equals(type)){
                    type = C_TYPE_PLAIN_NAME;
                }
                
                String translatedName = root + m_importPath + destination;
                if (C_TYPE_FOLDER_NAME.equals(type)) {
                    translatedName += C_FOLDER_SEPARATOR;                    
                }                    
                translatedName = m_cms.getRequestContext().getDirectoryTranslator().translateResource(translatedName);
                if (DEBUG > 3) System.err.println("Import: Translated resource name is " + translatedName); 
                
                boolean resourceNotImmutable = true;                
                if (immutableResources.contains(translatedName)) {
                    if (DEBUG > 1) System.err.println("Import: Translated resource name is immutable"); 
                    
                    // this resource must not be modified by an import if it already exists
                    try {
                        m_cms.readFileHeader("//" + translatedName);
                        resourceNotImmutable = false;
                        if (DEBUG > 0) System.err.println("Import: Immutable flag set for resource"); 
                    } catch (CmsException e) {
                        // resourceNotImmutable will be true
                        if (DEBUG > 0) System.err.println("Import: Immutable test caused exception " + e); 
                    } 
                }                                
                
                translatedName = translatedName.substring(root.length());
                if (resourceNotImmutable && (! excludeList.contains(translatedName))) {                   
                    
                    // print out the information to the report
                    m_report.print(m_report.key("report.importing"), I_CmsReport.C_FORMAT_NOTE);
                    m_report.print(translatedName + " ");                    
                        
                    // get all properties for this file
                    propertyNodes = currentElement.getElementsByTagName(C_EXPORT_TAG_PROPERTY);
                    // clear all stores for property information
                    properties = new HashMap();
                    // add the module property to properties
                    if (propertyName != null && propertyValue != null && !"".equals(propertyName)) {
                        if (!types.contains(type)) {
                            types.addElement(type);
                            createPropertydefinition(propertyName, type);
                        }
                        properties.put(propertyName, propertyValue);
                    }
                    // walk through all properties
                    for (int j = 0; j < propertyNodes.getLength(); j++) {
                        currentProperty = (Element) propertyNodes.item(j);
                        // get name information for this property
                        String name = getTextNodeValue(currentProperty, C_EXPORT_TAG_NAME);
						// check if this is an unwanted property
						if ((name != null) && (!deleteProperties.contains(name))) {
                            // get value information for this property
							String value = getTextNodeValue(currentProperty, C_EXPORT_TAG_VALUE);
							if (value == null) {
								// create an empty property
								value = "";
							}
							// add property
							properties.put(name, value);
							createPropertydefinition(name, type);
						}
                    }
    
                    // import the specified file 
                    importResource(source, destination, type, user, group, access, lastmodified, properties, launcherStartClass, writtenFilenames, fileCodes);
                } else {
                    // skip the file import, just print out the information to the report
                    m_report.print(m_report.key("report.skipping"), I_CmsReport.C_FORMAT_NOTE);
                    m_report.println(translatedName);
                }
            }
            if (!m_importingChannelData) {
                // at last we have to get the links from all new imported pages for the  linkmanagement
                m_report.println(m_report.key("report.check_links_begin"), I_CmsReport.C_FORMAT_HEADLINE);
                updatePageLinks();
                m_report.println(m_report.key("report.check_links_end"), I_CmsReport.C_FORMAT_HEADLINE);
                m_cms.joinLinksToTargets(m_report);
            }  
        } catch (Exception exc) {
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Imports the groups and writes them to the cms.<p>
     */
    private void importGroups() throws CmsException{
        NodeList groupNodes;
        Element currentElement;
        String name, description, flags, parentgroup;

        try{
            // getAll group nodes
            groupNodes = m_docXml.getElementsByTagName(C_EXPORT_TAG_GROUPDATA);
            // walk threw all groups in manifest
            for (int i = 0; i < groupNodes.getLength(); i++) {
                currentElement = (Element)groupNodes.item(i);
                name = getTextNodeValue(currentElement, C_EXPORT_TAG_NAME);
                description = getTextNodeValue(currentElement, C_EXPORT_TAG_DESCRIPTION);
                flags = getTextNodeValue(currentElement, C_EXPORT_TAG_FLAGS);
                parentgroup = getTextNodeValue(currentElement, C_EXPORT_TAG_PARENTGROUP);
                // import this group
                importGroup(name, description, flags, parentgroup);
            }

            // now try to import the groups in the stack
            while (!m_groupsToCreate.empty()){
                Stack tempStack = m_groupsToCreate;
                m_groupsToCreate = new Stack();
                while (tempStack.size() > 0){
                    Hashtable groupdata = (Hashtable)tempStack.pop();
                    name = (String)groupdata.get(C_EXPORT_TAG_NAME);
                    description = (String)groupdata.get(C_EXPORT_TAG_DESCRIPTION);
                    flags = (String)groupdata.get(C_EXPORT_TAG_FLAGS);
                    parentgroup = (String)groupdata.get(C_EXPORT_TAG_PARENTGROUP);
                    // try to import the group
                    importGroup(name, description, flags, parentgroup);
                }
            }
        } catch (Exception exc){
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
     }

    /**
     * Imports the users and writes them to the cms.<p>
     */
    private void importUsers() throws CmsException{
        NodeList userNodes;
        NodeList groupNodes;
        Element currentElement, currentGroup;
        Vector userGroups;
        Hashtable userInfo = new Hashtable();
        sun.misc.BASE64Decoder dec;
        String name, description, flags, password, recoveryPassword, firstname,
                lastname, email, address, section, defaultGroup, type, pwd, infoNode;
        // try to get the import resource
        getImportResource();
        try{
            // getAll user nodes
            userNodes = m_docXml.getElementsByTagName(C_EXPORT_TAG_USERDATA);
            // walk threw all groups in manifest
            for (int i = 0; i < userNodes.getLength(); i++) {
                currentElement = (Element)userNodes.item(i);
                name = getTextNodeValue(currentElement, C_EXPORT_TAG_NAME);
                // decode passwords using base 64 decoder
                dec = new sun.misc.BASE64Decoder();
                pwd = getTextNodeValue(currentElement, C_EXPORT_TAG_PASSWORD);
                password = new String(dec.decodeBuffer(pwd.trim()));
                dec = new sun.misc.BASE64Decoder();
                pwd = getTextNodeValue(currentElement, C_EXPORT_TAG_RECOVERYPASSWORD);
                recoveryPassword = new String(dec.decodeBuffer(pwd.trim()));

                description = getTextNodeValue(currentElement, C_EXPORT_TAG_DESCRIPTION);
                flags = getTextNodeValue(currentElement, C_EXPORT_TAG_FLAGS);
                firstname = getTextNodeValue(currentElement, C_EXPORT_TAG_FIRSTNAME);
                lastname = getTextNodeValue(currentElement, C_EXPORT_TAG_LASTNAME);
                email = getTextNodeValue(currentElement, C_EXPORT_TAG_EMAIL);
                address = getTextNodeValue(currentElement, C_EXPORT_TAG_ADDRESS);
                section = getTextNodeValue(currentElement, C_EXPORT_TAG_SECTION);
                defaultGroup = getTextNodeValue(currentElement, C_EXPORT_TAG_DEFAULTGROUP);
                type = getTextNodeValue(currentElement, C_EXPORT_TAG_TYPE);
                // get the userinfo and put it into the hashtable
                infoNode = getTextNodeValue(currentElement,C_EXPORT_TAG_USERINFO);
                try{
                    // read the userinfo from the dat-file
                    byte[] value = getFileBytes(infoNode);
                    // deserialize the object
                    ByteArrayInputStream bin= new ByteArrayInputStream(value);
                    ObjectInputStream oin = new ObjectInputStream(bin);
                    userInfo = (Hashtable)oin.readObject();
                } catch (IOException ioex){
                    m_report.println(ioex);
                }

                // get the groups of the user and put them into the vector
                groupNodes = currentElement.getElementsByTagName(C_EXPORT_TAG_GROUPNAME);
                userGroups = new Vector();
                for (int j=0; j < groupNodes.getLength(); j++){
                    currentGroup = (Element) groupNodes.item(j);
                    userGroups.addElement(getTextNodeValue(currentGroup, C_EXPORT_TAG_NAME));
                }
                // import this group
                importUser(name, description, flags, password, recoveryPassword, firstname,
                lastname, email, address, section, defaultGroup, type, userInfo, userGroups);
            }
        } catch (Exception exc){
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }

    /**
     * Creates an imported group in the cms.<p>
     * 
     * @param name the name of the group
     * @param description group description
     * @param flags group flags
     * @param parentgroupName name of the parent group
     */
    private void importGroup(String name, String description, String flags, String parentgroupName)
        throws CmsException{
        if(description == null) {
            description = "";
        }
        CmsGroup parentGroup = null;
        try{
            if ((parentgroupName != null) && (!"".equals(parentgroupName))) {
                try{
                    parentGroup = m_cms.readGroup(parentgroupName);
                } catch(CmsException exc){
                }
            }
            if (((parentgroupName != null) && (!"".equals(parentgroupName))) && (parentGroup == null)){
                // cannot create group, put on stack and try to create later
                Hashtable groupData = new Hashtable();
                groupData.put(C_EXPORT_TAG_NAME, name);
                    groupData.put(C_EXPORT_TAG_DESCRIPTION, description);
                groupData.put(C_EXPORT_TAG_FLAGS, flags);
                groupData.put(C_EXPORT_TAG_PARENTGROUP, parentgroupName);
                m_groupsToCreate.push(groupData);
            } else {
                try{
                    m_report.print(m_report.key("report.importing_group"), I_CmsReport.C_FORMAT_NOTE);
                    m_report.print(name);     
                    m_report.print(m_report.key("report.dots"), I_CmsReport.C_FORMAT_NOTE);                                   
                    m_cms.addGroup(name, description, Integer.parseInt(flags), parentgroupName);
                    m_report.println(m_report.key("report.ok"), I_CmsReport.C_FORMAT_OK);
                } catch (CmsException exc){
                    m_report.println(m_report.key("report.not_created"), I_CmsReport.C_FORMAT_OK);
                }
            }
        } catch (Exception exc){
            m_report.println(exc);
            throw new CmsException(CmsException.C_UNKNOWN_EXCEPTION, exc);
        }
    }
    
    /**
     * Creates an imported user in the cms.<p>
     * 
     * @param name user name
     * @param description user description
     * @param flags user flags
     * @param password user password 
     * @param recoveryPassword user recovery password
     * @param firstname firstname of the user
     * @param lastname lastname of the user
     * @param email user email
     * @param address user address 
     * @param section user section
     * @param defaultGroup user default group
     * @param type user type
     * @param userInfo user info
     * @param userGroups user groups
     * @throws CmsException in case something goes wrong
     */
    private void importUser(String name, String description, String flags, String password,

⌨️ 快捷键说明

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