resolver.java

来自「JAVA 所有包」· Java 代码 · 共 692 行 · 第 1/2 页

JAVA
692
字号
     * @param arg1 The first argument to the resolver.     * @param arg2 The second argument to the resolver, usually null.     *     * @return The Resolver constructed.     */    protected Resolver queryResolver(String resolver,				     String command,				     String arg1,				     String arg2) {	InputStream iStream = null;	String RFC2483 = resolver + "?command=" + command 	    + "&format=tr9401&uri=" + arg1 	    + "&uri2=" + arg2;	String line = null;	try {	    URL url = new URL(RFC2483);	    URLConnection urlCon = url.openConnection();	    urlCon.setUseCaches(false);	    Resolver r = (Resolver) newCatalog();	    String cType = urlCon.getContentType();	    // I don't care about the character set or subtype	    if (cType.indexOf(";") > 0) {		cType = cType.substring(0, cType.indexOf(";"));	    }	    r.parseCatalog(cType, urlCon.getInputStream());	    return r;	} catch (CatalogException cex) {	  if (cex.getExceptionType() == CatalogException.UNPARSEABLE) {	    catalogManager.debug.message(1, "Unparseable catalog: " + RFC2483);	  } else if (cex.getExceptionType()		     == CatalogException.UNKNOWN_FORMAT) {	    catalogManager.debug.message(1, "Unknown catalog format: " + RFC2483);	  }	  return null;	} catch (MalformedURLException mue) {	    catalogManager.debug.message(1, "Malformed resolver URL: " + RFC2483);	    return null;	} catch (IOException ie) {	    catalogManager.debug.message(1, "I/O Exception opening resolver: " + RFC2483);	    return null;	}    }    /**     * Append two vectors, returning the result.     *     * @param vec The first vector     * @param appvec The vector to be appended     * @return The vector vec, with appvec's elements appended to it     */    private Vector appendVector(Vector vec, Vector appvec) {	if (appvec != null) {	    for (int count = 0; count < appvec.size(); count++) {		vec.addElement(appvec.elementAt(count));	    }	}	return vec;    }    /**     * Find the URNs for a given system identifier in all catalogs.     *     * @param systemId The system ID to locate.     *     * @return A vector of URNs that map to the systemId.     */    public Vector resolveAllSystemReverse(String systemId)	throws MalformedURLException, IOException {	Vector resolved = new Vector();	// If there's a SYSTEM entry in this catalog, use it	if (systemId != null) {	    Vector localResolved = resolveLocalSystemReverse(systemId);	    resolved = appendVector(resolved, localResolved);	}	// Otherwise, look in the subordinate catalogs	Vector subResolved = resolveAllSubordinateCatalogs(SYSTEMREVERSE,							   null,							   null,							   systemId);	return appendVector(resolved, subResolved);    }    /**     * Find the URN for a given system identifier.     *     * @param systemId The system ID to locate.     *     * @return A (single) URN that maps to the systemId.     */    public String resolveSystemReverse(String systemId)	throws MalformedURLException, IOException {	Vector resolved = resolveAllSystemReverse(systemId);	if (resolved != null && resolved.size() > 0) {	    return (String) resolved.elementAt(0);	} else {	    return null;	}    }    /**     * Return the applicable SYSTEM system identifiers.     *     * <p>If one or more SYSTEM entries exists in the Catalog     * for the system ID specified, return the mapped values.</p>     *     * <p>The caller is responsible for doing any necessary     * normalization of the system identifier before calling     * this method. For example, a relative system identifier in     * a document might be converted to an absolute system identifier     * before attempting to resolve it.</p>     *     * <p>Note that this function will force all subordinate catalogs     * to be loaded.</p>     *     * <p>On Windows-based operating systems, the comparison between     * the system identifier provided and the SYSTEM entries in the     * Catalog is case-insensitive.</p>     *     * @param systemId The system ID to locate in the catalog.     *     * @return The system identifier to use for the notation.     *     * @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 Vector resolveAllSystem(String systemId)	throws MalformedURLException, IOException {	Vector resolutions = new Vector();	// If there are SYSTEM entries in this catalog, start with them	if (systemId != null) {	    Vector localResolutions = resolveAllLocalSystem(systemId);	    resolutions = appendVector(resolutions, localResolutions);	}	// Then look in the subordinate catalogs	Vector subResolutions = resolveAllSubordinateCatalogs(SYSTEM,							      null,							      null,							      systemId);	resolutions = appendVector(resolutions, subResolutions);	if (resolutions.size() > 0) {	    return resolutions;	} else {	    return null;	}    }    /**     * Return all applicable SYSTEM system identifiers in this     * catalog.     *     * <p>If one or more SYSTEM entries exists in the catalog file     * for the system ID specified, return the mapped values.</p>     *     * @param systemId The system ID to locate in the catalog     *     * @return A vector of the mapped system identifiers or null     */    private Vector resolveAllLocalSystem(String systemId) {	Vector map = new Vector();	String osname = System.getProperty("os.name");	boolean windows = (osname.indexOf("Windows") >= 0);	Enumeration en = catalogEntries.elements();	while (en.hasMoreElements()) {	    CatalogEntry e = (CatalogEntry) en.nextElement();	    if (e.getEntryType() == SYSTEM		&& (e.getEntryArg(0).equals(systemId)		    || (windows			&& e.getEntryArg(0).equalsIgnoreCase(systemId)))) {		map.addElement(e.getEntryArg(1));	    }	}	if (map.size() == 0) {	    return null;	} else {	    return map;	}    }    /**     * Find the URNs for a given system identifier in the current catalog.     *     * @param systemId The system ID to locate.     *     * @return A vector of URNs that map to the systemId.     */    private Vector resolveLocalSystemReverse(String systemId) {	Vector map = new Vector();	String osname = System.getProperty("os.name");	boolean windows = (osname.indexOf("Windows") >= 0);	Enumeration en = catalogEntries.elements();	while (en.hasMoreElements()) {	    CatalogEntry e = (CatalogEntry) en.nextElement();	    if (e.getEntryType() == SYSTEM		&& (e.getEntryArg(1).equals(systemId)		    || (windows			&& e.getEntryArg(1).equalsIgnoreCase(systemId)))) {		map.addElement(e.getEntryArg(0));	    }	}	if (map.size() == 0) {	    return null;	} else {	    return map;	}    }    /**     * Search the subordinate catalogs, in order, looking for all     * match.     *     * <p>This method searches the Catalog and returns all of the system     * identifiers specified for the given entity type with the given     * name, public, and system identifiers. In some contexts, these     * may be null.</p>     *     * @param entityType The CatalogEntry type for which this query is     * being conducted. This is necessary in order to do the approprate     * query on a subordinate catalog.     * @param entityName The name of the entity being searched for, if     * appropriate.     * @param publicId The public identifier of the entity in question     * (as provided in the source document).     * @param systemId The nominal system identifier for the entity     * in question (as provided in the source document).     *     * @throws MalformedURLException The formal system identifier of a     * delegated catalog cannot be turned into a valid URL.     * @throws IOException Error reading delegated catalog file.     *     * @return The system identifier to use.     * Note that the nominal system identifier is not returned if a     * match is not found in the catalog, instead null is returned     * to indicate that no match was found.     */    private synchronized Vector resolveAllSubordinateCatalogs(int entityType,					      String entityName,					      String publicId,					      String systemId)	throws MalformedURLException, IOException {	Vector resolutions = new Vector();	for (int catPos = 0; catPos < catalogs.size(); catPos++) {	    Resolver c = null;	    try {		c = (Resolver) catalogs.elementAt(catPos);	    } catch (ClassCastException e) {		String catfile = (String) catalogs.elementAt(catPos);		c = (Resolver) newCatalog();		try {		    c.parseCatalog(catfile);		} catch (MalformedURLException mue) {		    catalogManager.debug.message(1, "Malformed Catalog URL", catfile);		} catch (FileNotFoundException fnfe) {		    catalogManager.debug.message(1, "Failed to load catalog, file not found",			  catfile);		} catch (IOException ioe) {		    catalogManager.debug.message(1, "Failed to load catalog, I/O error", catfile);		}		catalogs.setElementAt(c, catPos);	    }	    String resolved = null;	    // Ok, now what are we supposed to call here?	    if (entityType == DOCTYPE) {		resolved = c.resolveDoctype(entityName,					    publicId,					    systemId);		if (resolved != null) {		    // Only find one DOCTYPE resolution		    resolutions.addElement(resolved);		    return resolutions;		}	    } else if (entityType == DOCUMENT) {		resolved = c.resolveDocument();		if (resolved != null) {		    // Only find one DOCUMENT resolution		    resolutions.addElement(resolved);		    return resolutions;		}	    } else if (entityType == ENTITY) {		resolved = c.resolveEntity(entityName,					   publicId,					   systemId);		if (resolved != null) {		    // Only find one ENTITY resolution		    resolutions.addElement(resolved);		    return resolutions;		}	    } else if (entityType == NOTATION) {		resolved = c.resolveNotation(entityName,					     publicId,					     systemId);		if (resolved != null) {		    // Only find one NOTATION resolution		    resolutions.addElement(resolved);		    return resolutions;		}	    } else if (entityType == PUBLIC) {		resolved = c.resolvePublic(publicId, systemId);		if (resolved != null) {		    // Only find one PUBLIC resolution		    resolutions.addElement(resolved);		    return resolutions;		}	    } else if (entityType == SYSTEM) {		Vector localResolutions = c.resolveAllSystem(systemId);		resolutions = appendVector(resolutions, localResolutions);		break;	    } else if (entityType == SYSTEMREVERSE) {		Vector localResolutions = c.resolveAllSystemReverse(systemId);		resolutions = appendVector(resolutions, localResolutions);	    }	}	if (resolutions != null) {	    return resolutions;	} else {	    return null;	}    }}

⌨️ 快捷键说明

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