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 |
Configuration flattener
|
|
18 |
'''
|
|
19 |
|
|
20 |
|
|
21 |
import os
|
|
22 |
import sys
|
|
23 |
import logging
|
3
|
24 |
import pkg_resources
|
0
|
25 |
|
|
26 |
pkg_resources.require('lxml')
|
|
27 |
|
3
|
28 |
from cone.public import exceptions
|
0
|
29 |
|
|
30 |
class XsltTransformer():
|
|
31 |
"""
|
|
32 |
XSLT Transformer
|
|
33 |
"""
|
|
34 |
|
|
35 |
def _init(self,ref,configuration):
|
|
36 |
self.logger = logging.getLogger('cone.gcfml(%s)' % self.ref)
|
|
37 |
|
|
38 |
|
3
|
39 |
def transform_lxml(self, input, xslt, enc, linesep=os.linesep):
|
|
40 |
"""
|
|
41 |
XSLT transform with lxml.
|
|
42 |
"""
|
|
43 |
from lxml import etree
|
0
|
44 |
|
3
|
45 |
if not enc:
|
|
46 |
enc = sys.getdefaultencoding()
|
|
47 |
try:
|
|
48 |
xslt_doc = etree.parse(xslt)
|
|
49 |
transform = etree.XSLT(xslt_doc)
|
|
50 |
|
|
51 |
input_doc = etree.parse(input)
|
|
52 |
result = str(transform(input_doc))
|
|
53 |
postprocessed_result = post_process_result(result, enc, linesep)
|
|
54 |
return postprocessed_result
|
0
|
55 |
|
3
|
56 |
except Exception, e:
|
|
57 |
logging.getLogger('cone.gcfml').error('Failed to do XSLT transformation: %s' % e)
|
|
58 |
raise exceptions.ConeException('Failed to do XSLT transformation: %s' % (e))
|
0
|
59 |
|
|
60 |
|
|
61 |
def post_process_result(string, enc, linesep):
|
3
|
62 |
"""
|
|
63 |
Does post process for result from XSLT transform
|
|
64 |
- encoding
|
|
65 |
- removes extra line separators
|
|
66 |
- changes line separators
|
|
67 |
"""
|
|
68 |
output_string = None
|
|
69 |
|
|
70 |
try:
|
|
71 |
output_string = string.decode(enc)
|
|
72 |
if not output_string.startswith('<'):
|
|
73 |
output_string = '\n' + output_string
|
|
74 |
output_string = output_string.replace('<?xml version="1.0" encoding="UTF-16"?>', '<?xml version="1.0" encoding="UTF-16"?>\n\n')
|
|
75 |
output_string = output_string.replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="UTF-8"?>\n\n')
|
|
76 |
output_string = output_string.replace('\n\n','\n')
|
|
77 |
output_string = output_string.replace('\n', linesep)
|
|
78 |
output_string+= linesep
|
|
79 |
except Exception, e:
|
|
80 |
logging.getLogger('cone.gcfml').error('Cannot post process result: %s \nException: %s' % (string, e))
|
|
81 |
raise exceptions.ConeException('Cannot post process result: %s \nException: %s' % (string, e))
|
|
82 |
|
|
83 |
return output_string
|
0
|
84 |
|
|
85 |
|
|
86 |
|