Changeset 4976


Ignore:
Timestamp:
Jan 25, 2008, 5:58:17 PM (16 years ago)
Author:
ole
Message:

Work on XML Objects
Tests are disabled because they produce output to screen, but it is getting
there

Location:
anuga_core/source/anuga/utilities
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/mainland_only.lic

    r4963 r4976  
    11<?xml version="1.0" encoding="iso-8859-1"?>
    22
    3   <ga_license_file>
    4     <metadata>
    5       <author>Ole Nielsen</author>
    6       <svn_keywords>
    7         <author>$Author$</author> 
    8         <date>$Date$</date>
    9         <revision>$Revision$</revision>
    10         <url>$URL$</url>
    11         <id>$Id$</id>
    12       </svn_keywords>
    13     </metadata>
    14     <datafile>
    15       <filename>mainland_only.csv</filename>
    16       <checksum>-1661725548</checksum>
    17       <publishable>No</publishable>
    18       <accountable>Jane Sexton</accountable>
    19       <source>Unknown</source>
    20       <IP_owner>Geoscience Australia</IP_owner>
    21       <IP_info>This is a polygon comprising easting and northing locations
    22       tracing parts of the coastline at Dampier WA as well as a rectangular area inland.
    23       This is used to specifically set the onshore initial condition in a tsunami scenario
    24       and here, it is used with a unit test in test_polygon.py.
    25      
    26       The coastline was derived from Maritime Boundaries which is a public dataset. However,
    27       rumour has it that some of it was digitised from a Landgate supplied image.
    28      
    29       The origin and license issues are still undecided</IP_info>
    30     </datafile>
     3<ga_license_file>
     4  <metadata>
     5    <author>Ole Nielsen</author>
     6    <svn_keywords>
     7      <author>$Author$</author> 
     8      <date>$Date$</date>
     9      <revision>$Revision$</revision>
     10      <url>$URL$</url>
     11      <id>$Id$</id>
     12    </svn_keywords>
     13  </metadata>
     14  <datafile>
     15    <filename>mainland_only.csv</filename>
     16    <checksum>-1661725548</checksum>
     17    <publishable>No</publishable>
     18    <accountable>Jane Sexton</accountable>
     19    <source>Unknown</source>
     20    <IP_owner>Geoscience Australia</IP_owner>
     21    <IP_info>This is a polygon comprising easting and northing locations
     22    tracing parts of the coastline at Dampier WA as well as a rectangular area inland.
     23    This is used to specifically set the onshore initial condition in a tsunami scenario
     24    and here, it is used with a unit test in test_polygon.py.
     25   
     26    The coastline was derived from Maritime Boundaries which is a public dataset. However,
     27    rumour has it that some of it was digitised from a Landgate supplied image.
     28   
     29    The origin and license issues are still undecided</IP_info>
     30  </datafile>
    3131
    32   </ga_license_file>
     32</ga_license_file>
  • anuga_core/source/anuga/utilities/xml_tools.py

    r4963 r4976  
    6161    if len(s)>0: s = s[:-2]
    6262    return s
     63
     64
     65
     66
     67
     68#----------------------------
     69# XML object model
     70#----------------------------
     71
     72class XML_element:
     73    def __init__(self, tag=None, contents=None):
     74        """
     75        contents can be either
     76          * An XML_element
     77          * a list of XML_elements
     78          * a text string
     79       
     80        """
     81       
     82        if isinstance(contents, XML_element):
     83            contents = [contents]
     84           
     85        self.elements = contents
     86
     87        self.tag = tag
     88
     89    def __add__(self, other):
     90        return str(self) + str(other)
     91
     92    def __radd__(self, other):
     93        return str(other) + str(self)    #Python swaps self and other   
     94
     95    def __repr__(self):
     96        return str(self)
     97
     98    def __str__(self):
     99        pass
     100
     101    def __str__(self, indent=0):
     102
     103        s = tab = ' '*indent
     104       
     105        s += '<%s>' %self.tag
     106        if isinstance(self.elements, basestring):
     107            s += self.elements
     108        else:
     109            s += '\n'
     110            for e in self.elements:
     111                s += e.__str__(indent+4)
     112            s += tab
     113
     114        s += '</%s>\n' %self.tag
     115        return s
     116
     117   
     118
     119    def pretty_print(self, indent=0):
     120        """Print the document without tags using indentation
     121        """
     122
     123        s = tab = ' '*indent
     124        s += '%s: ' %self.tag       
     125        if isinstance(self.elements, basestring):
     126            s += self.elements
     127        else:
     128            s += '\n'
     129            for e in self.elements:
     130                s += e.pretty_print(indent+4)
     131        s += '\n'
     132       
     133        return s
     134   
     135   
     136   
     137class XML_document(XML_element):
     138
     139    def __init__(self, contents=None, version='1.0', encoding='iso-8859-1'):
     140        tag = '?xml version="%s" encoding="%s"' %(version, encoding)
     141
     142        XML_element.__init__(self,
     143                             tag=tag,
     144                             contents=contents)
     145
     146
     147    def __str__(self):
     148        """Generate XML representation for use with xml2object function
     149        """
     150        s = '<%s>\n' %self.tag
     151        for e in self.elements:
     152            s += str(e)
     153        return s
     154
     155    def pretty_print(self):
     156        s = ''
     157        for e in self.elements:
     158            s += e.pretty_print()
     159
     160        return s
     161
     162
     163def xml2object(xml, verbose=False):
     164    """Generate XML object model from XML file or XML text
     165
     166    This is the inverse operation to the __str__ representation.
     167
     168    Input xml can be either an
     169    * xml string ??
     170    * xml file
     171    * open xml file object
     172
     173    Return XML_document instance.
     174    """
     175
     176    #FIXME(Ole): Do the input tests
     177    #Assume open file object for now
     178
     179    #fid = open(xml)
     180    dom = parse(xml)
     181
     182    print 'Res',    dom2object(dom)
     183
     184
     185
     186def dom2object(node):
     187
     188    if node.nodeType == 3 and len(node.nodeValue.strip()) == 0:
     189        return None
     190       
     191    print 'Node name: "%s",' %node.nodeName,\
     192          'Node type: "%s",' %node.nodeType,\
     193          'Node value: "%s",' %str(node.nodeValue).strip(),\
     194          'Node children: %d' %len(node.childNodes)
     195
     196   
     197
     198    if node.nodeType == 3:
     199        contents = node.nodeValue.strip()
     200    else:   
     201        contents = []
     202        for n in node.childNodes:
     203            x = dom2object(n)
     204            if x is not None:
     205                contents.append(x)
     206
     207
     208    if node.nodeType == 9:
     209        # Document
     210        X = XML_document()
     211       
     212    X = XML_element(tag=node.nodeName,
     213                    contents=contents)
     214    return X
Note: See TracChangeset for help on using the changeset viewer.