// ResolveURI.java by Mads N Noe (www.mntnoe.com)

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.tools.CatalogResolver;

public class ResolveURI
{
	public static void main(String[] args)
	{
		if(args.length != 1) {
			System.err.println("usage: ResolveURI identifier");
			System.exit(-1);
		}
		String identifier = args[0];

		String catalogEnv = System.getenv("XML_CATALOG_FILES");
		// default if XML_CATALOG_FILES is not set
		String [] catalogs = new String[]{"/etc/xml/catalog"}; 
		if (catalogEnv != null) catalogs = catalogEnv.split(" ");

		CatalogResolver resolver = new CatalogResolver();
		Catalog catalog = resolver.getCatalog();
		String result = "";
		try {
			for (String file : catalogs)
				catalog.parseCatalog(file);

			result = catalog.resolveURI(identifier);

		} 
		catch (IOException e1) {
			e1.printStackTrace();
		}

		if (result == null) {
			System.err.println("URI not found in catalogs");
			result = identifier;
		}
		System.out.println(result);
	}
}

