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

📄 xmlconfig.java

📁 Java Mail Server JAVA编写的邮件服务器
💻 JAVA
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.     * *                                                              * * Copyright 2008 Jun Li(SiChuan University, the School of      * * Software Engineering). All rights reserved.                  * *                                                              * * Licensed to the JMS under one  or more contributor license   * * agreements.  See the LICENCE file  distributed with this     * * work for additional information regarding copyright          * * ownership.  The JMS licenses this file  you may not use this * * file except in compliance  with the License.                 * *                                                              * * Unless required by applicable law or agreed to in writing,   * * software distributed under the License is distributed on an  * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       * * KIND, either express or implied.  See the License for the    * * specific language governing permissions and limitations      * * under the License.                                           * ****************************************************************/package org.jpxx.mail.Config;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Iterator;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import org.jdom.output.XMLOutputter;/** * Use XML to config this server. *  * @author Jun Li * @version $Revision: 0.0.1 $, $Date: 2008/04/22 $ * @since JMS 0.0.1 */public class XmlConfig implements Configuration {    private Document doc;    private String path;    /**     *      * @param path     */    public XmlConfig(String path) {        this.path = path;        try {            SAXBuilder builder = new SAXBuilder(false);            doc = builder.build(new File(path));        } catch (JDOMException ex) {        } catch (IOException ex) {            create();        }    }    /**     *      * @param param     * @return the value of param     */    public String getValue(String param) {        Element root = doc.getRootElement();        List list = root.getChildren("Config");        for (Iterator i = list.iterator(); i.hasNext();) {            Element e = (Element) i.next();            if (e.getChild("Name").getText().equals(param)) {                return e.getChild("Value").getText().trim();            }        }        return "";    }    /**     * Add new param to XML config file     * @param param     * @param value     * @return If add successfully then return true, else return flse.     */    public boolean add(String param, String value) {        if (this.isParamExist(param)) {            return this.modifyValue(param, value);        }        Element root = doc.getRootElement();        Element _name = new Element("Name");        _name.setText(param);        Element _value = new Element("Value");        _value.setText(value);        Element config = new Element("Config");        config.addContent(_name);        config.addContent(_value);        root.addContent(config);        return this.save();    }    /**     *      * @param param     * @param value     * @return If modify successfully then return true, else return flse.     */    public boolean modifyValue(String param, String value) {        Element root = doc.getRootElement();        List list = root.getChildren("Config");        for (Iterator i = list.iterator(); i.hasNext();) {            Element e = (Element) i.next();            if (e.getChild("Name").getText().equals(param)) {                e.getChild("Value").setText(value);                return this.save();            }        }        return false;    }    /**     * Delete a param from XML file     * @param param     * @return If delete successfully then return true, else return flse.     */    public boolean delete(String param) {        Element root = doc.getRootElement();        List list = root.getChildren("Config");        for (Iterator i = list.iterator(); i.hasNext();) {            Element e = (Element) i.next();            if (e.getChild("Name").getText().equals(param)) {                root.removeContent(e);                this.save();                return true;            }        }        return false;    }    /**     * Check param exist or not     * @param param     * @return If parameter already exists then return true, else return flse.     */    public boolean isParamExist(String param) {        Element root = doc.getRootElement();        List list = root.getChildren("Config");        for (Iterator i = list.iterator(); i.hasNext();) {            Element e = (Element) i.next();            if (e.getChild("Name").getText().equals(param)) {                return true;            }        }        return false;    }    /**     * Save the XML file     * @param doc     * @return return true if save correct, otherwise return false.     */    private boolean save() {        XMLOutputter outputter = new XMLOutputter();        try {            FileWriter writer = new FileWriter(path);            Format format = Format.getPrettyFormat();            format.setEncoding("UTF-8");            format.setIndent("        ");            outputter.setFormat(format);            outputter.output(doc, writer);            writer.close();            return true;        } catch (IOException e) {            return false;        }    }    /**     * Create Xml config File     *      * @param path Config file path     */    private void create() {        Element root = new Element("ConfigList");        doc = new Document();        doc.setContent(root);        File f = new File(path);        // If config file exist, return.        if (f.exists()) {            return;        }        String filePath = f.getAbsolutePath();        if (filePath.lastIndexOf(File.separator) != -1) {            String _filePath = filePath.replace(filePath.substring(                    filePath.lastIndexOf(File.separator) + 1), "");            try {                f = new File(_filePath);                if (!f.exists()) {                    f.mkdirs();                }            } catch (Exception e) {            }        }        FileWriter writer;        XMLOutputter outputter = new XMLOutputter();        try {            writer = new FileWriter(filePath);            Format format = Format.getPrettyFormat();            format.setEncoding("UTF-8");            outputter.setFormat(format);            outputter.output(doc, writer);            writer.close();        } catch (IOException e) {        }    }}

⌨️ 快捷键说明

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