PageRenderTime 31ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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