com.magicpwd._util.Jxml.java Source code

Java tutorial

Introduction

Here is the source code for com.magicpwd._util.Jxml.java

Source

/*
 *  Copyright (C) 2010 Amon
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.magicpwd._util;

import com.magicpwd._comn.mpwd.MpwdHeader;
import com.magicpwd._cons.ConsEnv;
import com.magicpwd._cons.LangRes;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author Amon
 */
public class Jxml {

    public static Document create() {
        Document doc = DocumentHelper.createDocument();

        // ProcessingInstruction
        // Map<String, String> pi = new HashMap<String, String>();
        // pi.put("type", "text/xsl");
        // pi.put("href", "http://amon.me/mpwd/MagicPwd.xsl");
        // document.addProcessingInstruction("xml-stylesheet", pi);

        return doc;
    }

    public static void save(String file, MpwdHeader keys, List pwds) {
        Document doc = create();

        // root element
        Element root = doc.addElement("MagicPwd").addAttribute("site", ConsEnv.HOMEPAGE);

        Element node = root.addElement("Key").addAttribute("type", "pwds");
        node.addElement(Lang.getLang(LangRes.P30F1C01, "status"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C02, "?"))
                .setText(keys.getP30F0102() + "");
        node.addElement(Lang.getLang(LangRes.P30F1C03, "catalog"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C04, "")).setText(keys.getP30F0106());
        node.addElement(Lang.getLang(LangRes.P30F1C04, "create"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C06, ""))
                .setText(keys.getP30F0107().toString());
        node.addElement(Lang.getLang(LangRes.P30F1C07, "title"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C08, "?")).setText(keys.getP30F0109());
        node.addElement(Lang.getLang(LangRes.P30F1C09, "meta"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C0A, "?")).setText(keys.getP30F010A());
        node.addElement(Lang.getLang(LangRes.P30F1C0B, "logo"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C0C, "?")).setText(keys.getP30F010B());
        node.addElement(Lang.getLang(LangRes.P30F1C0D, "limit"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C0E, ""))
                .setText(keys.getP30F010E().toString());
        node.addElement(Lang.getLang(LangRes.P30F1C0F, "hint"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C10, "??")).setText(keys.getP30F010F());
        node.addElement(Lang.getLang(LangRes.P30F1C11, "memo"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C12, "")).setText(keys.getP30F0110());
    }

    public static void save(String file, MpwdHeader keys) {
        Document doc = create();

        // root element
        Element root = doc.addElement("MagicPwd").addAttribute("site", ConsEnv.HOMEPAGE);

        Element node = root.addElement("Key").addAttribute("type", "note");
        node.addElement(Lang.getLang(LangRes.P30F1C03, "catalog"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C04, "")).setText(keys.getP30F0106());
        node.addElement(Lang.getLang(LangRes.P30F1C07, "title"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C08, "?")).setText(keys.getP30F0109());
        node.addElement(Lang.getLang(LangRes.P30F1C09, "meta"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C0A, "?")).setText(keys.getP30F010A());
        node.addElement(Lang.getLang(LangRes.P30F1C0B, "logo"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C0C, "?")).setText(keys.getP30F010B());
        node.addElement(Lang.getLang(LangRes.P30F1C11, "memo"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C12, "")).setText(keys.getP30F0110());
        node.addElement(Lang.getLang(LangRes.P30F1C11, "content"))
                .addAttribute("name", Lang.getLang(LangRes.P30F1C12, ""))
                .setText(keys.getMpwd().getP30F0203().toString());
    }

    public static void save(Document document, File file) throws IOException {
        // ?
        OutputFormat format = OutputFormat.createPrettyPrint();
        // ??
        // OutputFormat format = OutputFormat.createCompactFormat();
        // XML?
        // format.setEncoding("GBK");
        XMLWriter writer = new XMLWriter(new FileWriter(file), format);
        writer.write(document);
        writer.close();
    }

    public static Document load(File file) throws DocumentException {
        Document doc = new SAXReader().read(file);
        //doc.selectNodes("//students/student/@sn");
        return doc;
    }
}