dbdataimport.java

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

JAVA
1,395
字号
        private boolean isCreationDateSet = false;        private boolean isModifiedDateSet = false;        private boolean doCreateMessage = false;        private boolean messageCreated = false;        private StringBuffer subject = new StringBuffer();        private StringBuffer body = new StringBuffer();        private StringBuffer username = new StringBuffer();        private StringBuffer creationDate = new StringBuffer();        private StringBuffer modifiedDate = new StringBuffer();        private LinkedList parentHandlerList = new LinkedList();        public MessageHandler() {            super("Message");        }        public void addParentHandler(DefaultHandler parentHandler) {            parentHandlerList.addFirst(parentHandler);            // Now, reset all fields.            message = null;            parentMessage = null;            isSubjectSet = false;            isBodySet = false;            isCreationDateSet = false;            isModifiedDateSet = false;            doCreateMessage = false;            messageCreated = false;            subject.setLength(0);            body.setLength(0);            username.setLength(0);            creationDate.setLength(0);            modifiedDate.setLength(0);        }        public void setParentMessage(ForumMessage message) {            parentMessage = message;        }        public void addProperty(String name, String value) {            if (message != null) {                try {                    message.setProperty(name,value);                }                catch (UnauthorizedException ue) {}            }            else {                log("Message was null, can't add property.");            }        }        public void startElement(String uri, String localName, String tag, Attributes attribs)                throws SAXParseException        {            // Check for various elements under a <Message> tag            if (localName.equals("Subject")) {                mode = SUBJECT;                isSubjectSet = true;            }            else if (localName.equals("Body")) {                mode = BODY;                isBodySet = true;            }            else if (localName.equals("Username")) {                mode = USERNAME;            }            else if (localName.equals("CreationDate")) {                mode = CREATION_DATE;                isCreationDateSet = true;            }            else if (localName.equals("ModifiedDate")) {                mode = MODIFIED_DATE;                isModifiedDateSet = true;            }            else if (localName.equals("PropertyList")) {                // Get any extended message properties.                propertyListHandler.setParentHandler(this);                propertyListHandler.setPropertyStore(this);                parser.setContentHandler(propertyListHandler);            }            else if (localName.equals("MessageList")) {                // Get any child messages.                messageListHandler.addParentHandler(this);                messageListHandler.addMessage(message);                parser.setContentHandler(messageListHandler);            }        }        public void characters(char[] buf, int start, int length) throws SAXParseException {            switch (mode) {                case SUBJECT:                    subject.append(buf, start, length);                    break;                case BODY:                    body.append(buf, start, length);                    break;                case USERNAME:                    username.append(buf, start, length);                    break;                case CREATION_DATE:                    creationDate.append(buf, start, length);                    break;                case MODIFIED_DATE:                    modifiedDate.append(buf, start, length);                    break;            }            // See if we're ready to create a message yet.            doCreateMessage = (isSubjectSet && isBodySet && isCreationDateSet && isModifiedDateSet);        }        public void endElement(String uri, String localName, String tag)                throws SAXParseException        {            // Reset the mode now -- (fixes whitespace padding)            mode = 0;            // Create a message, if not created already            if (doCreateMessage && !messageCreated) {                createMessage();                messageCreated = true;            }            if (localName.equals("Message")) {                // Pop a parentHandler off the stack and return flow of control                // to it.                DefaultHandler parentHandler = (DefaultHandler)parentHandlerList.removeFirst();                parser.setContentHandler(parentHandler);            }        }        private void createMessage() {            boolean userNotFound = false;            //first, get the message user. If the user fails to load, we'll            //make this user an 'anonymous' user            String uName = username.toString();            User user = null;            if (!uName.equals("")) {                try {                    user = userManager.getUser(uName);                }                catch (UserNotFoundException unfe) {                    userNotFound = true;                }            }            try {                if (user != null) {                    message = forumFactory.createMessage(user);                }                else {                    message = forumFactory.createMessage();                }                // set the properties of the message                message.setSubject(subject.toString());                message.setBody(body.toString());                Date cDate = parseDate(creationDate.toString());                Date mDate = parseDate(modifiedDate.toString());                message.setCreationDate(cDate);                message.setModifiedDate(mDate);                // If a user was not found, add the username we found as the message                // property "name"                if (userNotFound) {                    addProperty("name", uName);                }            }            catch (Exception e) {                e.printStackTrace();            }            // set this message as the root message of the current            // thread if the current thread doesn't have a root message            if (!threadHasRootMessage) {                try {                    thread = forumFactory.createThread(message);                    forum.addThread(thread);                    threadHasRootMessage = true;                }                catch (UnauthorizedException ue) {                    ue.printStackTrace();                }            }            else {                if (parentMessage != null) {                    try {                        thread.addMessage(parentMessage,message);                    }                    catch (UnauthorizedException ue) {}                }            }        }    }    /**     * A handler for message list elements.     */    private class MessageListHandler extends JiveDefaultHandler {        private LinkedList messageList = new LinkedList();        private LinkedList parentHandlerList = new LinkedList();        public MessageListHandler() {            super("MessageList");        }        public void addParentHandler(DefaultHandler parentHandler) {            parentHandlerList.addFirst(parentHandler);        }        public void addMessage(ForumMessage message) {            messageList.addFirst(message);        }        public void startElement(String uri, String localName, String tag, Attributes attribs)                throws SAXParseException        {            if (localName.equals("Message")) {                messageHandler.addParentHandler(this);                ForumMessage parentMessage = (ForumMessage)messageList.getFirst();                messageHandler.setParentMessage(parentMessage);                parser.setContentHandler(messageHandler);            }        }        public void endElement(String uri, String localName, String tag)                throws SAXParseException        {            if (localName.equals("MessageList")) {                // Pop a parentHandler off the stack and return flow of control                // to it. We also pop a message of the messageList since it will                // no longer need to be used.                DefaultHandler parentHandler = (DefaultHandler)parentHandlerList.removeFirst();                messageList.removeFirst();                parser.setContentHandler(parentHandler);            }        }    }    /**     * A handler for property elements.     */    private class PropertyListHandler extends JiveDefaultHandler {        private PropertyStore propertyStore;        public PropertyListHandler() {            super("PropertyList");        }        /**         * Sets the property store that properties will be sent to.         */        public void setPropertyStore(PropertyStore propertyStore) {            this.propertyStore = propertyStore;        }        public void startElement(String uri, String localName, String tag,                Attributes attribs) throws SAXParseException        {            if (localName.equals("Property")) {                String propName = attribs.getValue("name");                String propValue = attribs.getValue("value");                // Make sure the property name and values aren't null or                // blank.                if (!(propName == null || "".equals(propName) ||                        propValue == null || "".equals(propValue)))                {                    propertyStore.addProperty(propName,propValue);                }            }        }    }    /**     * An interface that indicates an object has extended properties that     * can be set.     */    private interface PropertyStore {        /**         * Add a property to the store.         *         * @param name the name of the property.         * @param value the value of the property.         */        public void addProperty(String name, String value);    }     //    private static void log(String msg) {        // TODO: make message stream settable        System.err.println(msg);    }    /**     * main method (for testing purposes only)     */    public static void main(String args[]) {        // Get parameters        if (args.length < 3) {            log("USAGE: java DbDataImport <xml_data_file> <jive_username> "                + "<jive_password>");            return;        }        String xmlDataFile = args[0];        String username = args[1];        String password = args[2];        // Login to Jive, create a jive ForumFactory        Authorization authToken = null;        ForumFactory factory = null;        try {            authToken = AuthorizationFactory.getAuthorization(username, password);            log("Logged into Jive as '" + username + "'");            // Get the underlying forum factory implementation            factory = ForumFactory.getInstance(authToken);        }        catch (UnauthorizedException ue) {            log("ERROR: Jive authorization failed. Please "                + "verify that your username and password are correct and "                + "that a jive.properties file exists in your classpath.");            ue.printStackTrace();            return;        }        // Start an import, keep track of the time.        long time = 0;        try {            DbForumFactory dbForumFactory = (DbForumFactory)(((ForumFactoryProxy)factory).getProxiedForumFactory());            DbDataImport importer = new DbDataImport(dbForumFactory);            Reader in = new FileReader(new File(xmlDataFile));            time = System.currentTimeMillis();            importer.doImport(in);            time = System.currentTimeMillis() - time;            in.close();        }        catch (Exception e) {            e.printStackTrace();        }        log("Finished import.");        log("--------------------------------");        log("Total time: " + ((double)time/1000.0) + " seconds");        return;    }}

⌨️ 快捷键说明

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