1 | from xml.dom.xmlbuilder import * |
---|
2 | #from xml.dom.ext import PrettyPrint |
---|
3 | from xml.dom import Node |
---|
4 | import xml.dom as dom |
---|
5 | #from |
---|
6 | from StringIO import StringIO |
---|
7 | import re |
---|
8 | import sys |
---|
9 | import os.path |
---|
10 | from objects import * |
---|
11 | from cd import * |
---|
12 | def readFile(input_file_name): |
---|
13 | docstream = open(input_file_name) |
---|
14 | docIS=DOMInputSource() |
---|
15 | docIS.byteStream=docstream |
---|
16 | doc= DOMBuilder().parse(docIS) |
---|
17 | docstream.close() |
---|
18 | |
---|
19 | root=doc.lastChild |
---|
20 | return root |
---|
21 | |
---|
22 | def get_text_in_children(n): |
---|
23 | t = "" |
---|
24 | for c in n.childNodes: |
---|
25 | if c.nodeType== Node.TEXT_NODE: |
---|
26 | t += c.nodeValue |
---|
27 | return t |
---|
28 | def remove_white_space(node): |
---|
29 | remove_list=[] |
---|
30 | if node.nodeName=="OMC": |
---|
31 | return |
---|
32 | for child in node.childNodes: |
---|
33 | if child.nodeType==dom.Node.TEXT_NODE and not child.data.strip(): |
---|
34 | remove_list.append(child) |
---|
35 | elif child.hasChildNodes(): |
---|
36 | remove_white_space(child) |
---|
37 | for node in remove_list: |
---|
38 | node.parentNode.removeChild(node) |
---|
39 | node.unlink() |
---|
40 | |
---|
41 | class OMFromXMLBuilder: |
---|
42 | def buildFromNode(self, node): |
---|
43 | if (node.nodeName=="OMI"): |
---|
44 | content=get_text_in_children(node) |
---|
45 | return OMint(content) |
---|
46 | if (node.nodeName=="OMV"): |
---|
47 | name=node.getAttribute("name") #node.attributes["name"] |
---|
48 | #print dir(name) |
---|
49 | return OMvar(name) |
---|
50 | if (node.nodeName=="OMS"): |
---|
51 | if node.hasAttribute("cdbase"): |
---|
52 | #FIXME: obtain from ancestors |
---|
53 | cdbase=node.getAttribute("cdbase") |
---|
54 | else: |
---|
55 | cdbase=None |
---|
56 | cdname=node.getAttribute("cd") |
---|
57 | name=node.getAttribute("name") |
---|
58 | #print repr(node) |
---|
59 | #print node.attributes |
---|
60 | #print "cdbase", cdbase |
---|
61 | if cdbase==None: |
---|
62 | cd=OMcd(cdname) |
---|
63 | else: |
---|
64 | cd=OMcd(cdname,cdbase) |
---|
65 | return OMsymbol(name,cd) |
---|
66 | if (node.nodeName=="OMA"): |
---|
67 | children=[self.buildFromNode(c) for c in node.childNodes] |
---|
68 | return OMapplication(children[0],children[1:]) |
---|
69 | def build(self, root): |
---|
70 | remove_white_space(root) |
---|
71 | return self.buildFromNode(root) |
---|
72 | #TODO: handle hex floats |
---|
73 | #TODO: handle floats |
---|
74 | #TODO: handle ancestors cdbase |
---|
75 | if __name__=='__main__': |
---|
76 | import arith1 |
---|
77 | |
---|
78 | if len(sys.argv)<=2: |
---|
79 | print "Usage: python omxmlreader [--evaluate] input output" |
---|
80 | state=0 |
---|
81 | eval=False |
---|
82 | #TODO: use optparse |
---|
83 | for arg in sys.argv[1:]: |
---|
84 | if state==0: |
---|
85 | if arg=="--evaluate": |
---|
86 | eval=True |
---|
87 | else: |
---|
88 | inputf=arg |
---|
89 | state=1 |
---|
90 | continue |
---|
91 | if state==1: |
---|
92 | outputf=arg |
---|
93 | continue |
---|
94 | if state==2: |
---|
95 | print "argument ignored:", arg |
---|
96 | from context import Context |
---|
97 | #inputf=sys.argv[1] |
---|
98 | root=readFile(inputf) |
---|
99 | builder=OMFromXMLBuilder() |
---|
100 | context=Context() |
---|
101 | context.addCDImplementation(arith1.implementation) |
---|
102 | doc=builder.build(root) |
---|
103 | if eval: |
---|
104 | doc=context.evaluate(doc) |
---|
105 | output=context.XMLEncodeObject(doc) |
---|
106 | try: |
---|
107 | out=open(outputf,"w") |
---|
108 | except NameError: |
---|
109 | print "no output file" |
---|
110 | sys.exit(1) |
---|
111 | out.write(output) |
---|
112 | out.close() |
---|
113 | #print repr(root.getAttribute("blabla")) |
---|