Search in sources :

Example 21 with GraphIOException

use of android.filterfw.io.GraphIOException in project platform_frameworks_base by android.

the class TextGraphReader method readKeyValueAssignments.

private KeyValueMap readKeyValueAssignments(PatternScanner scanner, Pattern endPattern) throws GraphIOException {
    // Our parser is a state-machine, and these are our states
    final int STATE_IDENTIFIER = 0;
    final int STATE_EQUALS = 1;
    final int STATE_VALUE = 2;
    final int STATE_POST_VALUE = 3;
    final Pattern equalsPattern = Pattern.compile("=");
    final Pattern semicolonPattern = Pattern.compile(";");
    final Pattern wordPattern = Pattern.compile("[a-zA-Z]+[a-zA-Z0-9]*");
    final Pattern stringPattern = Pattern.compile("'[^']*'|\\\"[^\\\"]*\\\"");
    final Pattern intPattern = Pattern.compile("[0-9]+");
    final Pattern floatPattern = Pattern.compile("[0-9]*\\.[0-9]+f?");
    final Pattern referencePattern = Pattern.compile("\\$[a-zA-Z]+[a-zA-Z0-9]");
    final Pattern booleanPattern = Pattern.compile("true|false");
    int state = STATE_IDENTIFIER;
    KeyValueMap newVals = new KeyValueMap();
    String curKey = null;
    String curValue = null;
    while (!scanner.atEnd() && !(endPattern != null && scanner.peek(endPattern))) {
        switch(state) {
            case STATE_IDENTIFIER:
                curKey = scanner.eat(wordPattern, "<identifier>");
                state = STATE_EQUALS;
                break;
            case STATE_EQUALS:
                scanner.eat(equalsPattern, "=");
                state = STATE_VALUE;
                break;
            case STATE_VALUE:
                if ((curValue = scanner.tryEat(stringPattern)) != null) {
                    newVals.put(curKey, curValue.substring(1, curValue.length() - 1));
                } else if ((curValue = scanner.tryEat(referencePattern)) != null) {
                    String refName = curValue.substring(1, curValue.length());
                    Object referencedObject = mBoundReferences != null ? mBoundReferences.get(refName) : null;
                    if (referencedObject == null) {
                        throw new GraphIOException("Unknown object reference to '" + refName + "'!");
                    }
                    newVals.put(curKey, referencedObject);
                } else if ((curValue = scanner.tryEat(booleanPattern)) != null) {
                    newVals.put(curKey, Boolean.parseBoolean(curValue));
                } else if ((curValue = scanner.tryEat(floatPattern)) != null) {
                    newVals.put(curKey, Float.parseFloat(curValue));
                } else if ((curValue = scanner.tryEat(intPattern)) != null) {
                    newVals.put(curKey, Integer.parseInt(curValue));
                } else {
                    throw new GraphIOException(scanner.unexpectedTokenMessage("<value>"));
                }
                state = STATE_POST_VALUE;
                break;
            case STATE_POST_VALUE:
                scanner.eat(semicolonPattern, ";");
                state = STATE_IDENTIFIER;
                break;
        }
    }
    // Make sure end is expected
    if (state != STATE_IDENTIFIER && state != STATE_POST_VALUE) {
        throw new GraphIOException("Unexpected end of assignments on line " + scanner.lineNo() + "!");
    }
    return newVals;
}
Also used : Pattern(java.util.regex.Pattern) KeyValueMap(android.filterfw.core.KeyValueMap) String(java.lang.String) GraphIOException(android.filterfw.io.GraphIOException)

Example 22 with GraphIOException

use of android.filterfw.io.GraphIOException in project platform_frameworks_base by android.

the class FilterGraphEffect method createGraph.

private void createGraph(String graphString) {
    GraphReader reader = new TextGraphReader();
    try {
        mGraph = reader.readGraphString(graphString);
    } catch (GraphIOException e) {
        throw new RuntimeException("Could not setup effect", e);
    }
    if (mGraph == null) {
        throw new RuntimeException("Could not setup effect");
    }
    mRunner = new SyncRunner(getFilterContext(), mGraph, mSchedulerClass);
}
Also used : GraphReader(android.filterfw.io.GraphReader) TextGraphReader(android.filterfw.io.TextGraphReader) TextGraphReader(android.filterfw.io.TextGraphReader) GraphIOException(android.filterfw.io.GraphIOException) SyncRunner(android.filterfw.core.SyncRunner)

Example 23 with GraphIOException

use of android.filterfw.io.GraphIOException in project android_frameworks_base by AOSPA.

the class FilterGraphEffect method createGraph.

private void createGraph(String graphString) {
    GraphReader reader = new TextGraphReader();
    try {
        mGraph = reader.readGraphString(graphString);
    } catch (GraphIOException e) {
        throw new RuntimeException("Could not setup effect", e);
    }
    if (mGraph == null) {
        throw new RuntimeException("Could not setup effect");
    }
    mRunner = new SyncRunner(getFilterContext(), mGraph, mSchedulerClass);
}
Also used : GraphReader(android.filterfw.io.GraphReader) TextGraphReader(android.filterfw.io.TextGraphReader) TextGraphReader(android.filterfw.io.TextGraphReader) GraphIOException(android.filterfw.io.GraphIOException) SyncRunner(android.filterfw.core.SyncRunner)

Example 24 with GraphIOException

use of android.filterfw.io.GraphIOException in project android_frameworks_base by AOSPA.

the class GraphReader method readGraphResource.

public FilterGraph readGraphResource(Context context, int resourceId) throws GraphIOException {
    InputStream inputStream = context.getResources().openRawResource(resourceId);
    InputStreamReader reader = new InputStreamReader(inputStream);
    StringWriter writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        int bytesRead;
        while ((bytesRead = reader.read(buffer, 0, 1024)) > 0) {
            writer.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not read specified resource file!");
    }
    return readGraphString(writer.toString());
}
Also used : InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) GraphIOException(android.filterfw.io.GraphIOException) IOException(java.io.IOException)

Example 25 with GraphIOException

use of android.filterfw.io.GraphIOException in project android_frameworks_base by ResurrectionRemix.

the class GraphReader method readGraphResource.

public FilterGraph readGraphResource(Context context, int resourceId) throws GraphIOException {
    InputStream inputStream = context.getResources().openRawResource(resourceId);
    InputStreamReader reader = new InputStreamReader(inputStream);
    StringWriter writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        int bytesRead;
        while ((bytesRead = reader.read(buffer, 0, 1024)) > 0) {
            writer.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not read specified resource file!");
    }
    return readGraphString(writer.toString());
}
Also used : InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) GraphIOException(android.filterfw.io.GraphIOException) IOException(java.io.IOException)

Aggregations

GraphIOException (android.filterfw.io.GraphIOException)30 KeyValueMap (android.filterfw.core.KeyValueMap)18 TextGraphReader (android.filterfw.io.TextGraphReader)12 String (java.lang.String)12 Pattern (java.util.regex.Pattern)12 SyncRunner (android.filterfw.core.SyncRunner)6 GraphReader (android.filterfw.io.GraphReader)6 PatternScanner (android.filterfw.io.PatternScanner)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 InputStreamReader (java.io.InputStreamReader)6 StringWriter (java.io.StringWriter)6