/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2014 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.
 * #L%
 */

package net.imagej.legacy;

import java.net.URL;

import org.scijava.AbstractUIDetails;
import org.scijava.Identifiable;
import org.scijava.Locatable;
import org.scijava.Versioned;
import org.scijava.util.ClassUtils;
import org.scijava.util.Manifest;

/**
 * Metadata about an ImageJ 1.x {@code PlugIn}.
 *
 * @author Curtis Rueden
 */
public class LegacyPlugInInfo extends AbstractUIDetails implements
	Identifiable, Locatable, Versioned
{

	private final Class<?> clazz;
	private final String id;

	public LegacyPlugInInfo(final String className, final String arg,
		final ClassLoader classLoader)
	{
		try {
			clazz = classLoader.loadClass(className);
		}
		catch (final ClassNotFoundException exc) {
			throw new IllegalArgumentException(exc);
		}
		id = "legacy:" + className + (isWhitelisted(className) ? "?" + arg : "");
	}

	// -- Identifiable methods --

	@Override
	public String getIdentifier() {
		return id;
	}

	// -- Locatable methods --

	@Override
	public String getLocation() {
		final URL url = ClassUtils.getLocation(clazz);
		return url == null ? null : url.toExternalForm();
	}

	// -- Versioned methods --

	@Override
	public String getVersion() {
		final Manifest m = Manifest.getManifest(clazz);
		return m == null ? null : m.getImplementationVersion();
	}

	// -- Helper methods --

	/**
	 * Legacy commands whose {@code arg} parameter are known not to contain any
	 * user-specific details such as file paths. The classic example is {code
	 * ij.plugin.Commands}, which passes the subcommand to execute as its
	 * {@code arg} parameter. We want to record such subcommands when feasible,
	 * but not at the expense of user privacy.
	 */
	private boolean isWhitelisted(final String className) {
		if (className.equals("ij.plugin.Commands")) return true;
		return false;
	}

}

@@ -2379,29 +2379,25 @@
 Name + (
-isWhitelisted
+appendArg
 (classNa
@@ -2398,16 +2398,21 @@
 lassName
+, arg
 ) ? %22?%22 
@@ -2914,17 +2914,97 @@
 /**%0A%09 * 
-L
+Whether to append the %7B@code arg%7D parameter to the identifier.%0A%09 * %3Cp%3E%0A%09 * Some l
 egacy co
@@ -3014,12 +3014,14 @@
 nds 
-whos
+use th
 e %7B@
@@ -3040,16 +3040,52 @@
 rameter 
+as a subcommand to%0A%09 * execute, and 
 are know
@@ -3104,20 +3104,16 @@
 tain any
-%0A%09 *
  user-sp
@@ -3134,16 +3134,20 @@
  such as
+%0A%09 *
  file pa
@@ -3155,16 +3155,21 @@
 hs. The 
+most 
 classic 
@@ -3184,20 +3184,16 @@
 is %7Bcode
-%0A%09 *
  ij.plug
@@ -3208,81 +3208,8 @@
 nds%7D
-, which passes the subcommand to execute as its%0A%09 * %7B@code arg%7D parameter
 . We
@@ -3213,16 +3213,20 @@
  We want
+%0A%09 *
  to reco
@@ -3259,20 +3259,16 @@
 easible,
-%0A%09 *
  but not
@@ -3290,16 +3290,20 @@
  of user
+%0A%09 *
  privacy
@@ -3304,16 +3304,25 @@
 rivacy.%0A
+%09 * %3C/p%3E%0A
 %09 */%0A%09pr
@@ -3339,90 +3339,408 @@
 ean 
-isWhitelisted(final String className) %7B%0A%09%09if (className.equals(%22ij.plugin.Commands
+appendArg(final String className, final String arg) %7B%0A%09%09if (arg == null %7C%7C arg.isEmpty()) return false; // no need%0A%09%09if (className.equals(%22ij.plugin.Animator%22)) return true;%0A%09%09if (className.equals(%22ij.plugin.Commands%22)) return true;%0A%09%09if (className.equals(%22ij.plugin.Converter%22)) return true;%0A%09%09if (className.equals(%22ij.plugin.StackEditor%22)) return true;%0A%09%09if (className.startsWith(%22ij.plugin.filter.
 %22)) 

whitelist a few more plugins