/// <summary> /// /// </summary> /// <param name="rdr"></param> /// <param name="returnType"></param> /// <returns></returns> public XmlRpcResponse DeserializeResponse(XmlReader rdr, Type returnType) { try { IEnumerator <Node> iter = new XmlRpcParser().ParseResponse(rdr).GetEnumerator(); iter.MoveNext(); if (iter.Current is FaultNode) { var xmlRpcException = DeserializeFault(iter); throw xmlRpcException; } if (returnType == typeof(void) || !iter.MoveNext()) { return new XmlRpcResponse { retVal = null } } ; var valueNode = iter.Current as ValueNode; object retObj = MapValueNode(iter, returnType, new MappingStack("response"), MappingAction.Error); var response = new XmlRpcResponse { retVal = retObj }; return(response); } catch (XmlException ex) { throw new XmlRpcIllFormedXmlException("Response contains invalid XML", ex); } }
public XmlRpcResponse DeserializeResponse(XmlReader rdr, Type returnType) { try { IEnumerator<Node> enumerator = new XmlRpcParser().ParseResponse(rdr).GetEnumerator(); enumerator.MoveNext(); if (enumerator.Current is FaultNode) throw DeserializeFault(enumerator); if (returnType == typeof (void) || !enumerator.MoveNext()) { return new XmlRpcResponse { retVal = null }; } Node current = enumerator.Current; object obj = MapValueNode(enumerator, returnType, new MappingStack("response"), MappingAction.Error); return new XmlRpcResponse { retVal = obj }; } catch (XmlException ex) { throw new XmlRpcIllFormedXmlException("Response contains invalid XML", ex); } }
public void ParseResponse(string xml) { var rdr = XmlReader.Create(new StringReader(xml)); var iter = new XmlRpcParser().ParseResponse(rdr).GetEnumerator(); Assert.IsTrue(iter.MoveNext() && iter.Current is ResponseNode); Assert.IsTrue( iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "South Dakota"); Assert.IsFalse(iter.MoveNext()); }
/// <summary> /// /// </summary> /// <param name="rdr"></param> /// <param name="valType"></param> /// <param name="mappingStack"></param> /// <param name="mappingAction"></param> /// <returns></returns> public Object ParseValueElement( XmlReader rdr, Type valType, MappingStack mappingStack, MappingAction mappingAction) { var iter = new XmlRpcParser().ParseValue(rdr).GetEnumerator(); iter.MoveNext(); return(MapValueNode(iter, valType, mappingStack, mappingAction)); }
public XmlRpcResponse DeserializeResponse(XmlReader rdr, Type returnType) { try { IEnumerator<Node> iter = new XmlRpcParser().ParseResponse(rdr).GetEnumerator(); iter.MoveNext(); if (iter.Current is FaultNode) { var xmlRpcException = DeserializeFault(iter); throw xmlRpcException; } if (returnType == typeof(void) || !iter.MoveNext()) return new XmlRpcResponse { retVal = null }; var valueNode = iter.Current as ValueNode; object retObj = MapValueNode(iter, returnType, new MappingStack("response"), MappingAction.Error); var response = new XmlRpcResponse { retVal = retObj }; return response; } catch (XmlException ex) { throw new XmlRpcIllFormedXmlException("Response contains invalid XML", ex); } }
public XmlRpcRequest DeserializeRequest(XmlReader rdr, Type svcType) { try { XmlRpcRequest request = new XmlRpcRequest(); IEnumerator<Node> iter = new XmlRpcParser().ParseRequest(rdr).GetEnumerator(); iter.MoveNext(); string methodName = (iter.Current as MethodName).Name; request.method = methodName; request.mi = null; ParameterInfo[] pis = null; if (svcType != null) { // retrieve info for the method which handles this XML-RPC method XmlRpcServiceInfo svcInfo = XmlRpcServiceInfo.CreateServiceInfo(svcType); request.mi = svcInfo.GetMethodInfo(request.method); // if a service type has been specified and we cannot find the requested // method then we must throw an exception if (request.mi == null) { string msg = String.Format("unsupported method called: {0}", request.method); throw new XmlRpcUnsupportedMethodException(msg); } // method must be marked with XmlRpcMethod attribute Attribute attr = Attribute.GetCustomAttribute(request.mi, typeof(XmlRpcMethodAttribute)); if (attr == null) { throw new XmlRpcMethodAttributeException( "Method must be marked with the XmlRpcMethod attribute."); } pis = request.mi.GetParameters(); } bool gotParams = iter.MoveNext(); if (!gotParams) { if (svcType != null) { if (pis.Length == 0) { request.args = new object[0]; return request; } else { throw new XmlRpcInvalidParametersException( "Method takes parameters and params element is missing."); } } else { request.args = new object[0]; return request; } } int paramsPos = pis != null ? GetParamsPos(pis) : -1; Type paramsType = null; if (paramsPos != -1) paramsType = pis[paramsPos].ParameterType.GetElementType(); int minParamCount = pis == null ? int.MaxValue : (paramsPos == -1 ? pis.Length : paramsPos); MappingStack mappingStack = new MappingStack("request"); MappingAction mappingAction = MappingAction.Error; var objs = new List<object>(); var paramsObjs = new List<object>(); int paramCount = 0; while (iter.MoveNext()) { paramCount++; if (svcType != null && paramCount > minParamCount && paramsPos == -1) throw new XmlRpcInvalidParametersException( "Request contains too many param elements based on method signature."); if (paramCount <= minParamCount) { if (svcType != null) { mappingStack.Push(String.Format("parameter {0}", paramCount)); // TODO: why following commented out? // parseStack.Push(String.Format("parameter {0} mapped to type {1}", // i, pis[i].ParameterType.Name)); var obj = MapValueNode(iter, pis[paramCount - 1].ParameterType, mappingStack, mappingAction); objs.Add(obj); } else { mappingStack.Push(String.Format("parameter {0}", paramCount)); var obj = MapValueNode(iter, null, mappingStack, mappingAction); objs.Add(obj); } mappingStack.Pop(); } else { mappingStack.Push(String.Format("parameter {0}", paramCount + 1)); var paramsObj = MapValueNode(iter, paramsType, mappingStack, mappingAction); paramsObjs.Add(paramsObj); mappingStack.Pop(); } } if (svcType != null && paramCount < minParamCount) throw new XmlRpcInvalidParametersException( "Request contains too few param elements based on method signature."); if (paramsPos != -1) { Object[] args = new Object[1]; args[0] = paramCount - minParamCount; Array varargs = (Array)Activator.CreateInstance(pis[paramsPos].ParameterType, args); for (int i = 0; i < paramsObjs.Count; i++) varargs.SetValue(paramsObjs[i], i); objs.Add(varargs); } request.args = objs.ToArray(); return request; } catch (XmlException ex) { throw new XmlRpcIllFormedXmlException("Request contains invalid XML", ex); } }
public XmlRpcRequest DeserializeRequest(XmlReader rdr, Type svcType) { try { XmlRpcRequest request = new XmlRpcRequest(); IEnumerator <Node> iter = new XmlRpcParser().ParseRequest(rdr).GetEnumerator(); iter.MoveNext(); string methodName = (iter.Current as MethodName).Name; request.method = methodName; request.mi = null; ParameterInfo[] pis = null; if (svcType != null) { // retrieve info for the method which handles this XML-RPC method XmlRpcServiceInfo svcInfo = XmlRpcServiceInfo.CreateServiceInfo(svcType); request.mi = svcInfo.GetMethodInfo(request.method); // if a service type has been specified and we cannot find the requested // method then we must throw an exception if (request.mi == null) { string msg = String.Format("unsupported method called: {0}", request.method); throw new XmlRpcUnsupportedMethodException(msg); } // method must be marked with XmlRpcMethod attribute Attribute attr = Attribute.GetCustomAttribute(request.mi, typeof(XmlRpcMethodAttribute)); if (attr == null) { throw new XmlRpcMethodAttributeException( "Method must be marked with the XmlRpcMethod attribute."); } pis = request.mi.GetParameters(); } bool gotParams = iter.MoveNext(); if (!gotParams) { if (svcType != null) { if (pis.Length == 0) { request.args = new object[0]; return(request); } else { throw new XmlRpcInvalidParametersException( "Method takes parameters and params element is missing."); } } else { request.args = new object[0]; return(request); } } int paramsPos = pis != null?GetParamsPos(pis) : -1; Type paramsType = null; if (paramsPos != -1) { paramsType = pis[paramsPos].ParameterType.GetElementType(); } int minParamCount = pis == null ? int.MaxValue : (paramsPos == -1 ? pis.Length : paramsPos); MappingStack mappingStack = new MappingStack("request"); MappingAction mappingAction = MappingAction.Error; var objs = new List <object>(); var paramsObjs = new List <object>(); int paramCount = 0; while (iter.MoveNext()) { paramCount++; if (svcType != null && paramCount > minParamCount && paramsPos == -1) { throw new XmlRpcInvalidParametersException( "Request contains too many param elements based on method signature."); } if (paramCount <= minParamCount) { if (svcType != null) { mappingStack.Push(String.Format("parameter {0}", paramCount)); // TODO: why following commented out? // parseStack.Push(String.Format("parameter {0} mapped to type {1}", // i, pis[i].ParameterType.Name)); var obj = MapValueNode(iter, pis[paramCount - 1].ParameterType, mappingStack, mappingAction); objs.Add(obj); } else { mappingStack.Push(String.Format("parameter {0}", paramCount)); var obj = MapValueNode(iter, null, mappingStack, mappingAction); objs.Add(obj); } mappingStack.Pop(); } else { mappingStack.Push(String.Format("parameter {0}", paramCount + 1)); var paramsObj = MapValueNode(iter, paramsType, mappingStack, mappingAction); paramsObjs.Add(paramsObj); mappingStack.Pop(); } } if (svcType != null && paramCount < minParamCount) { throw new XmlRpcInvalidParametersException( "Request contains too few param elements based on method signature."); } if (paramsPos != -1) { Object[] args = new Object[1]; args[0] = paramCount - minParamCount; Array varargs = (Array)Activator.CreateInstance(pis[paramsPos].ParameterType, args); for (int i = 0; i < paramsObjs.Count; i++) { varargs.SetValue(paramsObjs[i], i); } objs.Add(varargs); } request.args = objs.ToArray(); return(request); } catch (XmlException ex) { throw new XmlRpcIllFormedXmlException("Request contains invalid XML", ex); } }
public void ParseRequest(string xml) { var rdr = XmlReader.Create(new StringReader(xml)); var iter = new XmlRpcParser().ParseRequest(rdr).GetEnumerator(); Assert.IsTrue(iter.MoveNext()); Assert.IsInstanceOf<MethodName>(iter.Current); Assert.AreEqual("examples.getValues", (iter.Current as MethodName).Name); Assert.IsTrue(iter.MoveNext() && iter.Current is ParamsNode); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "41"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "-12"); Assert.IsTrue(iter.MoveNext() && iter.Current is LongValue && (iter.Current as LongValue).Value == "123456789012"); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "Hello World"); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "Hello World"); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == " "); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == ""); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == ""); Assert.IsTrue(iter.MoveNext() && iter.Current is DoubleValue && (iter.Current as DoubleValue).Value == "-12.214"); Assert.IsTrue(iter.MoveNext() && iter.Current is DateTimeValue && (iter.Current as DateTimeValue).Value == "19980717T14:08:55"); Assert.IsTrue(iter.MoveNext() && iter.Current is Base64Value && (iter.Current as Base64Value).Value == "eW91IGNhbid0IHJlYWQgdGhpcyE="); Assert.IsTrue(iter.MoveNext() && iter.Current is StructValue); Assert.IsTrue(iter.MoveNext() && iter.Current is StructMember && (iter.Current as StructMember).Value == "lowerBound"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "18"); Assert.IsTrue(iter.MoveNext() && iter.Current is StructMember && (iter.Current as StructMember).Value == "upperBound"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "139"); Assert.IsTrue(iter.MoveNext()); Assert.IsTrue(iter.Current is EndStructValue); Assert.IsTrue(iter.MoveNext()); Assert.IsTrue(iter.Current is ArrayValue); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "12"); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "Egypt"); Assert.IsTrue(iter.MoveNext() && iter.Current is BooleanValue && (iter.Current as BooleanValue).Value == "0"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "-31"); Assert.IsTrue(iter.MoveNext()); Assert.IsTrue(iter.Current is EndArrayValue); Assert.IsFalse(iter.MoveNext()); }
public void ParseResponse(string xml) { var rdr = XmlReader.Create(new StringReader(xml)); var iter = new XmlRpcParser().ParseResponse(rdr).GetEnumerator(); Assert.IsTrue(iter.MoveNext() && iter.Current is ResponseNode); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "South Dakota"); Assert.IsFalse(iter.MoveNext()); }
public void ParseRequest(string xml) { var rdr = XmlReader.Create(new StringReader(xml)); var iter = new XmlRpcParser().ParseRequest(rdr).GetEnumerator(); Assert.IsTrue(iter.MoveNext()); Assert.IsInstanceOf <MethodName>(iter.Current); Assert.AreEqual("examples.getValues", (iter.Current as MethodName).Name); Assert.IsTrue(iter.MoveNext() && iter.Current is ParamsNode); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "41"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "-12"); Assert.IsTrue( iter.MoveNext() && iter.Current is LongValue && (iter.Current as LongValue).Value == "123456789012"); Assert.IsTrue( iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "Hello World"); Assert.IsTrue( iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "Hello World"); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == " "); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == string.Empty); Assert.IsTrue(iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == string.Empty); Assert.IsTrue( iter.MoveNext() && iter.Current is DoubleValue && (iter.Current as DoubleValue).Value == "-12.214"); Assert.IsTrue( iter.MoveNext() && iter.Current is DateTimeValue && (iter.Current as DateTimeValue).Value == "19980717T14:08:55"); Assert.IsTrue( iter.MoveNext() && iter.Current is Base64Value && (iter.Current as Base64Value).Value == "eW91IGNhbid0IHJlYWQgdGhpcyE="); Assert.IsTrue(iter.MoveNext() && iter.Current is StructValue); Assert.IsTrue( iter.MoveNext() && iter.Current is StructMember && (iter.Current as StructMember).Value == "lowerBound"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "18"); Assert.IsTrue( iter.MoveNext() && iter.Current is StructMember && (iter.Current as StructMember).Value == "upperBound"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "139"); Assert.IsTrue(iter.MoveNext()); Assert.IsTrue(iter.Current is EndStructValue); Assert.IsTrue(iter.MoveNext()); Assert.IsTrue(iter.Current is ArrayValue); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "12"); Assert.IsTrue( iter.MoveNext() && iter.Current is StringValue && (iter.Current as StringValue).Value == "Egypt"); Assert.IsTrue( iter.MoveNext() && iter.Current is BooleanValue && (iter.Current as BooleanValue).Value == "0"); Assert.IsTrue(iter.MoveNext() && iter.Current is IntValue && (iter.Current as IntValue).Value == "-31"); Assert.IsTrue(iter.MoveNext()); Assert.IsTrue(iter.Current is EndArrayValue); Assert.IsFalse(iter.MoveNext()); }