use of org.javarosa.core.util.OrderedMap in project javarosa by opendatakit.
the class ExternalizableTest method printObj.
public static String printObj(Object o) {
o = ExtUtil.unwrap(o);
if (o == null) {
return "(null)";
} else if (o instanceof List) {
StringBuilder sb = new StringBuilder();
sb.append("V[");
List lo = (List) o;
boolean first = true;
for (Object obj : lo) {
if (!first) {
sb.append(", ");
}
first = false;
sb.append(printObj(obj));
}
sb.append("]");
return sb.toString();
} else if (o instanceof HashMap) {
StringBuilder sb = new StringBuilder();
sb.append((o instanceof OrderedMap ? "oH" : "H") + "[");
for (Iterator e = ((HashMap) o).keySet().iterator(); e.hasNext(); ) {
Object key = e.next();
sb.append(printObj(key));
sb.append("=>");
sb.append(printObj(((HashMap) o).get(key)));
if (e.hasNext())
sb.append(", ");
}
sb.append("]");
return sb.toString();
} else {
return "{" + o.getClass().getName() + ":" + o.toString() + "}";
}
}
use of org.javarosa.core.util.OrderedMap in project javarosa by opendatakit.
the class ExtWrapMap method readExternal.
public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
if (type != TYPE_SLOW_READ_ONLY) {
java.util.Map h;
long size = ExtUtil.readNumeric(in);
switch(type) {
case (TYPE_NORMAL):
h = new HashMap((int) size);
break;
case (TYPE_ORDERED):
h = new OrderedMap();
break;
case (TYPE_SLOW_COMPACT):
h = new Map((int) size);
break;
default:
h = new HashMap((int) size);
}
for (int i = 0; i < size; i++) {
Object key = ExtUtil.read(in, keyType, pf);
Object elem = ExtUtil.read(in, dataType, pf);
h.put(key, elem);
}
val = h;
} else {
int size = ExtUtil.readInt(in);
Object[] k = new Object[size];
Object[] v = new Object[size];
for (int i = 0; i < size; i++) {
k[i] = ExtUtil.read(in, keyType, pf);
v[i] = ExtUtil.read(in, dataType, pf);
}
val = new Map(k, v);
}
}
use of org.javarosa.core.util.OrderedMap in project javarosa by opendatakit.
the class ExternalizableTest method doTests.
public void doTests() {
// base types (built-in + externalizable)
testExternalizable("string", String.class);
testExternalizable(new Byte((byte) 0), Byte.class);
testExternalizable(new Byte((byte) 0x55), Byte.class);
testExternalizable(new Byte((byte) 0xe9), Byte.class);
testExternalizable(new Short((short) 0), Short.class);
testExternalizable(new Short((short) -12345), Short.class);
testExternalizable(new Short((short) 12345), Short.class);
testExternalizable(new Integer(0), Integer.class);
testExternalizable(new Integer(1234567890), Integer.class);
testExternalizable(new Integer(-1234567890), Integer.class);
testExternalizable(new Long(0), Long.class);
testExternalizable(new Long(1234567890123456789l), Long.class);
testExternalizable(new Long(-1234567890123456789l), Long.class);
testExternalizable(Boolean.TRUE, Boolean.class);
testExternalizable(Boolean.FALSE, Boolean.class);
testExternalizable(new Character('e'), Character.class);
testExternalizable(new Float(123.45e6), Float.class);
testExternalizable(new Double(123.45e6), Double.class);
testExternalizable(new Date(), Date.class);
testExternalizable(new SampleExtz("your", "mom"), SampleExtz.class);
// base wrapper (end user will never use)
testExternalizable("string", new ExtWrapBase(String.class));
testExternalizable(new ExtWrapBase("string"), String.class);
// nullables on base types
testExternalizable(new ExtWrapNullable((String) null), new ExtWrapNullable(String.class));
testExternalizable(new ExtWrapNullable("string"), new ExtWrapNullable(String.class));
testExternalizable(new ExtWrapNullable((Integer) null), new ExtWrapNullable(Integer.class));
testExternalizable(new ExtWrapNullable(new Integer(17)), new ExtWrapNullable(Integer.class));
testExternalizable(new ExtWrapNullable((SampleExtz) null), new ExtWrapNullable(SampleExtz.class));
testExternalizable(new ExtWrapNullable(new SampleExtz("hi", "there")), new ExtWrapNullable(SampleExtz.class));
// lists of base types
List<Integer> v = new ArrayList<Integer>();
v.add(new Integer(27));
v.add(new Integer(-73));
v.add(new Integer(1024));
v.add(new Integer(66066066));
testExternalizable(new ExtWrapList(v), new ExtWrapList(Integer.class));
List<String> vs = new ArrayList<String>();
vs.add("alpha");
vs.add("beta");
vs.add("gamma");
testExternalizable(new ExtWrapList(vs), new ExtWrapList(String.class));
List<Object> w = new ArrayList<Object>();
w.add(new SampleExtz("where", "is"));
w.add(new SampleExtz("the", "beef"));
testExternalizable(new ExtWrapList(w), new ExtWrapList(SampleExtz.class));
// nullable lists; lists of nullables (no practical use)
testExternalizable(new ExtWrapNullable(new ExtWrapList(v)), new ExtWrapNullable(new ExtWrapList(Integer.class)));
testExternalizable(new ExtWrapNullable((ExtWrapList) null), new ExtWrapNullable(new ExtWrapList(Integer.class)));
testExternalizable(new ExtWrapList(v, new ExtWrapNullable()), new ExtWrapList(new ExtWrapNullable(Integer.class)));
// empty lists (base types)
testExternalizable(new ExtWrapList(new ArrayList<String>()), new ExtWrapList(String.class));
// sub-types don't matter for empties
testExternalizable(new ExtWrapList(new ArrayList(), new ExtWrapBase(Integer.class)), new ExtWrapList(String.class));
// lists of lists (including empties)
ArrayList x = new ArrayList();
x.add(new Integer(-35));
x.add(new Integer(-31415926));
ArrayList y = new ArrayList();
y.add(v);
y.add(x);
y.add(new ArrayList());
// risky to not specify 'leaf' type (Integer), but works in limited situations
testExternalizable(new ExtWrapList(y, new ExtWrapList()), new ExtWrapList(new ExtWrapList(Integer.class)));
// same as above
testExternalizable(new ExtWrapList(new ArrayList(), new ExtWrapList()), new ExtWrapList(new ExtWrapList(Integer.class)));
// tagged base types
testExternalizable(new ExtWrapTagged("string"), new ExtWrapTagged());
testExternalizable(new ExtWrapTagged(new Integer(5000)), new ExtWrapTagged());
// tagged custom type
PrototypeFactory pf = new PrototypeFactory();
pf.addClass(SampleExtz.class);
testExternalizable(new ExtWrapTagged(new SampleExtz("bon", "jovi")), new ExtWrapTagged(), pf);
// tagged list (base type)
testExternalizable(new ExtWrapTagged(new ExtWrapList(v)), new ExtWrapTagged());
testExternalizable(new ExtWrapTagged(new ExtWrapList(w)), new ExtWrapTagged(), pf);
// tagged nullables and compound lists
testExternalizable(new ExtWrapTagged(new ExtWrapNullable("string")), new ExtWrapTagged());
testExternalizable(new ExtWrapTagged(new ExtWrapNullable((String) null)), new ExtWrapTagged());
testExternalizable(new ExtWrapTagged(new ExtWrapList(y, new ExtWrapList(Integer.class))), new ExtWrapTagged());
testExternalizable(new ExtWrapTagged(new ExtWrapList(new ArrayList(), new ExtWrapList(Integer.class))), new ExtWrapTagged());
// polymorphic lists
List a = new ArrayList();
a.add(new Integer(47));
a.add("string");
a.add(Boolean.FALSE);
a.add(new SampleExtz("hello", "dolly"));
testExternalizable(new ExtWrapListPoly(a), new ExtWrapListPoly(), pf);
testExternalizable(new ExtWrapTagged(new ExtWrapListPoly(a)), new ExtWrapTagged(), pf);
// polymorphic list with complex sub-types
// note: must manually wrap children in polymorphic lists
a.add(new ExtWrapList(y, new ExtWrapList(Integer.class)));
testExternalizable(new ExtWrapListPoly(a), new ExtWrapListPoly(), pf);
testExternalizable(new ExtWrapListPoly(new ArrayList()), new ExtWrapListPoly());
// hashtables
OrderedMap oh = new OrderedMap();
testExternalizable(new ExtWrapMap(oh), new ExtWrapMap(String.class, Integer.class, ExtWrapMap.TYPE_ORDERED));
testExternalizable(new ExtWrapMapPoly(oh), new ExtWrapMapPoly(Date.class, true));
testExternalizable(new ExtWrapTagged(new ExtWrapMap(oh)), new ExtWrapTagged());
testExternalizable(new ExtWrapTagged(new ExtWrapMapPoly(oh)), new ExtWrapTagged());
oh.put("key1", new SampleExtz("a", "b"));
oh.put("key2", new SampleExtz("c", "d"));
oh.put("key3", new SampleExtz("e", "f"));
testExternalizable(new ExtWrapMap(oh), new ExtWrapMap(String.class, SampleExtz.class, ExtWrapMap.TYPE_ORDERED), pf);
testExternalizable(new ExtWrapTagged(new ExtWrapMap(oh)), new ExtWrapTagged(), pf);
HashMap h = new HashMap();
testExternalizable(new ExtWrapMap(h), new ExtWrapMap(String.class, Integer.class));
testExternalizable(new ExtWrapMapPoly(h), new ExtWrapMapPoly(Date.class));
testExternalizable(new ExtWrapTagged(new ExtWrapMap(h)), new ExtWrapTagged());
testExternalizable(new ExtWrapTagged(new ExtWrapMapPoly(h)), new ExtWrapTagged());
h.put("key1", new SampleExtz("e", "f"));
h.put("key2", new SampleExtz("c", "d"));
h.put("key3", new SampleExtz("a", "b"));
testExternalizable(new ExtWrapMap(h), new ExtWrapMap(String.class, SampleExtz.class), pf);
testExternalizable(new ExtWrapTagged(new ExtWrapMap(h)), new ExtWrapTagged(), pf);
HashMap j = new HashMap();
j.put(new Integer(17), h);
j.put(new Integer(-3), h);
HashMap k = new HashMap();
k.put("key", j);
testExternalizable(new ExtWrapMap(k, new ExtWrapMap(Integer.class, new ExtWrapMap(String.class, SampleExtz.class))), new ExtWrapMap(String.class, new ExtWrapMap(Integer.class, new ExtWrapMap(String.class, SampleExtz.class))), // note: this example contains mixed hashtable types; would choke if we used a tagging wrapper
pf);
OrderedMap m = new OrderedMap();
m.put("a", "b");
m.put("b", new Integer(17));
m.put("c", new Short((short) -443));
m.put("d", new SampleExtz("boris", "yeltsin"));
m.put("e", new ExtWrapList(vs));
testExternalizable(new ExtWrapMapPoly(m), new ExtWrapMapPoly(String.class, true), pf);
}
use of org.javarosa.core.util.OrderedMap in project javarosa by opendatakit.
the class Localizer method getLocaleData.
/**
* Get the set of mappings for a locale.
*
* @param locale Locale
* @returns HashMap representing text mappings for this locale. Returns null if locale not defined or null.
*/
public OrderedMap<String, String> getLocaleData(String locale) {
final CodeTimer codeTimer = new CodeTimer("getLocaleData");
if (locale == null || !this.locales.contains(locale)) {
return null;
}
// It's very important that any default locale contain the appropriate strings to localize the interface
// for any possible language. As such, we'll keep around a table with only the default locale keys to
// ensure that there are no localizations which are only present in another locale, which causes ugly
// and difficult to trace errors.
final OrderedMap<String, Boolean> defaultLocaleKeys = new OrderedMap<>();
// This table will be loaded with the default values first (when applicable), and then with any
// language specific translations overwriting the existing values.
final OrderedMap<String, String> data = new OrderedMap<>();
// the current locale to overwrite any differences between the two.
if (fallbackDefaultLocale && defaultLocale != null) {
for (LocaleDataSource defaultResource : localeResources.get(defaultLocale)) {
loadTable(data, defaultResource.getLocalizedText());
}
for (String key : data.keySet()) {
defaultLocaleKeys.put(key, Boolean.TRUE);
}
}
for (LocaleDataSource resource : localeResources.get(locale)) {
loadTable(data, resource.getLocalizedText());
}
// a locale that doesn't contain the key.
if (fallbackDefaultLocale && defaultLocale != null) {
StringBuilder missingKeys = new StringBuilder();
int keysMissing = 0;
for (String key : data.keySet()) {
if (!defaultLocaleKeys.containsKey(key)) {
missingKeys.append(key).append(",");
keysMissing++;
}
}
if (keysMissing > 0) {
// Is there a good way to localize these exceptions?
throw new NoLocalizedTextException("Error loading locale " + locale + ". There were " + keysMissing + " keys which were contained in this locale, but were not " + "properly registered in the default Locale. Any keys which are added to a locale should always " + "be added to the default locale to ensure appropriate functioning.\n" + "The missing translations were for the keys: " + missingKeys, missingKeys.toString(), defaultLocale);
}
}
codeTimer.logDone();
return data;
}
use of org.javarosa.core.util.OrderedMap in project javarosa by opendatakit.
the class LocalizationUtils method parseLocaleInput.
/**
* @param is A path to a resource file provided in the current environment
*
* @return a dictionary of key/value locale pairs from a file in the resource directory
* @throws IOException
*/
public static OrderedMap<String, String> parseLocaleInput(InputStream is) throws IOException {
// TODO: This might very well fail. Best way to handle?
OrderedMap<String, String> locale = new OrderedMap<String, String>();
int chunk = 100;
InputStreamReader isr;
isr = new InputStreamReader(is, "UTF-8");
boolean done = false;
char[] cbuf = new char[chunk];
int offset = 0;
int curline = 0;
String line = "";
while (!done) {
int read = isr.read(cbuf, offset, chunk - offset);
if (read == -1) {
done = true;
if (line.length() != 0) {
parseAndAdd(locale, line, curline);
}
break;
}
String stringchunk = String.valueOf(cbuf, offset, read);
int index = 0;
while (index != -1) {
int nindex = stringchunk.indexOf('\n', index);
// didn't find one, we'll try that
if (nindex == -1) {
nindex = stringchunk.indexOf('\r', index);
}
if (nindex == -1) {
line += stringchunk.substring(index);
break;
} else {
line += stringchunk.substring(index, nindex);
// Newline. process our string and start the next one.
curline++;
parseAndAdd(locale, line, curline);
line = "";
}
index = nindex + 1;
}
}
is.close();
return locale;
}
Aggregations