📄 configsafenode.java
字号:
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.
* @author fsn
*/
public class ConfigSafeNode
implements ConfigNode
{
private ConfigNode root;
private ConfigNode 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".
* <bR /> Note: This is an internal API, use the factory
* from external.
*/
protected ConfigSafeNode(ConfigNode cn, ConfigNode root)
{
this.root = root;
this.cn = cn;
}
/** Moved to a factory pattern to avoid unnecessary constructions */
public ConfigSafeNode createSafeNode(ConfigNode cn)
{
return createSafeNode(cn, cn);
}
/** Moved to a factory pattern to avoid unnecessary constructions */
public static ConfigSafeNode createSafeNode(ConfigNode cn, ConfigNode root)
{
if ( cn instanceof ConfigSafeNode
&& root == ((ConfigSafeNode)cn).getRoot() )
return (ConfigSafeNode) cn;
return new ConfigSafeNode(cn, root);
}
protected ConfigNode getRoot()
{
return root;
}
public ConfigNode node(String path)
{
return createSafeNode(cn.node(path), getRoot());
}
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 == getRoot() )
return null;
else
return createSafeNode(cn.getParent(), getRoot());
}
public boolean getBoolean()
{
return cn.getBoolean();
}
public int getInt()
{
return cn.getInt();
}
public double getDouble()
{
return cn.getDouble();
}
public String getUnexpanded()
{
return cn.getUnexpanded();
}
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(), getRoot());
}
public Iterator childrenNamed(String key)
{
return new SafeIterator(cn.childrenNamed(key), getRoot());
}
public int countChildrenNamed(String key)
{
return cn.countChildrenNamed(key);
}
public void printXML(PrintWriter os)
throws IOException
{
cn.printXML(os);
}
public String getSourceDescription()
{
return cn.getSourceDescription();
}
/**
* Wrapper class to return only safe ConfigNodes
*/
protected static class SafeIterator
implements Iterator
{
private Iterator it;
private 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 createSafeNode((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
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -