dbdataimport.java

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

JAVA
1,395
字号
         }        public void startElement(String uri, String localName, String tag,                Attributes attribs)                throws SAXParseException        {            if (localName.equals("Username")) {                mode = USERNAME;            }            else if (localName.equals("Password")) {                mode = PASSWORD;            }            else if (localName.equals("Email")) {                mode = EMAIL;                nameVisible = Boolean.valueOf(attribs.getValue(0)).booleanValue();            }            else if (localName.equals("Name")) {                mode = NAME;                emailVisible = Boolean.valueOf(attribs.getValue(0)).booleanValue();            }            else if (localName.equals("CreationDate")) {                mode = CREATION_DATE;            }            else if (localName.equals("ModifiedDate")) {                mode = MODIFIED_DATE;            }            else if (localName.equals("PropertyList")) {                propertyListHandler.setParentHandler(this);                propertyListHandler.setPropertyStore(this);                parser.setContentHandler(propertyListHandler);            }        }        public void characters(char[] buf, int start, int length)                throws SAXParseException        {            switch (mode) {                case USERNAME:                    username.append(buf,start,length);                    isUsernameSet = true;                    break;                case PASSWORD:                    password.append(buf,start,length);                    isPasswordSet = true;                    break;                case EMAIL:                    email.append(buf,start,length);                    isEmailSet = true;                    break;                case NAME:                    name.append(buf,start,length);                    break;                case CREATION_DATE:                    creationDate.append(buf,start,length);                    break;                case MODIFIED_DATE:                    modifiedDate.append(buf,start,length);                    break;            }            // check and see if we can create a message            doCreateUser = (isUsernameSet && isPasswordSet && isEmailSet);        }        public void endElement(String uri, String localName, String tag)                throws SAXParseException        {            // reset the mode now -- (fixes whitespace padding)            mode = 0;            if (localName.equals("User") && doCreateUser) {                // create the user                createUser();                // Let parent resume handling SAX events                parser.setContentHandler(parentHandler);            }        }        private void createUser() {            try {                // Create the user. We pass in a map of properties at creation                // time because this is the most efficient way to add                // extended properties                newUser = userManager.createUser(                    username.toString(),                    password.toString(),                    name.toString(),                    email.toString(),                    nameVisible,emailVisible,properties                );                // Set the password hash -- the XML file gives us the password                // hash, not the original password                newUser.setPasswordHash(password.toString());                // Set the creation date of the user                try {                    newUser.setCreationDate(dateFormatter.parse(creationDate.toString()));                } catch(ParseException pe) {}                // Set the modified date of the user                try {                    newUser.setModifiedDate(dateFormatter.parse(creationDate.toString()));                } catch(ParseException pe) {}            }            catch (UserAlreadyExistsException uaee) {                //log("User '" + username + "' already exists.");            }            catch (UnauthorizedException ue) {                ue.printStackTrace();            }        }    }    /**     * Handler for group list elements.     */    private class GroupListHandler extends JiveDefaultHandler {        public GroupListHandler() {            super("GroupList");        }        public void startElement(String uri, String localName, String tag,                Attributes attribs)                throws SAXParseException        {            if (localName.equals("Group")) {                groupHandler.setParentHandler(this);                parser.setContentHandler(groupHandler);            }        }    }    /**     * Handler for group elements.     */    private class GroupHandler extends JiveDefaultHandler implements PropertyStore {        private Group group;        private List memberList = new ArrayList();        private List adminList = new ArrayList();        private StringBuffer name = new StringBuffer();        private StringBuffer description = new StringBuffer();        private StringBuffer creationDate = new StringBuffer();        private StringBuffer modifiedDate = new StringBuffer();        private Map properties = null;        public GroupHandler() {            super("Group");        }        public void init() {            // Reset data.            group = null;            memberList.clear();            adminList.clear();            name.setLength(0);            description.setLength(0);            creationDate.setLength(0);            modifiedDate.setLength(0);            properties = new HashMap();        }        public void addProperty(String name, String value) {            properties.put(name,value);        }        public void setMemberList(List memberList) {            this.memberList = memberList;        }        public void setAdminList(List adminList) {            this.adminList = adminList;        }        public void startElement(String uri, String localName, String tag, Attributes attribs)                throws SAXParseException        {            if (localName.equals("Name")) {                mode = NAME;            }            else if (localName.equals("Description")) {                mode = DESCRIPTION;            }            else if (localName.equals("CreationDate")) {                mode = CREATION_DATE;            }            else if (localName.equals("ModifiedDate")) {                mode = MODIFIED_DATE;            }            else if (localName.equals("PropertyList")) {                propertyListHandler.setParentHandler(this);                propertyListHandler.setPropertyStore(this);                parser.setContentHandler(propertyListHandler);            }            else if (localName.equals("MemberList")) {                groupMemberHandler.setParentHandler(this);                parser.setContentHandler(groupMemberHandler);            }            else if (localName.equals("AdministratorList")) {                groupMemberHandler.setParentHandler(this);                parser.setContentHandler(groupMemberHandler);            }        }        public void characters(char[] buf, int start, int length) throws SAXParseException {            switch (mode) {                case NAME:                    name.append(buf,start,length);                    break;                case DESCRIPTION:                    description.append(buf,start,length);                    break;                case CREATION_DATE:                    creationDate.append(buf,start,length);                    break;                case MODIFIED_DATE:                    modifiedDate.append(buf,start,length);                    break;            }        }        public void endElement(String uri, String localName, String tag) throws SAXParseException {            if (localName.equals("Group")) {                // Create the group.                createGroup();                // Let parent resume handling SAX events.                parser.setContentHandler(parentHandler);            }        }        private void createGroup() {            try {                // Create the group.                group = groupManager.createGroup(name.toString());                group.setDescription(description.toString());                // Add administrators and members                for (Iterator i=memberList.iterator(); i.hasNext(); ) {                    String username = (String)i.next();                    try {                        User user = userManager.getUser(username);                        group.addMember(user);                    }                    catch (UserNotFoundException unfe) {                        unfe.printStackTrace();                    }                }                for (Iterator i=adminList.iterator(); i.hasNext(); ) {                    String username = (String)i.next();                    try {                        User user = userManager.getUser(username);                        group.addAdministrator(user);                    }                    catch (UserNotFoundException unfe) {                        unfe.printStackTrace();                    }                }                // Set the creation date of the group.                try {                    group.setCreationDate(dateFormatter.parse(creationDate.toString()));                }                catch(ParseException pe) {}                // Set the modified date of the user                try {                    group.setModifiedDate(dateFormatter.parse(creationDate.toString()));                }                catch(ParseException pe) {}                // Set properties of the group.                for (Iterator i=properties.keySet().iterator(); i.hasNext(); ) {                    String name = (String)i.next();                    String value = (String)properties.get(name);                    group.setProperty(name, value);                }            }            catch (GroupAlreadyExistsException uaee) {                log("Group '" + name + "' already exists.");            }            catch (UnauthorizedException ue) {                ue.printStackTrace();            }        }    }    /**     * Handler for group member and admin elements.     */    private class GroupMemberHandler extends JiveDefaultHandler {        private List usernameList = new ArrayList();        private StringBuffer username = new StringBuffer();        public GroupMemberHandler() {            super("GroupMember");        }        public void init() {            // Reset data.            usernameList.clear();            username.setLength(0);        }        public void characters(char[] buf, int start, int length) throws SAXParseException {            username.append(buf,start,length);        }        public void endElement(String uri, String localName, String tag)                throws SAXParseException        {            if (localName.equals("Username")) {                // Add the username to the list.                usernameList.add(username.toString());                // Clear out the username variable to prepare for the next.                username.setLength(0);            }            else if (localName.equals("MemberList")) {                // Let parent resume handling SAX events                parser.setContentHandler(parentHandler);                groupHandler.setMemberList(usernameList);            }            else if (localName.equals("AdministratorList")) {                // Let parent resume handling SAX events                parser.setContentHandler(parentHandler);                groupHandler.setAdminList(usernameList);            }        }    }    /**     * Handler for forum list elements.     */    private class ForumListHandler extends JiveDefaultHandler {        public ForumListHandler() {            super("ForumList");        }        public void startElement(String uri, String localName, String tag, Attributes attribs)                throws SAXParseException        {            if (localName.equals("Forum")) {                forumHandler.setParentHandler(this);                parser.setContentHandler(forumHandler);            }        }    }    /**     *  Handler for forum elements.     */

⌨️ 快捷键说明

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