catalog.java

来自「JAVA 所有包」· Java 代码 · 共 1,991 行 · 第 1/5 页

JAVA
1,991
字号
    spf.setValidating(false);    SAXCatalogReader saxReader = new SAXCatalogReader(spf);    saxReader.setCatalogParser(null, "XMLCatalog",			       "com.sun.org.apache.xml.internal.resolver.readers.XCatalogReader");    saxReader.setCatalogParser(OASISXMLCatalogReader.namespaceName,			       "catalog",			       "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader");    addReader("application/xml", saxReader);    TR9401CatalogReader textReader = new TR9401CatalogReader();    addReader("text/plain", textReader);  }  /**   * Add a new CatalogReader to the Catalog.   *   * <p>This method allows you to add a new CatalogReader to the   * catalog. The reader will be associated with the specified mimeType.   * You can only have one reader per mimeType.</p>   *   * <p>In the absence of a mimeType (e.g., when reading a catalog   * directly from a file on the local system), the readers are attempted   * in the order that you add them to the Catalog.</p>   *   * <p>Note that subordinate catalogs (created by CATALOG or   * DELEGATE* entries) get a copy of the set of readers present in   * the primary catalog when they are created. Readers added subsequently   * will not be available. For this reason, it is best to add all   * of the readers before the first call to parse a catalog.</p>   *   * @param mimeType The MIME type associated with this reader.   * @param reader The CatalogReader to use.   */  public void addReader(String mimeType, CatalogReader reader) {    if (readerMap.containsKey(mimeType)) {      Integer pos = (Integer) readerMap.get(mimeType);      readerArr.set(pos.intValue(), reader);    } else {      readerArr.add(reader);      Integer pos = new Integer(readerArr.size()-1);      readerMap.put(mimeType, pos);    }  }  /**   * Copies the reader list from the current Catalog to a new Catalog.   *   * <p>This method is used internally when constructing a new catalog.   * It copies the current reader associations over to the new catalog.   * </p>   *   * @param newCatalog The new Catalog.   */  protected void copyReaders(Catalog newCatalog) {    // Have to copy the readers in the right order...convert hash to arr    Vector mapArr = new Vector(readerMap.size());    // Pad the mapArr out to the right length    for (int count = 0; count < readerMap.size(); count++) {      mapArr.add(null);    }    Enumeration en = readerMap.keys();    while (en.hasMoreElements()) {      String mimeType = (String) en.nextElement();      Integer pos = (Integer) readerMap.get(mimeType);      mapArr.set(pos.intValue(), mimeType);    }    for (int count = 0; count < mapArr.size(); count++) {      String mimeType = (String) mapArr.get(count);      Integer pos = (Integer) readerMap.get(mimeType);      newCatalog.addReader(mimeType,			   (CatalogReader)			   readerArr.get(pos.intValue()));    }  }  /**   * Create a new Catalog object.   *   * <p>This method constructs a new instance of the running Catalog   * class (which might be a subtype of com.sun.org.apache.xml.internal.resolver.Catalog).   * All new catalogs are managed by the same CatalogManager.   * </p>   *   * <p>N.B. All Catalog subtypes should call newCatalog() to construct   * a new Catalog. Do not simply use "new Subclass()" since that will   * confuse future subclasses.</p>   */  protected Catalog newCatalog() {    String catalogClass = this.getClass().getName();    try {      Catalog c = (Catalog) (Class.forName(catalogClass).newInstance());      c.setCatalogManager(catalogManager);      copyReaders(c);      return c;    } catch (ClassNotFoundException cnfe) {      catalogManager.debug.message(1, "Class Not Found Exception: " + catalogClass);    } catch (IllegalAccessException iae) {      catalogManager.debug.message(1, "Illegal Access Exception: " + catalogClass);    } catch (InstantiationException ie) {      catalogManager.debug.message(1, "Instantiation Exception: " + catalogClass);    } catch (ClassCastException cce) {      catalogManager.debug.message(1, "Class Cast Exception: " + catalogClass);    } catch (Exception e) {      catalogManager.debug.message(1, "Other Exception: " + catalogClass);    }    Catalog c = new Catalog();    c.setCatalogManager(catalogManager);    copyReaders(c);    return c;  }  /**   * Returns the current base URI.   */  public String getCurrentBase() {    return base.toString();  }  /**   * Returns the default override setting associated with this   * catalog.   *   * <p>All catalog files loaded by this catalog will have the   * initial override setting specified by this default.</p>   */  public String getDefaultOverride() {    if (default_override) {      return "yes";    } else {      return "no";    }  }  /**   * Load the system catalog files.   *   * <p>The method adds all of the   * catalogs specified in the <tt>xml.catalog.files</tt> property   * to the Catalog list.</p>   *   * @throws MalformedURLException  One of the system catalogs is   * identified with a filename that is not a valid URL.   * @throws IOException One of the system catalogs cannot be read.   */  public void loadSystemCatalogs()    throws MalformedURLException, IOException {    Vector catalogs = catalogManager.getCatalogFiles();    if (catalogs != null) {      for (int count = 0; count < catalogs.size(); count++) {	catalogFiles.addElement(catalogs.elementAt(count));      }    }    if (catalogFiles.size() > 0) {      // This is a little odd. The parseCatalog() method expects      // a filename, but it adds that name to the end of the      // catalogFiles vector, and then processes that vector.      // This allows the system to handle CATALOG entries      // correctly.      //      // In this init case, we take the last element off the      // catalogFiles vector and pass it to parseCatalog. This      // will "do the right thing" in the init case, and allow      // parseCatalog() to do the right thing in the non-init      // case. Honest.      //      String catfile = (String) catalogFiles.lastElement();      catalogFiles.removeElement(catfile);      parseCatalog(catfile);    }  }  /**   * Parse a catalog file, augmenting internal data structures.   *   * @param fileName The filename of the catalog file to process   *   * @throws MalformedURLException The fileName cannot be turned into   * a valid URL.   * @throws IOException Error reading catalog file.   */  public synchronized void parseCatalog(String fileName)    throws MalformedURLException, IOException {    default_override = catalogManager.getPreferPublic();    catalogManager.debug.message(4, "Parse catalog: " + fileName);    // Put the file into the list of catalogs to process...    // In all cases except the case when initCatalog() is the    // caller, this will be the only catalog initially in the list...    catalogFiles.addElement(fileName);    // Now process all the pending catalogs...    parsePendingCatalogs();  }  /**   * Parse a catalog file, augmenting internal data structures.   *   * <p>Catalogs retrieved over the net may have an associated MIME type.   * The MIME type can be used to select an appropriate reader.</p>   *   * @param mimeType The MIME type of the catalog file.   * @param is The InputStream from which the catalog should be read   *   * @throws CatalogException Failed to load catalog   * mimeType.   * @throws IOException Error reading catalog file.   */  public synchronized void parseCatalog(String mimeType, InputStream is)    throws IOException, CatalogException {    default_override = catalogManager.getPreferPublic();    catalogManager.debug.message(4, "Parse " + mimeType + " catalog on input stream");    CatalogReader reader = null;    if (readerMap.containsKey(mimeType)) {      int arrayPos = ((Integer) readerMap.get(mimeType)).intValue();      reader = (CatalogReader) readerArr.get(arrayPos);    }    if (reader == null) {      String msg = "No CatalogReader for MIME type: " + mimeType;      catalogManager.debug.message(2, msg);      throw new CatalogException(CatalogException.UNPARSEABLE, msg);    }    reader.readCatalog(this, is);    // Now process all the pending catalogs...    parsePendingCatalogs();  }  /**   * Parse a catalog document, augmenting internal data structures.   *   * <p>This method supports catalog files stored in jar files: e.g.,   * jar:file:///path/to/filename.jar!/path/to/catalog.xml". That URI   * doesn't survive transmogrification through the URI processing that   * the parseCatalog(String) performs and passing it as an input stream   * doesn't set the base URI appropriately.</p>   *   * <p>Written by Stefan Wachter (2002-09-26)</p>   *   * @param aUrl The URL of the catalog document to process   *   * @throws IOException Error reading catalog file.   */  public synchronized void parseCatalog(URL aUrl) throws IOException {    catalogCwd = aUrl;    base = aUrl;    default_override = catalogManager.getPreferPublic();    catalogManager.debug.message(4, "Parse catalog: " + aUrl.toString());    DataInputStream inStream = null;    boolean parsed = false;    for (int count = 0; !parsed && count < readerArr.size(); count++) {      CatalogReader reader = (CatalogReader) readerArr.get(count);      try {      	inStream = new DataInputStream(aUrl.openStream());      } catch (FileNotFoundException fnfe) {        // No catalog; give up!        break;      }      try {        reader.readCatalog(this, inStream);        parsed=true;      } catch (CatalogException ce) {        if (ce.getExceptionType() == CatalogException.PARSE_FAILED) {          // give up!          break;        } else {          // try again!        }      }      try {      	inStream.close();      } catch (IOException e) {      	//nop      }    }    if (parsed) parsePendingCatalogs();  }  /**   * Parse all of the pending catalogs.   *   * <p>Catalogs may refer to other catalogs, this method parses   * all of the currently pending catalog files.</p>   */  protected synchronized void parsePendingCatalogs()    throws MalformedURLException, IOException {    if (!localCatalogFiles.isEmpty()) {      // Move all the localCatalogFiles into the front of      // the catalogFiles queue      Vector newQueue = new Vector();      Enumeration q = localCatalogFiles.elements();      while (q.hasMoreElements()) {	newQueue.addElement(q.nextElement());      }      // Put the rest of the catalogs on the end of the new list      for (int curCat = 0; curCat < catalogFiles.size(); curCat++) {	String catfile = (String) catalogFiles.elementAt(curCat);	newQueue.addElement(catfile);      }      catalogFiles = newQueue;      localCatalogFiles.clear();    }    // Suppose there are no catalog files to process, but the    // single catalog already parsed included some delegate    // entries? Make sure they don't get lost.    if (catalogFiles.isEmpty() && !localDelegate.isEmpty()) {      Enumeration e = localDelegate.elements();      while (e.hasMoreElements()) {	catalogEntries.addElement(e.nextElement());      }      localDelegate.clear();    }    // Now process all the files on the catalogFiles vector. This    // vector can grow during processing if CATALOG entries are    // encountered in the catalog    while (!catalogFiles.isEmpty()) {      String catfile = (String) catalogFiles.elementAt(0);      try {	catalogFiles.remove(0);      } catch (ArrayIndexOutOfBoundsException e) {	// can't happen      }      if (catalogEntries.size() == 0 && catalogs.size() == 0) {	// We haven't parsed any catalogs yet, let this	// catalog be the first...	try {	  parseCatalogFile(catfile);	} catch (CatalogException ce) {	  System.out.println("FIXME: " + ce.toString());	}      } else {	// This is a subordinate catalog. We save its name,	// but don't bother to load it unless it's necessary.	catalogs.addElement(catfile);      }      if (!localCatalogFiles.isEmpty()) {	// Move all the localCatalogFiles into the front of	// the catalogFiles queue	Vector newQueue = new Vector();	Enumeration q = localCatalogFiles.elements();	while (q.hasMoreElements()) {	  newQueue.addElement(q.nextElement());	}	// Put the rest of the catalogs on the end of the new list	for (int curCat = 0; curCat < catalogFiles.size(); curCat++) {	  catfile = (String) catalogFiles.elementAt(curCat);	  newQueue.addElement(catfile);	}	catalogFiles = newQueue;	localCatalogFiles.clear();      }      if (!localDelegate.isEmpty()) {	Enumeration e = localDelegate.elements();	while (e.hasMoreElements()) {	  catalogEntries.addElement(e.nextElement());	}	localDelegate.clear();      }    }    // We've parsed them all, reinit the vector...    catalogFiles.clear();  }  /**   * Parse a single catalog file, augmenting internal data structures.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?