Package de.timefinder.core.io.xml

Source Code of de.timefinder.core.io.xml.XmlSettings

/*
* This file is part of the TimeFinder project.
*  Visit http://www.timefinder.de for more information.
*  Copyright (c) 2009 the original author or authors.
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*        http://www.apache.org/licenses/LICENSE-2.0
*
*  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 de.timefinder.core.io.xml;

import de.timefinder.core.io.text.ObjectParsing;
import de.timefinder.core.io.text.ObjectWriting;
import de.timefinder.core.io.text.Parsing;
import de.timefinder.core.io.text.Writing;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javolution.util.FastMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map;
import java.util.Map.Entry;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
* @author Peter Karich, peat hal ‘at’ users ‘dot’ sourceforge ‘dot’ net
*/
public class XmlSettings {

//    private static final String couldnotLoad = "Couldn't load object from file - ";
//    private static final String zipEntryName = "xml";
    private Log logger = LogFactory.getLog(getClass());
    private Map<String, Object> settingsMap;
    private ObjectParsing objParsing = new ObjectParsing();
    private ObjectWriting objWriting = new ObjectWriting();
    private File settingsFile;

    public XmlSettings() {
        settingsMap = new FastMap<String, Object>();
        settingsFile = new File("settings.xml");
    }

    public void setSettingsFile(File file) {
        this.settingsFile = file;
    }

    public File getSettingsFile() {
        return settingsFile;
    }

    public void save() throws IOException {
        File xmlFile = getSettingsFile();
        Document document = DocumentHelper.createDocument();
        Element settingsElement = document.addElement("settings");
        settingsElement.addAttribute("version", "4.1");

        for (Entry<String, Object> entry : settingsMap.entrySet()) {
            Class clazz = entry.getValue().getClass();
            Writing w = objWriting.getWriting(clazz);
            if (w == null)
                throw new UnsupportedOperationException("No writing implemented for class:" + clazz);

            settingsElement.addElement(entry.getKey()).
                    addAttribute("class", clazz.getName()).
                    setText(w.toString(entry.getValue()));
        }

        XMLWriter output = new XMLWriter(new FileOutputStream(xmlFile), new OutputFormat("  ", true));
        output.write(document);
        output.close();
    }

    public void load() throws IOException {
        File xmlFile = getSettingsFile();
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(new FileInputStream(xmlFile));

            List list = document.getRootElement().elements();
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Element element = (Element) iter.next();
                try {
                    Class clazz = Class.forName(element.attributeValue("class"));
                    Parsing p = objParsing.getParsing(clazz);
                    Object obj = p.parse(element.getText());
                    setObject(element.getName(), obj);
                } catch (Exception ex) {
                    throw new UnsupportedOperationException("Couldn't read property:"
                            + element.getName() + " from settings file:" + xmlFile, ex);
                }
            }
        } catch (FileNotFoundException ex) {
            logger.warn("File not found. use default settings");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    protected void setObject(String key, Object newValue) {
        settingsMap.put(key, newValue);
    }

    protected Object getObject(String key) {
        return settingsMap.get(key);
    }

    protected boolean removeKey(String key) {
        return settingsMap.remove(key) != null;
    }
}
TOP

Related Classes of de.timefinder.core.io.xml.XmlSettings

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.