📄 xmlconfigurator.java
字号:
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.helpers.DefaultHandler;
import org.xml.sax.InputSource;
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;
//modified by me
private String strGlobalChar;
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
private 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 {
strGlobalChar += cs;
logCAT.debug(strGlobalChar);
}
}
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) {
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)) {
Object new_instance = null;
Class obj_class = null;
Constructor constructor = null;
try {
obj_class = Class.forName(attrs_class);
} catch (ClassNotFoundException cnfe) {
logCAT.error(
"endElement() - OBJECT Branch - Class.forName("
+ attrs_class
+ ") caught ClassNotFoundException");
}
if (argument_list.size() == 0) {
try {
new_instance = obj_class.newInstance();
} catch (InstantiationException ie) {
logCAT.error(
"endElement() - OBJECT Branch - obj_class.newInstance() caught InstantiationException");
} catch (IllegalAccessException iae) {
logCAT.error(
"endElement() - OBJECT Branch - obj_class.newInstance() caught IllegalAccessException");
}
} else {
int nargs = argclass_list.size();
Class[] ca = new Class[nargs];
try {
for (int i = 0; i < nargs; i++)
ca[i] = (Class) argclass_list.get(i);
constructor = obj_class.getConstructor(ca);
} catch (NoSuchMethodException nsme) {
logCAT.error(
"endElement() - OBJECT Branch - getConstructor(Class[]) caught NoSuchMethodFoundException");
}
try {
new_instance = constructor.newInstance(argument_list.toArray());
} catch (InstantiationException ie) {
logCAT.error(
"endElement() - OBJECT Branch - constructor.newInstance() caught InstantiationException");
} catch (IllegalAccessException iae) {
logCAT.error(
"endElement() - OBJECT Branch - constructor.newInstance() caught IllegalAccessException");
} catch (java.lang.reflect.InvocationTargetException ite) {
logCAT.error(
"endElement() - OBJECT Branch - constructor.newInstance() caught InvocationTargetException" + " uri:" + uri + " local:" + local + " raw:" + raw +" argumen :" + argument_list.toString()+ ite.getCause());
}
}
treemap.put(stack.peek(), new_instance);
stack.pop();
argument_list = null;
argclass_list = null;
context = N_CTX;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -