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;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import junit.framework.Assert;
import org.eehouse.android.xw4.DbgUtils;
public class LocIDs extends LocIDsData {
private static String[] s_keys;
@ -29,7 +33,7 @@ public class LocIDs extends LocIDsData {
protected static String getNthKey( int indx )
{
if ( null == s_keys ) {
HashMap<String, Integer> map = LocIDsData.s_map;
Map<String, Integer> map = LocIDsData.S_MAP;
s_keys = new String[map.size()];
Iterator<String> iter = map.keySet().iterator();
for ( int ii = 0; iter.hasNext(); ++ii ) {
@ -43,14 +47,16 @@ public class LocIDs extends LocIDsData {
protected static int getID( String key )
{
int result = LocIDsData.NOT_FOUND;
if ( null != key ) {
result = LocIDsData.s_map.get( key );
if ( null != key && LocIDsData.S_MAP.containsKey( key ) ) {
Assert.assertNotNull( LocIDsData.S_MAP );
DbgUtils.logf( "calling get with key %s", key );
result = LocIDsData.S_MAP.get( key ); // NPE
}
return result;
}
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;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Assert;
@ -137,7 +138,7 @@ public class LocUtils {
{
loadXlations( context );
HashMap<String,Integer> map = LocIDsData.s_map;
Map<String,Integer> map = LocIDsData.S_MAP;
int siz = map.size();
LocSearcher.Pair[] result = new LocSearcher.Pair[siz];
Iterator<String> iter = map.keySet().iterator();
@ -202,7 +203,7 @@ public class LocUtils {
private static String keyForID( int id )
{
if ( null == s_idsToKeys ) {
HashMap<String,Integer> map = LocIDsData.s_map;
Map<String,Integer> map = LocIDsData.S_MAP;
HashMap<Integer, String> idsToKeys =
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
# Must match both or neither
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)
# 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" ):
# Get all string IDs -- period
for path in glob.iglob( "res/values/strings.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
if "end" == action and 'string' == elem.tag:
pairs[elem.get('name')] = 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
@ -66,23 +73,25 @@ print """
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 HashMap<String,Integer> s_map;
static {
s_map = new HashMap<String,Integer>();
protected static final Map<String, Integer> S_MAP =
Collections.unmodifiableMap(new HashMap<String, Integer>() {{
"""
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
print """
}
}});
}
/* end generated file */
"""