buildframework/helium/sf/python/pythoncore/lib/amara.py
author wbernard
Tue, 27 Apr 2010 08:33:08 +0300
changeset 587 85df38eb4012
child 628 7c4a911dc066
permissions -rw-r--r--
helium_9.0-a7879c935424
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
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
# pylint: disable-msg=E1103
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import sys
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
if 'java' in sys.platform:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
    import xml.dom.minidom
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
    import urllib
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
    def parse(param):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
        """parse"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
        return MinidomAmara(param)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
    def create_document(name=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
        """create document"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
        impl = xml.dom.minidom.getDOMImplementation()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
        newdoc = impl.createDocument(None, name, None)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
        return MinidomAmara(newdoc)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    class MinidomAmara(object):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
        """ amara api using minidom """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
        def __init__(self, dom, parent=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
            self.parent = parent
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
            if isinstance(dom, file):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
                self.dom = xml.dom.minidom.parse(dom)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
            elif isinstance(dom, basestring):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
                if dom.startswith('file:///'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
                    dom = urllib.urlopen(dom).read()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
                self.dom = xml.dom.minidom.parseString(dom)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
                self.dom = dom
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
        def __getitem__(self, name):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
            return self.__getattr__(name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
        def __getattr__(self, attr):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
            if isinstance(attr, basestring):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
                res = self.dom.getElementsByTagName(attr)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
                if len(res) == 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
                    if hasattr(self.dom, 'documentElement'):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
                        val = self.dom.documentElement.getAttribute(attr)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
                    else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
                        val = self.dom.getAttribute(attr)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
                    if val == '':
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
                        raise Exception(attr + ' not found')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
                    return val
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
                return MinidomAmara(res[0], self.dom)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
            return MinidomAmara(self.parent.getElementsByTagName(self.dom.tagName)[attr])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
        def __iter__(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
            for entry in self.parent.getElementsByTagName(self.dom.tagName):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
                yield MinidomAmara(entry)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
        def __str__(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
            text = ''
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
            for t_text in self.dom.childNodes:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
                if t_text.nodeType == t_text.TEXT_NODE and t_text.data != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
                    text = text + t_text.data
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
            return text
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
        def xml(self, out=None, indent=True, omitXmlDeclaration=False, encoding=''):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
            """xml"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
            if out:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
                out.write(self.dom.toprettyxml())
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
            if indent:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
                return self.dom.toprettyxml()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
            return self.dom.toxml()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
        def xml_append_fragment(self, text):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
            """xml append fragment"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
            self.dom.appendChild(xml.dom.minidom.parseString(text).documentElement)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
        def xml_set_attribute(self, name, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
            """set XML attribute"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
            self.dom.setAttribute(name, value)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
        def _getxml_children(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
            """get xml children"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
            l_attrib = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
            for elem in self.dom.childNodes:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
                if elem.nodeType == elem.ELEMENT_NODE:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
                    l_attrib.append(MinidomAmara(elem))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
            return l_attrib
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   102
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   103
        def _getxml_attributes(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   104
            """get aml attributes"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   105
            l_attrib = self.dom.attributes
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   106
            out = {}
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   107
            for i in range(l_attrib.length):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   108
                out[l_attrib.item(i).name] = l_attrib.item(i).nodeValue
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   109
            return out
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   110
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   111
        def xml_append(self, value):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   112
            """append to XML """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   113
            if hasattr(self.dom, 'documentElement') and self.dom.documentElement != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   114
                value.dom.documentElement = self.dom.documentElement.appendChild(value.dom.documentElement)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   115
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   116
                value.dom.documentElement = self.dom.appendChild(value.dom.documentElement)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   117
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
        def xml_create_element(self, name, content=None, attributes=None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
            """ create XML element"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   120
            elem = create_document(name)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   121
            if attributes:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   122
                for attrib in attributes.keys():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   123
                    elem[name].dom.setAttribute(attrib, attributes[attrib])
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   124
            if content:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   125
                impl = xml.dom.minidom.getDOMImplementation()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   126
                newdoc = impl.createDocument(None, None, None)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   127
                elem[name].dom.appendChild(newdoc.createTextNode(content))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   128
            return elem
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   129
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   130
        def _getnodetype(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   131
            """get node type"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   132
            return self.dom.nodeType
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   133
        def _getnodename(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   134
            """get node nmae"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   135
            return self.dom.nodeName
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   136
            
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   137
        nodeType = property(_getnodetype)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   138
        nodeName = property(_getnodename)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   139
        childNodes = property(_getxml_children)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   140
        xml_children = property(_getxml_children)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   141
        xml_attributes = property(_getxml_attributes)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   142
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   143
        def __eq__(self, obj):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   144
            return str(self) == obj
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   145
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   146
        def __len__(self):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   147
            return len(self.parent.getElementsByTagName(self.dom.tagName))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   148
        
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   149
        def xml_xpath(self, xpath):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   150
            """append to the XML path"""
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   151
            import java.io.ByteArrayInputStream
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   152
            import org.dom4j.io.SAXReader
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   153
            import org.dom4j.DocumentHelper
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   154
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   155
            stream = java.io.ByteArrayInputStream(java.lang.String(self.dom.toxml()).getBytes("UTF-8"))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   156
            xmlReader = org.dom4j.io.SAXReader()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   157
            doc = xmlReader.read(stream)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   158
            xpath = org.dom4j.DocumentHelper.createXPath(xpath)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   159
            signalNodes = xpath.selectNodes(doc)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   160
            iterator = signalNodes.iterator()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   161
            out = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   162
            while iterator.hasNext():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   163
                p_iterator = iterator.next()
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   164
                out.append(MinidomAmara(p_iterator.asXML()))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   165
            return out