jtxmlhelper.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,383 行 · 第 1/3 页
JAVA
1,383 行
package Jt.xml;
import Jt.*;
import java.util.*;
import java.lang.reflect.*;
import java.beans.*;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
/**
* Convert objects from/to the XML format.
*/
public class JtXMLHelper extends JtObject implements ContentHandler {
private static final long serialVersionUID = 1L;
private transient XMLReader reader = null;
protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
protected static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
private transient Object currentObject = null; // current object
private String parserName = DEFAULT_PARSER_NAME;
private transient Stack contextStack = new Stack ();
private boolean excludeXMLHeader;
public static final String JtCLASS_NAME = JtXMLHelper.class.getName();
public JtXMLHelper() {
}
/**
* Specifies the parser.
* @param parserName parse
*/
void setParserName (String parserName) {
this.parserName = parserName;
}
/**
* Returns the parser.
*/
String getParserName () {
return (parserName);
}
/**
* Returns the excludeXMLHeader attribute
*/
public boolean getExcludeXMLHeader() {
return excludeXMLHeader;
}
/**
* Specifies if the XML header should be excluded from the output
*/
public void setExcludeXMLHeader(boolean excludeXMLHeader) {
this.excludeXMLHeader = excludeXMLHeader;
}
/**
* Start element (SAX API).
*/
public void startElement(String uri, String local, String raw,
Attributes attrs) throws SAXException {
JtXMLContext ctx;
HashMap map;
handleTrace ("Start element: " + raw, JtObject.JtMIN_LOG_LEVEL);
if ("java".equals (raw))
throw (new SAXException ("XMLEncoded"));
ctx = new JtXMLContext ();
ctx.setElement(raw);
contextStack.push(ctx);
if ("Object".equals (raw)) {
map = new HashMap ();
ctx.setProperties(map);
return;
}
}
Object setResources (Object obj) {
PropertyDescriptor[] prop;
int i;
BeanInfo info = null;
Object tval;
JtXMLContext ctx;
HashMap map;
ctx = (JtXMLContext) contextStack.peek ();
if (ctx == null)
return (null);
map = (HashMap) ctx.getProperties();
if (obj == null || map == null)
return (null);
// refactor this piece
try {
info = Introspector.getBeanInfo(
obj.getClass (), obj.getClass ().getSuperclass());
} catch(Exception e) {
handleException (e);
return (null); //check
}
prop = info.getPropertyDescriptors();
for(i = 0; i < prop.length; i++) {
tval = (Object) map.get (prop[i].getName());
//if (tval != null)
// handleTrace ("before setValue (class):" + tval.getClass().getName());
if (tval != null) // check
setValue (obj, prop[i].getName(), tval);
}
return (obj); // check
}
private boolean checkModifiers (Class cl, String prop) {
//Class cl;
Field field;
int mod;
if (cl == null || prop == null)
return (false);
field = null;
try {
field = cl.getDeclaredField (prop); // property dup property names
//System.out.println ("class:" + cl.getName ());
} catch (Exception e) {
//handleException (e);
if (cl.getSuperclass () == null) {
//handleException (e); check
return (false);
}
}
if (field == null) {
cl = cl.getSuperclass ();
return (checkModifiers (cl, prop));
}
mod = field.getModifiers ();
if (Modifier.isTransient (mod)) {
return (false);
}
if (Modifier.isStatic (mod)) {
return (false);
}
return (true);
}
private Hashtable getAttributes (Object obj) {
PropertyDescriptor[] prop;
int i;
Class cl;
Method m;
BeanInfo info = null;
Object value;
Hashtable attr;
String xmlFormat;
if (obj == null)
return (null);
attr = new Hashtable ();
try {
info = Introspector.getBeanInfo(
obj.getClass (), java.lang.Object.class);
} catch(Exception e) {
handleException (e);
return (null);
}
prop = info.getPropertyDescriptors();
for(i = 0; i < prop.length; i++) {
try {
cl = prop[i].getPropertyType();
if (!checkModifiers (obj.getClass (),prop[i].getName())) {
continue;
}
if (cl.isArray()) {
handleWarning ("JtXMLHelper.getAttributes: unable to convert arrays:" + prop[i].getName());
return (null);
}
m = prop[i].getReadMethod ();
if (m == null) {
handleError
("getAttributes: getReadMethod returned null");
return (null);
}
value = m.invoke (obj, null);
if (value == null) {
continue; // check
}
if (!(cl.isPrimitive() || value instanceof String)) {
//handleWarning ("JtXMLHelper.getAttributes: unable to convert non primitive type:" +
// prop[i].getName() + " " + cl.getName());
xmlFormat = convertToXML (value);
if (xmlFormat == null)
return (null);
attr.put (prop[i].getName(), xmlFormat);
continue;
}
if (value instanceof String) {
attr.put (prop[i].getName(), value);
//System.out.println ("=" + value);
continue;
}
if (value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Byte ||
value instanceof Boolean ||
value instanceof Short ||
value instanceof Double ||
value instanceof Character) {
attr.put (prop[i].getName(), value.toString () );
continue;
} else {
handleWarning
("JtXMLHelper.getAttributes: unable to convert attribute type to String:" + prop[i].getName());
return (null);
}
} catch (Exception e) {
handleException(e);
return (null);
}
}
return (attr);
}
private Object createInstance () {
String classname;
Class jtclass;
Object obj;
String value;
String stmp;
JtRemoteException jex;
JtXMLContext ctx;
HashMap map;
Exception ex;
ctx = (JtXMLContext) contextStack.peek();
map = (HashMap) ctx.getProperties();
if (map == null)
return (null); // check
handleTrace ("Creating instance:" + map.get ("classname"), JtObject.JtMIN_LOG_LEVEL);
classname = (String) map.get ("classname");
if (classname == null)
return (null); // check
try {
jtclass = Class.forName ((String) classname);
} catch (Exception e) {
handleException (e);
return (null);
}
/*
try {
obj = jtclass.newInstance ();
} catch (Exception e) {
handleException (e);
return (null);
}
*/
if (classname.equals ("Jt.JtRemoteException")) {
value = (String) map.get ("value");
stmp = (String) map.get ("trace");
//System.out.println ("stmp:" + stmp);
if (value == null) {
handleError ("createInstance: invalid value:" +
value);
return (null);
}
jex = new JtRemoteException (value);
jex.setTrace (stmp);
return (jex);
}
if (classname.equals ("Jt.JtException")) {
value = (String) map.get ("value");
//System.out.println ("stmp:" + stmp);
if (value == null) {
handleError ("createInstance: invalid value:" +
value);
return (null);
}
try {
obj = jtclass.newInstance ();
} catch (Exception e) {
handleException (e);
return (null);
}
ex = new JtException (value);
return (ex);
}
/*
if (classname.startsWith ("Jt.")) {
try {
obj = jtclass.newInstance ();
if (setResources (obj) == null)
return null;
return (obj); // check
} catch (Exception e) {
handleException (e);
return (null);
}
} else if (classname.startsWith ("java.lang")) {
value = (String) map.get ("value");
} else {
handleError ("createInstance: unable to handle class type:" +
classname);
return (null);
}
*/
if (classname.startsWith ("java.lang")) {
value = (String) map.get ("value");
} else {
try {
obj = jtclass.newInstance ();
if (setResources (obj) == null)
return null;
return (obj); // check
} catch (Exception e) {
handleException (e);
return (null);
}
}
if (value == null) {
handleError ("createInstance: invalid value:" +
value);
return (null);
}
if (classname.equals ("java.lang.Byte")) {
try {
obj = new Byte (value);
return (obj);
} catch (Exception e) {
handleException (e);
return (null);
}
}
if (classname.equals ("java.lang.Short")) {
try {
obj = new Short (value);
return (obj);
} catch (Exception e) {
handleException (e);
return (null);
}
}
if (classname.equals ("java.lang.Float")) {
try {
obj = new Float (value);
return (obj);
} catch (Exception e) {
handleException (e);
return (null);
}
}
if (classname.equals ("java.lang.Double")) {
try {
obj = new Double (value);
return (obj);
} catch (Exception e) {
handleException (e);
return (null);
}
}
if (classname.equals ("java.lang.Character")) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?