View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   // TR9401CatalogReader.java - Read OASIS Catalog files
6   
7   /*
8    * Copyright 2001-2004 The Apache Software Foundation or its licensors,
9    * as applicable.
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   */
23  
24  package com.sun.org.apache.xml.internal.resolver.readers;
25  
26  import java.io.InputStream;
27  import java.io.IOException;
28  import java.net.MalformedURLException;
29  import java.util.Vector;
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  
34  /**
35   * Parses OASIS Open Catalog files.
36   *
37   * <p>This class reads OASIS Open Catalog files, returning a stream
38   * of tokens.</p>
39   *
40   * <p>This code interrogates the following non-standard system properties:</p>
41   *
42   * <dl>
43   * <dt><b>xml.catalog.debug</b></dt>
44   * <dd><p>Sets the debug level. A value of 0 is assumed if the
45   * property is not set or is not a number.</p></dd>
46   * </dl>
47   *
48   * @see Catalog
49   *
50   * @author Norman Walsh
51   * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
52   *
53   */
54  public class TR9401CatalogReader extends TextCatalogReader {
55  
56    /**
57     * Start parsing an OASIS TR9401 Open Catalog file. The file is
58     * actually read and parsed
59     * as needed by <code>nextEntry</code>.
60     *
61     * <p>In a TR9401 Catalog the 'DELEGATE' entry delegates public
62     * identifiers. There is no delegate entry for system identifiers
63     * or URIs.</p>
64     *
65     * @param catalog The Catalog to populate
66     * @param is The input stream from which to read the TR9401 Catalog
67     *
68     * @throws MalformedURLException Improper fileUrl
69     * @throws IOException Error reading catalog file
70     */
71    public void readCatalog(Catalog catalog, InputStream is)
72      throws MalformedURLException, IOException {
73  
74      catfile = is;
75  
76      if (catfile == null) {
77        return;
78      }
79  
80      Vector unknownEntry = null;
81  
82      try {
83        while (true) {
84          String token = nextToken();
85  
86          if (token == null) {
87            if (unknownEntry != null) {
88              catalog.unknownEntry(unknownEntry);
89              unknownEntry = null;
90            }
91            catfile.close();
92            catfile = null;
93            return;
94          }
95  
96          String entryToken = null;
97          if (caseSensitive) {
98            entryToken = token;
99          } else {
100           entryToken = token.toUpperCase();
101         }
102 
103         if (entryToken.equals("DELEGATE")) {
104           entryToken = "DELEGATE_PUBLIC";
105         }
106 
107         try {
108           int type = CatalogEntry.getEntryType(entryToken);
109           int numArgs = CatalogEntry.getEntryArgCount(type);
110           Vector args = new Vector();
111 
112           if (unknownEntry != null) {
113             catalog.unknownEntry(unknownEntry);
114             unknownEntry = null;
115           }
116 
117           for (int count = 0; count < numArgs; count++) {
118             args.addElement(nextToken());
119           }
120 
121           catalog.addEntry(new CatalogEntry(entryToken, args));
122         } catch (CatalogException cex) {
123           if (cex.getExceptionType() == CatalogException.INVALID_ENTRY_TYPE) {
124             if (unknownEntry == null) {
125               unknownEntry = new Vector();
126             }
127             unknownEntry.addElement(token);
128           } else if (cex.getExceptionType() == CatalogException.INVALID_ENTRY) {
129             catalog.getCatalogManager().debug.message(1, "Invalid catalog entry", token);
130             unknownEntry = null;
131           } else if (cex.getExceptionType() == CatalogException.UNENDED_COMMENT) {
132             catalog.getCatalogManager().debug.message(1, cex.getMessage());
133           }
134         }
135       }
136     } catch (CatalogException cex2) {
137       if (cex2.getExceptionType() == CatalogException.UNENDED_COMMENT) {
138         catalog.getCatalogManager().debug.message(1, cex2.getMessage());
139       }
140     }
141 
142   }
143 }