示例#1
0
    def _walk(schema_node, xml_node):
        xml_children = collect_siblings(xml_node)
        actual_names = set(xml_children.keys())
        expected_names = collect_child_names(schema_node)

        remove_names = actual_names - expected_names

        for name, siblings in xml_children.items():
            if name in remove_names:
                for sib in siblings:
                    xml_node.remove(sib)
            else:
                child_schema_node = find_child_by_name(schema_node, name)
                for sib in siblings:
                    _walk(child_schema_node, sib)
示例#2
0
    def _walk(schema_node, xml_node):
        xml_children = collect_siblings(xml_node)
        actual_names = set(xml_children.keys())
        expected_names = collect_child_names(schema_node)

        remove_names = actual_names - expected_names

        for name, siblings in xml_children.items():
            if name in remove_names:
                for sib in siblings:
                    xml_node.remove(sib)
            else:
                child_schema_node = find_child_by_name(schema_node, name)
                for sib in siblings:
                    _walk(child_schema_node, sib)
示例#3
0
    def _translate_node(self,
                        is_collection,
                        schema_root,
                        xml_node,
                        prefixes,
                        depth=0):
        ''' Translates the given XML node into JSON

        is_collection -- True if the results is in the collection+json format
        xml_node -- the current XML element to translate
        '''
        json = list()
        first_child = False

        current_list = None
        current_name = schema_root.get_name()
        if schema_root.is_leafy():
            schema_root = schema_root.get_parent()

        siblings = collect_siblings(xml_node)

        if len(siblings) == 0:
            if xml_node.text is None:
                return '"empty":""'
            else:
                return '"%s":"%s"' % (current_name, xml_node.text)

        for sibling_tag, is_last in iterate_with_lookahead(siblings):

            sib_xml_nodes = siblings[sibling_tag]

            child_schema_node = find_child_by_name(schema_root, sibling_tag)

            has_siblings = child_schema_node.is_listy()

            if has_siblings:
                sib_name, sib_prefix, prefixes = _split_tag_and_update_prefixes(
                    sib_xml_nodes[0], prefixes)

                json.append('"%s%s" : [' % (sib_prefix, sib_name))

            for child_xml_node, sib_is_last in iterate_with_lookahead(
                    sib_xml_nodes):

                name, prefix, prefixes = _split_tag_and_update_prefixes(
                    child_xml_node, prefixes)
                value = child_xml_node.text
                if not child_schema_node.is_leafy():
                    # it's a container, so iterate over children
                    if has_siblings:
                        json.append('{')
                    else:
                        json.append('"%s%s":{' % (prefix, name))

                    body = self._translate_node(
                        is_collection,
                        child_schema_node,
                        child_xml_node,
                        prefixes,
                        depth + 1,
                    )

                    json.append(body)
                    json.append('}')

                else:
                    if child_schema_node.node_type(
                    ).leaf_type == RwYang.LeafType.LEAF_TYPE_EMPTY:
                        value = "[null]"
                    elif child_schema_node.node_type(
                    ).leaf_type == RwYang.LeafType.LEAF_TYPE_STRING:
                        if not value:
                            value = '""'
                        else:
                            value = tornado.escape.json_encode(value)
                    else:

                        if not value:
                            value = '""'
                        else:
                            try:
                                float(value)
                            except ValueError:
                                value = tornado.escape.json_encode(value)

                    if has_siblings:
                        json.append('%s' % (value))
                    else:
                        json.append('"%s" : %s' % (name, value))

                if not sib_is_last:
                    json.append(',')

                if prefix != "":
                    prefixes.pop()

            if has_siblings:
                json.append(']')
                if sib_prefix != "":
                    prefixes.pop()

            if not is_last:
                json.append(',')

        return ''.join(json)
示例#4
0
    def _translate_node(self, is_collection, schema_root, xml_node, prefixes, depth=0):
        ''' Translates the given XML node into JSON

        is_collection -- True if the results is in the collection+json format
        xml_node -- the current XML element to translate
        '''
        json = list()
        first_child = False

        current_list = None
        current_name = schema_root.get_name()        
        if schema_root.is_leafy():
            schema_root = schema_root.get_parent()

        siblings = collect_siblings(xml_node)

        if len(siblings) == 0:
            if xml_node.text is None:
                return '"empty":""' 
            else:
                return '"%s":"%s"' % (current_name,xml_node.text)


        for sibling_tag, is_last in iterate_with_lookahead(siblings):
            
            sib_xml_nodes = siblings[sibling_tag]            

            child_schema_node = find_child_by_name(schema_root, sibling_tag)
            if child_schema_node is None:
                self._logger.warning("Unknown Child schema node <%s> for parent <%s>",
                                     sibling_tag, current_name)
                if is_last and json[-1] == ",":
                    json.pop()
                continue

            has_siblings = child_schema_node.is_listy()

            if has_siblings:
                sib_name, sib_prefix, prefixes = _split_tag_and_update_prefixes(sib_xml_nodes[0], prefixes)

                json.append('"%s%s" : [' % (sib_prefix, sib_name))

            for child_xml_node, sib_is_last  in iterate_with_lookahead(sib_xml_nodes):

                name, prefix, prefixes = _split_tag_and_update_prefixes(child_xml_node, prefixes)
                value = child_xml_node.text
                if not child_schema_node.is_leafy():
                    # it's a container, so iterate over children
                    if has_siblings:
                        json.append('{')
                    else:
                        json.append('"%s%s":{' % (prefix,name))
    
                    body = self._translate_node(is_collection,
                                                     child_schema_node,
                                                     child_xml_node,
                                                     prefixes,
                                                     depth+1,
                                            )

                    json.append(body)
                    json.append('}')
                            
                else:
                    if child_schema_node.node_type().leaf_type == RwYang.LeafType.LEAF_TYPE_EMPTY:
                        value = "[null]"
                    elif child_schema_node.node_type().leaf_type == RwYang.LeafType.LEAF_TYPE_STRING:
                        if not value:
                            value = '""'
                        else:
                            value = tornado.escape.json_encode(value)
                    else:

                        if not value:
                            value = '""'
                        else:
                            try:
                                float(value)
                            except ValueError:
                                value = tornado.escape.json_encode(value)
                    
                    if has_siblings:
                        json.append('%s' % (value))
                    else:
                        json.append('"%s" : %s' % (name, value))

                if not sib_is_last:
                    json.append(',')

                if prefix != "":
                    prefixes.pop()

            if has_siblings:
                json.append(']')
                if sib_prefix != "":
                    prefixes.pop()        

            if not is_last:
                json.append(',')

        return ''.join(json)