buildframework/helium/sf/python/pythoncore/lib/amara.py
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 587 85df38eb4012
child 645 b8d81fa19e7d
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
#============================================================================ 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
#Name        : amara.py 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
#All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
#Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
#Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
#Description:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
#===============================================================================
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
"""amara"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    21
# pylint: disable=E1103
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    22
#import sys
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    23
#if 'java' in sys.platform:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    24
#    pass
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    25
if True:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    26
    import os
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
    import xml.dom.minidom
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
    import urllib
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    29
    import xpath
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    30
    import xml.etree.ElementTree
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    31
    import xml.etree.ElementInclude
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    32
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    33
    # pylint: disable=W0212
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    34
    def fixed_writexml(self, writer, indent="", addindent="", newl=""):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    35
        # indent = current indentation
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    36
        # addindent = indentation to add to higher levels
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    37
        # newl = newline string
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    38
        writer.write(indent + "<" + self.tagName)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    39
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    40
        attrs = self._get_attributes()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    41
        a_names = attrs.keys()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    42
        a_names.sort()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    43
    
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    44
        for a_name in a_names:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    45
            writer.write(" %s=\"" % a_name)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    46
            xml.dom.minidom._write_data(writer, attrs[a_name].value)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    47
            writer.write("\"")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    48
        if self.childNodes:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    49
            if len(self.childNodes) == 1 \
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    50
              and self.childNodes[0].nodeType == xml.dom.minidom.Node.TEXT_NODE:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    51
                writer.write(">")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    52
                self.childNodes[0].writexml(writer, "", "", "")
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    53
                writer.write("</%s>%s" % (self.tagName, newl))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    54
                return
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    55
            writer.write(">%s" % (newl))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    56
            for node in self.childNodes:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    57
                if node.__class__ == xml.dom.minidom.Text and node.data.strip() == '':
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    58
                    continue
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    59
                node.writexml(writer, indent + addindent, addindent, newl)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    60
            writer.write("%s</%s>%s" % (indent, self.tagName, newl))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    61
        else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    62
            writer.write("/>%s" % (newl))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    63
    # replace minidom's function with ours
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    64
    xml.dom.minidom.Element.writexml = fixed_writexml
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
    def parse(param):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
        """parse"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
        return MinidomAmara(param)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
    def create_document(name=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
        """create document"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
        impl = xml.dom.minidom.getDOMImplementation()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
        newdoc = impl.createDocument(None, name, None)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
        return MinidomAmara(newdoc)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
    class MinidomAmara(object):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
        """ amara api using minidom """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
        def __init__(self, dom, parent=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
            self.parent = parent
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
            if isinstance(dom, file):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
                self.dom = xml.dom.minidom.parse(dom)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
            elif isinstance(dom, basestring):
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    84
                cwd_backup = os.getcwd()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    85
                if dom.startswith('file:///') or dom.startswith('///') or os.path.exists(dom):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    86
                    try:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    87
                        path = urllib.url2pathname(dom)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    88
                        path = path.replace('file:///', '')
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    89
                        os.chdir(os.path.dirname(path))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    90
                    except IOError:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    91
                        pass
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
                    dom = urllib.urlopen(dom).read()
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    93
                
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    94
                ettree = xml.etree.ElementTree.fromstring(dom)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    95
                xml.etree.ElementInclude.include(ettree)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    96
                dom = xml.etree.ElementTree.tostring(ettree)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
    97
                os.chdir(cwd_backup)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
                self.dom = xml.dom.minidom.parseString(dom)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
                self.dom = dom
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
        def __getitem__(self, name):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
            return self.__getattr__(name)
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   104
            
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   105
        def __setitem__(self, key, value):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   106
            self.xml_set_attribute(key, value)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   107
            
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
        def __getattr__(self, attr):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
            if isinstance(attr, basestring):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
                res = self.dom.getElementsByTagName(attr)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
                if len(res) == 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
                    if hasattr(self.dom, 'documentElement'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
                        val = self.dom.documentElement.getAttribute(attr)
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   114
                        if not self.dom.documentElement.hasAttribute(attr):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   115
                            raise Exception(attr + ' not found')
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
                    else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
                        val = self.dom.getAttribute(attr)
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   118
                        if not self.dom.hasAttribute(attr):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   119
                            raise Exception(attr + ' not found')
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
                    return val
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   121
                return MinidomAmara(res[0], self.dom)
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   122
            if self.parent:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   123
                return MinidomAmara(self.parent.getElementsByTagName(self.dom.tagName)[attr])
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   124
            else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   125
                raise Exception(str(attr) + ' not found')
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
    
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   127
        def __setattr__(self, name, value):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   128
            if isinstance(value, basestring):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   129
                self.xml_set_attribute(name, value)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   130
            else:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   131
                object.__setattr__(self, name, value)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   132
        
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
        def __iter__(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   134
            for entry in self.parent.getElementsByTagName(self.dom.tagName):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   135
                yield MinidomAmara(entry)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   136
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   137
        def __str__(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   138
            text = ''
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   139
            for t_text in self.dom.childNodes:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   140
                if t_text.nodeType == t_text.TEXT_NODE and t_text.data != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   141
                    text = text + t_text.data
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   142
            return text
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   143
        
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   144
        def xml(self, out=None, indent=False, omitXmlDeclaration=False, encoding='utf-8'):
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   145
            """xml"""
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   146
            if omitXmlDeclaration:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   147
                pass
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   148
            if out:
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   149
                out.write(self.dom.toprettyxml(encoding=encoding))
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   150
            if indent:
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   151
                return self.dom.toprettyxml(encoding=encoding)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   152
            return self.dom.toxml(encoding=encoding)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   153
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   154
        def xml_append_fragment(self, text):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   155
            """xml append fragment"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   156
            self.dom.appendChild(xml.dom.minidom.parseString(text).documentElement)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   157
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   158
        def xml_set_attribute(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   159
            """set XML attribute"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   160
            self.dom.setAttribute(name, value)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   161
        
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   162
        def xml_remove_child(self, value):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   163
            self.dom.removeChild(value.dom)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   164
        
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   165
        def _getxml_children(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   166
            """get xml children"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   167
            l_attrib = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   168
            for elem in self.dom.childNodes:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   169
                if elem.nodeType == elem.ELEMENT_NODE:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   170
                    l_attrib.append(MinidomAmara(elem))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   171
            return l_attrib
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   172
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   173
        def _getxml_attributes(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   174
            """get aml attributes"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   175
            l_attrib = self.dom.attributes
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   176
            out = {}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   177
            for i in range(l_attrib.length):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   178
                out[l_attrib.item(i).name] = l_attrib.item(i).nodeValue
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   179
            return out
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   180
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   181
        def xml_append(self, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   182
            """append to XML """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   183
            if hasattr(self.dom, 'documentElement') and self.dom.documentElement != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   184
                value.dom.documentElement = self.dom.documentElement.appendChild(value.dom.documentElement)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   185
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   186
                value.dom.documentElement = self.dom.appendChild(value.dom.documentElement)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   187
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   188
        def xml_create_element(self, name, content=None, attributes=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   189
            """ create XML element"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   190
            elem = create_document(name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   191
            if attributes:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   192
                for attrib in attributes.keys():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   193
                    elem[name].dom.setAttribute(attrib, attributes[attrib])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   194
            if content:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   195
                impl = xml.dom.minidom.getDOMImplementation()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   196
                newdoc = impl.createDocument(None, None, None)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   197
                elem[name].dom.appendChild(newdoc.createTextNode(content))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   198
            return elem
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   199
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   200
        def _getnodetype(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   201
            """get node type"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   202
            return self.dom.nodeType
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   203
        def _getnodename(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   204
            """get node nmae"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   205
            return self.dom.nodeName
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   206
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   207
        nodeType = property(_getnodetype)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   208
        nodeName = property(_getnodename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   209
        childNodes = property(_getxml_children)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   210
        xml_children = property(_getxml_children)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   211
        xml_attributes = property(_getxml_attributes)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   212
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   213
        def __eq__(self, obj):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   214
            return str(self) == obj
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   215
        def __ne__(self, obj):
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   216
            return str(self) != obj
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   217
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   218
        def __len__(self):
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   219
            if self.parent:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   220
                return len(self.parent.getElementsByTagName(self.dom.tagName))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   221
            return 1
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   222
        
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   223
        def xml_xpath(self, axpath):
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   224
            """append to the XML path"""
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   225
            results  = [] 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   226
            for result in xpath.find(axpath, self.dom): 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   227
                results.append(MinidomAmara(result)) 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   228
            return results 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   229
#            import java.io.ByteArrayInputStream
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   230
#            import org.dom4j.io.SAXReader
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   231
#            import org.dom4j.DocumentHelper
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   232
#
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   233
#            stream = java.io.ByteArrayInputStream(java.lang.String(self.dom.toxml()).getBytes("UTF-8"))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   234
#            xmlReader = org.dom4j.io.SAXReader()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   235
#            doc = xmlReader.read(stream)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   236
#            xpath = org.dom4j.DocumentHelper.createXPath(xpath)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   237
#            signalNodes = xpath.selectNodes(doc)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   238
#            iterator = signalNodes.iterator()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   239
#            out = []
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   240
#            while iterator.hasNext():
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   241
#                p_iterator = iterator.next()
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   242
#                out.append(MinidomAmara(p_iterator.asXML()))
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 587
diff changeset
   243
#            return out