|
1 #============================================================================ |
|
2 #Name : amara.py |
|
3 #Part of : Helium |
|
4 |
|
5 #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
6 #All rights reserved. |
|
7 #This component and the accompanying materials are made available |
|
8 #under the terms of the License "Eclipse Public License v1.0" |
|
9 #which accompanies this distribution, and is available |
|
10 #at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
11 # |
|
12 #Initial Contributors: |
|
13 #Nokia Corporation - initial contribution. |
|
14 # |
|
15 #Contributors: |
|
16 # |
|
17 #Description: |
|
18 #=============================================================================== |
|
19 """amara""" |
|
20 |
|
21 # pylint: disable-msg=E1103 |
|
22 import sys |
|
23 if 'java' in sys.platform: |
|
24 import xml.dom.minidom |
|
25 import urllib |
|
26 |
|
27 def parse(param): |
|
28 """parse""" |
|
29 return MinidomAmara(param) |
|
30 |
|
31 def create_document(name=None): |
|
32 """create document""" |
|
33 impl = xml.dom.minidom.getDOMImplementation() |
|
34 newdoc = impl.createDocument(None, name, None) |
|
35 return MinidomAmara(newdoc) |
|
36 |
|
37 class MinidomAmara(object): |
|
38 """ amara api using minidom """ |
|
39 |
|
40 def __init__(self, dom, parent=None): |
|
41 self.parent = parent |
|
42 if isinstance(dom, file): |
|
43 self.dom = xml.dom.minidom.parse(dom) |
|
44 elif isinstance(dom, basestring): |
|
45 if dom.startswith('file:///'): |
|
46 dom = urllib.urlopen(dom).read() |
|
47 self.dom = xml.dom.minidom.parseString(dom) |
|
48 else: |
|
49 self.dom = dom |
|
50 |
|
51 def __getitem__(self, name): |
|
52 return self.__getattr__(name) |
|
53 |
|
54 def __getattr__(self, attr): |
|
55 if isinstance(attr, basestring): |
|
56 res = self.dom.getElementsByTagName(attr) |
|
57 if len(res) == 0: |
|
58 if hasattr(self.dom, 'documentElement'): |
|
59 val = self.dom.documentElement.getAttribute(attr) |
|
60 else: |
|
61 val = self.dom.getAttribute(attr) |
|
62 if val == '': |
|
63 raise Exception(attr + ' not found') |
|
64 return val |
|
65 return MinidomAmara(res[0], self.dom) |
|
66 return MinidomAmara(self.parent.getElementsByTagName(self.dom.tagName)[attr]) |
|
67 |
|
68 def __iter__(self): |
|
69 for entry in self.parent.getElementsByTagName(self.dom.tagName): |
|
70 yield MinidomAmara(entry) |
|
71 |
|
72 def __str__(self): |
|
73 text = '' |
|
74 for t_text in self.dom.childNodes: |
|
75 if t_text.nodeType == t_text.TEXT_NODE and t_text.data != None: |
|
76 text = text + t_text.data |
|
77 return text |
|
78 |
|
79 def xml(self, out=None, indent=True, omitXmlDeclaration=False, encoding=''): |
|
80 """xml""" |
|
81 if out: |
|
82 out.write(self.dom.toprettyxml()) |
|
83 if indent: |
|
84 return self.dom.toprettyxml() |
|
85 return self.dom.toxml() |
|
86 |
|
87 def xml_append_fragment(self, text): |
|
88 """xml append fragment""" |
|
89 self.dom.appendChild(xml.dom.minidom.parseString(text).documentElement) |
|
90 |
|
91 def xml_set_attribute(self, name, value): |
|
92 """set XML attribute""" |
|
93 self.dom.setAttribute(name, value) |
|
94 |
|
95 def _getxml_children(self): |
|
96 """get xml children""" |
|
97 l_attrib = [] |
|
98 for elem in self.dom.childNodes: |
|
99 if elem.nodeType == elem.ELEMENT_NODE: |
|
100 l_attrib.append(MinidomAmara(elem)) |
|
101 return l_attrib |
|
102 |
|
103 def _getxml_attributes(self): |
|
104 """get aml attributes""" |
|
105 l_attrib = self.dom.attributes |
|
106 out = {} |
|
107 for i in range(l_attrib.length): |
|
108 out[l_attrib.item(i).name] = l_attrib.item(i).nodeValue |
|
109 return out |
|
110 |
|
111 def xml_append(self, value): |
|
112 """append to XML """ |
|
113 if hasattr(self.dom, 'documentElement') and self.dom.documentElement != None: |
|
114 value.dom.documentElement = self.dom.documentElement.appendChild(value.dom.documentElement) |
|
115 else: |
|
116 value.dom.documentElement = self.dom.appendChild(value.dom.documentElement) |
|
117 |
|
118 def xml_create_element(self, name, content=None, attributes=None): |
|
119 """ create XML element""" |
|
120 elem = create_document(name) |
|
121 if attributes: |
|
122 for attrib in attributes.keys(): |
|
123 elem[name].dom.setAttribute(attrib, attributes[attrib]) |
|
124 if content: |
|
125 impl = xml.dom.minidom.getDOMImplementation() |
|
126 newdoc = impl.createDocument(None, None, None) |
|
127 elem[name].dom.appendChild(newdoc.createTextNode(content)) |
|
128 return elem |
|
129 |
|
130 def _getnodetype(self): |
|
131 """get node type""" |
|
132 return self.dom.nodeType |
|
133 def _getnodename(self): |
|
134 """get node nmae""" |
|
135 return self.dom.nodeName |
|
136 |
|
137 nodeType = property(_getnodetype) |
|
138 nodeName = property(_getnodename) |
|
139 childNodes = property(_getxml_children) |
|
140 xml_children = property(_getxml_children) |
|
141 xml_attributes = property(_getxml_attributes) |
|
142 |
|
143 def __eq__(self, obj): |
|
144 return str(self) == obj |
|
145 |
|
146 def __len__(self): |
|
147 return len(self.parent.getElementsByTagName(self.dom.tagName)) |
|
148 |
|
149 def xml_xpath(self, xpath): |
|
150 """append to the XML path""" |
|
151 import java.io.ByteArrayInputStream |
|
152 import org.dom4j.io.SAXReader |
|
153 import org.dom4j.DocumentHelper |
|
154 |
|
155 stream = java.io.ByteArrayInputStream(java.lang.String(self.dom.toxml()).getBytes("UTF-8")) |
|
156 xmlReader = org.dom4j.io.SAXReader() |
|
157 doc = xmlReader.read(stream) |
|
158 xpath = org.dom4j.DocumentHelper.createXPath(xpath) |
|
159 signalNodes = xpath.selectNodes(doc) |
|
160 iterator = signalNodes.iterator() |
|
161 out = [] |
|
162 while iterator.hasNext(): |
|
163 p_iterator = iterator.next() |
|
164 out.append(MinidomAmara(p_iterator.asXML())) |
|
165 return out |