/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs;

import java.util.*;
import java.net.*;
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.*;

public class PluginLoader extends URLClassLoader {

	public PluginLoader(URL url) throws PluginException {
		super(new URL[]{url});
		init();
	}

	public DetectorFactory[] getDetectorFactoryList() {
		return null;
	}

	public BugPattern[] getBugPatternList() {
		return null;
	}

	private void init() throws PluginException {
		Document pluginDescriptor; // a.k.a, "findbugs.xml"
		Document messageCollection; // a.k.a., "messages.xml"

		try {
			URL descriptorURL = findResource("findbugs.xml");
			if (descriptorURL == null)
				throw new PluginException("Couldn't find \"findbugs.xml\" in plugin");

			SAXReader reader = new SAXReader();
			pluginDescriptor = reader.read(descriptorURL);
		} catch (DocumentException e) {
			throw new PluginException("Couldn't parse \"findbugs.xml\"", e);
		}

		try {
			URL messageURL = null;

			Locale locale = Locale.getDefault();
			String language = locale.getLanguage();
			String country = locale.getCountry();

			if (!country.equals(""))
				messageURL = findResource("messages_" + language + "_" + country + ".xml");

			if (messageURL == null)
				messageURL = findResource("messages_" + language + ".xml");

			if (messageURL == null)
				messageURL = findResource("messages.xml");

			if (messageURL == null)
				throw new PluginException("Couldn't find messages.xml");

			SAXReader reader = new SAXReader();
			messageCollection = reader.read(messageURL);
		} catch (DocumentException e) {
			throw new PluginException("Couldn't parse \"messages.xml\"", e);
		}

		// Create a DetectorFactory for all Detector nodes
		List detectorNodeList = pluginDescriptor.selectNodes("/FindbugsPlugin/Detector");
		for (Iterator i = detectorNodeList.iterator(); i.hasNext(); ) {
			Element detectorElement = (Element) i.next();
			String className = detectorElement.valueOf("@class");

			System.out.println("Found detector: class="+className);
		}

	}


	public static void main(String[] argv) {
		try {

			if (argv.length != 1) {
				System.out.println("Usage: " + PluginLoader.class.getName() + " <url>");
				System.exit(1);
			}

			URL url = new URL(argv[0]);
			PluginLoader loader = new PluginLoader(url);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

// vim:ts=4

@@ -1013,16 +1013,74 @@
 ader %7B%0A%0A
+%09private ArrayList%3CDetectorFactory%3E detectorFactoryList;%0A%0A
 %09public 
@@ -2591,16 +2591,84 @@
 r nodes%0A
+%09%09try %7B%0A%09%09%09detectorFactoryList = new ArrayList%3CDetectorFactory%3E();%0A%09
 %09%09List d
@@ -2743,16 +2743,17 @@
 ctor%22);%0A
+%09
 %09%09for (I
@@ -2813,16 +2813,17 @@
  ) %7B%0A%09%09%09
+%09
 Element 
@@ -2856,24 +2856,25 @@
 ) i.next();%0A
+%09
 %09%09%09String cl
@@ -2922,64 +2922,465 @@
 %22);%0A
-%0A%09%09%09System.out.println(%22Found detector: class=%22+classNam
+%09%09%09%09String disabled = detectorElement.valueOf(%22@disabled%22);%0A%09%0A%09%09%09%09System.out.println(%22Found detector: class=%22+className+%22, disabled=%22+disabled);%0A%09%0A%09%09%09%09if (!disabled.equals(%22true%22)) %7B%0A%09%09%09%09%09Class detectorClass = loadClass(className);%0A%09%09%09%09%09DetectorFactory factory = new DetectorFactory(detectorClass);%0A%09%09%09%09%09detectorFactoryList.add(factory);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D catch (ClassNotFoundException e) %7B%0A%09%09%09throw new PluginException(%22Could not instantiate detector class%22, 
 e);%0A

Create the DetectorFactories.