use of org.javarosa.core.util.OrderedMap in project javarosa by opendatakit.
the class ResourceFileDataSource method loadLocaleResource.
/**
* @param resourceName 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
*/
private OrderedMap<String, String> loadLocaleResource(String resourceName) {
InputStream is = System.class.getResourceAsStream(resourceName);
// TODO: This might very well fail. Best way to handle?
OrderedMap<String, String> locale = new OrderedMap<String, String>();
int chunk = 100;
InputStreamReader isr;
try {
isr = new InputStreamReader(is, "UTF-8");
} catch (Exception e) {
throw new RuntimeException("Failed to load locale resource " + resourceName + ". Is it in the jar?");
}
boolean done = false;
char[] cbuf = new char[chunk];
int offset = 0;
int curline = 0;
try {
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;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
Std.printStack(e);
} finally {
try {
is.close();
} catch (IOException e) {
Std.out.println("Input Stream for resource file " + resourceURI + " failed to close. This will eat up your memory! Fix Problem! [" + e.getMessage() + "]");
Std.printStack(e);
}
}
return locale;
}
use of org.javarosa.core.util.OrderedMap in project javarosa by opendatakit.
the class ExtWrapMapPoly method readExternal.
public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
HashMap h = ordered ? new OrderedMap() : new HashMap();
long size = ExtUtil.readNumeric(in);
for (int i = 0; i < size; i++) {
Object key = ExtUtil.read(in, keyType, pf);
Object elem = ExtUtil.read(in, new ExtWrapTagged(), pf);
h.put(key, elem);
}
val = h;
}
Aggregations