]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
its: Minor refactoring
authorDaiki Ueno <ueno@gnu.org>
Mon, 28 Sep 2015 10:27:32 +0000 (19:27 +0900)
committerDaiki Ueno <ueno@gnu.org>
Mon, 28 Sep 2015 10:27:32 +0000 (19:27 +0900)
gettext-tools/src/its.c

index 682f684e74d51e7ab3a214aad2cbb50af24eb7d2..fc2fa01c2198b499c3d80aa69403097326c37a7f 100644 (file)
@@ -634,22 +634,6 @@ _its_pool_get_value_for_node (struct its_pool_ty *pool, xmlNode *node,
   return NULL;
 }
 
-static void
-_its_pool_set_value_for_node (struct its_pool_ty *pool, xmlNode *node,
-                              const char *name, const char *value)
-{
-  intptr_t index = (intptr_t) node->_private;
-  if (index > 0)
-    {
-      struct its_value_list_ty *values;
-
-      assert (index <= pool->nitems);
-      values = &pool->items[index - 1];
-
-      its_value_list_set_value (values, name, value);
-    }
-}
-
 struct its_value_list_ty *
 its_translate_rule_eval (struct its_rule_ty *pop, struct its_pool_ty *pool,
                          xmlNode *node)
@@ -658,17 +642,6 @@ its_translate_rule_eval (struct its_rule_ty *pop, struct its_pool_ty *pool,
 
   result = XCALLOC (1, struct its_value_list_ty);
 
-  /* A local attribute overrides the global rule.  */
-  if (xmlHasNsProp (node, BAD_CAST "translate", BAD_CAST ITS_NS))
-    {
-      char *prop;
-
-      prop = _its_get_attribute (node, "translate", ITS_NS);
-      its_value_list_append (result, "translate", prop);
-      free (prop);
-      return result;
-    }
-
   switch (node->type)
     {
     case XML_ATTRIBUTE_NODE:
@@ -690,9 +663,19 @@ its_translate_rule_eval (struct its_rule_ty *pop, struct its_pool_ty *pool,
     case XML_ELEMENT_NODE:
       /* Inherit from the parent elements.  */
       {
-        struct its_value_list_ty *values;
         const char *value;
 
+        /* A local attribute overrides the global rule.  */
+        if (xmlHasNsProp (node, BAD_CAST "translate", BAD_CAST ITS_NS))
+          {
+            char *prop;
+
+            prop = _its_get_attribute (node, "translate", ITS_NS);
+            its_value_list_append (result, "translate", prop);
+            free (prop);
+            return result;
+          }
+
         /* Check value for the current node.  */
         value = _its_pool_get_value_for_node (pool, node, "translate");
         if (value != NULL)
@@ -708,10 +691,10 @@ its_translate_rule_eval (struct its_rule_ty *pop, struct its_pool_ty *pool,
           its_value_list_append (result, "translate", "yes");
         else
           {
+            struct its_value_list_ty *values;
+
             values = its_translate_rule_eval (pop, pool, node->parent);
-            value = its_value_list_get_value (values, "translate");
-            its_value_list_set_value (result, "translate", value);
-            _its_pool_set_value_for_node (pool, node, "translate", value);
+            its_value_list_merge (result, values);
             its_value_list_destroy (values);
             free (values);
           }
@@ -795,32 +778,6 @@ its_localization_note_rule_eval (struct its_rule_ty *pop,
 
   result = XCALLOC (1, struct its_value_list_ty);
 
-  /* Local attributes overrides the global rule.  */
-  if (xmlHasNsProp (node, BAD_CAST "locNote", BAD_CAST ITS_NS)
-      || xmlHasNsProp (node, BAD_CAST "locNoteRef", BAD_CAST ITS_NS)
-      || xmlHasNsProp (node, BAD_CAST "locNoteType", BAD_CAST ITS_NS))
-    {
-      char *prop;
-
-      if (xmlHasNsProp (node, BAD_CAST "locNote", BAD_CAST ITS_NS))
-        {
-          prop = _its_get_attribute (node, "locNote", ITS_NS);
-          its_value_list_append (result, "locNote", prop);
-          free (prop);
-        }
-
-      /* FIXME: locNoteRef */
-
-      if (xmlHasNsProp (node, BAD_CAST "locNoteType", BAD_CAST ITS_NS))
-        {
-          prop = _its_get_attribute (node, "locNoteType", ITS_NS);
-          its_value_list_append (result, "locNoteType", prop);
-          free (prop);
-        }
-
-      return result;
-    }
-
   switch (node->type)
     {
     case XML_ATTRIBUTE_NODE:
@@ -851,29 +808,65 @@ its_localization_note_rule_eval (struct its_rule_ty *pop,
     case XML_ELEMENT_NODE:
       /* Inherit from the parent elements.  */
       {
-        xmlNode *n;
+        const char *value;
 
-        for (n = node; n && n->type == XML_ELEMENT_NODE; n = n->parent)
+        /* Local attributes overrides the global rule.  */
+        if (xmlHasNsProp (node, BAD_CAST "locNote", BAD_CAST ITS_NS)
+            || xmlHasNsProp (node, BAD_CAST "locNoteRef", BAD_CAST ITS_NS)
+            || xmlHasNsProp (node, BAD_CAST "locNoteType", BAD_CAST ITS_NS))
           {
-            const char *value;
+            char *prop;
 
-            value = _its_pool_get_value_for_node (pool, node, "locNoteType");
-            if (value != NULL)
-              its_value_list_set_value (result, "locNoteType", value);
-
-            value = _its_pool_get_value_for_node (pool, n, "locNote");
-            if (value != NULL)
+            if (xmlHasNsProp (node, BAD_CAST "locNote", BAD_CAST ITS_NS))
               {
-                its_value_list_set_value (result, "locNote", value);
-                return result;
+                prop = _its_get_attribute (node, "locNote", ITS_NS);
+                its_value_list_append (result, "locNote", prop);
+                free (prop);
               }
 
-            value = _its_pool_get_value_for_node (pool, n, "locNotePointer");
-            if (value != NULL)
+            /* FIXME: locNoteRef */
+
+            if (xmlHasNsProp (node, BAD_CAST "locNoteType", BAD_CAST ITS_NS))
               {
-                its_value_list_set_value (result, "locNotePointer", value);
-                return result;
+                prop = _its_get_attribute (node, "locNoteType", ITS_NS);
+                its_value_list_append (result, "locNoteType", prop);
+                free (prop);
               }
+
+            return result;
+          }
+
+        /* Check value for the current node.  */
+        value = _its_pool_get_value_for_node (pool, node, "locNoteType");
+        if (value != NULL)
+          its_value_list_set_value (result, "locNoteType", value);
+
+        value = _its_pool_get_value_for_node (pool, node, "locNote");
+        if (value != NULL)
+          {
+            its_value_list_set_value (result, "locNote", value);
+            return result;
+          }
+
+        value = _its_pool_get_value_for_node (pool, node, "locNotePointer");
+        if (value != NULL)
+          {
+            its_value_list_set_value (result, "locNotePointer", value);
+            return result;
+          }
+
+        /* Recursively check value for the parent node.  */
+        if (node->parent == NULL
+            || node->parent->type != XML_ELEMENT_NODE)
+          return result;
+        else
+          {
+            struct its_value_list_ty *values;
+
+            values = its_localization_note_rule_eval (pop, pool, node->parent);
+            its_value_list_merge (result, values);
+            its_value_list_destroy (values);
+            free (values);
           }
       }
       break;
@@ -933,6 +926,9 @@ its_element_within_text_rule_eval (struct its_rule_ty *pop,
 
   result = XCALLOC (1, struct its_value_list_ty);
 
+  if (node->type != XML_ELEMENT_NODE)
+    return result;
+
   /* A local attribute overrides the global rule.  */
   if (xmlHasNsProp (node, BAD_CAST "withinText", BAD_CAST ITS_NS))
     {
@@ -996,10 +992,14 @@ its_preserve_space_rule_eval (struct its_rule_ty *pop,
                               xmlNode *node)
 {
   struct its_value_list_ty *result;
-  xmlNode *n;
+  struct its_value_list_ty *values;
+  const char *value;
 
   result = XCALLOC (1, struct its_value_list_ty);
 
+  if (node->type != XML_ELEMENT_NODE)
+    return result;
+
   /* A local attribute overrides the global rule.  */
   if (xmlHasNsProp (node, BAD_CAST "space", BAD_CAST XML_NS))
     {
@@ -1011,19 +1011,27 @@ its_preserve_space_rule_eval (struct its_rule_ty *pop,
       return result;
     }
 
+  value = _its_pool_get_value_for_node (pool, node, "space");
+  if (value != NULL)
+    {
+      its_value_list_set_value (result, "space", value);
+      return result;
+    }
+
   /* Inherit from the parent elements.  */
-  for (n = node; n && n->type == XML_ELEMENT_NODE; n = n->parent)
+  if (node->parent == NULL
+      || node->parent->type != XML_ELEMENT_NODE)
     {
-      const char *value = _its_pool_get_value_for_node (pool, n, "space");
-      if (value != NULL)
-        {
-          its_value_list_set_value (result, "space", value);
-          return result;
-        }
+      /* The default value is space="default".  */
+      its_value_list_append (result, "space", "default");
+      return result;
     }
 
-  /* The default value is space="default".  */
-  its_value_list_append (result, "space", "default");
+  values = its_preserve_space_rule_eval (pop, pool, node->parent);
+  its_value_list_merge (result, values);
+  its_value_list_destroy (values);
+  free (values);
+
   return result;
 }