package de.spieleck.config;

import java.io.PrintWriter;
import java.io.IOException;
import java.util.Iterator;


/**
 * An implementation of config node, that does not allow
 * the client to proceed upwards in the configuration tree.
 * XXX This is not well thought out, just thinking in code!
 * This is not at all memory efficient, since the Safe nodes
 * can actually multiply the original tree.
 */
public class ConfigSafeNode
    implements ConfigNode
{
    protected ConfigNode root;

    protected ConfigNode cn;

    /**
     * Wrap this node in a safe one.
     */
    public ConfigSafeNode(ConfigNode cn)
    {
        this(cn, cn);
    }

    /**
     * Wrap a node into a safe one and prescibe a root.
     * The root actually marks the branch below which
     * we want to travel...
     * Note that root should be a parent of cn, otherwise
     * the created node is not really "safe".
     */
    public ConfigSafeNode(ConfigNode cn, ConfigNode root)
    {
        this.root = root;
        this.cn = cn;
    }

    public ConfigNode node(String path)
    {
        return new ConfigSafeNode(cn.node(path), root);
    }

    public ConfigNode nodeInh(String path)
    {
        return cn.node(path);
    }

    public ConfigFileNode getBranchNode()
    {
        // information hiding caveat!
        return null;
    }

    public String getName()
    {
        return cn.getName();
    }

    public String getPath()
    {
        // information hiding caveat!
        return cn.getName();
    }

    public ConfigNode getParent()
    {
        if ( cn == root )
            return null;
        else 
            return new ConfigSafeNode(cn.getParent(), root);
    }

    public boolean getBoolean()
    {
        return cn.getBoolean();
    }

    public int getInt()
    {
        return cn.getInt();
    }

    public double getDouble()
    {
        return cn.getDouble();
    }

    public String getString()
    {
        return cn.getString();
    }

    public boolean getBoolean(String path, boolean deflt)
    {
        return cn.getBoolean(path, deflt);
    }

    public int getInt(String path, int deflt)
    {
        return cn.getInt(path, deflt);
    }

    public double getDouble(String path, double deflt)
    {
        return cn.getDouble(path, deflt);
    }

    public String getString(String path, String deflt)
    {
        return cn.getString(path, deflt);
    }

    public boolean getInhBoolean(String path, boolean deflt)
    {
        return getBoolean(path, deflt);
    }

    public int getInhInt(String path, int deflt)
    {
        return getInt(path, deflt);
    }

    public double getInhDouble(String path, double deflt)
    {
        return getDouble(path, deflt);
    }

    public String getInhString(String path, String deflt)
    {
        return getString(path, deflt);
    }

    public int countChildren()
    {
        return cn.countChildren();
    }

    public Iterator children()
    {
        return new SafeIterator(cn.children(), root);
    }

    public Iterator childrenNamed(String key)
    {
        return new SafeIterator(cn.childrenNamed(key), root);
    }

    public int countChildrenNamed(String key)
    {
        return cn.countChildrenNamed(key);
    }

    public void print(PrintWriter os)
        throws IOException
    {
        cn.print(os);
    }

    /**
     * Wrapper class to return only safe ConfigNodes
     */
    protected static class SafeIterator
        implements Iterator
    {
        Iterator it;
        ConfigNode root;

        public SafeIterator(Iterator it, ConfigNode root)
        {
            this.it = it;
            this.root = root;
        }

        public boolean hasNext()
        {
            return it.hasNext();
        }

        public void remove()
        {
            it.remove();
        }

        public Object next()
        {
            return new ConfigSafeNode((ConfigNode)it.next(), root);
        }
    }
}
//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library 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
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//