| 1 |
package org.astrogrid.samp.xmlrpc.internal; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collections; |
| 5 |
import java.util.List; |
| 6 |
import org.w3c.dom.Document; |
| 7 |
import org.w3c.dom.Element; |
| 8 |
|
| 9 |
/** |
| 10 |
* Represents the content of an XML-RPC methodCall element. |
| 11 |
* |
| 12 |
* @author Mark Taylor |
| 13 |
* @since 11 Mar 2016 |
| 14 |
* @see <a href="http://www.xmlrpc.com/">XML-RPC</a> |
| 15 |
*/ |
| 16 |
public class XmlRpcCall { |
| 17 |
|
| 18 |
private final String methodName_; |
| 19 |
private final List params_; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor. |
| 23 |
* |
| 24 |
* @param methodName content of <code>methodName</code> element |
| 25 |
* @param params SAMP-friendly list of parameters as contained |
| 26 |
* in the <code>params</code> element |
| 27 |
*/ |
| 28 |
public XmlRpcCall( String methodName, List params ) { |
| 29 |
methodName_ = methodName; |
| 30 |
params_ = Collections.unmodifiableList( params ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns the method name. |
| 35 |
* |
| 36 |
* @return content of <code>methodName</code> element |
| 37 |
*/ |
| 38 |
public String getMethodName() { |
| 39 |
return methodName_; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns the parameter list. |
| 44 |
* |
| 45 |
* @return SAMP-friendly list of parameter values from |
| 46 |
* <code>params</code> element |
| 47 |
*/ |
| 48 |
public List getParams() { |
| 49 |
return params_; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructs an XmlRpcCall instance from a document with a |
| 54 |
* methodCall element at top level. |
| 55 |
* |
| 56 |
* @param callDoc node whose child is an XML-RPC |
| 57 |
* <code>methodCall</code> element |
| 58 |
* @return call instance |
| 59 |
* @throws XmlRpcFormatException if the document does not have the |
| 60 |
* expected form |
| 61 |
*/ |
| 62 |
public static XmlRpcCall createCall( Document callDoc ) |
| 63 |
throws XmlRpcFormatException { |
| 64 |
|
| 65 |
// Get expected top-level element. |
| 66 |
Element callEl = XmlUtils.getChild( callDoc, "methodCall" ); |
| 67 |
|
| 68 |
// Get methodName and params elements. |
| 69 |
String methodName = null; |
| 70 |
Element paramsEl = null; |
| 71 |
Element[] methodChildren = XmlUtils.getChildren( callEl ); |
| 72 |
for ( int i = 0; i < methodChildren.length; i++ ) { |
| 73 |
Element el = methodChildren[ i ]; |
| 74 |
String tagName = el.getTagName(); |
| 75 |
if ( tagName.equals( "methodName" ) ) { |
| 76 |
methodName = XmlUtils.getTextContent( el ); |
| 77 |
} |
| 78 |
else if ( tagName.equals( "params" ) ) { |
| 79 |
paramsEl = el; |
| 80 |
} |
| 81 |
} |
| 82 |
if ( methodName == null ) { |
| 83 |
throw new XmlRpcFormatException( "No methodName element" ); |
| 84 |
} |
| 85 |
|
| 86 |
// Extract parameter values as list. |
| 87 |
Element[] paramEls = paramsEl == null |
| 88 |
? new Element[ 0 ] |
| 89 |
: XmlUtils.getChildren( paramsEl ); |
| 90 |
int np = paramEls.length; |
| 91 |
List paramList = new ArrayList( np ); |
| 92 |
for ( int i = 0; i < np; i++ ) { |
| 93 |
Element paramEl = paramEls[ i ]; |
| 94 |
if ( ! "param".equals( paramEl.getTagName() ) ) { |
| 95 |
throw new XmlRpcFormatException( "Non-param child of params" ); |
| 96 |
} |
| 97 |
else { |
| 98 |
Element valueEl = XmlUtils.getChild( paramEl, "value" ); |
| 99 |
paramList.add( XmlUtils.parseSampValue( valueEl ) ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Construct and return call. |
| 104 |
return new XmlRpcCall( methodName, paramList ); |
| 105 |
} |
| 106 |
} |