configurationengine/source/cone/confml/confmltree.py
changeset 0 2e8eeb919028
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 try:
       
    18     from elementtree.ElementTree import *
       
    19 except ImportError:
       
    20     from xml.etree.ElementTree import *
       
    21 
       
    22 
       
    23 class ElementTreeNs(ElementTree):
       
    24     """ 
       
    25     A class inherited from elementtree.ElementTree that can write
       
    26     xml elements with qualified names. The xml namespaces that are defined 
       
    27     to the self.namespases are written out to the root element of the given
       
    28     root element.
       
    29     
       
    30     Example:
       
    31     
       
    32     elem = ElementTreeNs.Element('{http://www.test.com}test') 
       
    33     etreens = ElementTreeNs(elem, None, {'foo' : 'http://www.test.com'})
       
    34     outf = open('test.xml','w')
       
    35     ElementTreeNs.write(outf)
       
    36     
       
    37     content of test.xml would be
       
    38     <foo:test xmlns:foo="http://www.test.com"/>
       
    39     """
       
    40     def __init__(self, element=None, file=None, namespaces={}):
       
    41         ElementTree.__init__(self, element, file)
       
    42         self.namespaces = namespaces
       
    43 
       
    44     def write(self, file, encoding="us-ascii"):
       
    45         """
       
    46         Writes the element tree to a file, as XML with the existing namespaces
       
    47         
       
    48         @param file A file name, or a file object opened for writing.
       
    49         @param encoding Optional output encoding (default is US-ASCII).
       
    50         """
       
    51         assert self._root is not None
       
    52         if not hasattr(file, "write"):
       
    53             file = open(file, "wb")
       
    54         if not encoding:
       
    55             encoding = "us-ascii"
       
    56         elif encoding != "utf-8" and encoding != "us-ascii":
       
    57             file.write("<?xml version='1.0' encoding='%s'?>\n" % encoding)
       
    58         # set the namespaces for the root element
       
    59         for ns in self.namespaces:
       
    60             self._root.set('xmlns:%s' % self.namespaces[ns], ns)
       
    61         self._write(file, self._root, encoding, self.namespaces)
       
    62 
       
    63 
       
    64 def tostring(element, namespaces, encoding=None):
       
    65     """
       
    66     """
       
    67     class dummy:
       
    68         pass
       
    69     data = []
       
    70     file = dummy()
       
    71     file.write = data.append
       
    72     ElementTreeNs(element, None, namespaces).write(file, encoding)
       
    73     return "".join(data)
       
    74