📄 xmlconfigurator.java.new
字号:
package org.osu.ogsa.stream.util.xmlconfig;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.TreeMap;
import java.util.Set;
import java.util.Iterator;
import java.util.Stack;
import java.util.ArrayList;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A sample SAX2 parser to read XML configuration files into
* a collection object (hashtable) in memory.
* Code derived from Xerces 1.4.1 sample code SAX2Count.
* main() using the companion Arguments class from Xerces.
*
*/
public class XMLConfigurator extends DefaultHandler {
//
// Constants
//
/** Default parser name. */
private static final String
DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
private static final String
APPLICATION = "Application",
TOPIC = "Topic",
PACKAGE = "Package",
CLASS = "Class",
PARAMETER = "Parameter",
OBJECT = "Object",
PROPERTY = "Property",
REFERENCE = "Reference",
CLASSMETHOD = "ClassMethod",
INSTANCEMETHOD = "InstanceMethod",
BOOLEAN = "Boolean",
BYTE = "Byte",
SHORT = "Short",
INTEGER = "Integer",
LONG = "Long",
FLOAT = "Float",
DOUBLE = "Double",
STRING = "String";
private static boolean setValidation = true;
private static boolean setNameSpaces = true;
private static boolean setSchemaSupport = true;
private static boolean setSchemaFullSupport = false;
private static TreeMap treemap;
private Stack stack;
private ArrayList argument_list, argclass_list;
private String attrs_class, attrs_reference, attrs_method;
private String data_element_typename;
private Object arg_object;
private Class arg_class;
private static final char
N_CTX = 0 , // Null parsing context
P_CTX = 'p', // Parameter parsing context
O_CTX = 'o', // Object parsing context
C_CTX = 'c', // ClassMethod parsing context
I_CTX = 'i'; // InstanceMethod parsing context
private char context = N_CTX;
private static boolean initialized=false; // to grard against multiple initialization attempts
// This is needed for logging with Log4j
static Log logCAT = LogFactory.getLog(XMLConfigurator.class.getName());
private static Class BOOLEAN_CLASS = boolean.class;
private static Class BYTE_CLASS = byte.class;
private static Class SHORT_CLASS = short.class;
private static Class INTEGER_CLASS = int.class;
private static Class LONG_CLASS = long.class;
private static Class FLOAT_CLASS = float.class;
private static Class DOUBLE_CLASS = double.class;
private static Class STRING_CLASS = null;
static {
try {
STRING_CLASS = Class.forName("java.lang.String");
} catch (ClassNotFoundException cnfe) {
logCAT.error("Caught ClassNotFoundException within static block");
}
}
private static String configLog4j= null;
private static String configFile = null;
// private static ResourceBundle bundle = null;
// private static java.text.MessageFormat formatter = null;
/* static {
String s;
if ((s=System.getProperty("log4j.configuration")) == null) {
logCAT.error("\"log4j.configuration\" null property value in System Properties");
} else {
configLog4j = s;
org.apache.log4j.xml.DOMConfigurator.configure(configLog4j);
}
if ((s=System.getProperty("xmlconfig.XMLConfigurator.MsgBundle")) == null) {
logCAT.error("\"XMLConfigurator.bundle\" null property value in System Properties");
} else {
bundle = ResourceBundle.getBundle(s);
formatter = new java.text.MessageFormat("");
}
// Initialize at class loading ONLY if config file is specified by System property setting
if ((s=System.getProperty("xmlconfig.XMLConfigurator.configuration")) != null) {
String[] sa = {s};
try { init(sa);
} catch (ConfigurationException e) {
logCAT.error("Configuration exception: "+e);
}
}
} */
public void initLog(String strLogConfigFile)
{
String s;
if ((s=System.getProperty("log4j.configuration")) != null) {
configLog4j = s;
} else {
configLog4j = strLogConfigFile;
}
org.apache.log4j.xml.DOMConfigurator.configure(configLog4j);
}
public static boolean booleanValue(String key) {
if (initialized) {
Object obj = treemap.get(key);
if (obj == null) {
logCAT.error("booleanValue() found a null value for key '" + key + "'");
System.exit(1);
return false;
} else {
try {
return ((Boolean) obj).booleanValue();
} catch (java.lang.ClassCastException e) {
logCAT.error("booleanValue() caught ClassCastException: " + e);
System.exit(1);
return false;
}
}
} else {
logCAT.error("booleanValue() called before initialization");
System.exit(1);
return false;
}
}
public static byte byteValue(String key) {
if (initialized) {
Object obj = treemap.get(key);
if (obj == null) {
logCAT.error("byteValue() found a null value for key '" + key + "'");
System.exit(1);
return 0;
} else {
try {
return ((Byte) obj).byteValue();
} catch (java.lang.ClassCastException e) {
logCAT.error("byteValue() caught ClassCastException: " + e);
System.exit(1);
return 0;
}
}
} else {
logCAT.error("byteValue() called before initialization");
System.exit(1);
return 0;
}
}
public void characters(char[] ch, int start, int length) {
String cs = new String(ch, start, length);
logCAT.debug("start:" + start + " length:"+length +" "+ cs);
if (data_element_typename == null) {
// logCAT.debug("characters() method found text outside of a data element type");
return;
} else {
if (data_element_typename.equals(STRING)) {
arg_object = cs;
arg_class = STRING_CLASS;
return;
}
if (data_element_typename.equals(BOOLEAN)) {
arg_object = new Boolean(cs);
arg_class = BOOLEAN_CLASS;
return;
}
if (data_element_typename.equals(INTEGER)) {
arg_object = new Integer(cs);
arg_class = INTEGER_CLASS;
return;
}
if (data_element_typename.equals(BYTE)) {
arg_object = new Byte(cs);
arg_class = BYTE_CLASS;
return;
}
if (data_element_typename.equals(SHORT)) {
arg_object = new Short(cs);
arg_class = SHORT_CLASS;
return;
}
if (data_element_typename.equals(LONG)) {
arg_object = new Long(cs);
arg_class = LONG_CLASS;
return;
}
if (data_element_typename.equals(FLOAT)) {
arg_object = new Float(cs);
arg_class = FLOAT_CLASS;
return;
}
if (data_element_typename.equals(DOUBLE)) {
arg_object = new Double(cs);
arg_class = DOUBLE_CLASS;
return;
}
if (data_element_typename.equals(REFERENCE)) {
// get object reference from the TreeMap
arg_object = treemap.get(cs);
if (arg_object == null)
logCAT.error(
"characters() - Null reference returned for REFERENCE (" + cs + ")");
arg_class = arg_object.getClass();
return;
}
}
return;
}
public static double doubleValue(String key) {
if (initialized) {
Object obj = treemap.get(key);
if (obj == null) {
logCAT.error("doubleValue() found a null value for key '" + key + "'");
System.exit(1);
return 0;
} else {
try {
return ((Double) obj).doubleValue();
} catch (java.lang.ClassCastException e) {
logCAT.error("doubleValue() caught ClassCastException: " + e);
System.exit(1);
return 0;
}
}
} else {
logCAT.error("doubleValue() called before initialization");
System.exit(1);
return 0;
}
}
public void endDocument() {
// pStack can be much bigger than treemap and it can now be garbage collected
stack = null;
}
public void endElement(String uri, String local, String raw) {
data_element_typename = null; // clear reference to current element name
if (local.equals(APPLICATION)
| local.equals(TOPIC)
| local.equals(PACKAGE)
| local.equals(CLASS)) {
stack.pop();
return;
}
if (local.equals(PARAMETER)) {
treemap.put(stack.peek(), arg_object);
stack.pop();
context = N_CTX;
arg_object = null;
return;
}
if (local.equals(OBJECT)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -