combine the xml-manipulation and java-class-generation scripts

This commit is contained in:
Eric House 2014-04-08 18:57:31 -07:00
parent 9e228ab368
commit 0b93e77018
2 changed files with 65 additions and 21 deletions

View file

@ -86,7 +86,10 @@
<exec dir="." executable="../scripts/mk_xml.py"
failonerror="true" output="/dev/null"
/>
>
<arg value="-o"/>
<arg value="src/org/eehouse/android/xw4/loc/LocIDsData.java"/>
</exec>
<exec dir="." executable="../scripts/gen_gcmid.sh"
output="src/org/eehouse/android/xw4/GCMConsts.java"
@ -103,15 +106,10 @@
</exec>
</target>
<target name="-pre-compile">
<exec dir="." executable="../scripts/gen_loc_ids.py"
output="src/org/eehouse/android/xw4/loc/LocIDsData.java"
failonerror="true"
>
</exec>
</target>
<!--
<target name="-pre-compile">
</target>
/* This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir} */

View file

@ -1,6 +1,7 @@
#!/usr/bin/python
import glob, sys, re, os
import glob, sys, re, os, getopt
from lxml import etree
# sys.exit(0)
@ -23,7 +24,6 @@ g_pairs = {}
STR_REF = re.compile('@string/(.*)$')
def xform(src, dest):
print "looking at file", src
doc = etree.parse(src)
root = doc.getroot();
for child in root.iter():
@ -34,7 +34,6 @@ def xform(src, dest):
if match:
key = match.group(1)
if key in g_pairs:
print child.tag, key, "loc:" + key
child.set(elem['attrType'], "loc:" + key)
# create directory if needed, then write file
@ -42,14 +41,61 @@ def xform(src, dest):
if not os.path.exists(dir): os.makedirs(dir)
doc.write( dest, pretty_print=True )
# Gather all localizable strings
for path in glob.iglob( "res/values/strings.xml" ):
for action, elem in etree.iterparse(path):
if "end" == action and 'string' == elem.tag:
g_pairs[elem.get('name')] = True
def printStrings( pairs, outfile ):
fil = open( outfile, "w" )
for subdir, dirs, files in os.walk('res_src'):
for file in files:
src = subdir + '/' + file
dest = src.replace( 'res_src', 'res', 1 )
xform( src, dest )
# beginning of the class file
lines = """
/***********************************************************************
* Generated file; do not edit!!!
***********************************************************************/
package org.eehouse.android.xw4.loc;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eehouse.android.xw4.R;
public class LocIDsData {
public static final int NOT_FOUND = -1;
protected static final Map<String, Integer> S_MAP =
Collections.unmodifiableMap(new HashMap<String, Integer>() {{
"""
fil.write( lines )
for key in pairs.keys():
fil.write( " put(\"%s\", R.string.%s);\n" % (key, key) )
# Now the end of the class
lines = """
}});
}
/* end generated file */
"""
fil.write( lines )
def main():
outfile = ''
pairs, rest = getopt.getopt(sys.argv[1:], "o:")
for option, value in pairs:
if option == '-o': outfile = value
# Gather all localizable strings
for path in glob.iglob( "res/values/strings.xml" ):
for action, elem in etree.iterparse(path):
if "end" == action and 'string' == elem.tag:
g_pairs[elem.get('name')] = True
for subdir, dirs, files in os.walk('res_src'):
for file in files:
src = subdir + '/' + file
dest = src.replace( 'res_src', 'res', 1 )
xform( src, dest )
printStrings( g_pairs, outfile )
main()