001: // TR9401CatalogReader.java - Read OASIS Catalog files
002:
003: /*
004: * Copyright 2001-2004 The Apache Software Foundation or its licensors,
005: * as applicable.
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package com.sun.org.apache.xml.internal.resolver.readers;
021:
022: import java.io.InputStream;
023: import java.io.IOException;
024: import java.net.MalformedURLException;
025: import java.util.Vector;
026: import com.sun.org.apache.xml.internal.resolver.Catalog;
027: import com.sun.org.apache.xml.internal.resolver.CatalogEntry;
028: import com.sun.org.apache.xml.internal.resolver.CatalogException;
029:
030: /**
031: * Parses OASIS Open Catalog files.
032: *
033: * <p>This class reads OASIS Open Catalog files, returning a stream
034: * of tokens.</p>
035: *
036: * <p>This code interrogates the following non-standard system properties:</p>
037: *
038: * <dl>
039: * <dt><b>xml.catalog.debug</b></dt>
040: * <dd><p>Sets the debug level. A value of 0 is assumed if the
041: * property is not set or is not a number.</p></dd>
042: * </dl>
043: *
044: * @see Catalog
045: *
046: * @author Norman Walsh
047: * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
048: *
049: * @version 1.0
050: */
051: public class TR9401CatalogReader extends TextCatalogReader {
052:
053: /**
054: * Start parsing an OASIS TR9401 Open Catalog file. The file is
055: * actually read and parsed
056: * as needed by <code>nextEntry</code>.
057: *
058: * <p>In a TR9401 Catalog the 'DELEGATE' entry delegates public
059: * identifiers. There is no delegate entry for system identifiers
060: * or URIs.</p>
061: *
062: * @param catalog The Catalog to populate
063: * @param is The input stream from which to read the TR9401 Catalog
064: *
065: * @throws MalformedURLException Improper fileUrl
066: * @throws IOException Error reading catalog file
067: */
068: public void readCatalog(Catalog catalog, InputStream is)
069: throws MalformedURLException, IOException {
070:
071: catfile = is;
072:
073: if (catfile == null) {
074: return;
075: }
076:
077: Vector unknownEntry = null;
078:
079: try {
080: while (true) {
081: String token = nextToken();
082:
083: if (token == null) {
084: if (unknownEntry != null) {
085: catalog.unknownEntry(unknownEntry);
086: unknownEntry = null;
087: }
088: catfile.close();
089: catfile = null;
090: return;
091: }
092:
093: String entryToken = null;
094: if (caseSensitive) {
095: entryToken = token;
096: } else {
097: entryToken = token.toUpperCase();
098: }
099:
100: if (entryToken.equals("DELEGATE")) {
101: entryToken = "DELEGATE_PUBLIC";
102: }
103:
104: try {
105: int type = CatalogEntry.getEntryType(entryToken);
106: int numArgs = CatalogEntry.getEntryArgCount(type);
107: Vector args = new Vector();
108:
109: if (unknownEntry != null) {
110: catalog.unknownEntry(unknownEntry);
111: unknownEntry = null;
112: }
113:
114: for (int count = 0; count < numArgs; count++) {
115: args.addElement(nextToken());
116: }
117:
118: catalog
119: .addEntry(new CatalogEntry(entryToken, args));
120: } catch (CatalogException cex) {
121: if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
122: if (unknownEntry == null) {
123: unknownEntry = new Vector();
124: }
125: unknownEntry.addElement(token);
126: } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
127: catalog.getCatalogManager().debug.message(1,
128: "Invalid catalog entry", token);
129: unknownEntry = null;
130: } else if (cex.getExceptionType() == CatalogException.UNENDED_COMMENT) {
131: catalog.getCatalogManager().debug.message(1,
132: cex.getMessage());
133: }
134: }
135: }
136: } catch (CatalogException cex2) {
137: if (cex2.getExceptionType() == CatalogException.UNENDED_COMMENT) {
138: catalog.getCatalogManager().debug.message(1, cex2
139: .getMessage());
140: }
141: }
142:
143: }
144: }
|