diff options
author | kkimlabs@chromium.org <kkimlabs@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-10 10:49:12 +0000 |
---|---|---|
committer | kkimlabs@chromium.org <kkimlabs@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-05-10 10:49:12 +0000 |
commit | 1c36fc4e54a794b9b7119b9267b215d9c58a8d5e (patch) | |
tree | 4d6a48eac6c49c36aa9736fc511ff00d5cca45a9 /build | |
parent | d74fa8f5142843dea11d283d66d0ccb36c8db9e5 (diff) | |
download | chromium_src-1c36fc4e54a794b9b7119b9267b215d9c58a8d5e.zip chromium_src-1c36fc4e54a794b9b7119b9267b215d9c58a8d5e.tar.gz chromium_src-1c36fc4e54a794b9b7119b9267b215d9c58a8d5e.tar.bz2 |
[Android] Warning if Left/Right attributes are used in layout and style xmls.
Since crbug.com/235118, Left/Right attributes are deprecated in favor of Start/End.
So warn if it contains Left/Right.
BUG=239606
Review URL: https://chromiumcodereview.appspot.com/14969022
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@199452 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'build')
-rwxr-xr-x | build/android/gyp/generate_v14_resources.py | 110 |
1 files changed, 53 insertions, 57 deletions
diff --git a/build/android/gyp/generate_v14_resources.py b/build/android/gyp/generate_v14_resources.py index a07a42a..beaa745 100755 --- a/build/android/gyp/generate_v14_resources.py +++ b/build/android/gyp/generate_v14_resources.py @@ -27,8 +27,10 @@ import xml.dom.minidom as minidom from util import build_utils +# Note that we are assuming 'android:' is an alias of +# the namespace 'http://schemas.android.com/apk/res/android'. -ATTRIBUTE_NAMESPACE = 'http://schemas.android.com/apk/res/android' +GRAVITY_ATTRIBUTES = ('android:gravity', 'android:layout_gravity') # Almost all the attributes that has "Start" or "End" in # its name should be mapped. @@ -45,12 +47,11 @@ ATTRIBUTES_TO_MAP = {'paddingStart' : 'paddingLeft', 'layout_alignParentEnd' : 'layout_alignParentRight', 'layout_toEndOf' : 'layout_toRightOf'} -ATTRIBUTES_TO_MAP_NS = {} +ATTRIBUTES_TO_MAP = dict(['android:' + k, 'android:' + v] for k, v + in ATTRIBUTES_TO_MAP.iteritems()) -for k, v in ATTRIBUTES_TO_MAP.items(): - ATTRIBUTES_TO_MAP_NS[(ATTRIBUTE_NAMESPACE, k)] = (ATTRIBUTE_NAMESPACE, v) - -ATTRIBUTES_TO_MAP_NS_VALUES = set(ATTRIBUTES_TO_MAP_NS.values()) +ATTRIBUTES_TO_MAP_REVERSED = dict([v,k] for k, v + in ATTRIBUTES_TO_MAP.iteritems()) def IterateXmlElements(node): @@ -63,85 +64,80 @@ def IterateXmlElements(node): yield child_node_element -def GenerateV14StyleResource(dom, output_file): +def WarnDeprecatedAttribute(name, value, filename): + if name in ATTRIBUTES_TO_MAP_REVERSED: + print >> sys.stderr, ('warning: ' + filename + ' should use ' + + ATTRIBUTES_TO_MAP_REVERSED[name] + + ' instead of ' + name) + elif name in GRAVITY_ATTRIBUTES and ('left' in value or 'right' in value): + print >> sys.stderr, ('warning: ' + filename + + ' should use start/end instead of left/right for ' + + name) + + +def GenerateV14StyleResource(input_filename, output_filename): """Convert style resource to API 14 compatible style resource. It's mostly a simple replacement, s/Start/Left s/End/Right, on the attribute names specified by <item> element. + If input_filename does not contain style resources, do nothing. """ - for style_element in dom.getElementsByTagName('style'): + dom = minidom.parse(input_filename) + style_elements = dom.getElementsByTagName('style') + + if not style_elements: + return + + for style_element in style_elements: for item_element in style_element.getElementsByTagName('item'): - namespace, name = item_element.attributes['name'].value.split(':') - # Note: namespace == 'android' is not precise because - # we are looking for 'http://schemas.android.com/apk/res/android' and - # 'android' can be aliased to another name in layout xml files where - # this style is used. e.g. xmlns:android="http://crbug.com/". - if namespace == 'android' and name in ATTRIBUTES_TO_MAP: - mapped_name = ATTRIBUTES_TO_MAP[name] - item_element.attributes['name'] = namespace + ':' + mapped_name - - build_utils.MakeDirectory(os.path.dirname(output_file)) - with open(output_file, 'w') as f: + name = item_element.attributes['name'].value + value = item_element.childNodes[0].nodeValue + if name in ATTRIBUTES_TO_MAP: + item_element.attributes['name'].value = ATTRIBUTES_TO_MAP[name] + else: + WarnDeprecatedAttribute(name, value, input_filename) + + build_utils.MakeDirectory(os.path.dirname(output_filename)) + with open(output_filename, 'w') as f: dom.writexml(f, '', ' ', '\n', encoding='utf-8') -def GenerateV14LayoutResource(input_file, output_file): +def GenerateV14LayoutResource(input_filename, output_filename): """Convert layout resource to API 14 compatible layout resource. It's mostly a simple replacement, s/Start/Left s/End/Right, on the attribute names. """ - dom = minidom.parse(input_file) + dom = minidom.parse(input_filename) + # Iterate all the elements' attributes to find attributes to convert. for element in IterateXmlElements(dom): - all_names = element.attributes.keysNS() - - # Iterate all the attributes to find attributes to convert. - # Note that name variable is actually a tuple that has namespace and name. - # For example, - # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') - for name, value in list(element.attributes.itemsNS()): + for name, value in list(element.attributes.items()): + # Convert any other API 17 Start/End attributes to Left/Right attributes. + # For example, from paddingStart="10dp" to paddingLeft="10dp" # Note: gravity attributes are not necessary to convert because # start/end values are backward-compatible. Explained at # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom - - # Convert any other API 17 Start/End attributes to Left/Right attributes. - # For example, from paddingStart="10dp" to paddingLeft="10dp" - if name in ATTRIBUTES_TO_MAP_NS: - mapped_name = ATTRIBUTES_TO_MAP_NS[name] - - # Add the new mapped attribute and remove the original attribute. - # For example, add paddingLeft and remove paddingStart. - # Note that instead of element.setAttribute(...), this is more correct. - # element.setAttributeNS(mapped_name[0], mapped_name[1], value) - # However, there is a minidom bug that doesn't print namespace set by - # setAttributeNS. Hence this workaround. - # This is a similar bug discussion about minidom namespace normalizing. - # http://stackoverflow.com/questions/863774/how-to-generate-xml-documents-with-namespaces-in-python - element.setAttribute('android:' + mapped_name[1], value) + if name in ATTRIBUTES_TO_MAP: + element.setAttribute(ATTRIBUTES_TO_MAP[name], value) del element.attributes[name] - elif name in ATTRIBUTES_TO_MAP_NS_VALUES: - # TODO(kkimlabs): Enable warning once layouts have been converted - # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' - pass + else: + WarnDeprecatedAttribute(name, value, input_filename) - build_utils.MakeDirectory(os.path.dirname(output_file)) - with open(output_file, 'w') as f: + build_utils.MakeDirectory(os.path.dirname(output_filename)) + with open(output_filename, 'w') as f: dom.writexml(f, '', ' ', '\n', encoding='utf-8') def GenerateV14XmlResourcesInDir(input_dir, output_dir, only_styles=False): """Convert resources to API 14 compatible XML resources in the directory.""" - for input_file in build_utils.FindInDirectory(input_dir, '*.xml'): - output_file = os.path.join(output_dir, - os.path.relpath(input_file, input_dir)) + for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): + output_filename = os.path.join(output_dir, + os.path.relpath(input_filename, input_dir)) if only_styles: - dom = minidom.parse(input_file) - if not dom.getElementsByTagName('style'): - continue - GenerateV14StyleResource(dom, output_file) + GenerateV14StyleResource(input_filename, output_filename) else: - GenerateV14LayoutResource(input_file, output_file) + GenerateV14LayoutResource(input_filename, output_filename) def ParseArgs(): |