dbdataimport.java

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

JAVA
1,395
字号
/** * $RCSfile: DbDataImport.java,v $ * $Revision: 1.7 $ * $Date: 2002/07/29 17:48:13 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import java.io.*;import java.text.*;import java.util.*;import com.jivesoftware.crimson.parser.*;import com.jivesoftware.sax.*;import com.jivesoftware.sax.Attributes;import com.jivesoftware.sax.helpers.*;import com.jivesoftware.forum.*;/** * Imports Jive data that is stored in the Jive XML format. The class can * handle plain XML files or compressed JAR files. All XML must strictly * comply with the Jive DTD. */public class DbDataImport {    public static final String XML_DATE_FORMAT = "yyyy/MM/dd HH:mm:ss.SS z";    public static final String XML_VERSION = "1.1";    // A SAX parser    private XMLReader parser = null;    // ForumFactory object -- all Jive objects come from this    private ForumFactory forumFactory = null;    // UserManager and GroupManager objects to create users & groups    private UserManager userManager = null;    private GroupManager groupManager = null;    // The current forum being imported    private Forum forum;    // The current thread being imported    private ForumThread thread;    // Indicates if the current thread has a root message.    private boolean threadHasRootMessage = false;    // A simple date formatter that will convert Date objects to the Jive    // date format. For example: 1978/01/17 21:17:33.83 CST    private static SimpleDateFormat dateFormatter =        new SimpleDateFormat(XML_DATE_FORMAT);    // Load Jive permission types. We map each permission name to its int value.    private static HashMap jivePermissions = new HashMap();    static {        jivePermissions.put("CATEGORY_ADMIN",            new Integer(ForumPermissions.CATEGORY_ADMIN));        jivePermissions.put("CREATE_ATTACHMENT",            new Integer(ForumPermissions.CREATE_ATTACHMENT));        jivePermissions.put("CREATE_MESSAGE",            new Integer(ForumPermissions.CREATE_MESSAGE));        jivePermissions.put("CREATE_THREAD",            new Integer(ForumPermissions.CREATE_THREAD));        jivePermissions.put("FORUM_ADMIN",            new Integer(ForumPermissions.FORUM_ADMIN));        jivePermissions.put("GROUP_ADMIN",            new Integer(ForumPermissions.GROUP_ADMIN));        jivePermissions.put("MODERATE_MESSAGES",            new Integer(ForumPermissions.MODERATE_MESSAGES));        jivePermissions.put("MODERATE_THREADS",            new Integer(ForumPermissions.MODERATE_THREADS));        jivePermissions.put("READ",            new Integer(ForumPermissions.READ_FORUM));        jivePermissions.put("READ_FORUM",            new Integer(ForumPermissions.READ_FORUM));        jivePermissions.put("SYSTEM_ADMIN",            new Integer(ForumPermissions.SYSTEM_ADMIN));        jivePermissions.put("USER_ADMIN",            new Integer(ForumPermissions.USER_ADMIN));    }    // Parsing mode definitions    private int mode = 0;    private final static int USERNAME      = 1;    private final static int PASSWORD      = 2;    private final static int EMAIL         = 3;    private final static int NAME          = 4;    private final static int DESCRIPTION   = 5;    private final static int CREATION_DATE = 6;    private final static int MODIFIED_DATE = 7;    private final static int SUBJECT       = 8;    private final static int BODY          = 9;    /**     * Parses a date string and returns a Date object. If the the date cannot     * be parsed, a new Date is returned.     */    private static Date parseDate(String dateText) {        try {            return dateFormatter.parse(dateText);        }        catch (ParseException pe) {            return new Date();        }    }    /**     * Initializes Jive data importer.     *     * @param dbForumFactory     */    public DbDataImport(DbForumFactory dbForumFactory) throws UnauthorizedException,            Exception    {        this.forumFactory = dbForumFactory;        // Get user and group managers        this.userManager = forumFactory.getUserManager();        this.groupManager = forumFactory.getGroupManager();        parser = new XMLReaderImpl();    }    /**     * Imports Jive data.     *     * @param in An InputStream object     */    public void doImport(Reader in) throws IOException, Exception {        try {            parser.setContentHandler(new JiveHandler());            parser.parse(new InputSource(in));        }        catch (SAXException saxe) {            if (saxe instanceof SAXParseException) {                SAXParseException saxpe = (SAXParseException)saxe;                int line = saxpe.getLineNumber();                int col = saxpe.getColumnNumber();                String message = "XML parsing exception line " + line + ":" + col;                throw new Exception(message);            }            saxe.printStackTrace();        }    }    /**     * Setup global SAX handlers ala the Flyweight design pattern. Each of     * these objects will be re-used again and again to parse the subsets of     * the XML document that they know how to handle. The only handlers that are     * a bit tricky are MessageHandler and MessageListHandler because they can     * be nested in the XML document. Therefore, those classes use an internal     * stacks to remember where to pass back the flow of control after ending.     */    private ForumHandler forumHandler = new ForumHandler();    private ForumListHandler forumListHandler = new ForumListHandler();    private GroupHandler groupHandler = new GroupHandler();    private GroupMemberHandler groupMemberHandler = new GroupMemberHandler();    private GroupListHandler groupListHandler = new GroupListHandler();    private GroupPermissionListHandler groupPermissionListHandler        = new GroupPermissionListHandler();    private MessageHandler messageHandler = new MessageHandler();    private MessageListHandler messageListHandler = new MessageListHandler();    private PermissionListHandler permissionListHandler        = new PermissionListHandler();    private PropertyListHandler propertyListHandler = new PropertyListHandler();    private ThreadHandler threadHandler = new ThreadHandler();    private ThreadListHandler threadListHandler = new ThreadListHandler();    private UserHandler userHandler = new UserHandler();    private UserListHandler userListHandler = new UserListHandler();    private UserPermissionListHandler userPermissionListHandler        = new UserPermissionListHandler();    /**     * Default Jive handler (all handlers should extend this class)     */    private class JiveDefaultHandler extends DefaultHandler {        // The name of the element this handler handles, ie "Forum" or "UserList"        private String localName;        // The parent handler of this handler        protected ContentHandler parentHandler;        /**         * Creates the default handler.         *         * @param localName the name of the element this handler handles,         * ie, "Forum" or "UserList"         */        public JiveDefaultHandler(String localName) {            this.localName = localName;        }        /**         * This method should be overridden if a subclass needs to reinitialize         * locale fields after the parent handler is set. This method is not         * abstract because it's not required that it be overriddent.         */        public void init() {}        /**         * Sets the parentHandler of the handler. Control will be passed back         * to the parent when this handler is done parsing the XML it knows         * how to handle.         *         * @param parentHandler the parent handler for this handler         */        public void setParentHandler(DefaultHandler parentHandler) {            this.parentHandler = parentHandler;            init();        }        public void endElement(String uri, String localName, String tag)                throws SAXParseException        {            if (localName.equals(this.localName)) {                // Let the parent resume handling of SAX events                if (parentHandler != null) {                    parser.setContentHandler(parentHandler);                }            }        }        public void warning(SAXParseException e) throws SAXException {            log("Received a parse warning in element " + this.localName + " at line "                    + e.getLineNumber());        }        public void error(SAXParseException e) throws SAXException {            log("Received a parse error in element " + this.localName + " at line "                    + e.getLineNumber());        }        public void fatalError(SAXParseException e) throws SAXException {            log("Received a parse fatal error in element " + this.localName + " at line "                    + e.getLineNumber());        }    }    /**     * Main handler -- it passes control of flow to each of the main     * sub-elements.     */    private final class JiveHandler extends JiveDefaultHandler {        public JiveHandler() {            super("Jive");        }        public void startElement(String uri, String localName, String tag, Attributes attribs)                throws SAXParseException        {            if (localName.equals("Jive")) {                // Check attributes here such as the Jive XML version, etc.            }            else if (localName.equals("UserList")) {                userListHandler.setParentHandler(this);                parser.setContentHandler(userListHandler);            }            else if (localName.equals("GroupList")) {                groupListHandler.setParentHandler(this);                parser.setContentHandler(groupListHandler);            }            else if (localName.equals("ForumList")) {                forumListHandler.setParentHandler(this);                parser.setContentHandler(forumListHandler);            }            else if (localName.equals("UserPermissionList")) {                userPermissionListHandler.setParentHandler(this);                parser.setContentHandler(userPermissionListHandler);            }            else if (localName.equals("GroupPermissionList")) {            }        }    }    /**     * Handler for user list elements.     */    private class UserListHandler extends JiveDefaultHandler {        public UserListHandler() {            super("UserList");        }        public void startElement(String uri, String localName, String tag,                Attributes attribs)                throws SAXParseException        {            if (localName.equals("User")) {                userHandler.setParentHandler(this);                parser.setContentHandler(userHandler);            }        }    }    /**     * Handler for user elements.     */    private class UserHandler extends JiveDefaultHandler implements PropertyStore {        private User newUser;        private boolean isUsernameSet = false;        private boolean isPasswordSet = false;        private boolean isEmailSet = false;        private StringBuffer username = new StringBuffer();        private StringBuffer password = new StringBuffer();        private StringBuffer email = new StringBuffer();        private StringBuffer name = new StringBuffer();        private StringBuffer creationDate = new StringBuffer();        private StringBuffer modifiedDate = new StringBuffer();        private boolean nameVisible = false;        private boolean emailVisible = false;        private Map properties = null;        private boolean doCreateUser = false;        public UserHandler() {            super("User");        }        /**         * Re-initializes the fields of this handler.         */        public void init() {            //reset data            newUser = null;            isUsernameSet = false;            isPasswordSet = false;            isEmailSet = false;            username.setLength(0);            password.setLength(0);            email.setLength(0);            name.setLength(0);            creationDate.setLength(0);            modifiedDate.setLength(0);            nameVisible = false;            emailVisible = false;            properties = new HashMap();            doCreateUser = false;        }        public void addProperty(String name, String value) {            properties.put(name,value);

⌨️ 快捷键说明

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