Package it.freedomotic.persistence

Source Code of it.freedomotic.persistence.PropertiesConverter

/**
*
* Copyright (c) 2009-2013 Freedomotic team
* http://freedomotic.com
*
* This file is part of Freedomotic
*
* 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 2, 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 Freedomotic; see the file COPYING.  If not, see
* <http://www.gnu.org/licenses/>.
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package it.freedomotic.persistence;

import it.freedomotic.model.object.Properties;

import java.util.HashMap;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import java.util.logging.Logger;

/**
*
* @author gpt
*/
public class PropertiesConverter
        implements Converter {

    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
        Properties rea = (Properties) o;

        for (String name : rea.stringPropertyNames()) {
            writer.startNode("property");
            writer.addAttribute("name", name);
            writer.addAttribute("value",
                    rea.getProperty(name));
            writer.endNode();
        }
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
        HashMap<String, String> propertiesHashMap = new HashMap<String, String>();

        while (reader.hasMoreChildren()) {
            reader.moveDown();
            propertiesHashMap.put(reader.getAttribute("name"),
                    reader.getAttribute("value"));
            reader.moveUp();
        }

        return new Properties(propertiesHashMap);
    }

    @Override
    public boolean canConvert(Class type) {
        if (type == Properties.class) {
            return true;
        }

        return false;
    }
    private static final Logger LOG = Logger.getLogger(PropertiesConverter.class.getName());
}
TOP

Related Classes of it.freedomotic.persistence.PropertiesConverter

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.