Author: gchapelle
Date: Wed Nov 16 17:55:52 2005
New Revision: 1844

Modified:
   azax/trunk/azax/azax.js
   azax/trunk/azaxresponse.py
Log:
changed commands format


Modified: azax/trunk/azax/azax.js
==============================================================================
--- azax/trunk/azax/azax.js     (original)
+++ azax/trunk/azax/azax.js     Wed Nov 16 17:55:52 2005
@@ -26,7 +26,7 @@
     if (nodes[i].rel=="azax")
     {
       href = nodes[i].href;
-      preprocessor = new AzaxPreProcessor();
+      preprocessor = new Azax.RuleProcessor();
       domdoc = preprocessor.loadRuleSheet(href);
       preprocessor.parseRuleDom(domdoc);
       preprocessor.setupEvents();
@@ -36,11 +36,11 @@
 
 
 // preprocessor 
-function AzaxPreProcessor() {
+Azax.RuleProcessor = function() {
   this.rules = new Array();
 }
 
-AzaxPreProcessor.prototype.loadRuleSheet = function(uri)
+Azax.RuleProcessor.prototype.loadRuleSheet = function(uri)
 {
   // Opera doesn't support getDomDocument.load, so we use XMLHttpRequest
   domDoc.open("GET", uri, false);
@@ -48,7 +48,7 @@
   return domDoc.responseXML;
 }
 
-AzaxPreProcessor.prototype.parseRuleDom = function(domDocument)
+Azax.RuleProcessor.prototype.parseRuleDom = function(domDocument)
 {
   var rules = domDocument.getElementsByTagName("rule");
 
@@ -89,7 +89,7 @@
 
         if (selector != "" && property_type != "" && name != "" && action != 
"")
         {
-            rule = new AzaxRule(selector, property_type, name, action);
+            rule = new Azax.Rule(selector, property_type, name, action);
             this.rules[this.rules.length] = rule;
         } else {
             if (selector == "")
@@ -106,7 +106,7 @@
   }
 }
 
-AzaxRule = function(selector, property_type, name, action)
+Azax.Rule = function(selector, property_type, name, action)
 {
   this.selector = selector;
   this.property_type = property_type;
@@ -115,7 +115,7 @@
 }
 
 
-AzaxPreProcessor.prototype.setupEvents = function()
+Azax.RuleProcessor.prototype.setupEvents = function()
 {
   var rules = this.rules
   for (var y=0;y < rules.length;y++)
@@ -125,7 +125,7 @@
   }
 }
 
-AzaxPreProcessor.prototype.setupEvent = function(rule)
+Azax.RuleProcessor.prototype.setupEvent = function(rule)
 {
   var nodes = cssQuery(rule.selector);
   for (var y=0; y < nodes.length; y++)
@@ -226,59 +226,92 @@
   {
     alert(Sarissa.getParseErrorText(domDoc));
   }
-  selectors = domDoc.getElementsByTagName("selector");
-  if (selectors.length == 0)
+  commands = domDoc.getElementsByTagName("command");
+  if (commands.length == 0)
   {
-    alert('Error : no selectors in azax response');
+    alert('Error : no commands in azax response');
   }
-  for (var y=0;y < selectors.length;y++)
+  var command_processor = new Azax.CommandProcessor();
+  command_processor.parseCommands(commands);
+  command_processor.executeCommands();
+}
+
+Azax.CommandProcessor = function() {
+  this.commands = new Array();
+}
+
+Azax.CommandProcessor.prototype.parseCommands = function(commands) {
+  for (var y=0;y < commands.length;y++)
   {
-    var selector = selectors[y];
-    var selector_value = "";
-    var childNodes = selector.childNodes;
+    var command = commands[y];
+    this.parseCommand(command);
+  }
+}
 
-    valueNodes = selector.getElementsByTagName("value");
-    if (valueNodes.length == 1)
-    {
-      selector_value = valueNodes[0].firstChild.nodeValue;
-    }
-    if (selector_value)
+Azax.CommandProcessor.prototype.parseCommand = function(command) {
+    var selector = "";
+    var params = new Array();
+    var name = "";
+    var has_params = false;
+
+    selector = command.getAttribute("selector");
+    if (selector == null)
+        selector = "";
+    if (selector)
     {
-      var commandsNodes = selector.getElementsByTagName("commands");
-      if (commandsNodes.length == 1)
-      {
-        var commands = commandsNodes[0].childNodes;
-      }
-    
-      for (var i=0;i < commands.length;i++)
-      {
-        var command_data = new Array();
-        var command_name = "";
-        var has_data = false;
-        command = commands[i];
-        nameNodes = command.getElementsByTagName("name");
-        if (nameNodes.length == 1)
-        {
-          command_name = nameNodes[0].firstChild.nodeValue;
-        }
+        name = command.getAttribute("name");
+        if (name == null)
+            name = "";
 
-        dataNodes = command.getElementsByTagName("data");
-        for (var n=0;n < dataNodes.length;n++)
+        var paramNodes = command.getElementsByTagName("param");
+        for (var n=0;n < paramNodes.length;n++)
         {
-          var data = dataNodes[n].getAttribute('name');
+          var data = paramNodes[n].getAttribute('name');
           if (data != null)
           { 
-            command_data[data] = dataNodes[n];
-            has_data = true;
+            params[data] = paramNodes[n];
+            has_params = true;
           }
         }
-        if (selector_value != "" && has_data && command_name != "")
+        if (selector != "" && has_params && name != "")
         {
-           Azax.executeCommand(selector_value, command_name, command_data);
+           var command = new Azax.Command(selector, name, params);
+           this.addCommand(command);
         }
-      }
     }
+} 
+
+Azax.CommandProcessor.prototype.addCommand = function(command) {
+  this.commands[this.commands.length] = command;
+}
+
+Azax.CommandProcessor.prototype.executeCommands = function()
+{
+  var commands = this.commands
+  for (var y=0;y < commands.length;y++)
+  {
+    var command = commands[y];
+    this.executeCommand(command);  
+  }
+}
+
+Azax.CommandProcessor.prototype.executeCommand = function(command)
+{
+  nodes = cssQuery(command.selector);
+  for (var i=0;i < nodes.length;i++)
+  {
+    var node = nodes[i];
+    var results = Azax.commandFunctions[command.name](node, command.params);
+    Azax.storeResults(results);
   }
+
+}
+
+Azax.Command = function(selector, name, params)
+{
+  this.selector = selector;
+  this.name = name;
+  this.params = params;
 }
 
 Azax.insertBefore = function(nodeFrom, parentNode, nodeTo) {
@@ -298,18 +331,6 @@
     return result;
 }
 
-Azax.executeCommand = function(selector_value, command_name, command_data)
-{
-  nodes = cssQuery(selector_value);
-  for (var i=0;i < nodes.length;i++)
-  {
-    var node = nodes[i];
-    var results = Azax.commandFunctions[command_name](node, command_data);
-    Azax.storeResults(results);
-  }
-
-}
-
 Azax.commandFunctions = new Array();
 Azax.commandFunctions['setHtmlAsChild']=function(node, command_data)
 {

Modified: azax/trunk/azaxresponse.py
==============================================================================
--- azax/trunk/azaxresponse.py  (original)
+++ azax/trunk/azaxresponse.py  Wed Nov 16 17:55:52 2005
@@ -25,34 +25,18 @@
 from interfaces import IAzaxResponse
 import config
 
-class AzaxSelector:
-    """ azax selector
-
-        it contains a set of commands related to the selector
-    """
-
-    def __init__(self, selectors_ob, selector):
-        etree = config.etree
-        self.ob = etree.SubElement(selectors_ob, 'selector')
-        self.value = etree.SubElement(self.ob, 'value')
-        self.commands = etree.SubElement(self.ob, 'commands')
-        self.value.text = selector
-
-    def addCommand(self, command):
-        return AzaxCommand(self.commands, command)
-
 class AzaxCommand:
-    def __init__(self, commands_ob, name):
+    def __init__(self, commands_ob, name, selector):
         etree = config.etree
         self.ob = etree.SubElement(commands_ob, 'command')
-        name_ob = etree.SubElement(self.ob, 'name')
-        name_ob.text = name
+        self.ob.set('name', name)
+        self.ob.set('selector', selector)
 
     def addData(self, name):
         etree = config.etree
-        data = etree.SubElement(self.ob, 'data')
-        data.set('name', name)
-        return data
+        param = etree.SubElement(self.ob, 'param')
+        param.set('name', name)
+        return param
 
 
 class AzaxResponse(object):
@@ -62,14 +46,16 @@
 
     def __init__(self, response=None):
         etree = config.etree
-        self._selectors = etree.Element('selectors')
-        self.selectors = {}
+        self._commands = etree.Element('commands')
         self._response = response
 
+    def addCommand(self, name, selector):
+        return AzaxCommand(self._commands, name, selector)
+        
     def __str__(self):
         """ renders the xml """
         etree = config.etree
-        return etree.tostring(self._selectors)
+        return etree.tostring(self._commands)
 
     def __call__(self):
         """ set response content type, if given """
@@ -77,59 +63,42 @@
             self._response.setHeader('Content-Type', 'text/xml')
         return str(self)
 
-    def _getSelector(self, selector):
-        """ return a selector, given its value tag """
-        if self.selectors.has_key(selector):
-            return self.selectors[selector]
-        else:
-            return self._createSelector(selector)
-
-    def _createSelector(self, selector):
-        """ creates a selector branch """
-        result = AzaxSelector(self._selectors, selector)
-        self.selectors[selector] = result
-        return result
-
+    def wrapInHtmlNamespace(self, new_value, data):
+        etree = config.etree
+        new_value = '<root>%s</root>' % new_value
+        valuetree = etree.fromstring(new_value)
+        data.text = valuetree.text
+        for elem in valuetree.getchildren():
+            elem.set('xmlns', 'http://www.w3.org/1999/xhtml')
+            data.append(elem)
+    
+    # commands
     def setHtmlAsChild(self, selector, new_value):
         """ see interfaces.py """
-        selector_ob = self._getSelector(selector)
-        command = selector_ob.addCommand('setHtmlAsChild')
+        command = self.addCommand('setHtmlAsChild', selector)
         data = command.addData('html')
         self.wrapInHtmlNamespace(new_value, data)        
     
     def addAfter(self, selector, new_value):
         """ see interfaces.py """
-        selector_ob = self._getSelector(selector)
-        command = selector_ob.addCommand('addAfter')
+        command = self.addCommand('addAfter', selector)
         data = command.addData('html')
         self.wrapInHtmlNamespace(new_value, data)        
 
-    def wrapInHtmlNamespace(self, new_value, data):
-        new_value = '<root>%s</root>' % new_value
-        etree = config.etree
-        valuetree = etree.fromstring(new_value)
-        data.text = valuetree.text
-        for elem in valuetree.getchildren():
-            elem.set('xmlns', 'http://www.w3.org/1999/xhtml')
-            data.append(elem)
-    
     def clearChildren(self, selector):
         """ see interfaces.py """
-        selector_ob = self._getSelector(selector)
-        command = selector_ob.addCommand('clearChildren')
+        command = self.addCommand('clearChildren', selector)
         data = command.addData('none')
 
     def copyChildrenFrom(self, selector, id):
         """ see interfaces.py """
-        selector_ob = self._getSelector(selector)
-        command = selector_ob.addCommand('copyChildrenFrom')
+        command = self.addCommand('copyChildrenFrom', selector)
         data = command.addData('html_id')
         data.text = id
 
     def copyChildrenTo(self, selector, id):
         """ see interfaces.py """
-        selector_ob = self._getSelector(selector)
-        command = selector_ob.addCommand('copyChildrenTo')
+        command = self.addCommand('copyChildrenTo', selector)
         data = command.addData('html_id')
         data.text = id
 
@@ -139,7 +108,6 @@
         self.clearChildren(selector)
 
     def executeCode(self, selector, code):
-        selector_ob = self._getSelector(selector)
-        command = selector_ob.addCommand('executeCode')
+        command = self.addCommand('executeCode', selector)
         data = command.addData('code')
         data.text = code 
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to