dbdataexport.java

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

JAVA
887
字号
        for (int i=0; i<filename.length(); i++) {            char ch = filename.charAt(i);            if (!((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z')                    || (ch >= 'a' && ch <= 'z') || (ch == '_')                    || (ch == '-') || (ch == '.')))            {                throw new IllegalArgumentException("Invalid filename");            }        }        // Also check that "." is not repeated:        if (filename.indexOf("..") > -1) {            throw new IllegalArgumentException("Invalid filename");        }        this.exportFile = new File(dataPath, filename);    }    /**     * Returns the name of the file Jive will export data to. Note, this does     * not return the full path - just the filename.     *     * @return the name of the file Jive will export data to.     */    public String getFilename() {        return exportFile.getName();    }    /*    public void generateStats() {        if (reportRunning) {            return;        }        else {            reportRunning = true;        }        TaskEngine.addTask(new ReportRunner());    }    */    /**     * Starts the export process. Before calling this method, you should call     * the isRunning() method to check if there is an export currently going.     * If this method is called and an export is already running, the method     * will just return.     *     * @throws IOException if the export file can't be created or if there is     * a problem writing to the file during the export process.     * @throws UnauthorizedException if the user calling this method doesn't     * have permission to read certain Jive objects. Only a system admin     * should be allowed to export data.     */    public void export() throws IOException,            UnauthorizedException    {        // Check if we're already doing an export - if so, return:        if (running) {            return;        }        else {            running = true;        }        TaskEngine.addTask(TaskEngine.MEDIUM_PRIORITY, new ExportRunner());    }    private String getHeaderXML() {        StringBuffer buf = new StringBuffer(200);        buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");        buf.append("<!DOCTYPE Jive SYSTEM \"http://www.jivesoftware.com/jive.dtd\">");        buf.append("<Jive xmlversion=\"").append(XML_VERSION).append("\" exportDate=\"" +            dateFormatter.format(new Date()) + "\">"        );        return buf.toString();    }    private String getPermissionsXML() {        StringBuffer buf = new StringBuffer(1024);        PermissionsManager permManager = factory.getPermissionsManager();        // Global user permissions        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=\"").append(permNames[i]).append("\"/>");                }            }            if (permManager.anonymousUserHasPermission(permTypes[i])) {                buf.append("<UserPermission usertype=\"ANONYMOUS\"");                buf.append(" permission=\"").append(permNames[i]).append("\"/>");            }            if (permManager.registeredUserHasPermission(permTypes[i])) {                buf.append("<UserPermission usertype=\"REGISTERED_USERS\"");                buf.append(" permission=\"").append(permNames[i]).append("\"/>");            }        }        buf.append("</UserPermissionList>");        // Global group permissions        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=\"").append(permNames[i]).append("\"/>");                }            }        }        buf.append("</GroupPermissionList>");        return buf.toString();    }    private String getAdminPermXML() {        StringBuffer buf = new StringBuffer(1024);        PermissionsManager permManager = factory.getPermissionsManager();        buf.append("<UserPermissionList>");        for (int i=0; i<permNames.length; i++) {            Iterator userList = permManager.usersWithPermission(ForumPermissions.SYSTEM_ADMIN);            while (userList.hasNext()) {                User user = (User)userList.next();                if (user != null) {                    buf.append("<UserPermission usertype=\"USER\" username=\"");                    buf.append(user.getUsername());                    buf.append("\" permission=\"").append(permNames[i]).append("\"/>");                }            }        }        buf.append("</UserPermissionList>");        return buf.toString();    }    private String getThreadXML(ForumThread thread) {        StringBuffer buf = new StringBuffer(4096);        if (exportIDs) {            buf.append("<Thread id=\"").append(thread.getID()).append("\">");        }        else {            buf.append("<Thread>");        }        buf.append("<CreationDate>");        buf.append(dateFormatter.format(thread.getCreationDate()));        buf.append("</CreationDate><ModifiedDate>");        buf.append(dateFormatter.format(thread.getModifiedDate()));        buf.append("</ModifiedDate>");        // Properties        Iterator propertyNames = thread.propertyNames();        if (propertyNames.hasNext()) {            buf.append("<PropertyList>");            while (propertyNames.hasNext()) {                String propName = (String)propertyNames.next();                String propValue = StringUtils.escapeForXML(                        thread.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 all messages in thread.        messageToXML(thread.getRootMessage(), buf);        buf.append("</Thread>");        return buf.toString();    }    private void messageToXML(ForumMessage message, StringBuffer buf) {        if (exportIDs) {            buf.append("<Message id=\"").append(message.getID()).append("\">");        }        else {            buf.append("<Message>");        }        buf.append("<Subject>");        buf.append(StringUtils.escapeForXML(message.getUnfilteredSubject()));        buf.append("</Subject><Body>");        buf.append(StringUtils.escapeForXML(message.getUnfilteredBody()));        buf.append("</Body>");        if (!message.isAnonymous()) {            buf.append("<Username>");            buf.append(StringUtils.escapeForXML(message.getUser().getUsername()));            buf.append("</Username>");        }        buf.append("<CreationDate>");        buf.append(dateFormatter.format(message.getCreationDate()));        buf.append("</CreationDate><ModifiedDate>");        buf.append(dateFormatter.format(message.getModifiedDate()));        buf.append("</ModifiedDate>");        // Message properties        Iterator propertyNames = message.propertyNames();        if (propertyNames.hasNext()) {            buf.append("<PropertyList>");            while (propertyNames.hasNext()) {                String propName = (String)propertyNames.next();                String propValue = StringUtils.escapeForXML(                        message.getUnfilteredProperty(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 children messages        TreeWalker walker = message.getForumThread().treeWalker();        int childCount = walker.getChildCount(message);        if (childCount > 0) {            buf.append("<MessageList>");            for (int i=0; i<childCount; i++) {                try {                    ForumMessage childMessage = walker.getChild(message, i);                    messageToXML(childMessage, buf);                }                catch (ForumMessageNotFoundException fmnfe) {                    fmnfe.printStackTrace();                }                catch (Exception e) {                    e.printStackTrace();                }            }            buf.append("</MessageList>");        }        buf.append("</Message>");    }    private String getUserXML(User user) throws UnauthorizedException {        StringBuffer buf = new StringBuffer(512);        if (exportIDs) {            buf.append("<User id=\"").append(user.getID()).append("\">");        }        else {            buf.append("<User>");        }        buf.append("<Username>");        buf.append(StringUtils.escapeForXML(user.getUsername()));        buf.append("</Username><Password>");        buf.append(StringUtils.escapeForXML(user.getPasswordHash()));        boolean emailVisible = user.isEmailVisible();        boolean nameVisible = user.isNameVisible();        String name = user.getName();        buf.append("</Password><Email visible=\"" + emailVisible + "\">");        buf.append(StringUtils.escapeForXML(user.getEmail()));        buf.append("</Email>");        if (name != null) {            buf.append("<Name visible=\"" + nameVisible + "\">");            buf.append(StringUtils.escapeForXML(name));            buf.append("</Name>");        }        // creation date / modified date        buf.append("<CreationDate>").append(user.getCreationDate()).append("</CreationDate>");        buf.append("<ModifiedDate>").append(user.getModifiedDate()).append("</ModifiedDate>");        // Properties        Iterator userProps = user.propertyNames();        if (userProps.hasNext()) {            buf.append("<PropertyList>");            while (userProps.hasNext()) {                String propName = (String)userProps.next();                String propValue = StringUtils.escapeForXML(user.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>");        }        buf.append("</User>");        return buf.toString();    }    private String getGroupXML(Group group) throws UnauthorizedException {        StringBuffer buf = new StringBuffer(512);        if (exportIDs) {            buf.append("<Group id=\"").append(group.getID()).append("\">");        }        else {            buf.append("<Group>");        }        buf.append("<Name>");        buf.append(StringUtils.escapeForXML(group.getName()));        buf.append("</Name><Description>");        buf.append(StringUtils.escapeForXML(group.getDescription()));        buf.append("</Description>");        // creation date / modified date

⌨️ 快捷键说明

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