LCOV - code coverage report
Current view: top level - src - Xml2JsonParker.dart (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 24 26 92.3 %
Date: 2017-07-14 Functions: 0 0 -

          Line data    Source code
       1             : /*
       2             :  * Package : xml2json
       3             :  * Author : S. Hamblett <steve.hamblett@linux.com>
       4             :  * Date   : 12/09/2013
       5             :  * Copyright :  S.Hamblett@OSCF
       6             :  *
       7             :  * Parker conversion class.
       8             :  * 
       9             :  * The Parker convention is lossy, it simply ignores XML attributes. 
      10             :  * The advantage is that it generates much leaner and cleaner JSON closer
      11             :  * to javascript Json than Badgerfish or GData which attempt to preserve
      12             :  * the XML structure.
      13             :  * 
      14             :  * Good for arrays of books, people etc that just happen to be in XML format
      15             :  * but are basically simple collections.
      16             :  * 
      17             :  * This transform conforms to the Parker convention here 
      18             :  * https://code.google.com/p/xml2json-xslt/wiki/TransformingRules except
      19             :  * for item 7, the name elements are still grouped as an array but under a 
      20             :  * property named after the elements, so 
      21             :  * 
      22             :  * <root><item>1</item><item>2</item><item>three</item></root>
      23             :  * 
      24             :  * becomes :-
      25             :  * 
      26             :  * {"item":["1","2","three"]} NOT ["1","2","three"]
      27             :  * 
      28             :  * This allows all 'items' to be pulled out of the data in one go and is felt to be
      29             :  * more useful than the conventional Parker transform.
      30             :  * 
      31             :  */
      32             : 
      33             : part of xml2json;
      34             : 
      35             : class _Xml2JsonParker {
      36             :   /// Parker transformer function.
      37             :   Map _transform(var node, var obj) {
      38           1 :     if (node is XmlElement) {
      39           3 :       final nodeName = "\"${node.name.qualified}\"";
      40           2 :       if (obj[nodeName] is List) {
      41           3 :         obj[nodeName].add({});
      42           2 :         obj = obj[nodeName].last;
      43           2 :       } else if (obj[nodeName] is Map) {
      44           4 :         obj[nodeName] = [obj[nodeName], {}];
      45           2 :         obj = obj[nodeName].last;
      46             :       } else {
      47           3 :         if (node.children.length >= 1) {
      48           3 :           if (node.children[0] is XmlText) {
      49             :             final String sanitisedNodeData =
      50           4 :                 _Xml2JsonUtils.escapeTextForJson(node.children[0].text);
      51           2 :             String nodeData = '"' + sanitisedNodeData + '"';
      52           1 :             if (nodeData.isEmpty) nodeData = null;
      53           1 :             obj[nodeName] = nodeData;
      54             :           } else {
      55           2 :             obj[nodeName] = {};
      56           1 :             obj = obj[nodeName];
      57             :           }
      58             :         } else {
      59             :           /* No children, empty element */
      60           1 :           obj[nodeName] = null;
      61             :         }
      62             :       }
      63             : 
      64           4 :       for (var j = 0; j < node.children.length; j++) {
      65           3 :         _transform(node.children[j], obj);
      66             :       }
      67           1 :     } else if (node is XmlDocument) {
      68           4 :       for (var j = 0; j < node.children.length; j++) {
      69           3 :         _transform(node.children[j], obj);
      70             :       }
      71             :     }
      72             : 
      73             :     return obj;
      74             :   }
      75             : 
      76             :   /// Transformer function
      77             :   String transform(XmlDocument xmlNode) {
      78             :     Map json = null;
      79             :     try {
      80           2 :       json = _transform(xmlNode, {});
      81             :     } catch (e) {
      82           0 :       throw new Xml2JsonException(
      83           0 :           "Parker internal transform error => ${e.toString()}");
      84             :     }
      85             : 
      86           1 :     return json.toString();
      87             :   }
      88             : }

Generated by: LCOV version 1.10