generate hashmap from strings.xml -- all of them -- making it

unmodifiable, and test for stuff not being in it.
This commit is contained in:
Eric House 2014-04-06 18:58:33 -07:00
parent 86cc8f6dbf
commit 437e2024cc
3 changed files with 45 additions and 29 deletions

View file

@ -20,8 +20,12 @@
package org.eehouse.android.xw4.loc; package org.eehouse.android.xw4.loc;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map;
import junit.framework.Assert;
import org.eehouse.android.xw4.DbgUtils;
public class LocIDs extends LocIDsData { public class LocIDs extends LocIDsData {
private static String[] s_keys; private static String[] s_keys;
@ -29,7 +33,7 @@ public class LocIDs extends LocIDsData {
protected static String getNthKey( int indx ) protected static String getNthKey( int indx )
{ {
if ( null == s_keys ) { if ( null == s_keys ) {
HashMap<String, Integer> map = LocIDsData.s_map; Map<String, Integer> map = LocIDsData.S_MAP;
s_keys = new String[map.size()]; s_keys = new String[map.size()];
Iterator<String> iter = map.keySet().iterator(); Iterator<String> iter = map.keySet().iterator();
for ( int ii = 0; iter.hasNext(); ++ii ) { for ( int ii = 0; iter.hasNext(); ++ii ) {
@ -43,14 +47,16 @@ public class LocIDs extends LocIDsData {
protected static int getID( String key ) protected static int getID( String key )
{ {
int result = LocIDsData.NOT_FOUND; int result = LocIDsData.NOT_FOUND;
if ( null != key ) { if ( null != key && LocIDsData.S_MAP.containsKey( key ) ) {
result = LocIDsData.s_map.get( key ); Assert.assertNotNull( LocIDsData.S_MAP );
DbgUtils.logf( "calling get with key %s", key );
result = LocIDsData.S_MAP.get( key ); // NPE
} }
return result; return result;
} }
protected static int size() protected static int size()
{ {
return LocIDsData.s_map.size(); return LocIDsData.S_MAP.size();
} }
} }

View file

@ -27,8 +27,9 @@ import android.view.Menu;
import android.view.MenuItem.OnMenuItemClickListener; import android.view.MenuItem.OnMenuItemClickListener;
import android.view.MenuItem; import android.view.MenuItem;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Assert; import junit.framework.Assert;
@ -137,7 +138,7 @@ public class LocUtils {
{ {
loadXlations( context ); loadXlations( context );
HashMap<String,Integer> map = LocIDsData.s_map; Map<String,Integer> map = LocIDsData.S_MAP;
int siz = map.size(); int siz = map.size();
LocSearcher.Pair[] result = new LocSearcher.Pair[siz]; LocSearcher.Pair[] result = new LocSearcher.Pair[siz];
Iterator<String> iter = map.keySet().iterator(); Iterator<String> iter = map.keySet().iterator();
@ -202,7 +203,7 @@ public class LocUtils {
private static String keyForID( int id ) private static String keyForID( int id )
{ {
if ( null == s_idsToKeys ) { if ( null == s_idsToKeys ) {
HashMap<String,Integer> map = LocIDsData.s_map; Map<String,Integer> map = LocIDsData.S_MAP;
HashMap<Integer, String> idsToKeys = HashMap<Integer, String> idsToKeys =
new HashMap<Integer, String>( map.size() ); new HashMap<Integer, String>( map.size() );

View file

@ -26,25 +26,32 @@ for path in glob.iglob( "res/values*/strings.xml" ):
if not name: continue if not name: continue
# Must match both or neither # Must match both or neither
if bool(ENDS_WITH_FMT.match(name)) != bool(HAS_FMT.match(elem.text)): if bool(ENDS_WITH_FMT.match(name)) != bool(HAS_FMT.match(elem.text)):
print "bad format string name:", name, "in", path, "with text", elem.text print "bad format string name:", name, "in", \
path, "with text", elem.text
sys.exit(1) sys.exit(1)
# Get all string IDs that are used in menus -- the ones we care about # Get all string IDs -- period
TITLE = re.compile('.*android:title="loc:(.*)".*') for path in glob.iglob( "res/values/strings.xml" ):
for path in glob.iglob( "res/menu/*.xml" ):
for line in open( path, "r" ):
line.strip()
mtch = TITLE.match(line)
if mtch:
pairs[mtch.group(1)] = True
LOC_START = re.compile('loc:(.*)')
for path in glob.iglob( "res/values/common_rsrc.xml" ):
for action, elem in etree.iterparse(path): for action, elem in etree.iterparse(path):
if "end" == action and elem.text: if "end" == action and 'string' == elem.tag:
mtch = LOC_START.match(elem.text) pairs[elem.get('name')] = True
if mtch:
pairs[mtch.group(1)] = True # # Get all string IDs that are used in menus -- the ones we care about
# TITLE = re.compile('.*android:title="loc:(.*)".*')
# for path in glob.iglob( "res/menu/*.xml" ):
# for line in open( path, "r" ):
# line.strip()
# mtch = TITLE.match(line)
# if mtch:
# pairs[mtch.group(1)] = True
# LOC_START = re.compile('loc:(.*)')
# for path in glob.iglob( "res/values/common_rsrc.xml" ):
# for action, elem in etree.iterparse(path):
# if "end" == action and elem.text:
# mtch = LOC_START.match(elem.text)
# if mtch:
# pairs[mtch.group(1)] = True
# Get all string IDs, but only keep those we've seen in menus # Get all string IDs, but only keep those we've seen in menus
@ -66,23 +73,25 @@ print """
package org.eehouse.android.xw4.loc; package org.eehouse.android.xw4.loc;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import org.eehouse.android.xw4.R; import org.eehouse.android.xw4.R;
public class LocIDsData { public class LocIDsData {
public static final int NOT_FOUND = -1; public static final int NOT_FOUND = -1;
protected static HashMap<String,Integer> s_map;
static { protected static final Map<String, Integer> S_MAP =
s_map = new HashMap<String,Integer>(); Collections.unmodifiableMap(new HashMap<String, Integer>() {{
""" """
for key in pairs.keys(): for key in pairs.keys():
print " s_map.put(\"%s\", R.string.%s);" % (key, key) print " put(\"%s\", R.string.%s);" % (key, key)
# Now the end of the class # Now the end of the class
print """ print """
} }});
} }
/* end generated file */ /* end generated file */
""" """