OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Convert Android xml resources to API 14 compatible. | 7 """Convert Android xml resources to API 14 compatible. |
8 | 8 |
9 There are two reasons that we cannot just use API attributes, | 9 There are two reasons that we cannot just use API attributes, |
10 so we are generating another set of resources by this script. | 10 so we are generating another set of resources by this script. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
56 def IterateXmlElements(node): | 56 def IterateXmlElements(node): |
57 """minidom helper function that iterates all the element nodes. | 57 """minidom helper function that iterates all the element nodes. |
58 Iteration order is pre-order depth-first.""" | 58 Iteration order is pre-order depth-first.""" |
59 if node.nodeType == node.ELEMENT_NODE: | 59 if node.nodeType == node.ELEMENT_NODE: |
60 yield node | 60 yield node |
61 for child_node in node.childNodes: | 61 for child_node in node.childNodes: |
62 for child_node_element in IterateXmlElements(child_node): | 62 for child_node_element in IterateXmlElements(child_node): |
63 yield child_node_element | 63 yield child_node_element |
64 | 64 |
65 | 65 |
66 def GenerateV14Resource(input_filename, output_filename): | 66 def GenerateV14StyleResource(dom, output_file): |
67 """Convert resource to API 14 compatible resource. | 67 """Convert style resource to API 14 compatible style resource. |
68 | |
69 It's mostly a simple replacement, s/Start/Left s/End/Right, | |
70 on the attribute names specified by <item> element. | |
71 """ | |
72 for style_element in dom.getElementsByTagName('style'): | |
73 for item_element in style_element.getElementsByTagName('item'): | |
74 namespace, name = item_element.attributes['name'].value.split(':') | |
75 # Note: namespace == 'android' is not precise because | |
newt (away)
2013/05/06 20:55:55
agreed, checking for "android" is plenty. if we st
| |
76 # we are looking for 'http://schemas.android.com/apk/res/android' and | |
77 # 'android' can be aliased to another name in layout xml files where | |
78 # this style is used. e.g. xmlns:android="http://crbug.com/". | |
79 if namespace == 'android' and name in ATTRIBUTES_TO_MAP: | |
80 mapped_name = ATTRIBUTES_TO_MAP[name] | |
81 item_element.attributes['name'] = namespace + ':' + mapped_name | |
82 | |
83 build_utils.MakeDirectory(os.path.dirname(output_file)) | |
84 with open(output_file, 'w') as f: | |
85 dom.writexml(f, '', ' ', '\n', encoding='utf-8') | |
86 | |
87 | |
88 def GenerateV14LayoutResource(dom, output_file): | |
89 """Convert layout resource to API 14 compatible layout resource. | |
68 | 90 |
69 It's mostly a simple replacement, s/Start/Left s/End/Right, | 91 It's mostly a simple replacement, s/Start/Left s/End/Right, |
70 on the attribute names. | 92 on the attribute names. |
71 """ | 93 """ |
72 dom = minidom.parse(input_filename) | |
73 | |
74 for element in IterateXmlElements(dom): | 94 for element in IterateXmlElements(dom): |
75 all_names = element.attributes.keysNS() | 95 all_names = element.attributes.keysNS() |
76 | 96 |
77 # Iterate all the attributes to find attributes to convert. | 97 # Iterate all the attributes to find attributes to convert. |
78 # Note that name variable is actually a tuple that has namespace and name. | 98 # Note that name variable is actually a tuple that has namespace and name. |
79 # For example, | 99 # For example, |
80 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') | 100 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') |
81 for name, value in list(element.attributes.itemsNS()): | 101 for name, value in list(element.attributes.itemsNS()): |
82 # Note: gravity attributes are not necessary to convert because | 102 # Note: gravity attributes are not necessary to convert because |
83 # start/end values are backward-compatible. Explained at | 103 # start/end values are backward-compatible. Explained at |
(...skipping 12 matching lines...) Expand all Loading... | |
96 # setAttributeNS. Hence this workaround. | 116 # setAttributeNS. Hence this workaround. |
97 # This is a similar bug discussion about minidom namespace normalizing. | 117 # This is a similar bug discussion about minidom namespace normalizing. |
98 # http://stackoverflow.com/questions/863774/how-to-generate-xml-document s-with-namespaces-in-python | 118 # http://stackoverflow.com/questions/863774/how-to-generate-xml-document s-with-namespaces-in-python |
99 element.setAttribute('android:' + mapped_name[1], value) | 119 element.setAttribute('android:' + mapped_name[1], value) |
100 del element.attributes[name] | 120 del element.attributes[name] |
101 elif name in ATTRIBUTES_TO_MAP_NS_VALUES: | 121 elif name in ATTRIBUTES_TO_MAP_NS_VALUES: |
102 # TODO(kkimlabs): Enable warning once layouts have been converted | 122 # TODO(kkimlabs): Enable warning once layouts have been converted |
103 # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' | 123 # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' |
104 pass | 124 pass |
105 | 125 |
106 build_utils.MakeDirectory(os.path.dirname(output_filename)) | 126 build_utils.MakeDirectory(os.path.dirname(output_file)) |
107 with open(output_filename, 'w') as f: | 127 with open(output_file, 'w') as f: |
108 dom.writexml(f, ' ', '\n', encoding='utf-8') | 128 dom.writexml(f, '', ' ', '\n', encoding='utf-8') |
109 | 129 |
110 | 130 |
111 def GenerateV14ResourcesInDir(input_dir, output_dir): | 131 def GenerateV14XmlResourcesInDir(input_dir, output_dir): |
112 """Convert resources to API 14 compatible XML resources in the directory.""" | 132 """Convert resources to API 14 compatible XML resources in the directory.""" |
113 for input_file in build_utils.FindInDirectory(input_dir, '*.xml'): | 133 for input_file in build_utils.FindInDirectory(input_dir, '*.xml'): |
114 output_path = os.path.join(output_dir, | 134 output_file = os.path.join(output_dir, |
115 os.path.relpath(input_file, input_dir)) | 135 os.path.relpath(input_file, input_dir)) |
116 GenerateV14Resource(input_file, output_path) | 136 dom = minidom.parse(input_file) |
137 root_element = IterateXmlElements(dom).next() | |
138 if root_element.nodeName == 'resources': | |
139 if root_element.getElementsByTagName('style'): | |
140 GenerateV14StyleResource(dom, output_file) | |
141 else: | |
142 GenerateV14LayoutResource(dom, output_file) | |
117 | 143 |
118 | 144 |
119 def ParseArgs(): | 145 def ParseArgs(): |
120 """Parses command line options. | 146 """Parses command line options. |
121 | 147 |
122 Returns: | 148 Returns: |
123 An options object as from optparse.OptionsParser.parse_args() | 149 An options object as from optparse.OptionsParser.parse_args() |
124 """ | 150 """ |
125 parser = optparse.OptionParser() | 151 parser = optparse.OptionParser() |
126 parser.add_option('--res-dir', | 152 parser.add_option('--res-dir', |
(...skipping 22 matching lines...) Expand all Loading... | |
149 build_utils.MakeDirectory(options.res_v14_dir) | 175 build_utils.MakeDirectory(options.res_v14_dir) |
150 | 176 |
151 for name in os.listdir(options.res_dir): | 177 for name in os.listdir(options.res_dir): |
152 if not os.path.isdir(os.path.join(options.res_dir, name)): | 178 if not os.path.isdir(os.path.join(options.res_dir, name)): |
153 continue | 179 continue |
154 | 180 |
155 dir_pieces = name.split('-') | 181 dir_pieces = name.split('-') |
156 resource_type = dir_pieces[0] | 182 resource_type = dir_pieces[0] |
157 qualifiers = dir_pieces[1:] | 183 qualifiers = dir_pieces[1:] |
158 | 184 |
159 # We only convert resources under layout*/ and xml*/. | |
160 if resource_type not in ('layout', 'xml'): | |
161 continue | |
162 | |
163 # Android pre-v17 API doesn't support RTL. Skip. | 185 # Android pre-v17 API doesn't support RTL. Skip. |
164 if 'ldrtl' in qualifiers: | 186 if 'ldrtl' in qualifiers: |
165 continue | 187 continue |
166 | 188 |
167 # Convert all the resource files. | 189 input_dir = os.path.join(options.res_dir, name) |
168 input_path = os.path.join(options.res_dir, name) | 190 output_dir = os.path.join(options.res_v14_dir, name) |
169 output_path = os.path.join(options.res_v14_dir, name) | 191 |
170 GenerateV14ResourcesInDir(input_path, output_path) | 192 # We only convert resources under layout*/, xml*/, |
193 # and style resources under values*/. | |
194 if resource_type in ('layout', 'xml', 'values'): | |
newt (away)
2013/05/06 20:55:55
we know that style resources live in "values", and
Kibeom Kim (inactive)
2013/05/06 22:28:01
Done.
| |
195 GenerateV14XmlResourcesInDir(input_dir, output_dir) | |
171 | 196 |
172 if options.stamp: | 197 if options.stamp: |
173 build_utils.Touch(options.stamp) | 198 build_utils.Touch(options.stamp) |
174 | 199 |
175 if __name__ == '__main__': | 200 if __name__ == '__main__': |
176 sys.exit(main(sys.argv)) | 201 sys.exit(main(sys.argv)) |
177 | 202 |
OLD | NEW |