KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > xml > catalog > readers > TR9401CatalogReader


1 // TR9401CatalogReader.java - Read OASIS Catalog files
2

3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" must
29  * not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * nor may "Apache" appear in their name, without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 package org.jboss.util.xml.catalog.readers;
58
59 import java.io.InputStream JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.net.MalformedURLException JavaDoc;
62 import java.util.Vector JavaDoc;
63
64 import org.jboss.util.xml.catalog.Catalog;
65 import org.jboss.util.xml.catalog.CatalogEntry;
66 import org.jboss.util.xml.catalog.CatalogException;
67
68 /**
69  * Parses OASIS Open Catalog files.
70  *
71  * <p>This class reads OASIS Open Catalog files, returning a stream
72  * of tokens.</p>
73  *
74  * <p>This code interrogates the following non-standard system properties:</p>
75  *
76  * <dl>
77  * <dt><b>xml.catalog.debug</b></dt>
78  * <dd><p>Sets the debug level. A value of 0 is assumed if the
79  * property is not set or is not a number.</p></dd>
80  * </dl>
81  *
82  * @see Catalog
83  *
84  * @author Norman Walsh
85  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
86  *
87  * @version 1.0
88  */

89 public class TR9401CatalogReader extends TextCatalogReader {
90
91   /**
92    * Start parsing an OASIS TR9401 Open Catalog file. The file is
93    * actually read and parsed
94    * as needed by <code>nextEntry</code>.
95    *
96    * <p>In a TR9401 Catalog the 'DELEGATE' entry delegates public
97    * identifiers. There is no delegate entry for system identifiers
98    * or URIs.</p>
99    *
100    * @param fileUrl The URL or filename of the catalog file to process
101    *
102    * @throws MalformedURLException Improper fileUrl
103    * @throws IOException Error reading catalog file
104    */

105   public void readCatalog(Catalog catalog, InputStream JavaDoc is)
106     throws MalformedURLException JavaDoc, IOException JavaDoc {
107
108     catfile = is;
109
110     if (catfile == null) {
111       return;
112     }
113
114     Vector JavaDoc unknownEntry = null;
115
116     while (true) {
117       String JavaDoc token = nextToken();
118
119       if (token == null) {
120     if (unknownEntry != null) {
121       catalog.unknownEntry(unknownEntry);
122       unknownEntry = null;
123     }
124     catfile.close();
125     catfile = null;
126     return;
127       }
128
129       String JavaDoc entryToken = null;
130       if (caseSensitive) {
131     entryToken = token;
132       } else {
133     entryToken = token.toUpperCase();
134       }
135
136       if (entryToken.equals("DELEGATE")) {
137     entryToken = "DELEGATE_PUBLIC";
138       }
139
140       try {
141     int type = CatalogEntry.getEntryType(entryToken);
142     int numArgs = CatalogEntry.getEntryArgCount(type);
143     Vector JavaDoc args = new Vector JavaDoc();
144
145     if (unknownEntry != null) {
146       catalog.unknownEntry(unknownEntry);
147       unknownEntry = null;
148     }
149
150     for (int count = 0; count < numArgs; count++) {
151       args.addElement(nextToken());
152     }
153
154     catalog.addEntry(new CatalogEntry(entryToken, args));
155       } catch (CatalogException cex) {
156     if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
157       if (unknownEntry == null) {
158         unknownEntry = new Vector JavaDoc();
159       }
160       unknownEntry.addElement(token);
161     } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
162       catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", token);
163       unknownEntry = null;
164     }
165       }
166     }
167   }
168 }
169
Popular Tags