dbdataexport.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 887 行 · 第 1/3 页

JAVA
887
字号
        buf.append("<CreationDate>").append(group.getCreationDate()).append("</CreationDate>");        buf.append("<ModifiedDate>").append(group.getModifiedDate()).append("</ModifiedDate>");        // Properties        Iterator groupProps = group.propertyNames();        if (groupProps.hasNext()) {            buf.append("<PropertyList>");            while (groupProps.hasNext()) {                String propName = (String)groupProps.next();                String propValue = StringUtils.escapeForXML(group.getProperty(propName));                if (propValue != null) {                    // XML escape these sequences                    propName = StringUtils.escapeForXML(propName);                    buf.append("<Property name=\"").append(propName).append("\" ");                    buf.append("value=\"").append(propValue).append("\"/>");                }            }            buf.append("</PropertyList>");        }        // Admin list        Iterator admins = group.administrators();        if (admins.hasNext()) {            buf.append("<AdministratorList>");            while (admins.hasNext()) {                String username = ((User)admins.next()).getUsername();                buf.append("<Username>").append(username).append("</Username>");            }            buf.append("</AdministratorList>");        }        // Member list        Iterator members = group.members();        if (members.hasNext()) {            buf.append("<MemberList>");            while (members.hasNext()) {                String username = ((User)members.next()).getUsername();                buf.append("<Username>").append(username).append("</Username>");            }            buf.append("</MemberList>");        }        buf.append("</Group>");        return buf.toString();    }    private void exportUsers(Writer out) throws IOException,            UnauthorizedException    {        UserManager userManager = factory.getUserManager();        for (Iterator iter = userManager.users(); iter.hasNext(); ) {            User user = (User)iter.next();            out.write(getUserXML(user));        }    }    private void exportGroups(Writer out) throws IOException,            UnauthorizedException    {        GroupManager groupManager = factory.getGroupManager();        for (Iterator iter = groupManager.groups(); iter.hasNext(); ) {            Group group = (Group)iter.next();            out.write(getGroupXML(group));        }    }    private void exportForums(Writer out) throws IOException,            UnauthorizedException    {        // Export all forums in the root category:        ForumCategory rootCategory = factory.getRootForumCategory();        for (Iterator iter=rootCategory.forums(); iter.hasNext();) {            Forum forum = (Forum)iter.next();            exportForum(forum, out);        }        // Traverse the category tree with a recursive iterator, export        // forums found in each category:        for (Iterator iter=rootCategory.recursiveCategories(); iter.hasNext();) {            ForumCategory category = (ForumCategory)iter.next();            for (Iterator forums=category.forums(); forums.hasNext();) {                Forum forum = (Forum)forums.next();                exportForum(forum, out);            }        }    }    private void exportForum(Forum forum, Writer out) throws IOException,            UnauthorizedException    {        StringBuffer buf = new StringBuffer(4096);        // If the size of forumList is 0, we export all forums. If it's        // greater than zero, check to see if the current forum is in the        // list of exportable forums:        if (forumList.size() == 0 || forumList.containsKey(new Long(forum.getID())))        {            String name = forum.getName();            if (exportIDs) {                buf.append("<Forum id=\"").append(forum.getID()).append("\">");            }            else {                buf.append("<Forum>");            }            buf.append("<Name>");            buf.append(StringUtils.escapeForXML(name));            buf.append("</Name>");            if (forum.getDescription() != null) {                buf.append("<Description>");                buf.append(StringUtils.escapeForXML(forum.getDescription()));                buf.append("</Description>");            }            buf.append("<CreationDate>");            buf.append(dateFormatter.format(forum.getCreationDate()));            buf.append("</CreationDate><ModifiedDate>");            buf.append(dateFormatter.format(forum.getModifiedDate()));            buf.append("</ModifiedDate>");            if (exportPerms) {                // export forum permissions                PermissionsManager permManager = forum.getPermissionsManager();                buf.append("<PermissionList>");                // User List                buf.append("<UserPermissionList>");                for (int i=0; i<permNames.length; i++) {                    Iterator userList = permManager.usersWithPermission(permTypes[i]);                    while (userList.hasNext()) {                        User user = (User)userList.next();                        if( user != null ) {                            buf.append("<UserPermission usertype=\"USER\" username=\"");                            buf.append(user.getUsername());                            buf.append("\" permission=\"" + permNames[i] + "\"/>");                        }                    }                    if (permManager.anonymousUserHasPermission(permTypes[i])) {                        buf.append("<UserPermission usertype=\"ANONYMOUS\"");                        buf.append(" permission=\"" + permNames[i] + "\"/>");                    }                    if (permManager.registeredUserHasPermission(permTypes[i])) {                        buf.append("<UserPermission usertype=\"REGISTERED_USERS\"");                        buf.append(" permission=\"" + permNames[i] + "\"/>");                    }                }                buf.append("</UserPermissionList>");                // Group List                buf.append("<GroupPermissionList>");                for( int i=0; i<permNames.length; i++ ) {                Iterator groupList = permManager.groupsWithPermission(permTypes[i]);                while( groupList.hasNext() ) {                    Group group = (Group)groupList.next();                    if( group != null ) {                            buf.append("<GroupPermission groupname=\"" + group.getName() + "\"");                            buf.append(" permission=\"" + permNames[i] + "\"/>");                        }                    }                }                buf.append("</GroupPermissionList>");                buf.append("</PermissionList>");            }            // Properties            Iterator propertyNames = forum.propertyNames();            if (propertyNames.hasNext()) {                buf.append("<PropertyList>");                while (propertyNames.hasNext()) {                    String propName = (String)propertyNames.next();                    String propValue = StringUtils.escapeForXML(                        forum.getProperty(propName));                    if (propValue != null) {                        propName = StringUtils.escapeForXML(propName);                        buf.append("<Property name=\"").append(propName).append("\" ");                        buf.append("value=\"").append(propValue).append("\"/>");                    }                }                buf.append("</PropertyList>");            }            // write out what we have so far            out.write(buf.toString());            // export threads            out.write("<ThreadList>");            exportThreads(forum, out);            out.write("</ThreadList>");            out.write("</Forum>");        }    }    private void exportThreads(Forum forum, Writer out) throws IOException {        for (Iterator iter = forum.threads(); iter.hasNext(); ) {            ForumThread thread = (ForumThread)iter.next();            out.write(getThreadXML(thread));        }    }    /**     * Runs the export process in a thread.     */    private class ExportRunner implements Runnable {        public ExportRunner() {        }        public void run() {            // 256 K buffer for writing:            int buffer = 1024*256;            Writer out = null;            try {                out = new BufferedWriter(new OutputStreamWriter(                        new FileOutputStream(exportFile), "UTF-8"), buffer);                // Write out the header XML                out.write(getHeaderXML());                // Export users                if (exportUsers) {                    out.write("<UserList>");                    exportUsers(out);                    out.write("</UserList>");                }                // Export gropus                if (exportGroups) {                    out.write("<GroupList>");                    exportGroups(out);                    out.write("</GroupList>");                }                // Export forums                if (exportForums) {                    out.write("<ForumList>");                    exportForums(out);                    out.write("</ForumList>");                }                // Export global permissions                if (exportPerms) {                    out.write(getPermissionsXML());                }                else {                    // If permissions are not exported, we at least need to export                    // just the system admin perm.                    out.write(getAdminPermXML());                }                // Ending tag                out.write("</Jive>");                out.flush();            }            catch (Exception e) {                System.err.println("Error exporing forum data:");                e.printStackTrace();            }            finally {                // Always indicate that we're stopping the export                synchronized (this) {                    exporter = null;                }                try {                    out.close();                } catch (Exception e) {}            }            // We're done so indicate so:            running = false;        }    }    public static void main(String args[]) {        /*        if (args.length < 3) {            System.err.println( "USAGE: java DbDataExport [xml_uri] "                + "[jive_username] [jive_password]" );            System.exit(1);        }        try {            authToken = AuthorizationFactory.getAuthorization(args[1],args[2]);        }        catch( UnauthorizedException e ) {            System.err.println( "Error authenicating user " + args[1] + ". "                + "Please make sure you're using the correct username and "                + "password." );            System.exit(1);        }        try {            DbDataExport exporter = new DbDataExport(new DbForumFactory());            System.out.println("\nBeginning export, writing to " + args[0] + "...");            long time = System.currentTimeMillis();            exporter.export(true);            time = System.currentTimeMillis() - time;            System.out.println("\nExport done, time = "                + ((double)time/1000.0) + " seconds" );        } catch( Exception e ) {            e.printStackTrace();        }        System.exit(1);        */    }}

⌨️ 快捷键说明

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