/************************************************************************
 *
 *  PropertySet.java
 *
 *  Copyright: 2002-2014 by Henrik Just
 *
 *  This file is part of Writer2LaTeX.
 *  
 *  Writer2LaTeX 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.
 *  
 *  Writer2LaTeX 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 Writer2LaTeX.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Version 1.4 (2014-08-27)
 *
 */

package writer2latex.office;

import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import java.util.Enumeration;
import java.util.Hashtable;

/** <p> Class representing a set of style properties in OOo (actually this
    is simply the set of attributes of an element). </p> 
  */
public class PropertySet implements Cloneable {
    private Hashtable<String, String> properties = new Hashtable<String, String>();
    private String sName;

    public PropertySet() {
        properties = new Hashtable<String, String>();
        sName="";
    }
    public PropertySet(String name, Hashtable<String,String> properties ) {
      this.properties = (Hashtable<String, String>) properties.clone();
      sName = new String(name);
    }
	
    public String getProperty(String sPropName) {
        if (sPropName!=null) {
            String sValue = properties.get(sPropName);
            if (sValue!=null && sValue.endsWith("inch")) {
                // Cut of inch to in
                return sValue.substring(0,sValue.length()-2);
            }
            else {
                return sValue;
            }
        }
        else {
            return null;
        }
    }
    
    public PropertySet Clone() {
    	return new PropertySet(sName, properties);
    }
	
    public String getName() { return sName; }

    public void loadFromDOM(Node node) {
        // read the attributes of the node, if any
        if (node!=null) {
            sName = node.getNodeName();
            NamedNodeMap attrNodes = node.getAttributes();
            if (attrNodes!=null) {    
                int nLen = attrNodes.getLength();
                for (int i=0; i<nLen; i++){
                    Node attr = attrNodes.item(i);
                    properties.put(attr.getNodeName(),attr.getNodeValue());
                //    System.out.println(attr.getNodeName()+"  " + attr.getNodeValue());
                }
            }
        }
    }
	
    public boolean containsProperty(String sProperty) {
        return sProperty!=null && properties.containsKey(sProperty);
    }
	
    public void setProperty(String sProperty, String sValue){
        properties.put(sProperty,sValue);
    }
	
    public String toString() {
        String s="";
        Enumeration<String> keys = properties.keys();
        while (keys.hasMoreElements()) {
            String sKey = keys.nextElement();
            String sValue = properties.get(sKey);
            s += sKey+"="+sValue+" ";
        }
        return s;
    }

}