source: git/modules/openmathserver/omxmlreader.py @ 859b51

spielwiese
Last change on this file since 859b51 was 859b51, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken: OMBIND encode decode git-svn-id: file:///usr/local/Singular/svn/trunk@8365 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.5 KB
Line 
1from xml.dom.xmlbuilder import *
2#from xml.dom.ext import PrettyPrint
3from xml.dom import Node
4import xml.dom as dom
5#from
6from StringIO import StringIO
7import re
8import sys
9import os.path
10from objects import *
11from binding import *
12from cd import *
13from exceptions import *
14def readFile(input_file_name):
15    docstream = open(input_file_name)
16    docIS=DOMInputSource()
17    docIS.byteStream=docstream
18    doc= DOMBuilder().parse(docIS)
19    docstream.close()
20   
21    root=doc.lastChild
22    return root
23   
24def  get_text_in_children(n):
25    t = ""
26    for c in n.childNodes:
27        if c.nodeType== Node.TEXT_NODE:
28            t += c.nodeValue
29    return t
30def remove_white_space(node):
31    remove_list=[]
32    if node.nodeName=="OMC":
33        return
34    for child in node.childNodes:
35        if child.nodeType==dom.Node.TEXT_NODE and not child.data.strip():
36            remove_list.append(child)
37        elif child.hasChildNodes():
38            remove_white_space(child)
39    for node in remove_list:
40        node.parentNode.removeChild(node)
41        node.unlink()
42       
43class OMFromXMLBuilder:
44    def buildFromNode(self, node):
45        if (node.nodeName=="OMI"):
46            content=get_text_in_children(node)
47            return OMint(content)
48        if (node.nodeName=="OMV"):
49            name=node.getAttribute("name") #node.attributes["name"]
50            #print dir(name)
51            return OMvar(name)
52        if (node.nodeName=="OMS"):
53            if node.hasAttribute("cdbase"):
54                #FIXME: obtain from ancestors
55                cdbase=node.getAttribute("cdbase")
56            else: 
57                cdbase=None
58            cdname=node.getAttribute("cd")
59            name=node.getAttribute("name")
60            #print repr(node)
61            #print node.attributes
62            #print "cdbase", cdbase
63            if cdbase==None:
64                cd=OMcd(cdname)
65            else:
66                cd=OMcd(cdname,cdbase)
67            return OMsymbol(name,cd)
68        if (node.nodeName=="OMA"):
69            children=[self.buildFromNode(c) for c in node.childNodes]
70            return OMapplication(children[0],children[1:])
71        if (node.nodeName=="OMBIND"):
72            children=[self.buildFromNode(c) for c in node.childNodes]
73            return OMbinding(children[0],children[1:-1],children[-1])
74        raise NotImplementedError
75    def build(self, root):
76        remove_white_space(root)
77        return self.buildFromNode(root)
78#TODO: handle hex floats
79#TODO: handle floats
80#TODO: handle ancestors cdbase
81if __name__=='__main__':
82    import arith1
83   
84    if len(sys.argv)<=2:
85        print "Usage: python omxmlreader [--evaluate] input output"
86    state=0
87    eval=False
88    #TODO: use optparse
89    for arg in sys.argv[1:]:
90        if state==0:
91            if arg=="--evaluate":
92                eval=True
93            else:
94                inputf=arg
95                state=1
96            continue
97        if state==1:
98            outputf=arg
99            continue
100        if state==2:
101            print "argument ignored:", arg
102    from context import Context
103    #inputf=sys.argv[1]
104    root=readFile(inputf)
105    builder=OMFromXMLBuilder()
106    context=Context()
107    context.addCDImplementation(arith1.implementation)
108    doc=builder.build(root)
109    if eval:
110        doc=context.evaluate(doc)
111    output=context.XMLEncodeObject(doc)
112    try:
113        out=open(outputf,"w")
114    except NameError:
115        print "no output file"
116        sys.exit(1)
117    out.write(output)
118    out.close()
119    #print repr(root.getAttribute("blabla"))
Note: See TracBrowser for help on using the repository browser.