⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlaccessor.java

📁 该系统是一个基于p2p的即时聊天系统
💻 JAVA
字号:
/* * @(#) XMLAccessor.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.utility.admin;//导入核心Java类库import java.io.File;import java.io.IOException;import java.util.List;import java.util.ArrayList;//导入自定义Java类库import hws.item.smart.misc.PopToolkit;import hws.item.smart.utility.chat.UserInfo;import hws.item.smart.utility.share.ShareInfo;//导入第三方Java类库import org.jdom.Element;import org.jdom.Document;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;/** * 读取和修改XML服务文件 * * @version 0.1 2005-08-30 * @author Hwerz */public class XMLAccessor extends Object {    /*------------------------------------------------------------------------*     *                                属性定义                                *     *------------------------------------------------------------------------*/    /**     * 静态常量     */    private static final String USERS_NODE = "Users";    /**     * 静态常量     */    private static final String BASIC_NODE = "Basic";    /**     * 静态常量     */    private static final String ID_NODE = "ID";    /**     * 静态常量     */    private static final String FRIENDS_NODE = "Friends";    /**     * 静态常量     */    private static final String SHARES_NODE = "Shares";    /**     * 静态常量     */    private static final String FILE_ATTR = "file";    /**     * 配置文件的文档     */    private static Document document;    /*------------------------------------------------------------------------*     *                                构造函数                                *     *------------------------------------------------------------------------*/    /**     * Create a new instance of this class     */    private XMLAccessor() {        super();        document = null;    }    /*------------------------------------------------------------------------*     *                                公共方法                                *     *------------------------------------------------------------------------*/    /**     * 返回所有注册用户     *     * @return 所有注册用户     */    public static List getAllUsers() {        List users = new ArrayList();        Document doc = getDocument();        Element root = doc.getRootElement();        Element child = root.getChild(USERS_NODE);        List children = child.getChildren();        for (int i = 0; i < children.size(); i++) {            Element user = (Element) children.get(i);            users.add(UserInfo.userElement2UserInfo(user));        }        return users;    }    /**     * 返回所有注册用户的ID     *     * @return 所有注册用户的ID     */    public static List getAllUsersID() {        List ids = new ArrayList();        Document doc = getDocument();        Element root = doc.getRootElement();        Element child = root.getChild(USERS_NODE);        List children = child.getChildren();        for (int i = 0; i < children.size(); i++) {            Element user = (Element) children.get(i);            ids.add(user.getChild(BASIC_NODE).getChild(ID_NODE).getText());        }        return ids;    }    /**     * 返回指定用户的所有好友     *     * @param id 指定用户的ID     * @return 指定用户的所有好友     */    public static List getAllFriends(String id) {        UserInfo user = getUser(id);        return user.getFriendsInfo().getAllFriends();    }    /**     * 返回具有指定ID的注册用户     *     * @param id 指定注册用户的ID     * @return 具有指定ID的注册用户     */    public static UserInfo getUser(String id) {        UserInfo user = null;        Document doc = getDocument();        Element root = doc.getRootElement();        Element child = root.getChild(USERS_NODE);        List children = child.getChildren();        for (int i = 0; i < children.size(); i++) {            Element e = (Element) children.get(i);            String id2 = e.getChild(BASIC_NODE).getChild(ID_NODE).getText();            if (id2.equals(id) == true) {                user = UserInfo.userElement2UserInfo(e);                break;            }        }        return user;    }    /**     * 返回具有指定ID的注册用户     *     * @param id 指定注册用户的ID     * @param clone 如果为true则返回克隆元素,否则返回原始元素     * @return 具有指定ID的注册用户     */    public static Element getUserElement(String id, boolean clone) {        Element user = null;        Document doc = getDocument();        Element root = doc.getRootElement();        Element child = root.getChild(USERS_NODE);        List children = child.getChildren();        for (int i = 0; i < children.size(); i++) {            Element user2 = (Element) children.get(i);            String id2 = user2.getChild(BASIC_NODE).getChild(ID_NODE).getText();            if (id2.equals(id) == true) {                if (clone == true) {                    user = (Element) user2.clone();                } else {                    user = user2;                }                break;            }        }        return user;    }    /**     * 生成正在注册用户的ID     *     * @return 正在注册用户的ID     */    public static String genUserID() {        String id = null;        for (int i = 1; ; i++) {            id = String.valueOf(i);            if (getUser(id) == null) {                break;            }        }        return id;    }    /**     * 添加注册用户     *     * @param user 注册用户     * @param save 是否立即保存文件     */    public static void addUser(Element user, boolean save) {        Document doc = getDocument();        Element root = doc.getRootElement();        Element child = root.getChild(USERS_NODE);        child.addContent(user);        if (save == true) {            saveDocument();        }    }    /**     * 添加注册用户     *     * @param user 注册用户     * @param index 待添加用户的位置     * @param save 是否立即保存文件     */    public static void addUser(Element user, int index, boolean save) {        Document doc = getDocument();        Element root = doc.getRootElement();        Element child = root.getChild(USERS_NODE);        child.addContent(index, user);        if (save == true) {            saveDocument();        }    }    /**     * 删除指定的用户     *     * @param id 待删除用户的ID     * @param save 是否立即保存文件     */    public static void deleteUser(String id, boolean save) {        Document doc = getDocument();        Element root = doc.getRootElement();        Element child = root.getChild(USERS_NODE);        List children = child.getChildren();        for (int i = 0; i < children.size(); i++) {            Element user = (Element) children.get(i);            String id2 = user.getChild(BASIC_NODE).getChild(ID_NODE).getText();            if (id2.equals(id) == true) {                child.removeContent(user);            } else {                Element friends = user.getChild(FRIENDS_NODE);                List list = friends.getChildren();                for (int j = 0; j < list.size(); j++) {                    Element friend = (Element) list.get(j);                    if (friend.getAttributeValue("id").equals(id)) {                        friends.removeContent(friend);                        break;                    }                }                Element shares = user.getChild("Shares");                list = shares.getChildren();                for (int k = 0; k < list.size(); k++) {                    Element share = (Element) list.get(k);                    friends = share.getChild(FRIENDS_NODE);                    List list2 = friends.getChildren();                    for (int l = 0; l < list2.size(); l++) {                        Element friend = (Element) list2.get(l);                        if (friend.getAttributeValue("id").equals(id)) {                            friends.removeContent(friend);                            break;                        }                    }                    if (friends.getChildren().size() == 0) {                        shares.removeContent(share);                    }                }            }        }        if (save == true) {            saveDocument();        }    }    /**     * 设置共享信息     *     * @param id 待设置共享信息的用户     * @param share 待设置的共享信息     * @param save 是否立即保存文件     */    public static void setShare(String id, Element share, boolean save) {        String file = share.getAttributeValue(FILE_ATTR);        Document doc = getDocument();        Element root = doc.getRootElement();        List users = root.getChild(USERS_NODE).getChildren();        for (int i = 0; i < users.size(); i++) {            Element user = (Element) users.get(i);            if (user.getChild(BASIC_NODE).getChild(ID_NODE).getText()                .equals(id)) {                Element shares = user.getChild(SHARES_NODE);                List shares2 = shares.getChildren();                for (int j = 0; j < shares2.size(); j++) {                    Element share2 = (Element) shares2.get(j);                    if (share2.getAttributeValue(FILE_ATTR).equals(file)) {                        shares.removeContent(share2);                        break;                    }                }                if (share.getChild(FRIENDS_NODE).getChildren().size() > 0) {                    shares.addContent(share);                }                break;            }        }        if (save == true) {            saveDocument();        }    }    /**     * 返回指定用户的Shares元素     *     * @param id 指定用户的ID     * @return 指定用户的Shares元素     */    public static Element getSharesElement(String id) {        Element shares = null;        Document doc = getDocument();        Element root = doc.getRootElement();        List users = root.getChild(USERS_NODE).getChildren();        for (int i = 0; i < users.size(); i++) {            Element user = (Element) users.get(i);            if (user.getChild(BASIC_NODE).getChild(ID_NODE).getText()                .equals(id)) {                shares = user.getChild(SHARES_NODE);                break;            }        }        return shares;    }    /**     * 返回指定用户和指定共享文件的克隆Friends元素     *     * @param id 指定用户的ID     * @param file 指定的共享文件     * @return 指定用户和指定共享文件的克隆Friends元素     */    public static Element getFriendsElement(String id, String file) {        Element friends = null;        Document doc = getDocument();        Element root = doc.getRootElement();        List users = root.getChild(USERS_NODE).getChildren();        for (int i = 0; i < users.size(); i++) {            Element user = (Element) users.get(i);            if (user.getChild(BASIC_NODE).getChild(ID_NODE).getText()                .equals(id)) {                List shares = user.getChild(SHARES_NODE).getChildren();                for (int j = 0; j < shares.size(); j++) {                    Element share = (Element) shares.get(j);                    if (share.getAttributeValue(FILE_ATTR).equals(file)) {                        friends = (Element) share.getChild(FRIENDS_NODE)                            .clone();                        break;                    }                }            }            if (friends != null) {                break;            }        }        return friends;    }    /**     * 返回所有共享给指定用户的文件     *     * @param id 指定用户的ID     * @return 所有共享给指定用户的文件     */    public static Element getMyShares(String id) {        Element sharesE = new Element(SHARES_NODE);        boolean contained;        List users = getAllUsersID();        for (int i = 0; i < users.size(); i++) {            contained = false;            String id2 = users.get(i).toString();            Element shareE = new Element("Share");            shareE.setAttribute("id", id2);            Element filesE = new Element("Files");            shareE.addContent(filesE);            if (id2.equals(id) == false) {                List shares = getSharesElement(id2).getChildren();                for (int j = 0; j < shares.size(); j++) {                    ShareInfo share = ShareInfo.shareElement2ShareInfo(                        (Element) shares.get(j));                    if (share.getFriends().getAllFriends().contains(id)) {                        Element fileE = new Element("File");                        fileE.setAttribute(FILE_ATTR, share.getFile());                        filesE.addContent(fileE);                        contained = true;                    }                }            }            if (contained) {                sharesE.addContent(shareE);            }        }        return sharesE;    }    /**     * 保存文档     */    public static void saveDocument() {        Format format = Format.getPrettyFormat();        format.setEncoding("GB2312");        format.setIndent("    ");        format.setOmitDeclaration(false);        XMLOutputter outputter = new XMLOutputter(format);        String xml = outputter.outputString(getDocument());        xml = xml.replaceAll("\n\r", "");        xml = xml.substring(0, xml.length() - 2);        PopToolkit.saveString2File(xml, new File(getServicesFileName()));    }    /*------------------------------------------------------------------------*     *                                私有方法                                *     *------------------------------------------------------------------------*/    /**     * 返回被解析文档的org.jdom.Document对象     *     * @return 被解析文档的org.jdom.Document对象     */    private static Document getDocument() {        if (document == null) {            try {                SAXBuilder builder = new SAXBuilder(true);                File parsedFile = new File(getServicesFileName());                document = builder.build(parsedFile);            } catch (JDOMException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }        }        return document;    }    /**     * 返回服务文件的文件名     *     * @return 服务文件的文件名     */    private static String getServicesFileName() {        String url = XMLAccessor.class.getResource("XMLAccessor.class")            .getFile();        String directory = new File(url).getParent();        String fileName = directory + "\\xml\\services.xml";        return fileName;    }}

⌨️ 快捷键说明

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