📄 xmlconfig.java
字号:
public static String getAddressClass() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Address"); return child.getAttributeValue(CLASS_ATTR); } /** * 返回地址服务的方法名 * * @return 地址服务的方法名 */ public static String getAddressMethod() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Address"); return child.getAttributeValue(METHOD_ATTR); } /** * 返回更新服务的端口 * * @return 更新服务的端口 */ public static int getUpdatePort() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Update"); return Integer.parseInt(child.getAttributeValue(PORT_ATTR)); } /** * 返回更新服务的类名 * * @return 更新服务的类名 */ public static String getUpdateClass() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Update"); return child.getAttributeValue(CLASS_ATTR); } /** * 返回更新服务的方法名 * * @return 更新服务的方法名 */ public static String getUpdateMethod() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Update"); return child.getAttributeValue(METHOD_ATTR); } /** * 返回用户服务的端口 * * @return 用户服务的端口 */ public static int getUserPort() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("User"); return Integer.parseInt(child.getAttributeValue(PORT_ATTR)); } /** * 返回用户服务的类名 * * @return 用户服务的类名 */ public static String getUserClass() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("User"); return child.getAttributeValue(CLASS_ATTR); } /** * 返回用户服务的方法名 * * @return 用户服务的方法名 */ public static String getUserMethod() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("User"); return child.getAttributeValue(METHOD_ATTR); } /** * 返回好友服务的端口 * * @return 好友服务的端口 */ public static int getFriendPort() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Friend"); return Integer.parseInt(child.getAttributeValue(PORT_ATTR)); } /** * 返回好友服务的类名 * * @return 好友服务的类名 */ public static String getFriendClass() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Friend"); return child.getAttributeValue(CLASS_ATTR); } /** * 返回好友服务的方法名 * * @return 好友服务的方法名 */ public static String getFriendMethod() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Friend"); return child.getAttributeValue(METHOD_ATTR); } /** * 返回设置服务的端口 * * @return 设置服务的端口 */ public static int getSettingPort() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Setting"); return Integer.parseInt(child.getAttributeValue(PORT_ATTR)); } /** * 返回设置服务的类名 * * @return 设置服务的类名 */ public static String getSettingClass() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Setting"); return child.getAttributeValue(CLASS_ATTR); } /** * 返回设置服务的方法名 * * @return 设置服务的方法名 */ public static String getSettingMethod() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Setting"); return child.getAttributeValue(METHOD_ATTR); } /** * 返回获取服务的端口 * * @return 获取服务的端口 */ public static int getGettingPort() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Getting"); return Integer.parseInt(child.getAttributeValue(PORT_ATTR)); } /** * 返回获取服务的类名 * * @return 获取服务的类名 */ public static String getGettingClass() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Getting"); return child.getAttributeValue(CLASS_ATTR); } /** * 返回获取服务的方法名 * * @return 获取服务的方法名 */ public static String getGettingMethod() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("Getting"); return child.getAttributeValue(METHOD_ATTR); } /** * 返回文件服务的端口 * * @return 文件服务的端口 */ public static int getFilePort() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("File"); return Integer.parseInt(child.getAttributeValue(PORT_ATTR)); } /** * 返回文件服务的类名 * * @return 文件服务的类名 */ public static String getFileClass() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("File"); return child.getAttributeValue(CLASS_ATTR); } /** * 返回文件服务的方法名 * * @return 文件服务的方法名 */ public static String getFileMethod() { Document doc = getDocument(); Element root = doc.getRootElement(); Element child = root.getChild(SERVICES_NODE).getChild("File"); return child.getAttributeValue(METHOD_ATTR); } /*------------------------------------------------------------------------* * 私有方法 * *------------------------------------------------------------------------*/ /** * 将指定的Account元素转换成AccountInfo对象 * * @param account 指定的Account元素 * @return 转换后的AccountInfo对象 */ private static AccountInfo accountElement2AccountInfo(Element account) { Element element = account.getChild("Personal"); PersonalInfo info1 = new PersonalInfo( element.getChild(NAME_NODE).getValue(), element.getChild("Organization").getValue(), element.getChild("Email").getValue(), element.getChild("Reply").getValue()); element = account.getChild("SMTP"); SMTPInfo info2 = new SMTPInfo( element.getChild(URL_NODE).getValue(), element.getChild(PORT_NODE).getValue(), element.getChild(USERID_NODE).getValue(), element.getChild(PASSWORD_NODE).getValue()); element = account.getChild("POP3"); POP3Info info3 = new POP3Info( element.getChild(URL_NODE).getValue(), element.getChild(PORT_NODE).getValue(), element.getChild(USERID_NODE).getValue(), element.getChild(PASSWORD_NODE).getValue()); return new AccountInfo(info1, info2, info3); } /** * 将指定的AccountInfo对象转换成Account元素 * * @param info 指定的AccountInfo对象 * @return 转换后的Account元素 */ private static Element accountInfo2AccountElement(AccountInfo info) { //账号节点 Element account = new Element("Account"); //个人信息节点 PersonalInfo info1 = info.getPersonalInfo(); Element personal = new Element("Personal"); account.addContent(personal); Element element = new Element(NAME_NODE); element.setText(info1.getName()); personal.addContent(element); element = new Element("Organization"); element.setText(info1.getOrganization()); personal.addContent(element); element = new Element("Email"); element.setText(info1.getEmail()); personal.addContent(element); element = new Element("Reply"); element.setText(info1.getReply()); personal.addContent(element); //SMTP节点 SMTPInfo info2 = info.getSMTPInfo(); Element smtp = new Element("SMTP"); account.addContent(smtp); element = new Element(URL_NODE); element.setText(info2.getURL()); smtp.addContent(element); element = new Element(PORT_NODE); element.setText(info2.getPort()); smtp.addContent(element); element = new Element(USERID_NODE); element.setText(info2.getUserID()); smtp.addContent(element); element = new Element(PASSWORD_NODE); element.setText(info2.getPassword()); smtp.addContent(element); //POP3节点 POP3Info info3 = info.getPOP3Info(); Element pop3 = new Element("POP3"); account.addContent(pop3); element = new Element(URL_NODE); element.setText(info3.getURL()); pop3.addContent(element); element = new Element(PORT_NODE); element.setText(info3.getPort()); pop3.addContent(element); element = new Element(USERID_NODE); element.setText(info3.getUserID()); pop3.addContent(element); element = new Element(PASSWORD_NODE); element.setText(info3.getPassword()); pop3.addContent(element); return account; } /** * 返回被解析文档的org.jdom.Document对象 * * @return 被解析文档的org.jdom.Document对象 */ private static Document getDocument() { if (document == null) { try { SAXBuilder builder = new SAXBuilder(true); File parsedFile = new File(getConfigFileName()); document = builder.build(parsedFile); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return document; } /** * 保存文档 */ private 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(getConfigFileName())); } /** * 返回配置文件的文件名 * * @return 配置文件的文件名 */ private static String getConfigFileName() { String url = XMLConfig.class.getResource("XMLConfig.class").getFile(); String directory = new File(url).getParent(); String fileName = directory + "\\resources\\config\\config.xml"; return fileName; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -