Search in sources :

Example 6 with OrderedMap

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) OrderedMap(org.javarosa.core.util.OrderedMap) IOException(java.io.IOException) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) IOException(java.io.IOException)

Example 7 with OrderedMap

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;
}
Also used : HashMap(java.util.HashMap) OrderedMap(org.javarosa.core.util.OrderedMap)

Aggregations

OrderedMap (org.javarosa.core.util.OrderedMap)7 HashMap (java.util.HashMap)4 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExtWrapList (org.javarosa.core.util.externalizable.ExtWrapList)2 DataInputStream (java.io.DataInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 CodeTimer (org.javarosa.core.util.CodeTimer)1 Map (org.javarosa.core.util.Map)1 NoLocalizedTextException (org.javarosa.core.util.NoLocalizedTextException)1 DeserializationException (org.javarosa.core.util.externalizable.DeserializationException)1 ExtWrapBase (org.javarosa.core.util.externalizable.ExtWrapBase)1 ExtWrapListPoly (org.javarosa.core.util.externalizable.ExtWrapListPoly)1 ExtWrapMap (org.javarosa.core.util.externalizable.ExtWrapMap)1 ExtWrapMapPoly (org.javarosa.core.util.externalizable.ExtWrapMapPoly)1 ExtWrapNullable (org.javarosa.core.util.externalizable.ExtWrapNullable)1