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