catalog.java

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

JAVA
1,991
字号
   *   * @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.   */  protected synchronized void parseCatalogFile(String fileName)    throws MalformedURLException, IOException, CatalogException {    CatalogEntry entry;    // The base-base is the cwd. If the catalog file is specified    // with a relative path, this assures that it gets resolved    // properly...    try {      // tack on a basename because URLs point to files not dirs      catalogCwd = FileURL.makeURL("basename");    } catch (MalformedURLException e) {      String userdir = System.getProperty("user.dir");      userdir.replace('\\', '/');      catalogManager.debug.message(1, "Malformed URL on cwd", userdir);      catalogCwd = null;    }    // The initial base URI is the location of the catalog file    try {      base = new URL(catalogCwd, fixSlashes(fileName));    } catch (MalformedURLException e) {      try {	base = new URL("file:" + fixSlashes(fileName));      } catch (MalformedURLException e2) {	catalogManager.debug.message(1, "Malformed URL on catalog filename",		      fixSlashes(fileName));	base = null;      }    }    catalogManager.debug.message(2, "Loading catalog", fileName);    catalogManager.debug.message(4, "Default BASE", base.toString());    fileName = base.toString();    DataInputStream inStream = null;    boolean parsed = false;    boolean notFound = false;    for (int count = 0; !parsed && count < readerArr.size(); count++) {      CatalogReader reader = (CatalogReader) readerArr.get(count);      try {	notFound = false;	inStream = new DataInputStream(base.openStream());      } catch (FileNotFoundException fnfe) {	// No catalog; give up!	notFound = true;	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) {      if (notFound) {	catalogManager.debug.message(3, "Catalog does not exist", fileName);      } else {	catalogManager.debug.message(1, "Failed to parse catalog", fileName);      }    }  }  /**   * Cleanup and process a Catalog entry.   *   * <p>This method processes each Catalog entry, changing mapped   * relative system identifiers into absolute ones (based on the current   * base URI), and maintaining other information about the current   * catalog.</p>   *   * @param entry The CatalogEntry to process.   */  public void addEntry(CatalogEntry entry) {    int type = entry.getEntryType();    if (type == BASE) {      String value = entry.getEntryArg(0);      URL newbase = null;      if (base == null) {	catalogManager.debug.message(5, "BASE CUR", "null");      } else {	catalogManager.debug.message(5, "BASE CUR", base.toString());      }      catalogManager.debug.message(4, "BASE STR", value);      try {	value = fixSlashes(value);	newbase = new URL(base, value);      } catch (MalformedURLException e) {	try {	  newbase = new URL("file:" + value);	} catch (MalformedURLException e2) {	  catalogManager.debug.message(1, "Malformed URL on base", value);	  newbase = null;	}      }      if (newbase != null) {	base = newbase;      }      catalogManager.debug.message(5, "BASE NEW", base.toString());    } else if (type == CATALOG) {      String fsi = makeAbsolute(entry.getEntryArg(0));      catalogManager.debug.message(4, "CATALOG", fsi);      localCatalogFiles.addElement(fsi);    } else if (type == PUBLIC) {      String publicid = PublicId.normalize(entry.getEntryArg(0));      String systemid = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, publicid);      entry.setEntryArg(1, systemid);      catalogManager.debug.message(4, "PUBLIC", publicid, systemid);      catalogEntries.addElement(entry);    } else if (type == SYSTEM) {      String systemid = normalizeURI(entry.getEntryArg(0));      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "SYSTEM", systemid, fsi);      catalogEntries.addElement(entry);    } else if (type == URI) {      String uri = normalizeURI(entry.getEntryArg(0));      String altURI = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(1, altURI);      catalogManager.debug.message(4, "URI", uri, altURI);      catalogEntries.addElement(entry);    } else if (type == DOCUMENT) {      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));      entry.setEntryArg(0, fsi);      catalogManager.debug.message(4, "DOCUMENT", fsi);      catalogEntries.addElement(entry);    } else if (type == OVERRIDE) {      catalogManager.debug.message(4, "OVERRIDE", entry.getEntryArg(0));      catalogEntries.addElement(entry);    } else if (type == SGMLDECL) {      // meaningless in XML      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(0)));      entry.setEntryArg(0, fsi);      catalogManager.debug.message(4, "SGMLDECL", fsi);      catalogEntries.addElement(entry);    } else if (type == DELEGATE_PUBLIC) {      String ppi = PublicId.normalize(entry.getEntryArg(0));      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, ppi);      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "DELEGATE_PUBLIC", ppi, fsi);      addDelegate(entry);    } else if (type == DELEGATE_SYSTEM) {      String psi = normalizeURI(entry.getEntryArg(0));      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, psi);      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "DELEGATE_SYSTEM", psi, fsi);      addDelegate(entry);    } else if (type == DELEGATE_URI) {      String pui = normalizeURI(entry.getEntryArg(0));      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, pui);      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "DELEGATE_URI", pui, fsi);      addDelegate(entry);    } else if (type == REWRITE_SYSTEM) {      String psi = normalizeURI(entry.getEntryArg(0));      String rpx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, psi);      entry.setEntryArg(1, rpx);      catalogManager.debug.message(4, "REWRITE_SYSTEM", psi, rpx);      catalogEntries.addElement(entry);    } else if (type == REWRITE_URI) {      String pui = normalizeURI(entry.getEntryArg(0));      String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, pui);      entry.setEntryArg(1, upx);      catalogManager.debug.message(4, "REWRITE_URI", pui, upx);      catalogEntries.addElement(entry);    } else if (type == SYSTEM_SUFFIX) {      String pui = normalizeURI(entry.getEntryArg(0));      String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, pui);      entry.setEntryArg(1, upx);      catalogManager.debug.message(4, "SYSTEM_SUFFIX", pui, upx);      catalogEntries.addElement(entry);    } else if (type == URI_SUFFIX) {      String pui = normalizeURI(entry.getEntryArg(0));      String upx = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(0, pui);      entry.setEntryArg(1, upx);      catalogManager.debug.message(4, "URI_SUFFIX", pui, upx);      catalogEntries.addElement(entry);    } else if (type == DOCTYPE) {      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "DOCTYPE", entry.getEntryArg(0), fsi);      catalogEntries.addElement(entry);    } else if (type == DTDDECL) {      // meaningless in XML      String fpi = PublicId.normalize(entry.getEntryArg(0));      entry.setEntryArg(0, fpi);      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "DTDDECL", fpi, fsi);      catalogEntries.addElement(entry);    } else if (type == ENTITY) {      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "ENTITY", entry.getEntryArg(0), fsi);      catalogEntries.addElement(entry);    } else if (type == LINKTYPE) {      // meaningless in XML      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "LINKTYPE", entry.getEntryArg(0), fsi);      catalogEntries.addElement(entry);    } else if (type == NOTATION) {      String fsi = makeAbsolute(normalizeURI(entry.getEntryArg(1)));      entry.setEntryArg(1, fsi);      catalogManager.debug.message(4, "NOTATION", entry.getEntryArg(0), fsi);      catalogEntries.addElement(entry);    } else {      catalogEntries.addElement(entry);    }  }  /**   * Handle unknown CatalogEntry types.   *   * <p>This method exists to allow subclasses to deal with unknown   * entry types.</p>   */  public void unknownEntry(Vector strings) {    if (strings != null && strings.size() > 0) {      String keyword = (String) strings.elementAt(0);      catalogManager.debug.message(2, "Unrecognized token parsing catalog", keyword);    }  }  /**   * Parse all subordinate catalogs.   *   * <p>This method recursively parses all of the subordinate catalogs.   * If this method does not throw an exception, you can be confident that   * no subsequent call to any resolve*() method will either, with two   * possible exceptions:</p>   *   * <ol>   * <li><p>Delegated catalogs are re-parsed each time they are needed   * (because a variable list of them may be needed in each case,   * depending on the length of the matching partial public identifier).</p>   * <p>But they are parsed by this method, so as long as they don't   * change or disappear while the program is running, they shouldn't   * generate errors later if they don't generate errors now.</p>   * <li><p>If you add new catalogs with <code>parseCatalog</code>, they   * won't be loaded until they are needed or until you call   * <code>parseAllCatalogs</code> again.</p>   * </ol>   *   * <p>On the other hand, if you don't call this method, you may   * successfully parse documents without having to load all possible   * catalogs.</p>   *   * @throws MalformedURLException The filename (URL) for a   * subordinate or delegated catalog is not a valid URL.   * @throws IOException Error reading some subordinate or delegated   * catalog file.   */  public void parseAllCatalogs()    throws MalformedURLException, IOException {    // Parse all the subordinate catalogs    for (int catPos = 0; catPos < catalogs.size(); catPos++) {      Catalog c = null;      try {	c = (Catalog) catalogs.elementAt(catPos);      } catch (ClassCastException e) {	String catfile = (String) catalogs.elementAt(catPos);	c = newCatalog();	c.parseCatalog(catfile);	catalogs.setElementAt(c, catPos);	c.parseAllCatalogs();      }    }    // Parse all the DELEGATE catalogs    Enumeration en = catalogEntries.elements();    while (en.hasMoreElements()) {      CatalogEntry e = (CatalogEntry) en.nextElement();      if (e.getEntryType() == DELEGATE_PUBLIC	  || e.getEntryType() == DELEGATE_SYSTEM	  || e.getEntryType() == DELEGATE_URI) {	Catalog dcat = newCatalog();	dcat.parseCatalog(e.getEntryArg(1));      }    }  }  /**   * Return the applicable DOCTYPE system identifier.   *   * @param entityName The name of the entity (element) for which   * a doctype is required.   * @param publicId The nominal public identifier for the doctype   * (as provided in the source document).   * @param systemId The nominal system identifier for the doctype   * (as provided in the source document).   *   * @return The system identifier to use for the doctype.   *   * @throws MalformedURLException The formal system identifier of a   * subordinate catalog cannot be turned into a valid URL.   * @throws IOException Error reading subordinate catalog file.   */  public String resolveDoctype(String entityName,			       String publicId,			       String systemId)    throws MalformedURLException, IOException {    String resolved = null;    catalogManager.debug.message(3, "resolveDoctype("		  +entityName+","+publicId+","+systemId+")");    systemId = normalizeURI(systemId);    if (publicId != null && publicId.startsWith("urn:publicid:")) {      publicId = PublicId.decodeURN(publicId);

⌨️ 快捷键说明

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