protected String resolveLocalURI()

in xmlresolver-1.2/src/main/java/org/apache/xml/resolver/Catalog.java [1885:1988]


  protected String resolveLocalURI(String uri)
    throws MalformedURLException, IOException {
    Enumeration en = catalogEntries.elements();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();
      if (e.getEntryType() == URI
	  && (e.getEntryArg(0).equals(uri))) {
	return e.getEntryArg(1);
      }
    }

    // If there's a REWRITE_URI entry in this catalog, use it
    en = catalogEntries.elements();
    String startString = null;
    String prefix = null;
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == REWRITE_URI) {
	String p = (String) e.getEntryArg(0);
	if (p.length() <= uri.length()
	    && p.equals(uri.substring(0, p.length()))) {
	  // Is this the longest prefix?
	  if (startString == null
	      || p.length() > startString.length()) {
	    startString = p;
	    prefix = e.getEntryArg(1);
	  }
	}
      }
    }

    if (prefix != null) {
      // return the uri with the new prefix
      return prefix + uri.substring(startString.length());
    }

    // If there's a URI_SUFFIX entry in this catalog, use it
    en = catalogEntries.elements();
    String suffixString = null;
    String suffixURI = null;
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == URI_SUFFIX) {
	String p = (String) e.getEntryArg(0);
	if (p.length() <= uri.length()
	    && uri.endsWith(p)) {
	  // Is this the longest prefix?
	  if (suffixString == null
	      || p.length() > suffixString.length()) {
	    suffixString = p;
	    suffixURI = e.getEntryArg(1);
	  }
	}
      }
    }

    if (suffixURI != null) {
      // return the uri for the suffix
      return suffixURI;
    }

    // If there's a DELEGATE_URI entry in this catalog, use it
    en = catalogEntries.elements();
    Vector delCats = new Vector();
    while (en.hasMoreElements()) {
      CatalogEntry e = (CatalogEntry) en.nextElement();

      if (e.getEntryType() == DELEGATE_URI) {
	String p = (String) e.getEntryArg(0);
	if (p.length() <= uri.length()
	    && p.equals(uri.substring(0, p.length()))) {
	  // delegate this match to the other catalog

	  delCats.addElement(e.getEntryArg(1));
	}
      }
    }

    if (delCats.size() > 0) {
      Enumeration enCats = delCats.elements();

      if (catalogManager.debug.getDebug() > 1) {
	catalogManager.debug.message(2, "Switching to delegated catalog(s):");
	while (enCats.hasMoreElements()) {
	  String delegatedCatalog = (String) enCats.nextElement();
	  catalogManager.debug.message(2, "\t" + delegatedCatalog);
	}
      }

      Catalog dcat = newCatalog();

      enCats = delCats.elements();
      while (enCats.hasMoreElements()) {
	String delegatedCatalog = (String) enCats.nextElement();
	dcat.parseCatalog(delegatedCatalog);
      }

      return dcat.resolveURI(uri);
    }

    return null;
  }