0
|
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 cElementTree import ElementTree
|
|
19 |
except ImportError:
|
|
20 |
try:
|
|
21 |
from elementtree import ElementTree
|
|
22 |
except ImportError:
|
|
23 |
try:
|
|
24 |
from xml.etree import cElementTree as ElementTree
|
|
25 |
except ImportError:
|
|
26 |
from xml.etree import ElementTree
|
|
27 |
import StringIO
|
|
28 |
import os
|
|
29 |
|
|
30 |
from cone.public import exceptions, persistence
|
|
31 |
|
|
32 |
class Metadata(object):
|
|
33 |
"""
|
|
34 |
metadata container objectl, which is only a dictionary container
|
|
35 |
"""
|
|
36 |
META_ROOT_FILE = 'cpf.rootFile'
|
|
37 |
|
|
38 |
def __init__(self, copyobj=None):
|
|
39 |
"""
|
|
40 |
Constructor initializes the default values
|
|
41 |
"""
|
|
42 |
self.data = {}
|
|
43 |
if copyobj != None:
|
|
44 |
self.data = copyobj.data.copy()
|
|
45 |
pass
|
|
46 |
|
|
47 |
def get_root_file(self):
|
|
48 |
return self.data.get(self.META_ROOT_FILE,"")
|
|
49 |
|
|
50 |
def set_root_file(self,filename):
|
|
51 |
self.data[self.META_ROOT_FILE] = filename
|
|
52 |
|
|
53 |
class MetadataReader(persistence.ConeReader):
|
|
54 |
"""
|
|
55 |
Parses a single metadata file
|
|
56 |
"""
|
|
57 |
class_type = "Metadata"
|
|
58 |
NAMESPACES = ['http://www.nokia.com/xml/ns/confml-core/metadata-2.0']
|
|
59 |
def __init__(self):
|
|
60 |
return
|
|
61 |
|
|
62 |
def fromstring(self, xml_as_string):
|
|
63 |
meta = Metadata()
|
|
64 |
etree = ElementTree.fromstring(xml_as_string)
|
|
65 |
iter = etree.getiterator("{%s}property" % self.NAMESPACES[0])
|
|
66 |
for elem in iter:
|
|
67 |
(key,value) = self.get_property(elem)
|
|
68 |
meta.data[key] = value
|
|
69 |
return meta
|
|
70 |
|
|
71 |
def get_property(self, elem):
|
|
72 |
key = elem.get('name')
|
|
73 |
value = ''
|
|
74 |
if elem.get('value'): value = elem.get('value')
|
|
75 |
return (key,value)
|
|
76 |
|
|
77 |
class MetadataWriter(persistence.ConeWriter):
|
|
78 |
"""
|
|
79 |
Writes a single metadata file
|
|
80 |
"""
|
|
81 |
class_type = "Metadata"
|
|
82 |
NAMESPACES = ['http://www.nokia.com/xml/ns/confml-core/metadata-2.0']
|
|
83 |
DEFAULT_ENCODING = "ASCII"
|
|
84 |
def __init__(self):
|
|
85 |
self.encoding = self.DEFAULT_ENCODING
|
|
86 |
return
|
|
87 |
|
|
88 |
def tostring(self,obj,indent=True):
|
|
89 |
stringdata = StringIO.StringIO()
|
|
90 |
self.toresource(obj, stringdata, indent)
|
|
91 |
return stringdata.getvalue()
|
|
92 |
|
|
93 |
def toresource(self,obj,res,indent=True):
|
|
94 |
root = ElementTree.Element("metadata")
|
|
95 |
root.set('xmlns',self.NAMESPACES[0])
|
|
96 |
if not obj.__class__ == Metadata:
|
|
97 |
raise exceptions.IncorrectClassError('The given object is not a instance of %s' % Metadata)
|
|
98 |
for key in obj.data.keys():
|
|
99 |
prop = ElementTree.SubElement(root,'property')
|
|
100 |
self.set_property(prop, key, obj.data[key])
|
|
101 |
if indent:
|
|
102 |
persistence.indent(root)
|
|
103 |
# some smarter way to implement adding of the encoding to the beginning of file
|
|
104 |
res.write('<?xml version="1.0" encoding="%s"?>%s' % (self.encoding,os.linesep))
|
|
105 |
ElementTree.ElementTree(root).write(res)
|
|
106 |
|
|
107 |
def set_property(self, elem, key, value):
|
|
108 |
elem.attrib['name'] = key
|
|
109 |
if value != '':
|
|
110 |
elem.attrib['value'] = value
|
|
111 |
return elem |