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 |
| 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(input_file, 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) | 94 dom = minidom.parse(input_file) |
73 | 95 |
74 for element in IterateXmlElements(dom): | 96 for element in IterateXmlElements(dom): |
75 all_names = element.attributes.keysNS() | 97 all_names = element.attributes.keysNS() |
76 | 98 |
77 # Iterate all the attributes to find attributes to convert. | 99 # Iterate all the attributes to find attributes to convert. |
78 # Note that name variable is actually a tuple that has namespace and name. | 100 # Note that name variable is actually a tuple that has namespace and name. |
79 # For example, | 101 # For example, |
80 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') | 102 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') |
81 for name, value in list(element.attributes.itemsNS()): | 103 for name, value in list(element.attributes.itemsNS()): |
82 # Note: gravity attributes are not necessary to convert because | 104 # Note: gravity attributes are not necessary to convert because |
(...skipping 13 matching lines...) Expand all Loading... |
96 # setAttributeNS. Hence this workaround. | 118 # setAttributeNS. Hence this workaround. |
97 # This is a similar bug discussion about minidom namespace normalizing. | 119 # 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 | 120 # http://stackoverflow.com/questions/863774/how-to-generate-xml-document
s-with-namespaces-in-python |
99 element.setAttribute('android:' + mapped_name[1], value) | 121 element.setAttribute('android:' + mapped_name[1], value) |
100 del element.attributes[name] | 122 del element.attributes[name] |
101 elif name in ATTRIBUTES_TO_MAP_NS_VALUES: | 123 elif name in ATTRIBUTES_TO_MAP_NS_VALUES: |
102 # TODO(kkimlabs): Enable warning once layouts have been converted | 124 # TODO(kkimlabs): Enable warning once layouts have been converted |
103 # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' | 125 # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' |
104 pass | 126 pass |
105 | 127 |
106 build_utils.MakeDirectory(os.path.dirname(output_filename)) | 128 build_utils.MakeDirectory(os.path.dirname(output_file)) |
107 with open(output_filename, 'w') as f: | 129 with open(output_file, 'w') as f: |
108 dom.writexml(f, ' ', '\n', encoding='utf-8') | 130 dom.writexml(f, '', ' ', '\n', encoding='utf-8') |
109 | 131 |
110 | 132 |
111 def GenerateV14ResourcesInDir(input_dir, output_dir): | 133 def GenerateV14XmlResourcesInDir(input_dir, output_dir, only_styles=False): |
112 """Convert resources to API 14 compatible XML resources in the directory.""" | 134 """Convert resources to API 14 compatible XML resources in the directory.""" |
113 for input_file in build_utils.FindInDirectory(input_dir, '*.xml'): | 135 for input_file in build_utils.FindInDirectory(input_dir, '*.xml'): |
114 output_path = os.path.join(output_dir, | 136 output_file = os.path.join(output_dir, |
115 os.path.relpath(input_file, input_dir)) | 137 os.path.relpath(input_file, input_dir)) |
116 GenerateV14Resource(input_file, output_path) | 138 if only_styles: |
| 139 dom = minidom.parse(input_file) |
| 140 if not dom.getElementsByTagName('style'): |
| 141 continue |
| 142 GenerateV14StyleResource(dom, output_file) |
| 143 else: |
| 144 GenerateV14LayoutResource(input_file, output_file) |
117 | 145 |
118 | 146 |
119 def ParseArgs(): | 147 def ParseArgs(): |
120 """Parses command line options. | 148 """Parses command line options. |
121 | 149 |
122 Returns: | 150 Returns: |
123 An options object as from optparse.OptionsParser.parse_args() | 151 An options object as from optparse.OptionsParser.parse_args() |
124 """ | 152 """ |
125 parser = optparse.OptionParser() | 153 parser = optparse.OptionParser() |
126 parser.add_option('--res-dir', | 154 parser.add_option('--res-dir', |
(...skipping 22 matching lines...) Expand all Loading... |
149 build_utils.MakeDirectory(options.res_v14_dir) | 177 build_utils.MakeDirectory(options.res_v14_dir) |
150 | 178 |
151 for name in os.listdir(options.res_dir): | 179 for name in os.listdir(options.res_dir): |
152 if not os.path.isdir(os.path.join(options.res_dir, name)): | 180 if not os.path.isdir(os.path.join(options.res_dir, name)): |
153 continue | 181 continue |
154 | 182 |
155 dir_pieces = name.split('-') | 183 dir_pieces = name.split('-') |
156 resource_type = dir_pieces[0] | 184 resource_type = dir_pieces[0] |
157 qualifiers = dir_pieces[1:] | 185 qualifiers = dir_pieces[1:] |
158 | 186 |
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. | 187 # Android pre-v17 API doesn't support RTL. Skip. |
164 if 'ldrtl' in qualifiers: | 188 if 'ldrtl' in qualifiers: |
165 continue | 189 continue |
166 | 190 |
167 # Convert all the resource files. | 191 input_dir = os.path.join(options.res_dir, name) |
168 input_path = os.path.join(options.res_dir, name) | 192 output_dir = os.path.join(options.res_v14_dir, name) |
169 output_path = os.path.join(options.res_v14_dir, name) | 193 |
170 GenerateV14ResourcesInDir(input_path, output_path) | 194 # We only convert resources under layout*/, xml*/, |
| 195 # and style resources under values*/. |
| 196 # TODO(kkimlabs): don't process xml directly once all layouts have |
| 197 # been moved out of XML directory. see http://crbug.com/238458 |
| 198 if resource_type in ('layout', 'xml'): |
| 199 GenerateV14XmlResourcesInDir(input_dir, output_dir) |
| 200 elif resource_type in ('values'): |
| 201 GenerateV14XmlResourcesInDir(input_dir, output_dir, only_styles=True) |
171 | 202 |
172 if options.stamp: | 203 if options.stamp: |
173 build_utils.Touch(options.stamp) | 204 build_utils.Touch(options.stamp) |
174 | 205 |
175 if __name__ == '__main__': | 206 if __name__ == '__main__': |
176 sys.exit(main(sys.argv)) | 207 sys.exit(main(sys.argv)) |
177 | 208 |
OLD | NEW |