| 1 |
package org.astrogrid.samp.xmlrpc.apache; |
| 2 |
|
| 3 |
import java.util.Hashtable; |
| 4 |
import java.util.Iterator; |
| 5 |
import java.util.List; |
| 6 |
import java.util.Map; |
| 7 |
import java.util.Vector; |
| 8 |
|
| 9 |
/** |
| 10 |
* Provides utility methods to perform translations between the |
| 11 |
* data structurs used by the org.apache.xmlrpc classes and those used |
| 12 |
* by JSAMP. |
| 13 |
* |
| 14 |
* @author Mark Taylor |
| 15 |
* @since 22 Aug 2008 |
| 16 |
*/ |
| 17 |
class ApacheUtils { |
| 18 |
|
| 19 |
/** |
| 20 |
* Private constructor prevents instantiation. |
| 21 |
*/ |
| 22 |
private ApacheUtils() { |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Converts an object from JSAMP XML-RPC form to Apache XML-RPC form. |
| 27 |
* Basically, this means converting |
| 28 |
* {@link java.util.Map}s to {@link java.util.Hashtable}s and |
| 29 |
* {@link java.util.List}s to {@link java.util.Vector}s. |
| 30 |
* |
| 31 |
* @param obj XML-RPC data structure suitable for use within JSAMP |
| 32 |
* @return XML-RPC data structure suitable for use within Apache |
| 33 |
*/ |
| 34 |
public static Object toApache( Object obj ) { |
| 35 |
if ( obj instanceof List ) { |
| 36 |
Vector vec = new Vector(); |
| 37 |
for ( Iterator it = ((List) obj).iterator(); it.hasNext(); ) { |
| 38 |
vec.add( toApache( it.next() ) ); |
| 39 |
} |
| 40 |
return vec; |
| 41 |
} |
| 42 |
else if ( obj instanceof Map ) { |
| 43 |
Hashtable hash = new Hashtable(); |
| 44 |
for ( Iterator it = ((Map) obj).entrySet().iterator(); |
| 45 |
it.hasNext(); ) { |
| 46 |
Map.Entry entry = (Map.Entry) it.next(); |
| 47 |
hash.put( entry.getKey(), toApache( entry.getValue() ) ); |
| 48 |
} |
| 49 |
return hash; |
| 50 |
} |
| 51 |
else if ( obj instanceof String ) { |
| 52 |
return obj; |
| 53 |
} |
| 54 |
else { |
| 55 |
throw new IllegalArgumentException( |
| 56 |
"Non-SAMP object type " + ( obj == null |
| 57 |
? null |
| 58 |
: obj.getClass().getName() ) ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Converts an object from Apache XML-RPC form to JSAMP XML-RPC form. |
| 64 |
* Since Hashtable implements Map and Vector implements List, this is |
| 65 |
* a no-op. |
| 66 |
* |
| 67 |
* @param data XML-RPC data structure suitable for use within Apache |
| 68 |
* @return XML-RPC data structure suitable for use within JSAMP |
| 69 |
*/ |
| 70 |
public static Object fromApache( Object data ) { |
| 71 |
return data; |
| 72 |
} |
| 73 |
} |