1 3 56 57 package org.jboss.util.xml.catalog.readers; 58 59 import java.io.InputStream ; 60 import java.io.IOException ; 61 import java.io.FileNotFoundException ; 62 import java.net.URL ; 63 import java.net.URLConnection ; 64 import java.net.MalformedURLException ; 65 import java.util.Vector ; 66 import java.util.Stack ; 67 import org.jboss.util.xml.catalog.Catalog; 68 import org.jboss.util.xml.catalog.CatalogEntry; 69 import org.jboss.util.xml.catalog.CatalogException; 70 import org.jboss.util.xml.catalog.readers.CatalogReader; 71 72 84 public class TextCatalogReader implements CatalogReader { 85 86 protected InputStream catfile = null; 87 88 92 protected int[] stack = new int[3]; 93 94 98 protected Stack tokenStack = new Stack (); 99 100 101 protected int top = -1; 102 103 104 protected boolean caseSensitive = false; 105 106 109 public TextCatalogReader() { } 110 111 public void setCaseSensitive(boolean isCaseSensitive) { 112 caseSensitive = isCaseSensitive; 113 } 114 115 public boolean getCaseSensitive() { 116 return caseSensitive; 117 } 118 119 129 public void readCatalog(Catalog catalog, String fileUrl) 130 throws MalformedURLException , IOException { 131 URL catURL = null; 132 133 try { 134 catURL = new URL (fileUrl); 135 } catch (MalformedURLException e) { 136 catURL = new URL ("file:///" + fileUrl); 137 } 138 139 URLConnection urlCon = catURL.openConnection(); 140 try { 141 readCatalog(catalog, urlCon.getInputStream()); 142 } catch (FileNotFoundException e) { 143 catalog.getCatalogManager().debug.message(1, "Failed to load catalog, file not found", 144 catURL.toString()); 145 } 146 } 147 148 public void readCatalog(Catalog catalog, InputStream is) 149 throws MalformedURLException , IOException { 150 151 catfile = is; 152 153 if (catfile == null) { 154 return; 155 } 156 157 Vector unknownEntry = null; 158 159 while (true) { 160 String token = nextToken(); 161 162 if (token == null) { 163 if (unknownEntry != null) { 164 catalog.unknownEntry(unknownEntry); 165 unknownEntry = null; 166 } 167 catfile.close(); 168 catfile = null; 169 return; 170 } 171 172 String entryToken = null; 173 if (caseSensitive) { 174 entryToken = token; 175 } else { 176 entryToken = token.toUpperCase(); 177 } 178 179 try { 180 int type = CatalogEntry.getEntryType(entryToken); 181 int numArgs = CatalogEntry.getEntryArgCount(type); 182 Vector args = new Vector (); 183 184 if (unknownEntry != null) { 185 catalog.unknownEntry(unknownEntry); 186 unknownEntry = null; 187 } 188 189 for (int count = 0; count < numArgs; count++) { 190 args.addElement(nextToken()); 191 } 192 193 catalog.addEntry(new CatalogEntry(entryToken, args)); 194 } catch (CatalogException cex) { 195 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 196 if (unknownEntry == null) { 197 unknownEntry = new Vector (); 198 } 199 unknownEntry.addElement(token); 200 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 201 catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", token); 202 unknownEntry = null; 203 } 204 } 205 } 206 } 207 208 213 protected void finalize() { 214 if (catfile != null) { 215 try { 216 catfile.close(); 217 } catch (IOException e) { 218 } 220 } 221 catfile = null; 222 } 223 224 226 232 protected String nextToken() throws IOException { 233 String token = ""; 234 int ch, nextch; 235 236 if (!tokenStack.empty()) { 237 return (String ) tokenStack.pop(); 238 } 239 240 while (true) { 242 ch = catfile.read(); 244 while (ch <= ' ') { ch = catfile.read(); 246 if (ch < 0) { 247 return null; 248 } 249 } 250 251 nextch = catfile.read(); 253 if (nextch < 0) { 254 return null; 255 } 256 257 if (ch == '-' && nextch == '-') { 258 ch = ' '; 260 nextch = nextChar(); 261 while (ch != '-' || nextch != '-') { 262 ch = nextch; 263 nextch = nextChar(); 264 } 265 266 } else { 269 stack[++top] = nextch; 270 stack[++top] = ch; 271 break; 272 } 273 } 274 275 ch = nextChar(); 276 if (ch == '"' || ch == '\'') { 277 int quote = ch; 278 while ((ch = nextChar()) != quote) { 279 char[] chararr = new char[1]; 280 chararr[0] = (char) ch; 281 String s = new String (chararr); 282 token = token.concat(s); 283 } 284 return token; 285 } else { 286 while (ch > ' ') { 289 nextch = nextChar(); 290 if (ch == '-' && nextch == '-') { 291 stack[++top] = ch; 292 stack[++top] = nextch; 293 return token; 294 } else { 295 char[] chararr = new char[1]; 296 chararr[0] = (char) ch; 297 String s = new String (chararr); 298 token = token.concat(s); 299 ch = nextch; 300 } 301 } 302 return token; 303 } 304 } 305 306 314 protected int nextChar() throws IOException { 315 if (top < 0) { 316 return catfile.read(); 317 } else { 318 return stack[top--]; 319 } 320 } 321 } 322 | Popular Tags |