| 1 |
// Copyright 2001, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.IOException; |
| 5 |
import java.io.InputStream; |
| 6 |
import java.util.Properties; |
| 7 |
|
| 8 |
/** |
| 9 |
* The ConditionalInputStream reads a stream and filters certain parts depending |
| 10 |
* of properties and statements in the input. |
| 11 |
* <P> |
| 12 |
* The following statements, all start with the |
| 13 |
* |
| 14 |
* &at;-sign, are allowed: |
| 15 |
* |
| 16 |
* <UL> |
| 17 |
* <LI><B>@ifdef property</B>, reads everything up to the next |
| 18 |
* @statement if the property is defined. |
| 19 |
* <LI><B>@ifndef property</B>, reads everything up to the next |
| 20 |
* @statement if the property is not defined. |
| 21 |
* <LI><B>@else</B>, corresponding else statement |
| 22 |
* <LI><B>@endif</B>, corresponging endif statement |
| 23 |
* </UL> |
| 24 |
* |
| 25 |
* The |
| 26 |
* &at;-sign itself must be escaped by a backslash, if used in the text followed by |
| 27 |
* any of the keywords described above and no action should be taken. |
| 28 |
* |
| 29 |
* <P> |
| 30 |
* IMPORTANT: inherits from InputStream rather than FilterInputStream so that |
| 31 |
* the correct read(byte[], int, int) method is used. |
| 32 |
* |
| 33 |
* @author Mark Donszelmann |
| 34 |
* @version $Id: ConditionalInputStream.java 8584 2006-08-10 23:06:37Z duns $ |
| 35 |
*/ |
| 36 |
public class ConditionalInputStream extends InputStream { |
| 37 |
|
| 38 |
private int[] buffer = new int[4096]; |
| 39 |
|
| 40 |
private int index; |
| 41 |
|
| 42 |
private int len; |
| 43 |
|
| 44 |
private InputStream in; |
| 45 |
|
| 46 |
private Properties defines; |
| 47 |
|
| 48 |
private int nesting; |
| 49 |
|
| 50 |
private boolean[] ok = new boolean[50]; |
| 51 |
|
| 52 |
private boolean escape; |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates a Conditional Input Stream from given stream. |
| 56 |
* |
| 57 |
* @param input stream to read from |
| 58 |
* @param defines set of properties to be used in ifdefs |
| 59 |
*/ |
| 60 |
public ConditionalInputStream(InputStream input, Properties defines) { |
| 61 |
super(); |
| 62 |
in = input; |
| 63 |
this.defines = defines; |
| 64 |
nesting = 0; |
| 65 |
escape = false; |
| 66 |
index = 0; |
| 67 |
len = 0; |
| 68 |
} |
| 69 |
|
| 70 |
public int read() throws IOException { |
| 71 |
|
| 72 |
int b; |
| 73 |
int n; |
| 74 |
|
| 75 |
// read from buffer if possible |
| 76 |
if (index < len) { |
| 77 |
b = buffer[index]; |
| 78 |
index++; |
| 79 |
} else { |
| 80 |
b = in.read(); |
| 81 |
} |
| 82 |
|
| 83 |
// return if End Of Stream |
| 84 |
if (b < 0) |
| 85 |
return -1; |
| 86 |
|
| 87 |
// escape \@-signs |
| 88 |
if (b == '\\') { |
| 89 |
n = in.read(); |
| 90 |
if (n == '@') { |
| 91 |
b = ' '; |
| 92 |
escape = true; |
| 93 |
} |
| 94 |
buffer[0] = n; |
| 95 |
index = 0; |
| 96 |
len = 1; |
| 97 |
} |
| 98 |
|
| 99 |
// check on @ sign |
| 100 |
if (b == '@') { |
| 101 |
if (escape) { |
| 102 |
escape = false; |
| 103 |
} else { |
| 104 |
// read keyword (ifdef, ifndef, else, endif |
| 105 |
index = 0; |
| 106 |
StringBuffer s = new StringBuffer(); |
| 107 |
n = in.read(); |
| 108 |
while ((n >= 0) && !Character.isWhitespace((char) n)) { |
| 109 |
s.append((char) n); |
| 110 |
buffer[index] = n; |
| 111 |
n = in.read(); |
| 112 |
index++; |
| 113 |
} |
| 114 |
buffer[index] = n; |
| 115 |
index++; |
| 116 |
b = ' '; |
| 117 |
|
| 118 |
// check on keyword |
| 119 |
String keyword = s.toString(); |
| 120 |
if (keyword.equals("ifdef") || keyword.equals("ifndef")) { |
| 121 |
|
| 122 |
// skip whitespace and read property |
| 123 |
s = new StringBuffer(); |
| 124 |
n = in.read(); |
| 125 |
while ((n >= 0) && Character.isWhitespace((char) n)) { |
| 126 |
buffer[index] = n; |
| 127 |
n = in.read(); |
| 128 |
index++; |
| 129 |
} |
| 130 |
while ((n >= 0) && !Character.isWhitespace((char) n)) { |
| 131 |
s.append((char) n); |
| 132 |
buffer[index] = n; |
| 133 |
n = in.read(); |
| 134 |
index++; |
| 135 |
} |
| 136 |
buffer[index] = n; |
| 137 |
index++; |
| 138 |
|
| 139 |
// check on property |
| 140 |
String property = s.toString(); |
| 141 |
if (defines.getProperty(property) != null) { |
| 142 |
ok[nesting] = (nesting > 0 ? ok[nesting - 1] : true) |
| 143 |
&& keyword.equals("ifdef"); |
| 144 |
} else { |
| 145 |
ok[nesting] = (nesting > 0 ? ok[nesting - 1] : true) |
| 146 |
&& keyword.equals("ifndef"); |
| 147 |
} |
| 148 |
nesting++; |
| 149 |
replaceBufferWithWhitespace(index); |
| 150 |
} else if (keyword.equals("else")) { |
| 151 |
// FIXME one could have multiple elses without endifs... |
| 152 |
// calculate inclusion based on ifdef nesting |
| 153 |
if (nesting <= 0) |
| 154 |
throw new RuntimeException( |
| 155 |
"@else without corresponding @ifdef"); |
| 156 |
ok[nesting - 1] = (nesting > 1 ? ok[nesting - 2] : true) |
| 157 |
&& !ok[nesting - 1]; |
| 158 |
replaceBufferWithWhitespace(index); |
| 159 |
} else if (keyword.equals("endif")) { |
| 160 |
// calculate inclusion based on ifdef nesting |
| 161 |
if (nesting <= 0) |
| 162 |
throw new RuntimeException( |
| 163 |
"@endif without corresponding @ifdef"); |
| 164 |
nesting--; |
| 165 |
replaceBufferWithWhitespace(index); |
| 166 |
} else { |
| 167 |
// not an known @ |
| 168 |
b = '@'; |
| 169 |
} |
| 170 |
len = index; |
| 171 |
index = 0; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if ((nesting > 0) && !ok[nesting - 1]) { |
| 176 |
if (!Character.isWhitespace((char) b)) |
| 177 |
b = ' '; |
| 178 |
} |
| 179 |
return b & 0x00FF; |
| 180 |
} |
| 181 |
|
| 182 |
private void replaceBufferWithWhitespace(int size) { |
| 183 |
for (int i = 0; i < size; i++) { |
| 184 |
if (!Character.isWhitespace((char) buffer[i])) { |
| 185 |
buffer[i] = ' '; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |