Proste serializowanie obiektu do XML-a
private static StringBuffer describeNodeMark(StringBuffer sb, Object o, String name, int level, boolean end) {for (int i = 0; i < level-1; i++)
sb.append("\t");
sb.append(end ? "</" : "<").append( name ).append(">\r\n");
return sb;
}
private static StringBuffer describeNode(StringBuffer sb, Object o, String name, int level) throws Exception {
if (o==null) {
for (int i = 0; i < level; i++)
sb.append("\t");
sb.append("<").append( name ).append("/>\r\n");
return sb;
}
else if (o.getClass().getName().startsWith("[B")) {
sb.append("<byte[]>").append( new String( (byte[]) o) ).append("</byte[]>");
return sb;
}
else if (o.getClass().getName().startsWith("[C")) {
char[] cs = (char[])o;
for (int i=0; i<cs.length; i++) {
Character c = (Character)cs[i];
sb.append(c);
//toXmlString(sb, c, level);
}
return sb;
}
else {
for (int i = 0; i < level; i++)
sb.append("\t");
sb.append("<").append(name).append(">");
sb.append(o);
sb.append("</").append(name).append(">\r\n");
return sb;
}
}
@SuppressWarnings("unchecked")
public final static StringBuffer toXmlString(StringBuffer sb, Object o, int level) throws Exception {
if (sb==null)
sb = new StringBuffer();
level++;
if (o!=null) {
String nm = o.getClass().getName();
if (nm.contains("org.apache.axis") || nm.endsWith(";")) // skip axis TypeDef and list terminator
return sb;
}
File f = new File( o.getClass().getName().replaceAll("\\.", "/") );
if (o==null) {
return describeNode(sb, o, f.getName(), level);
}
if ((o.getClass().getName().startsWith("java.lang") || // basic Java types
o.getClass().getName().startsWith("java.math") ||
o.getClass().getName().startsWith("[B") ||
o.getClass().getName().startsWith("[C")) &&
( !o.getClass().getName().startsWith("java.lang.String")) &&
!o.getClass().getName().startsWith("java.lang.Character")
)
{
return describeNode(sb, o, f.getName(), level);
}
/////////////////////
describeNodeMark(sb, o, f.getName(), level, false);
for (Field fld : o.getClass().getDeclaredFields()) {
fld.setAccessible(true);
String name = fld.getName();
Object obj = fld.get(o);
if (!name.startsWith("__")) { // skip axis fields
if (obj==null) {
describeNode(sb, obj, name, level);
}
else {
String cname = obj.getClass().getName();
if (cname.startsWith("[L")) {
Object[] objs = (Object[]) obj;
for (Object oo : objs) {
if (oo!=null)
sb.append( toXmlString(null, oo, level) );
}
}
if (cname.startsWith("java.lang.Class"))
return sb;
if (cname.startsWith("java.lang") || cname.startsWith("java.math")) {
describeNode(sb, obj, name, level);
}
else {
if (cname.startsWith("java.util.LinkedList$Entry")) {
List list = (List)o;
for (int i = 0; i < list.size(); i++)
sb.append( toXmlString(null, list.get(i), level-1 ) );
}
else if (cname.startsWith("java.util.TreeMap$Entry")){
Map map = (TreeMap) o;
for (Object k : map.keySet()) {
sb.append( toXmlString(null, k, level-1 ) );
}
}
else {
sb.append( toXmlString(null, obj, level) );
}
}
}
}
}
for (int i = 0; i < level-1; i++)
sb.append("\t");
sb.append("</").append( f.getName() ).append(">\r\n");
return sb;
}
0 komentarze:
Prześlij komentarz