|
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 re |
|
22 import os |
|
23 import sys |
|
24 import codecs |
|
25 import logging |
|
26 import xml.parsers.expat |
|
27 import unittest, os, sys, pkg_resources |
|
28 |
|
29 pkg_resources.require('lxml') |
|
30 |
|
31 try: |
|
32 from cElementTree import ElementTree |
|
33 except ImportError: |
|
34 try: |
|
35 from elementtree import ElementTree |
|
36 except ImportError: |
|
37 try: |
|
38 from xml.etree import cElementTree as ElementTree |
|
39 except ImportError: |
|
40 from xml.etree import ElementTree |
|
41 |
|
42 import __init__ |
|
43 |
|
44 from cone.public import exceptions,plugin,utils,api |
|
45 |
|
46 class XsltTransformer(): |
|
47 """ |
|
48 XSLT Transformer |
|
49 """ |
|
50 |
|
51 def _init(self,ref,configuration): |
|
52 self.logger = logging.getLogger('cone.gcfml(%s)' % self.ref) |
|
53 |
|
54 |
|
55 def transform_lxml(self, input, xslt, output, enc, linesep=os.linesep): |
|
56 """ |
|
57 XSLT transform with lxml. |
|
58 """ |
|
59 from lxml import etree |
|
60 |
|
61 if not enc: |
|
62 enc = sys.getdefaultencoding() |
|
63 try: |
|
64 xslt_doc = etree.parse(xslt) |
|
65 transform = etree.XSLT(xslt_doc) |
|
66 |
|
67 input_doc = etree.parse(input) |
|
68 result = str(transform(input_doc)) |
|
69 postprocessed_result = post_process_result(result, enc, linesep) |
|
70 |
|
71 if not filter_file_writing(postprocessed_result): |
|
72 write_string_to_file(postprocessed_result, output, enc) |
|
73 |
|
74 except Exception, e: |
|
75 logging.getLogger('cone.gcfml').error('Failed to do XSLT transformation: %s' % e) |
|
76 raise exceptions.ConeException('Failed to do XSLT transformation: %s' % (e)) |
|
77 |
|
78 |
|
79 def transform_4s(self, input, xslt, output, enc, linesep=os.linesep): |
|
80 """ |
|
81 XSLT transform with 4Suite |
|
82 """ |
|
83 from Ft.Xml.Xslt import Transform |
|
84 from Ft.Xml.Xslt import Processor |
|
85 from Ft.Xml import InputSource |
|
86 from Ft.Lib.Uri import OsPathToUri |
|
87 |
|
88 |
|
89 if not enc: |
|
90 enc = sys.getdefaultencoding() |
|
91 |
|
92 try: |
|
93 processor = Processor.Processor() |
|
94 |
|
95 srcAsUri = OsPathToUri(input) |
|
96 source = InputSource.DefaultFactory.fromUri(srcAsUri) |
|
97 |
|
98 ssAsUri = OsPathToUri(xslt) |
|
99 transform = InputSource.DefaultFactory.fromUri(ssAsUri) |
|
100 |
|
101 processor.appendStylesheet(transform) |
|
102 result = processor.run(source) |
|
103 |
|
104 postprocessed_result = post_process_result(result, enc, linesep) |
|
105 |
|
106 if not filter_file_writing(postprocessed_result): |
|
107 write_string_to_file(postprocessed_result, output, enc) |
|
108 |
|
109 except Exception, e: |
|
110 logging.getLogger('cone.gcfml').error('Failed to do XSLT transformation: %s' % e) |
|
111 raise exceptions.ConeException('Failed to do XSLT transformation: %s' % (e)) |
|
112 |
|
113 def filter_file_writing(string): |
|
114 """ |
|
115 Returns True if writing result file should be ignored. |
|
116 """ |
|
117 string = string.rstrip('\n\r') |
|
118 if string == '' or string == '<?xml version="1.0" encoding="UTF-16"?>' or \ |
|
119 string == '<?xml version="1.0" encoding="UTF-8"?>': |
|
120 return True |
|
121 |
|
122 return False |
|
123 |
|
124 |
|
125 def post_process_result(string, enc, linesep): |
|
126 """ |
|
127 Does post process for result from XSLT transform |
|
128 - encoding |
|
129 - removes extra line separators |
|
130 - changes line separators |
|
131 """ |
|
132 output_string = None |
|
133 |
|
134 try: |
|
135 output_string = string.decode(enc) |
|
136 if not output_string.startswith('<'): |
|
137 output_string = '\n' + output_string |
|
138 output_string = output_string.replace('<?xml version="1.0" encoding="UTF-16"?>', '<?xml version="1.0" encoding="UTF-16"?>\n\n') |
|
139 output_string = output_string.replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="UTF-8"?>\n\n') |
|
140 output_string = output_string.replace('\n\n','\n') |
|
141 output_string = output_string.replace('\n', linesep) |
|
142 output_string+= linesep |
|
143 except Exception, e: |
|
144 logging.getLogger('cone.gcfml').error('Cannot post process result: %s \nException: %s' % (string, e)) |
|
145 raise exceptions.ConeException('Cannot post process result: %s \nException: %s' % (string, e)) |
|
146 |
|
147 return output_string |
|
148 |
|
149 def write_string_to_file(string, output, enc): |
|
150 """ |
|
151 Writes string to file |
|
152 """ |
|
153 try: |
|
154 outfile = os.path.abspath(output) |
|
155 |
|
156 if not os.path.exists(os.path.dirname(outfile)): |
|
157 os.makedirs(os.path.dirname(outfile)) |
|
158 |
|
159 fl = codecs.open(outfile, 'w', enc) |
|
160 fl.write(string) |
|
161 fl.close() |
|
162 |
|
163 except Exception, e: |
|
164 logging.getLogger('cone.gcfml').error('Cannot write Element to file (%s). Exception: %s' % (output, e)) |
|
165 raise exceptions.ConeException('Cannot write Element to file (%s). Exception: %s' % (output, e)) |
|
166 |
|
167 |
|
168 |