📄 xmlconfig.java
字号:
/* * @(#) XMLConfig.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.misc;//导入核心Java类库import java.io.File;import java.io.IOException;import java.awt.Image;import java.util.List;import java.util.ArrayList;import javax.swing.ImageIcon;//导入自定义Java类库import hws.item.smart.utility.mail.POP3Info;import hws.item.smart.utility.mail.SMTPInfo;import hws.item.smart.utility.mail.AccountInfo;import hws.item.smart.utility.mail.PersonalInfo;//导入第三方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-26 * @author Hwerz */public class XMLConfig extends Object { /*------------------------------------------------------------------------* * 属性定义 * *------------------------------------------------------------------------*/ /** * 静态常量 */ private static final String READ_NODE = "Read"; /** * 静态常量 */ private static final String NAME_NODE = "Name"; /** * 静态常量 */ private static final String NAVBAR_NODE = "NAVBar"; /** * 静态常量 */ private static final String URL_NODE = "URL"; /** * 静态常量 */ private static final String PORT_NODE = "Port"; /** * 静态常量 */ private static final String USERID_NODE = "UserID"; /** * 静态常量 */ private static final String PASSWORD_NODE = "Password"; /** * 静态常量 */ private static final String CHAT_NODE = "Chat"; /** * 静态常量 */ private static final String SHARE_NODE = "Share"; /** * 静态常量 */ private static final String DOWNLOAD_NODE = "Download"; /** * 静态常量 */ private static final String SERVICES_NODE = "Services"; /** * 静态常量 */ private static final String ID_ATTR = "id"; /** * 静态常量 */ private static final String PORT_ATTR = "port"; /** * 静态常量 */ private static final String CLASS_ATTR = "class"; /** * 静态常量 */ private static final String METHOD_ATTR = "method"; /** * 配置文件的文档 */ private static Document document; /*------------------------------------------------------------------------* * 构造函数 * *------------------------------------------------------------------------*/ /** * Create a new instance of this class */ private XMLConfig() { super(); document = null; } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 返回软件的名称 * * @return 软件的名称 */ public static String getSoftwareName() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(READ_NODE).getChild(NAME_NODE); return child.getValue(); } /** * 返回软件的版本 * * @return 软件的版本 */ public static String getSoftwareVersion() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(READ_NODE).getChild("Version"); return child.getValue(); } /** * 返回软件的描述信息 * * @return 软件的描述信息 */ public static String getSoftwareDescription() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(READ_NODE).getChild("Description"); return child.getValue(); } /** * 返回软件的主页 * * @return 软件的主页 */ public static String getSoftwareHomepage() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(READ_NODE).getChild("Homepage"); return child.getValue(); } /** * 返回软件的版权信息 * * @return 软件的版权信息 */ public static String getSoftwareCopyright() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(READ_NODE).getChild("Copyright"); return child.getValue(); } /** * 返回导航栏中父按钮的个数 * * @return 导航栏中父按钮的个数 */ public static int getParentButtonCount() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); return child.getChildren().size(); } /** * 返回指定序号的父按钮的标签 * * @param index 父按钮的序号 * @return 指定序号的父按钮的标签 */ public static String getParentButtonLabel(int index) { String label = null; if (index >= 0 && index < getParentButtonCount()) { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); Element e = (Element) child.getChildren().get(index); label = e.getAttributeValue("label"); } return label; } /** * 返回默认情况下选中的父按钮序号 * * @return 默认情况下选中的父按钮序号 */ public static int getDefaultParentButtonIndex() { int index = 0; Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); String label = child.getAttributeValue("default"); for (int i = 0; i < getParentButtonCount(); i++) { if (getParentButtonLabel(i).equals(label) == true) { index = i; break; } } return index; } /** * 返回指定父按钮的子按钮个数 * * @param index 指定父按钮的序号 * @return 指定父按钮的子按钮个数 */ public static int getSonButtonCount(int index) { int count = 0; if (index >= 0 && index < getParentButtonCount()) { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); Element e = (Element) child.getChildren().get(index); count = e.getChildren().size(); } return count; } /** * 返回默认情况下选中的子按钮序号 * * @param index 指定父按钮的序号 * @return 默认情况下选中的子按钮序号 */ public static int getDefaultSonButtonIndex(int index) { int index2 = 0; Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); Element e = (Element) child.getChildren().get(index); String label = e.getAttributeValue("default"); for (int i = 0; i < getSonButtonCount(index); i++) { if (getSonButtonLabel(index, i).equals(label) == true) { index2 = i; break; } } return index2; } /** * 返回指定父按钮的指定子按钮的标签 * * @param index1 指定父按钮的序号 * @param index2 指定子按钮的序号 * @return 指定父按钮的指定子按钮的标签 */ public static String getSonButtonLabel(int index1, int index2) { String label = null; if (index1 >= 0 && index1 < getParentButtonCount() && index2 >= 0 && index2 < getSonButtonCount(index1)) { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); Element e1 = (Element) child.getChildren().get(index1); Element e2 = (Element) e1.getChildren().get(index2); label = e2.getAttributeValue("label"); } return label; } /** * 返回指定父按钮的指定子按钮的图片 * * @param index1 指定父按钮的序号 * @param index2 指定子按钮的序号 * @return 指定父按钮的指定子按钮的图片 */ public static Image getSonButtonImage(int index1, int index2) { Image image = null; if (index1 >= 0 && index1 < getParentButtonCount() && index2 >= 0 && index2 < getSonButtonCount(index1)) { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); Element e1 = (Element) child.getChildren().get(index1); Element e2 = (Element) e1.getChildren().get(index2); String i = e2.getAttributeValue("image"); image = (new ImageIcon(XMLConfig.class.getResource(i))).getImage(); } return image; } /** * 返回指定父按钮的指定子按钮的标签 * * @param index1 指定父按钮的序号 * @param index2 指定子按钮的序号 * @return 指定父按钮的指定子按钮的标签 */ public static String getSonButtonClass(int index1, int index2) { String class2 = null; if (index1 >= 0 && index1 < getParentButtonCount() && index2 >= 0 && index2 < getSonButtonCount(index1)) { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(NAVBAR_NODE); Element e1 = (Element) child.getChildren().get(index1); Element e2 = (Element) e1.getChildren().get(index2); class2 = e2.getAttributeValue(CLASS_ATTR); } return class2; } /** * 返回所有的账号 * * @return 所有的账号 */ public static List getAllAccounts() { List accounts = new ArrayList(); Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild("Mail").getChild("Accounts"); List children = child.getChildren(); for (int i = 0; i < children.size(); i++) { Element account = (Element) children.get(i); accounts.add(accountElement2AccountInfo(account)); } return accounts; } /** * 添加账号 * * @param info 待添加的账号信息 * @param save 是否立即保存文件 */ public static void addAccount(AccountInfo info, boolean save) { Document doc = getDocument(); Element root = doc.getRootElement(); Element accounts = root.getChild("Mail").getChild("Accounts"); accounts.addContent(accountInfo2AccountElement(info)); if (save == true) { saveDocument(); } } /** * 添加所有账号 * * @param accounts 待的添加信息列表 * @param save 是否立即保存文件 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -