1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 package android.filterfw.io; 19 20 import android.content.Context; 21 import android.filterfw.core.FilterGraph; 22 import android.filterfw.core.KeyValueMap; 23 import android.filterfw.io.GraphIOException; 24 25 import java.io.InputStream; 26 import java.io.InputStreamReader; 27 import java.io.IOException; 28 import java.io.StringWriter; 29 30 /** 31 * @hide 32 */ 33 public abstract class GraphReader { 34 35 protected KeyValueMap mReferences = new KeyValueMap(); 36 readGraphString(String graphString)37 public abstract FilterGraph readGraphString(String graphString) throws GraphIOException; 38 readKeyValueAssignments(String assignments)39 public abstract KeyValueMap readKeyValueAssignments(String assignments) throws GraphIOException; 40 readGraphResource(Context context, int resourceId)41 public FilterGraph readGraphResource(Context context, int resourceId) throws GraphIOException { 42 InputStream inputStream = context.getResources().openRawResource(resourceId); 43 InputStreamReader reader = new InputStreamReader(inputStream); 44 StringWriter writer = new StringWriter(); 45 char[] buffer = new char[1024]; 46 try { 47 int bytesRead; 48 while ((bytesRead = reader.read(buffer, 0, 1024)) > 0) { 49 writer.write(buffer, 0, bytesRead); 50 } 51 } catch (IOException e) { 52 throw new RuntimeException("Could not read specified resource file!"); 53 } 54 return readGraphString(writer.toString()); 55 } 56 addReference(String name, Object object)57 public void addReference(String name, Object object) { 58 mReferences.put(name, object); 59 } 60 addReferencesByMap(KeyValueMap refs)61 public void addReferencesByMap(KeyValueMap refs) { 62 mReferences.putAll(refs); 63 } 64 addReferencesByKeysAndValues(Object... references)65 public void addReferencesByKeysAndValues(Object... references) { 66 mReferences.setKeyValues(references); 67 } 68 69 } 70