configurationengine/source/cone/storage/configurationpersistence.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 from cone.public import persistence
       
    18  
       
    19 # list of namespaces supported. the first one in the list is always used in writing
       
    20 CONFIGURATION_NAMESPACES = ["http://www.s60.com/xml/confml/1"]
       
    21 INCLUDE_NAMESPACES       = ["http://www.w3.org/2001/xinclude","http://www.w3.org/2001/XInclude"]
       
    22 
       
    23 class ConfigurationReader(persistence.ConeReader):
       
    24     """
       
    25     Parses a single CPF configuration project root confml file. Parses the XInclude statements to 
       
    26     find out the layers inside the project
       
    27     """ 
       
    28 
       
    29     class_type = "Configuration"
       
    30 
       
    31     def __init__(self):
       
    32         self.configuration_namespaces = CONFIGURATION_NAMESPACES
       
    33         self.include_namespaces       = INCLUDE_NAMESPACES
       
    34             
       
    35     def fromstring(self, xml_as_string):
       
    36         configuration = api.CompositeConfiguration(None)
       
    37         etree = ElementTree.fromstring(xml_as_string)
       
    38         configuration.desc = self.parse_desc(etree)
       
    39         configuration.meta = self.parse_meta(etree)
       
    40         configuration.set_name(self.parse_name(etree))
       
    41         for inc in self.parse_includes(etree):
       
    42             configuration.add_layer(inc)
       
    43     
       
    44         return configuration
       
    45 
       
    46     def parse_includes(self,etree):
       
    47         include_elems = []
       
    48         include_elems.extend(etree.getiterator("{%s}include" % self.include_namespaces[0]))
       
    49         include_elems.extend(etree.getiterator("{%s}include" % self.include_namespaces[1]))
       
    50         includes = []
       
    51         for inc in include_elems:
       
    52             includes.append(inc.get('href').replace('#/',''))
       
    53         return includes
       
    54     
       
    55     def parse_meta(self,etree):
       
    56         meta_elem = etree.find("{%s}meta" % self.configuration_namespaces[0])
       
    57         meta = {}
       
    58         if meta_elem:      
       
    59             # There must be a nicer way to do this!! :(
       
    60             for elem in meta_elem.getiterator():
       
    61                 m = re.match("{.*}(?P<tagname>.*)",elem.tag)
       
    62                 if m and m.group('tagname') != 'meta':
       
    63                     meta[m.group('tagname')] = elem.text 
       
    64         return meta
       
    65      
       
    66     def parse_desc(self,etree):
       
    67         desc = ""
       
    68         desc_elem = etree.find("{%s}desc" % self.configuration_namespaces[0])
       
    69         if desc_elem != None:
       
    70             desc = desc_elem.text
       
    71         return desc
       
    72     
       
    73     def parse_name(self,etree):
       
    74         return etree.get("name")
       
    75 
       
    76 
       
    77 class ConfigurationWriter(persistence.ConeWriter):
       
    78     """
       
    79     Parses a single CPF configuration project root confml file. Parses the XInclude statements to 
       
    80     find out the layers inside the project
       
    81     """ 
       
    82 
       
    83     class_type = "Configuration"
       
    84 
       
    85     def __init__(self):
       
    86         self.configuration_namespace = CONFIGURATION_NAMESPACES[0]
       
    87         self.include_namespace       = INCLUDE_NAMESPACES[0]
       
    88     
       
    89     def tostring(self,configuration,indent=True):
       
    90         root = ElementTree.Element("configuration")
       
    91         root.set("xmlns",self.configuration_namespace)
       
    92         root.set("xmlns:xi",self.include_namespace)
       
    93         root.set("name",configuration.ref) 
       
    94         root.append(self.to_desc(configuration.desc))
       
    95         root.append(self.to_meta(configuration.meta))
       
    96         for inc in configuration.list_layers():
       
    97             root.append(self.to_include(inc))
       
    98         if indent:
       
    99             self.indent(root)
       
   100         return ElementTree.tostring(root)
       
   101  
       
   102     def to_desc(self,desc):
       
   103         elem = ElementTree.Element("desc")
       
   104         elem.text = desc
       
   105         return elem 
       
   106       
       
   107     def to_meta(self,meta):
       
   108         elem = ElementTree.Element("meta")
       
   109         for key in meta.keys():
       
   110             selem = ElementTree.SubElement(elem,key)
       
   111             selem.text = meta[key]
       
   112         return elem 
       
   113     
       
   114     def to_include(self,include):
       
   115         elem = ElementTree.Element("xi:include")
       
   116         elem.set("href",include)
       
   117         return elem 
       
   118