Home | History | Annotate | Download | only in templates
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
      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 package com.android.ide.eclipse.ndk.internal.templates;
     18 
     19 import java.io.IOException;
     20 import java.io.InputStream;
     21 import java.util.Map;
     22 
     23 /**
     24  * Reads from a template substituting marked values from the supplied Map.
     25  */
     26 public class TemplatedInputStream extends InputStream {
     27 
     28     private final InputStream mIn;
     29     private final Map<String, String> mMap;
     30     private char[] mSub;
     31     private int mPos;
     32     private int mMark;
     33 
     34     public TemplatedInputStream(InputStream in, Map<String, String> map) {
     35         this.mIn = in;
     36         this.mMap = map;
     37     }
     38 
     39     @Override
     40     public int read() throws IOException {
     41         // if from a mark, return the char
     42         if (mMark != 0) {
     43             int c = mMark;
     44             mMark = 0;
     45             return c;
     46         }
     47 
     48         // return char from sub layer if available
     49         if (mSub != null) {
     50             char c = mSub[mPos++];
     51             if (mPos >= mSub.length)
     52                 mSub = null;
     53             return c;
     54         }
     55 
     56         int c = mIn.read();
     57         if (c == '%') {
     58             // check if it's a sub
     59             c = mIn.read();
     60             if (c == '{') {
     61                 // it's a sub
     62                 StringBuffer buff = new StringBuffer();
     63                 for (c = mIn.read(); c != '}' && c >= 0; c = mIn.read())
     64                     buff.append((char) c);
     65                 String str = mMap.get(buff.toString());
     66                 if (str != null) {
     67                     mSub = str.toCharArray();
     68                     mPos = 0;
     69                 }
     70                 return read(); // recurse to get the real char
     71             } else {
     72                 // not a sub
     73                 mMark = c;
     74                 return '%';
     75             }
     76         }
     77 
     78         return c;
     79     }
     80 
     81     @Override
     82     public void close() throws IOException {
     83         super.close();
     84         mIn.close();
     85     }
     86 
     87 }
     88