📄 config.java
字号:
package de.spieleck.config;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* Manager and Factory for Configurations.
* @author fsn
* @version ?
*/
public class Config
{
/** Registered listeners for change of data */
protected static List listeners;
/**
* Semi-Singleton: The default setup is an singleton while still
* many setup objects may coexist.
*/
protected static ConfigFileNode defaultConfig = null;
/** Cannot construct this from externally */
private Config() { }
/**
* Parse setup tree from an InputSource without {@link ConfigParamMap}
* @return a freshly parsed {@link ConfigFileNode}
*/
public static ConfigFileNode parse(InputSource is)
throws IOException, SAXException
{
return parse(is, NullParamMap.getInstance());
}
/**
* Parse setup tree from an InputSource with {@link ConfigParamMap}
* @return a freshly parsed {@link ConfigFileNode}
*/
public static ConfigFileNode parse(InputSource is, ConfigParamMap pm)
throws IOException, SAXException
{
ConfigFileNode top = new ConfigFileNode(null, null,
is.getSystemId(), pm);
ConfigBuilder builder = new ConfigBuilder(top, pm);
try
{
SAXParser parser = newSAXParser();
parser.parse(is, builder);
}
catch (Exception e)
{
e.printStackTrace();
throw new SAXException(e);
}
return top;
}
/**
* Obtain a new parser.
* @return a new SAXParser
*/
protected static synchronized SAXParser newSAXParser()
throws SAXException, ParserConfigurationException
{
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(false);
spf.setValidating(false);
return spf.newSAXParser();
}
/**
* Set the global visible (singleton) config node.
*/
public static synchronized ConfigNode setConfig(ConfigFileNode setup)
{
ConfigFileNode old = defaultConfig;
defaultConfig = setup;
handleChange(setup);
return old;
}
/**
* Get the global visible (singleton) config node.
* @return the global ConfigNode
*/
public static synchronized ConfigNode getConfig()
{
return defaultConfig;
}
/**
* We supply a preliminary api to inform listeners about a
* change in configuration.
*/
public synchronized static void addListener(ConfigListener listener)
{
if (listeners == null)
listeners = new LinkedList();
listeners.add(listener);
}
public synchronized static void removeListener(ConfigListener listener)
{
if (listeners != null)
listeners.remove(listener);
}
protected static void handleChange(ConfigNode s)
{
if (listeners == null)
return;
Iterator e = listeners.iterator();
while (e.hasNext())
{
ConfigListener listener = (ConfigListener)e.next();
listener.handleConfigChange(s);
}
}
}
//
// 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 + -