KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > resolver > readers > TextCatalogReader


1 // TextCatalogReader.java - Read text/plain Catalog files
2

3 /*
4  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
5  * as applicable.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.sun.org.apache.xml.internal.resolver.readers;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.Stack JavaDoc;
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 /**
36  * Parses plain text Catalog files.
37  *
38  * <p>This class reads plain text Open Catalog files.</p>
39  *
40  * @see Catalog
41  *
42  * @author Norman Walsh
43  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
44  *
45  * @version 1.0
46  */

47 public class TextCatalogReader implements CatalogReader {
48   /** The input stream used to read the catalog */
49   protected InputStream JavaDoc catfile = null;
50
51   /**
52    * Character lookahead stack. Reading a catalog sometimes requires
53    * up to two characters of lookahead.
54    */

55   protected int[] stack = new int[3];
56
57   /**
58    * Token stack. Recognizing an unexpected catalog entry requires
59    * the ability to "push back" a token.
60    */

61   protected Stack JavaDoc tokenStack = new Stack JavaDoc();
62
63   /** The current position on the lookahead stack */
64   protected int top = -1;
65
66   /** Are keywords in the catalog case sensitive? */
67   protected boolean caseSensitive = false;
68
69   /**
70    * Construct a CatalogReader object.
71    */

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   /**
83    * Start parsing a text catalog file. The file is
84    * actually read and parsed
85    * as needed by <code>nextEntry</code>.</p>
86    *
87    * @param fileUrl The URL or filename of the catalog file to process
88    *
89    * @throws MalformedURLException Improper fileUrl
90    * @throws IOException Error reading catalog file
91    */

92   public void readCatalog(Catalog catalog, String JavaDoc fileUrl)
93     throws MalformedURLException JavaDoc, IOException JavaDoc {
94     URL JavaDoc catURL = null;
95
96     try {
97       catURL = new URL JavaDoc(fileUrl);
98     } catch (MalformedURLException JavaDoc e) {
99       catURL = new URL JavaDoc("file:///" + fileUrl);
100     }
101
102     URLConnection JavaDoc urlCon = catURL.openConnection();
103     try {
104       readCatalog(catalog, urlCon.getInputStream());
105     } catch (FileNotFoundException JavaDoc 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 JavaDoc is)
112     throws MalformedURLException JavaDoc, IOException JavaDoc {
113
114     catfile = is;
115
116     if (catfile == null) {
117       return;
118     }
119
120     Vector JavaDoc unknownEntry = null;
121
122     try {
123       while (true) {
124     String JavaDoc 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 JavaDoc 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 JavaDoc args = new Vector JavaDoc();
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 JavaDoc();
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   /**
180      * The destructor.
181      *
182      * <p>Makes sure the catalog file is closed.</p>
183      */

184   protected void finalize() {
185     if (catfile != null) {
186       try {
187     catfile.close();
188       } catch (IOException JavaDoc e) {
189     // whatever...
190
}
191     }
192     catfile = null;
193   }
194
195   // -----------------------------------------------------------------
196

197     /**
198      * Return the next token in the catalog file.
199      *
200      * <p>FYI: This code does not throw any sort of exception for
201      * a file that contains an n
202      *
203      * @return The Catalog file token from the input stream.
204      * @throws IOException If an error occurs reading from the stream.
205      */

206   protected String JavaDoc nextToken() throws IOException JavaDoc, CatalogException {
207     String JavaDoc token = "";
208     int ch, nextch;
209
210     if (!tokenStack.empty()) {
211       return (String JavaDoc) tokenStack.pop();
212     }
213
214     // Skip over leading whitespace and comments
215
while (true) {
216       // skip leading whitespace
217
ch = catfile.read();
218       while (ch <= ' ') { // all ctrls are whitespace
219
ch = catfile.read();
220     if (ch < 0) {
221       return null;
222     }
223       }
224
225       // now 'ch' is the current char from the file
226
nextch = catfile.read();
227       if (nextch < 0) {
228     return null;
229       }
230
231       if (ch == '-' && nextch == '-') {
232     // we've found a comment, skip it...
233
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     // Ok, we've found the end of the comment,
246
// loop back to the top and start again...
247
} 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 JavaDoc s = new String JavaDoc(chararr);
261     token = token.concat(s);
262       }
263       return token;
264     } else {
265       // return the next whitespace or comment delimited
266
// string
267
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 JavaDoc s = new String JavaDoc(chararr);
277       token = token.concat(s);
278       ch = nextch;
279     }
280       }
281       return token;
282     }
283   }
284
285   /**
286      * Return the next logical character from the input stream.
287      *
288      * @return The next (logical) character from the input stream. The
289      * character may be buffered from a previous lookahead.
290      *
291      * @throws IOException If an error occurs reading from the stream.
292      */

293   protected int nextChar() throws IOException JavaDoc {
294     if (top < 0) {
295       return catfile.read();
296     } else {
297       return stack[top--];
298     }
299   }
300 }
301
Popular Tags