/src/name/carter/mark/flex/exist/EXistXMLRPCClient.as

http://transcriptstudio4isha.googlecode.com/ · ActionScript · 88 lines · 78 code · 10 blank · 0 comment · 9 complexity · e17cdeb22349e5bb2f8b6ec2ce0c26fa MD5 · raw file

  1. package name.carter.mark.flex.exist
  2. {
  3. import flash.utils.ByteArray;
  4. import mx.utils.Base64Decoder;
  5. import mx.utils.Base64Encoder;
  6. import name.carter.mark.flex.util.Utils;
  7. import name.carter.mark.flex.util.XMLUtils;
  8. import name.carter.mark.flex.util.remote.xmlrpc.MethodCall;
  9. import name.carter.mark.flex.util.remote.xmlrpc.XMLRPCClient;
  10. public class EXistXMLRPCClient
  11. {
  12. private var xmlRpcClient:XMLRPCClient;
  13. public function EXistXMLRPCClient(xmlRpcClient:XMLRPCClient)
  14. {
  15. this.xmlRpcClient = xmlRpcClient;
  16. }
  17. public function retrieveCollection(collectionPath:String, successFunction:Function, failureFunction:Function):void {
  18. var methodCall:MethodCall = new MethodCall("getCollectionDesc");
  19. methodCall.addParamAsString(collectionPath);
  20. xmlRpcClient.call(methodCall.xml, successFunction, failureFunction);
  21. }
  22. public function retrieveXML(xmlPath:String, successFunction:Function, failureFunction:Function, ignoreWhitespace:Boolean = true):void {
  23. var methodCall:MethodCall = new MethodCall("getDocumentAsString");
  24. methodCall.addParamAsString(Utils.encodePath(xmlPath));
  25. methodCall.addParamAsStruct({indent: "yes", encoding: "UTF-8"});
  26. xmlRpcClient.call(methodCall.xml, function(response:Object):void {
  27. var xml:XML = XMLUtils.convertToXML(response, ignoreWhitespace);
  28. successFunction(xml);
  29. }, function(msg:String):void {
  30. failureFunction("XML file could not be retrieved because: " + msg);
  31. });
  32. }
  33. public function storeXML(xmlPath:String, xml:XML, successFunction:Function, failureFunction:Function):void {
  34. if (xmlPath == null || Utils.normalizeSpace(xmlPath).length == 0) {
  35. throw new Error("Passed blank xmlPath");
  36. }
  37. var methodCall:MethodCall = new MethodCall("parse");
  38. methodCall.addParamAsString(xml.toXMLString());
  39. methodCall.addParamAsString(Utils.encodePath(xmlPath));
  40. methodCall.addParamAsInt(1);
  41. xmlRpcClient.call(methodCall.xml, function(response:Object):void {
  42. successFunction();
  43. }, function (msg:String):void {
  44. failureFunction("XML file could not be stored because: " + msg);
  45. });
  46. }
  47. public function query(xQuery:String, args:Array, successFunc:Function, failureFunc:Function):void {
  48. var enc:Base64Encoder = new Base64Encoder();
  49. enc.encode(xQuery);
  50. var struct:Object = new Object();
  51. struct.indent = "yes";
  52. struct.encoding = "UTF-8";
  53. if (args != null) {
  54. struct.variables = new Object();
  55. for (var i:int = 0; i < args.length; i++) {
  56. var arg:Object = args[i];
  57. if (arg != null) {
  58. struct.variables["arg" + i] = arg.toString();
  59. }
  60. }
  61. }
  62. var methodCall:MethodCall = new MethodCall("query");
  63. methodCall.addParamAsBase64(enc.drain());
  64. methodCall.addParamAsInt(int.MAX_VALUE);
  65. methodCall.addParamAsInt(1);
  66. methodCall.addParamAsStruct(struct);
  67. xmlRpcClient.call(methodCall.xml, function(response:Object):void {
  68. var dec:Base64Decoder = new Base64Decoder();
  69. dec.decode(response.toString());
  70. var ba:ByteArray = dec.toByteArray();
  71. var xml:XML = XMLUtils.convertToXML(ba, false);
  72. successFunc(xml);
  73. }, function (msg:String):void {
  74. failureFunc("XQuery could not be executed because: " + msg);
  75. });
  76. }
  77. }
  78. }