view src/MPipeline/src/mpipeline/Plist.java @ 52:daf4eab1a51e database-connection-manager tip

Fix. Default password should be [] not an empty string
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 07 Dec 2011 17:29:47 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * A class to capture a java representation of a plist - a list of parameter 
 * objects.
 * 
 * @author hewitson
 */
public class Plist implements Serializable {

    MBlock owner = null;
    ArrayList<Param> params = new ArrayList<Param>();
    String name = "";

    /**
     * Construct an empty plist.
     */
    public Plist() {
    }

    /**
     * Construct an empty plist with the given name.
     * @param n
     */
    public Plist(String n) {
        name = n;
    }
    
    /**
     * Construct a plist from the node of an XML DOM.
     * @param pn
     */
    public Plist(Node pn){
        
        // get name attribute
        NamedNodeMap nm = pn.getAttributes();
        // get mcategory
        Node nameNode = nm.getNamedItem("name");
        name = nameNode.getNodeValue();
        
        // read child nodes
        NodeList cns = pn.getChildNodes();
        for(int jj=0; jj<cns.getLength(); jj++){
            Node cn = cns.item(jj);
            if (cn.getNodeName().equals("param")){
                Param p = new Param(cn);
                params.add(p);
            }
        }
    }

    /**
     * Get the block that owns this plist.
     * @return
     */
    public MBlock getOwner() {
        return owner;
    }

    /**
     * Set the block which owns this plist.
     * @param owner
     */
    public void setOwner(MBlock owner) {
        this.owner = owner;
    }

        
    /**
     * Set the name of this plist.
     * @param n
     */
    public void setName(String n) {
        name = n;
    }

    /**
     * Get the name of this plist.
     * @return
     */
    public String getName() {
        return name;
    }
    
    /**
     * Convert this plist to a string.
     * @return
     */
    @Override
    public String toString() {
        String out = "<html>-------------------<br>" + name + "<br>";
        Iterator it = params.iterator();
        while (it.hasNext()) {
            Param p = (Param) it.next();
            out += "  ";
            out += p.toString();
            out += "<br>";
        }
        out += "-------------------</html>";

        return out;
    }

    /**
     * Add a parameter to the parameter list.
     * 
     * @param p
     */
    public void addParam(Param p) {
        // check against all others
        String newKey = checkParamKey(p.getKey());
        p.setKey(newKey);
        params.add(p);
    }

    /**
     * Change the key of the parameter with the given key.
     * @param key
     * @param value
     */
    public void setKeyForKey(String key, Object value) {
        Iterator it = params.iterator();
        while (it.hasNext()) {
            Param p = (Param) it.next();
            if (p.getKey().equals(key)) {
                p.setKey((String) value);
            }
        }
    }

    /**
     * Get a list of the param objects in the list.
     * @return
     */
    public ArrayList<Param> getParams() {
        return params;
    }

    /**
     * Set the params list.
     * @param params
     */
    public void setParams(ArrayList<Param> params) {
        this.params = params;
    }

    /**
     * Attach this plist to an XML DOM.
     * @param doc
     * @param node
     */
    public void attachToDom(Document doc, Element node) {

        // start new document node
        Element docnode = doc.createElement("plist");
        docnode.setAttribute("name", name);

        // attach each parameter
        Iterator it = params.iterator();
        while(it.hasNext()){
            Param p = (Param)it.next();
            p.attachToDom(doc, docnode);
        }

        // attach to the DOM
        node.appendChild(docnode);
    }

    /**
     * Check a parameter key and return a valid key.
     * @param s
     * @return
     */
    private String checkParamKey(String s) {

        Iterator it = params.iterator();
        while (it.hasNext()) {
            Param pt = (Param) it.next();
            if (pt.getKey().equals(s)) {
                s += "_1";
            }
        }

        return s;
    }

    /**
     * Display this plist to standard output.
     */
    public void display() {
        String header = "-------" + getName() + "--------";
        String footer = "-";
        while (footer.length() < header.length()) {
            footer += "-";
        }
        System.out.println(header);
        Iterator it = params.iterator();
        while (it.hasNext()) {
            Param p = (Param) it.next();
            p.display();
        }
        System.out.println(footer);
    }

    /**
     * Add a parameter to this plist.
     * @param key
     * @param val
     * @param type 
     */
    public void addParam(String key, Object val, String type) {
        key = checkParamKey(key);
        Param p = new Param(key, val, type);
        params.add(p);
    }

    /**
     * Return the value of the parameter with the given key.
     * 
     * @param key
     * @return
     */
    public Object findVal(String key) {
        Iterator it = params.iterator();
        while (it.hasNext()) {
            Param p = (Param) it.next();
            if (p.getKey().equals(key)) {
                return p.getVal();
            }
        }
        return null;
    }

    /**
     * Set the value of the parameter with the given key.
     * @param key
     * @param val
     */
    public void setValueForKey(String key, Object val) {
        Iterator it = params.iterator();
        while (it.hasNext()) {
            Param p = (Param) it.next();
            if (p.getKey().equals(key)) {
                p.setVal(val);
            }
        }
    }

    /**
     * Return the ith parameter.
     * @param i
     * @return
     */
    public Param getParam(int i) {
        return params.get(i);
    }

    /**
     * Return the key of the parameter at index idx.
     * @param idx
     * @return 
     */
    public String getKey(int idx) {
        return params.get(idx).getKey();
    }

    /**
     * Return the value of the parameter at index idx.
     * @param idx
     * @return
     */
    public Object getVal(int idx) {
        return params.get(idx).getVal();
    }

    /**
     * Return the number of parameters in this parameter list.
     * @return
     */
    public int getNparams() {
        return params.size();
    }

    /**
     * Remove the parameter with the given key.
     * @param pm
     */
    public void removeParam(String pm) {
        Param prmv = null;
        Iterator it = params.iterator();
        while (it.hasNext()) {
            Param p = (Param) it.next();
            if (p.getKey().equals(pm)) {
                prmv = p;
                break;
            }
        }
        params.remove(prmv);
    }
}