source: TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/ETxmlView.py @ 963

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XSLT/browse/portal/cgi/ETxmlView.py@963
Revision 963, 2.8 KB checked in by lawrence, 13 years ago (diff)

Coding to support showing full xml documents etc in browse, note
not tested as underlying web services not workign at moment.

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
7
8import ElementTree as ET
9
10def et2text(elem,indent='',html=0,space='   '):
11        '''Lightweight pretty printing of elementTree elements'''
12        def estrip(elem):
13                ''' Just want to get rid of unwanted whitespace '''
14                if elem is None:
15                        return ''
16                else:
17                        return elem.strip()
18        strAttrib=''
19        for att in elem.attrib:
20                strAttrib+=' %s="%s"'%(att,elem.attrib[att])
21        result='%s<%s%s>%s'%(indent,elem.tag,strAttrib,estrip(elem.text))
22        children=len(elem)
23        if children:
24                for item in elem:
25                        result+='\n'+PrettyPrint(item,indent=indent+space)
26                result+='\n%s%s</%s>'%(indent,estrip(item.tail),elem.tag)
27        else:
28                result+='</%s>'%(elem.tag)
29        return result
30
31
32def et2html(elem,matchList=[],number=0):
33    #this method is NDG code ... copyright CCLRC ...
34    '''Lightweight HTML pretty printing of elementTree elements + highlight
35    any words which occur in the element text (and tails) which occur in matchList,
36    and formatted using a css something like this:
37    ===
38    DIV.xmlElem {PADDING-LEFT: 20px;}
39    .xmlAttrVal {COLOR:Red; }
40    .xmlAttrTyp {COLOR:Green; }
41    .xmlElemTag {COLOR:Blue; }
42.   highlight {BACKGROUND-COLOR:Yellow; }
43    ===
44    Line number is not yet implemented.
45    '''
46    def span(x,c): return '<span class="%s">%s</span>'%(c,x)
47    def div(x,c): return '<div class="%s">%s</div>'%(c,x)
48    def match(x): 
49        if x is None: return ''
50        for w in matchList: x=re.sub(w,span(w,'highlight'),x)
51        return x
52    lt,gt='<b>&lt;</b>','<b>&gt;</b>'
53    strAttrib=''
54    for att in elem.attrib:
55        strAttrib+=' %s="%s"'%(span(att,'xmlAttrTyp'),span(elem.attrib[att],'xmlAttrVal'))
56    result='%s%s%s%s%s'%(lt,span(elem.tag,"xmlElemTag"),strAttrib,gt,match(elem.text))
57    children=len(elem)
58    if children:
59        for item in elem:
60            result+=PrettyPrintHTML(item,matchList)
61        result+='%s%s/%s%s'%(match(item.tail),lt,span(elem.tag,'xmlElemTag'),gt)
62    else:
63        result+='%s/%s%s'%(lt,span(elem.tag,'xmlElemTag'),gt)
64    return div(result,'xmlElem')
65
66def xmlCleanup(string):
67    '''This is a hack to make sure that xml doesn't contain any naughty & characters alone
68    (typically from URL copies)'''
69    #match ? but not (?! if it's followed by alphanumeric \w characters + and a semicolon ;
70    #don't want to use urlencode cos we don't know where it came from ...
71    return re.sub(r'&(?!\w+;)', '&amp;', string)
72
73def xml2text(xmlString):
74    tree=ET.fromstring(xmlCleanup(xmlString))
75    return et2text(tree)
76
77def xml2HTML(xmlString,**kw):
78    tree=ET.fromstring(xmlCleanup(xmlString))
79    return et2HTML(tree,**kw)
Note: See TracBrowser for help on using the repository browser.