KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > io > AbstractTokenFilterReader


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.io;
25
26 import java.io.FilterReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.Reader JavaDoc;
29
30 /**
31  * Abstract base class for FilterReaders that replace <code>${placehoder}</code>
32  * tokens.
33  *
34  * @author Felix Gnass [fgnass at neteye dot de]
35  */

36 public abstract class AbstractTokenFilterReader extends FilterReader JavaDoc {
37
38     private String JavaDoc replacement = null;
39     
40     private int replaceIndex = -1;
41         
42     public AbstractTokenFilterReader(Reader JavaDoc in) {
43         super(in);
44     }
45     
46     public int read() throws IOException JavaDoc {
47     
48         if (replaceIndex != -1) {
49             // Fill in replacement ...
50
int i = replacement.charAt(replaceIndex++);
51             if (replaceIndex == replacement.length()) {
52                 replaceIndex = -1;
53             }
54             return i;
55         }
56         
57         int i = super.read();
58         if (i != '$') {
59             // Normal character - no further processing is required
60
return i;
61         }
62                 
63         // Read ahead to check if next character is '{'
64
char c = (char) super.read();
65         if (c != '{') {
66             // Just a single '$' so we set the replacement to the second char
67
replacement = Character.toString(c);
68             replaceIndex = 0;
69             return '$';
70         }
71
72         // We encountered a '${' sequence, so we'll read on until '}' or EOF
73
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
74         boolean endReached = false;
75         while (!endReached) {
76             c = (char) super.read();
77             endReached = c == -1 || c == '}';
78             if (!endReached) {
79                 buffer.append(c);
80             }
81         }
82         
83         if (c == -1) {
84             throw new IOException JavaDoc("EOF encountered but '}' was expected.");
85         }
86         
87         String JavaDoc key = buffer.toString();
88         replacement = getReplacement(key);
89         if (replacement != null && replacement.length() > 0) {
90             replaceIndex = 0;
91         }
92         return read();
93     }
94     
95     /**
96      * @see Reader#read(char[], int, int)
97      */

98     public final int read(char[] cbuf, int off, int len) throws IOException JavaDoc {
99         for (int i = 0; i < len; i++) {
100             final int ch = read();
101             if (ch == -1) {
102                 return i == 0 ? -1 : i;
103             }
104             cbuf[off + i] = (char) ch;
105         }
106         return len;
107     }
108     
109     protected abstract String JavaDoc getReplacement(String JavaDoc key);
110
111 }
112
Popular Tags