/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2012 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of any organization.
 * #L%
 */

package imagej.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

/**
 * Helper class for working with Maven POMs.
 * 
 * @author Curtis Rueden
 */
public class POM extends XML {

	/** Parses a POM from the given file. */
	public POM(final File file) throws ParserConfigurationException,
		SAXException, IOException
	{
		super(file);
	}

	/** Parses a POM from the given input stream. */
	public POM(final InputStream in) throws ParserConfigurationException,
		SAXException, IOException
	{
		super(in);
	}

	/** Parses a POM from the given string. */
	public POM(final String s) throws ParserConfigurationException,
		SAXException, IOException
	{
		super(s);
	}

	// -- POM methods --

	/** Gets the POM's groupId. */
	public String getGroupId() {
		final String groupId = cdata("//project/groupId");
		if (groupId != null) return groupId;
		return cdata("//project/parent/groupId");
	}

	/** Gets the POM's artifactId. */
	public String getArtifactId() {
		return cdata("//project/artifactId");
	}

	/** Gets the POM's version. */
	public String getVersion() {
		final String version = cdata("//project/version");
		if (version != null) return version;
		return cdata("//project/parent/version");
	}

	// -- Utility methods --

	/**
	 * Gets the Maven POM associated with the given class.
	 * 
	 * @param c The class to use as a base when searching for a pom.xml.
	 * @param groupId The Maven groupId of the desired POM.
	 * @param artifactId The Maven artifactId of the desired POM.
	 */
	public static POM getPOM(final Class<?> c, final String groupId,
		final String artifactId)
	{
		try {
			final URL location = ClassUtils.getLocation(c);
			final File file = FileUtils.urlToFile(location);
			if (file.getAbsolutePath().endsWith(".jar")) {
				// look for pom.xml in JAR's META-INF/maven subdirectory
				final JarFile jarFile = new JarFile(file);
				final String pomPath =
					"META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml";
				final ZipEntry entry = jarFile.getEntry(pomPath);
				if (entry == null) return null;
				final InputStream pomStream = jarFile.getInputStream(entry);
				return new POM(pomStream);
			}
			// look for the POM in the class's base directory
			final File baseDir = AppUtils.getBaseDirectory(file, null);
			final File pomFile = new File(baseDir, "pom.xml");
			return new POM(pomFile);
		}
		catch (final IOException e) {
			return null;
		}
		catch (final ParserConfigurationException e) {
			return null;
		}
		catch (final SAXException e) {
			return null;
		}
	}

}

@@ -3677,84 +3677,29 @@
 %0A%09%09%09
-final File file = FileUtils.urlToFile(location);%0A%09%09%09if (file.getAbsolutePath
+if (location.toString
 ().e
@@ -3782,55 +3782,8 @@
 ory%0A
-%09%09%09%09final JarFile jarFile = new JarFile(file);%0A
 %09%09%09%09
@@ -3885,86 +3885,75 @@
 nal 
-ZipEntry entry = jarFile.getEntry(pomPath);%0A%09%09%09%09if (entry == null) return null
+URL pomURL = new URL(%22jar:%22 + location.toString() + %22!/%22 + pomPath)
 ;%0A%09%09
@@ -3988,36 +3988,26 @@
 m = 
-jarFile.getInput
+pomURL.open
 Stream(
-entry
 );%0A%09
@@ -4094,16 +4094,68 @@
 rectory%0A
+%09%09%09final File file = FileUtils.urlToFile(location);%0A
 %09%09%09final

Allow ij-core to be loaded non-locally