source: exist/trunk/python/ndgUtils/ETxmlView.py @ 3125

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/ETxmlView.py@3125
Revision 3125, 8.0 KB checked in by lawrence, 12 years ago (diff)

ndgUtils taking shape with test cases etc

Line 
1# Copyright Bryan Lawrence, Rutherford Appleton Laboratory, CCLRC, 2006
2#
3# This code is made available under the GPL, if you don't know what
4# that means, you have no rights to copy it or use it!
5#
6# Code to support xml and elementTree viewing as text and html
7try: #python 2.5
8    from xml.etree import ElementTree as ET
9except ImportError:
10    try:
11        # if you've installed it yourself it comes this way
12        import ElementTree as ET
13    except ImportError:
14        # if you've egged it this is the way it comes
15        from elementtree import ElementTree as ET
16import re
17
18class subAI:
19    ''' This is Alan Iwi's substitute and replace orphan <> and & code '''
20    def __init__(self):
21        self.r1=re.compile('<([^>]*(<|$))')
22        self.r2=re.compile('((^|>)[^<]*)>')
23        self.r3=re.compile('&(?!(amp|lt|gt);)')
24    def sub(self,s):
25        old=''
26        s=self.r3.sub(r'&amp;',s)
27        s=s.replace(';amp',';')
28        while s != old:
29            old=s
30            s=self.r1.sub(r'&lt;\1',s)
31            s=self.r2.sub(r'\1&gt;',s)
32        return s
33       
34class nsdumb:
35    ''' provides an xpath interface to element tree nodes which
36    is namespace agnostic '''
37    def __init__(self,root=None,encoding='utf-8'):
38        ''' Provide a root element with namespace definitions when
39        instantiatin '''
40        self.encoding=encoding
41        self.cleanup=subAI()
42        if root is None: 
43            self.xmlns=''
44            return
45        ns=['xmlns','{http://www.w3.org/2001/XMLSchema-instance}schemaLocation']
46       
47        for i in ns: 
48            if i in root.keys():
49                self.xmlns='{%s}'%root.attrib[i].split(' ')[0]
50                break
51            else:
52                if root.tag[0]<>'{': 
53                    self.xmlns=''
54                else:
55                    ns,local=root.tag.split('}')
56                    self.xmlns='{%s}'%ns[1:]
57       
58    def __str__(self):
59        return 'Element Tree namespace helper with namespace: [%s]'%self.xmlns
60   
61    def __distributens(self,xpathExpression):
62        ''' Actually we only support tag finding in this '''
63        tags=xpathExpression.split('/')
64        new=['%s%s'%(self.xmlns,i) for i in tags]
65        return '/'.join(new)
66    def getText(self,elem,xpathExpression,multiple=0):
67        ''' Get a text object sensibly '''
68        if elem is None: 
69            if multiple:
70                return ['',]
71            else: return '' 
72        if multiple:
73                r=elem.findall(self.__distributens(xpathExpression))
74        else:
75                r=[elem.find(self.__distributens(xpathExpression)),]
76        rr=[]
77        for i in r:
78            if i is not None: 
79                rr.append(self.cleanup.sub(i.text or ''))
80            else: rr.append('') 
81        if multiple: 
82                return rr
83        else: return rr[0]
84       
85    def find(self,elem,xpathExpression):
86       ''' Return relevant subelement '''
87       if elem is None: return ''
88       xe=self.__distributens(xpathExpression)
89       return elem.find(xe)
90   
91    def findall(self,elem,xpathExpression):
92       ''' Return all relevant subelements '''
93       if elem is None: return []
94       xe=self.__distributens(xpathExpression)
95       return elem.findall(xe)
96
97    def strip(self,tag):
98        ''' Given a tag, strip the default namespace '''
99        return tag.lstrip(self.xmlns)
100
101def et2text(elem,indent='',html=0,space='   ',helper=None):
102        '''Lightweight pretty printing of elementTree elements'''
103        def estrip(elem):
104                ''' Just want to get rid of unwanted whitespace '''
105                if elem is None:
106                        return ''
107                else:
108                        return elem.strip()
109        ns=helper
110        if ns is None: ns=nsdumb(elem)
111        strAttrib=''
112        for att in elem.attrib:
113                strAttrib+=' %s="%s"'%(att,elem.attrib[att])
114        result='%s<%s%s>%s'%(indent,ns.strip(elem.tag),strAttrib,estrip(elem.text))
115        children=len(elem)
116        if children:
117                for item in elem:
118                        result+='\n'+et2text(item,indent=indent+space,helper=ns)
119                result+='\n%s%s</%s>'%(indent,estrip(item.tail),ns.strip(elem.tag))
120        else:
121                result+='</%s>'%(ns.strip(elem.tag))
122        return result
123
124
125def et2html(elem,matchList=[],number=0,helper=None):
126    #this method is NDG code ... copyright CCLRC ...
127    '''Lightweight HTML pretty printing of elementTree elements + highlight
128    any words which occur in the element text (and tails) which occur in matchList,
129    and formatted using a css something like this:
130    ===
131    DIV.xmlElem {PADDING-LEFT: 20px;}
132    .xmlAttrVal {COLOR:Red; }
133    .xmlAttrTyp {COLOR:Green; }
134    .xmlElemTag {COLOR:Blue; }
135.   highlight {BACKGROUND-COLOR:Yellow; }
136    ===
137    Line number is not yet implemented.
138    '''
139    def span(x,c): return '<span class="%s">%s</span>'%(c,x)
140    def div(x,c): return '<div class="%s">%s</div>'%(c,x)
141    def match(x): 
142        if x is None: return ''
143        for w in matchList: x=re.sub(w,span(w,'highlight'),x)
144        return x
145    lt,gt='<b>&lt;</b>','<b>&gt;</b>'
146    ns=helper
147    if ns is None: ns=nsdumb(elem) 
148    strAttrib=''
149    for att in elem.attrib:
150        strAttrib+=' %s="%s"'%(span(att,'xmlAttrTyp'),span(elem.attrib[att],'xmlAttrVal'))
151    result='%s%s%s%s%s'%(lt,span(ns.strip(elem.tag),"xmlElemTag"),strAttrib,gt,match(elem.text))
152    children=len(elem)
153    if children:
154        for item in elem:
155            result+=et2html(item,matchList,helper=ns)
156        result+='%s%s/%s%s'%(match(item.tail),lt,span(ns.strip(elem.tag),'xmlElemTag'),gt)
157    else:
158        result+='%s/%s%s'%(lt,span(ns.strip(elem.tag),'xmlElemTag'),gt)
159    return div(result,'xmlElem')
160   
161def loadET(inputString):
162    ''' This method returns an elementtree object after some cleaning
163    of the string, essentially a hack to make sure that xml doesn't contain any
164    naughty & characters alone (typically from URL copies), or unescaped orphan
165    < or > signs ... and that the unicode has been processed to something that
166    might work'''
167   
168    if inputString is None: return None
169    inputString=re.sub(r'&(?!\w+;)', '&amp;', inputString)
170 
171    # first just try and do it so we don't waste time if we don't need to ...
172    try:
173        elem=ET.fromstring(inputString)
174        return elem
175    except:
176        pass
177        # and carry on
178
179    #ok, let's deal with orphan > and < signs then ...
180    subtool=subAI()
181    s=subtool.sub(inputString)
182
183    #now let's sort out an encoding
184    encodings=['utf-8','latin-1','iso-8859-1','ascii',]
185    elem=None
186    for option in encodings:
187        try:
188            s=s.encode(option,'replace')
189            try:
190                elem=ET.fromstring(s)
191            except:
192                s=re.sub('\n','<br/>LINE: ',s)
193                print '<p>%s</p>'%s
194                raise
195        except UnicodeError:
196            pass
197        else:
198            break
199    return elem
200
201def xml2text(xmlString):
202    tree=loadET(xmlString)
203    return et2text(tree)
204
205def xml2HTML(xmlString,**kw):
206    tree=loadET(xmlString)
207    return et2HTML(tree,**kw)
208
209import unittest
210from os.path import splitext
211
212class TestCase(unittest.TestCase):
213    """
214    """
215    def setUp(self):
216        ''' Load example files for testing, and get pointers to their dataset identifiers '''
217        inputFiles = ['examples/neodc.eg1.dif','examples/ukho.eg1.mdip',
218                      'examples/bodc.eg2.edmed.dif']
219        indexes={'.dif':'Entry_ID','.mdip':'DatasetIdentifier'}
220        results=['NOCSDAT192','RSDRA2006000377384','grid.bodc.nerc.ac.uk__DIF__EDMED1048006']
221        ids=[]
222        for f in inputFiles:
223            ps=splitext(f)
224            ids.append(indexes[ps[1]])
225        files=[file(i,'r') for i in inputFiles]
226        self.strings=[i.read() for i in files]
227        self.ids=ids
228        self.results=results
229
230    def testEntries(self):
231        ''' Testing the file objects can be loaded using loadET'''
232        for s in self.strings:
233            tree=loadET(s)
234            #for i in tree: print i.tag
235           
236    def testnsDump(self):
237        ''' Test that nsdumb can mediate access to identifiers '''
238        for i in range(len(self.strings)):
239            tree=loadET(self.strings[i])
240            helper=nsdumb(tree)
241            self.assertEqual(helper.getText(tree,self.ids[i]),self.results[i])
242       
243
244if __name__=="__main__":
245    unittest.main()
Note: See TracBrowser for help on using the repository browser.