[prev in list] [next in list] [prev in thread] [next in thread]
List: xmlrpc-user
Subject: Minimalist Object Dumper
From: scohen () javactivity ! org
Date: 2006-10-13 20:01:22
Message-ID: 20061013130122.981cacb32bc3402c908f2d9621aa4002.37e4a45764.wbe () email ! secureserver ! net
[Download RAW message or body]
Hello, this is the same Object Dumper I tried to submit through my corporate email \
which did not allow the submission to be sent.
["ObjectDumper.java" (TEXT/x-java)]
package org.apache.xmlrpc.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* This class creates a String representation of an object
* such as would be sent or received by an xml-rpc client or
* server.
*
* This version is very minimalist. The returned String provides special
* treatment for nulls, Maps, Collections, and arrays of Object.
* All other types are output via their toString() method. <p>
* The Maps are shown comma-delimited inside of curly brackets. <code>{}</code><p>
* The Collections are shown comma-delimited inside of parentheses. <code>()</code><p>
* The Object[]s are shown comma-delimited inside of square brackets. <code>[]</code><p>
* To use simply pass the object to the public constructor and call toString() on the result.
*/
/**
* @author sc1478
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ObjectDumper {
private StringBuffer buf;
/**
* Sole public constructor. Simply invoking
* will cause the internal buffer to be
* filled with the String representation of
* the supplied object.
* @param obj the object to be dumped.
*/
public ObjectDumper(Object obj) {
this(obj, null);
}
/**
* private constructor for recursive descent into
* maps, collections, and arrays.
* @param obj object to be dumped
* @param object parent dumper which shares its buffer.
*/
private ObjectDumper(Object obj, ObjectDumper parent) {
if (parent == null) {
this.buf = new StringBuffer();
} else {
this.buf = parent.buf;
}
appendObject(obj);
}
/**
* This simply returns the string constructed in the constructor.
* @return the String representation of the Object upon which
* this dumper was built.
*/
public String toString() {
return this.buf.toString();
}
/**
* appends a string representation of the supplied object and
* any subobject (through recursion) to this dumper's internal buffer
* @param obj Object to be dumped.
*/
public void appendObject(Object obj) {
if (obj == null) {
this.buf.append("null");
} else if (obj instanceof Map) {
Map m = (Map) obj;
this.buf.append("{");
Iterator it = m.keySet().iterator();
if (it.hasNext()) {
Object key = it.next();
new ObjectDumper(key, this);
this.buf.append("=");
new ObjectDumper(m.get(key), this);
while( it.hasNext()) {
this.buf.append(", ");
key = it.next();
new ObjectDumper(key, this);
this.buf.append("=");
new ObjectDumper(m.get(key), this);
}
}
this.buf.append("}");
} else if (obj instanceof Collection) {
Collection c = (Collection) obj;
this.buf.append("(");
Iterator it = c.iterator();
if (it.hasNext()) {
new ObjectDumper(it.next());
while(it.hasNext()) {
this.buf.append(", ");
new ObjectDumper(it.next(), this);
}
}
this.buf.append(")");
} else if (obj instanceof Object[]) {
Object[] a = (Object[]) obj;
this.buf.append("[");
int idx = 0;
if (idx < a.length) {
new ObjectDumper(a[idx++], this);
while(idx < a.length) {
this.buf.append(", ");
new ObjectDumper(a[idx++], this);
}
}
this.buf.append("]");
} else {
this.buf.append(obj.toString());
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xmlrpc-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: xmlrpc-dev-help@ws.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic