1 28 29 package net.n3.nanoxml; 30 31 import java.io.IOException ; 32 import java.io.Reader ; 33 34 40 class ContentReader extends Reader 41 { 42 43 46 private IXMLReader reader; 47 48 51 private IXMLEntityResolver entityResolver; 52 53 56 private char escapeChar; 57 58 61 private char[] delimiter; 62 63 66 private String charsReadTooMuch; 67 68 71 private int charsToGo; 72 73 76 private boolean useLowLevelReader; 77 78 81 private boolean pastInitialPrefix; 82 83 95 ContentReader(IXMLReader reader, IXMLEntityResolver entityResolver, char escapeChar, 96 char[] delimiter, boolean useLowLevelReader, String prefix) 97 { 98 this.delimiter = delimiter; 99 this.charsToGo = this.delimiter.length; 100 this.charsReadTooMuch = prefix; 101 this.useLowLevelReader = useLowLevelReader; 102 this.pastInitialPrefix = false; 103 this.reader = reader; 104 this.entityResolver = entityResolver; 105 this.escapeChar = escapeChar; 106 } 107 108 111 protected void finalize() throws Throwable 112 { 113 this.reader = null; 114 this.entityResolver = null; 115 this.delimiter = null; 116 this.charsReadTooMuch = null; 117 super.finalize(); 118 } 119 120 131 public int read(char[] buffer, int offset, int size) throws IOException 132 { 133 int charsRead = 0; 134 boolean isEntity[] = new boolean[1]; 135 isEntity[0] = false; 136 137 if ((offset + size) > buffer.length) 138 { 139 size = buffer.length - offset; 140 } 141 142 while ((this.charsToGo > 0) && (charsRead < size)) 143 { 144 char ch; 145 146 if (this.charsReadTooMuch.length() > 0) 147 { 148 ch = this.charsReadTooMuch.charAt(0); 149 this.charsReadTooMuch = this.charsReadTooMuch.substring(1); 150 } 151 else 152 { 153 this.pastInitialPrefix = true; 154 155 try 156 { 157 if (useLowLevelReader) 158 { 159 ch = this.reader.read(); 160 } 161 else 162 { 163 ch = XMLUtil.read(this.reader, isEntity, this.escapeChar, 164 this.entityResolver); 165 166 if (!isEntity[0]) 167 { 168 if (ch == '&') 169 { 170 this.reader.startNewStream(XMLUtil.scanEntity(isEntity, 171 this.reader, this.escapeChar, this.entityResolver)); 172 ch = this.reader.read(); 173 } 174 } 175 } 176 } 177 catch (XMLParseException e) 178 { 179 throw new RuntimeException (e.getMessage()); 180 } 182 } 183 184 if (isEntity[0]) 185 { 186 buffer[offset + charsRead] = ch; 187 charsRead++; 188 } 189 else 190 { 191 if ((ch == (this.delimiter[this.charsToGo - 1])) && pastInitialPrefix) 192 { 193 --this.charsToGo; 194 } 195 else if (this.charsToGo < this.delimiter.length) 196 { 197 this.charsReadTooMuch = new String (this.delimiter, this.charsToGo + 1, 198 this.delimiter.length - this.charsToGo) 199 + ch; 200 this.charsToGo = this.delimiter.length; 201 buffer[offset + charsRead] = this.delimiter[this.charsToGo - 1]; 202 charsRead++; 203 } 204 else 205 { 206 buffer[offset + charsRead] = ch; 207 charsRead++; 208 } 209 } 210 } 211 212 if (charsRead == 0) 213 { 214 charsRead = -1; 215 } 216 217 return charsRead; 218 } 219 220 225 public void close() throws IOException 226 { 227 while (this.charsToGo > 0) 228 { 229 char ch; 230 231 if (this.charsReadTooMuch.length() > 0) 232 { 233 ch = this.charsReadTooMuch.charAt(0); 234 this.charsReadTooMuch = this.charsReadTooMuch.substring(1); 235 } 236 else 237 { 238 if (useLowLevelReader) 239 { 240 ch = this.reader.read(); 241 } 242 else 243 { 244 try 245 { 246 ch = XMLUtil.read(this.reader, null, this.escapeChar, this.entityResolver); 247 } 248 catch (XMLParseException e) 249 { 250 throw new RuntimeException (e.getMessage()); 251 } 253 } 254 } 255 256 if (ch == (this.delimiter[this.charsToGo - 1])) 257 { 258 --this.charsToGo; 259 } 260 else if (this.charsToGo < this.delimiter.length) 261 { 262 this.charsReadTooMuch = new String (this.delimiter, this.charsToGo + 1, 263 this.delimiter.length - this.charsToGo) 264 + ch; 265 this.charsToGo = this.delimiter.length; 266 } 267 } 268 } 269 270 } 271 | Popular Tags |