1 3 19 20 package com.sun.org.apache.xml.internal.resolver.readers; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.FileNotFoundException ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.net.MalformedURLException ; 28 import java.util.Vector ; 29 import java.util.Stack ; 30 import com.sun.org.apache.xml.internal.resolver.Catalog; 31 import com.sun.org.apache.xml.internal.resolver.CatalogEntry; 32 import com.sun.org.apache.xml.internal.resolver.CatalogException; 33 import com.sun.org.apache.xml.internal.resolver.readers.CatalogReader; 34 35 47 public class TextCatalogReader implements CatalogReader { 48 49 protected InputStream catfile = null; 50 51 55 protected int[] stack = new int[3]; 56 57 61 protected Stack tokenStack = new Stack (); 62 63 64 protected int top = -1; 65 66 67 protected boolean caseSensitive = false; 68 69 72 public TextCatalogReader() { } 73 74 public void setCaseSensitive(boolean isCaseSensitive) { 75 caseSensitive = isCaseSensitive; 76 } 77 78 public boolean getCaseSensitive() { 79 return caseSensitive; 80 } 81 82 92 public void readCatalog(Catalog catalog, String fileUrl) 93 throws MalformedURLException , IOException { 94 URL catURL = null; 95 96 try { 97 catURL = new URL (fileUrl); 98 } catch (MalformedURLException e) { 99 catURL = new URL ("file:///" + fileUrl); 100 } 101 102 URLConnection urlCon = catURL.openConnection(); 103 try { 104 readCatalog(catalog, urlCon.getInputStream()); 105 } catch (FileNotFoundException e) { 106 catalog.getCatalogManager().debug.message(1, "Failed to load catalog, file not found", 107 catURL.toString()); 108 } 109 } 110 111 public void readCatalog(Catalog catalog, InputStream is) 112 throws MalformedURLException , IOException { 113 114 catfile = is; 115 116 if (catfile == null) { 117 return; 118 } 119 120 Vector unknownEntry = null; 121 122 try { 123 while (true) { 124 String token = nextToken(); 125 126 if (token == null) { 127 if (unknownEntry != null) { 128 catalog.unknownEntry(unknownEntry); 129 unknownEntry = null; 130 } 131 catfile.close(); 132 catfile = null; 133 return; 134 } 135 136 String entryToken = null; 137 if (caseSensitive) { 138 entryToken = token; 139 } else { 140 entryToken = token.toUpperCase(); 141 } 142 143 try { 144 int type = CatalogEntry.getEntryType(entryToken); 145 int numArgs = CatalogEntry.getEntryArgCount(type); 146 Vector args = new Vector (); 147 148 if (unknownEntry != null) { 149 catalog.unknownEntry(unknownEntry); 150 unknownEntry = null; 151 } 152 153 for (int count = 0; count < numArgs; count++) { 154 args.addElement(nextToken()); 155 } 156 157 catalog.addEntry(new CatalogEntry(entryToken, args)); 158 } catch (CatalogException cex) { 159 if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) { 160 if (unknownEntry == null) { 161 unknownEntry = new Vector (); 162 } 163 unknownEntry.addElement(token); 164 } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) { 165 catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", token); 166 unknownEntry = null; 167 } else if (cex.getExceptionType() == CatalogException.UNENDED_COMMENT) { 168 catalog.getCatalogManager().debug.message(1, cex.getMessage()); 169 } 170 } 171 } 172 } catch (CatalogException cex2) { 173 if (cex2.getExceptionType() == CatalogException.UNENDED_COMMENT) { 174 catalog.getCatalogManager().debug.message(1, cex2.getMessage()); 175 } 176 } 177 } 178 179 184 protected void finalize() { 185 if (catfile != null) { 186 try { 187 catfile.close(); 188 } catch (IOException e) { 189 } 191 } 192 catfile = null; 193 } 194 195 197 206 protected String nextToken() throws IOException , CatalogException { 207 String token = ""; 208 int ch, nextch; 209 210 if (!tokenStack.empty()) { 211 return (String ) tokenStack.pop(); 212 } 213 214 while (true) { 216 ch = catfile.read(); 218 while (ch <= ' ') { ch = catfile.read(); 220 if (ch < 0) { 221 return null; 222 } 223 } 224 225 nextch = catfile.read(); 227 if (nextch < 0) { 228 return null; 229 } 230 231 if (ch == '-' && nextch == '-') { 232 ch = ' '; 234 nextch = nextChar(); 235 while ((ch != '-' || nextch != '-') && nextch > 0) { 236 ch = nextch; 237 nextch = nextChar(); 238 } 239 240 if (nextch < 0) { 241 throw new CatalogException(CatalogException.UNENDED_COMMENT, 242 "Unterminated comment in catalog file; EOF treated as end-of-comment."); 243 } 244 245 } else { 248 stack[++top] = nextch; 249 stack[++top] = ch; 250 break; 251 } 252 } 253 254 ch = nextChar(); 255 if (ch == '"' || ch == '\'') { 256 int quote = ch; 257 while ((ch = nextChar()) != quote) { 258 char[] chararr = new char[1]; 259 chararr[0] = (char) ch; 260 String s = new String (chararr); 261 token = token.concat(s); 262 } 263 return token; 264 } else { 265 while (ch > ' ') { 268 nextch = nextChar(); 269 if (ch == '-' && nextch == '-') { 270 stack[++top] = ch; 271 stack[++top] = nextch; 272 return token; 273 } else { 274 char[] chararr = new char[1]; 275 chararr[0] = (char) ch; 276 String s = new String (chararr); 277 token = token.concat(s); 278 ch = nextch; 279 } 280 } 281 return token; 282 } 283 } 284 285 293 protected int nextChar() throws IOException { 294 if (top < 0) { 295 return catfile.read(); 296 } else { 297 return stack[top--]; 298 } 299 } 300 } 301 | Popular Tags |