<commit_before>/*
 * Generate a Java classpath from an Eclipse plugin.xml file
 * Copyright (C) 2004, 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.tools.eclipse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;

import org.dom4j.io.SAXReader;

/**
 * Starting from an Eclipse plugin, finds all required plugins
 * (in an Eclipse installation) and recursively finds the classpath
 * required to compile the original plugin.  Different Eclipse
 * releases will generally have different version numbers on the
 * plugins they contain, which makes this task slightly difficult.
 *
 * <p> Basically, this is a big complicated hack to allow compilation
 * of the FindBugs Eclipse plugin outside of the Eclipse workspace,
 * in a way that doesn't depend on any specific release of Eclipse.
 *
 * @author <NAME>
 */
public class EclipseClasspath {

	public static class EclipseClasspathException extends Exception {
		public EclipseClasspathException(String msg) {
			super(msg);
		}
	}

	/**
	 * A customized Reader for Eclipse plugin descriptor files.
	 * The problem is that Eclipse uses invalid XML in the form of
	 * directives like
	 * <pre>
	 * &lt;?eclipse version="3.0"?&gt;
	 * </pre>
	 * outside of the root element.  This Reader class
	 * filters out this crap.
	 */
	private static class EclipseXMLReader extends Reader {
		private BufferedReader reader;
		private LinkedList<String> lineList;

		public EclipseXMLReader(Reader reader) {
			this.reader = new BufferedReader(reader);
			this.lineList = new LinkedList<String>();
		}

		public int read(char[] cbuf, int off, int len) throws IOException {
			if (!fill())
				return -1;
			String line = lineList.getFirst();
			if (len > line.length())
				len = line.length();
			for (int i = 0; i < len; ++i)
				cbuf[i+off] = line.charAt(i);
			if (len == line.length())
				lineList.removeFirst();
			else
				lineList.set(0, line.substring(len));
			return len;
		}

		public void close() throws IOException {
			reader.close();
		}

		private boolean fill() throws IOException {
			if (!lineList.isEmpty())
				return true;

			String line;
			do {
				line = reader.readLine();
				if (line == null)
					return false;
			} while (isIllegal(line));
			lineList.add(line+"\n");
			return true;
		}

		private boolean isIllegal(String line) {
			return line.startsWith("<?eclipse");
		}
	}

	private class Plugin {
		private String directory;
		private Document document;
		private boolean isDependent;
		private String pluginId;
		private String pluginVersion;
		private List<String> requiredPluginIdList;
		private List<String> exportedLibraryList;

		public Plugin(String directory, Document document, boolean isDependent)
				throws DocumentException, EclipseClasspathException {
			this.directory = directory;
			this.document = document;
			this.isDependent = isDependent;

			// Get the plugin id
			Node plugin = document.selectSingleNode("/plugin");
			if (plugin == null)
				throw new EclipseClasspathException("No plugin node in plugin descriptor");
			pluginId = plugin.valueOf("@id");
			if (pluginId.equals(""))
				throw new EclipseClasspathException("Cannot determine plugin id");
			//System.out.println("Plugin id is " + pluginId);
			pluginVersion = plugin.valueOf("@version");
			if (pluginVersion.equals(""))
				throw new EclipseClasspathException("Cannot determine plugin version");

			// Extract required plugins
			requiredPluginIdList = new LinkedList<String>();
			List requiredPluginNodeList = document.selectNodes("/plugin/requires/import");
			for (Iterator i = requiredPluginNodeList.iterator(); i.hasNext(); ) {
				Node node = (Node) i.next();
				String requiredPluginId = node.valueOf("@plugin");
				if (requiredPluginId.equals(""))
					throw new EclipseClasspathException("Import has no plugin id");
				//System.out.println(" Required plugin ==> " + requiredPluginId);
				requiredPluginIdList.add(requiredPluginId);
			}

			// Extract exported libraries
			exportedLibraryList = new LinkedList<String>();
			List exportedLibraryNodeList = document.selectNodes("/plugin/runtime/library");
			for (Iterator i = exportedLibraryNodeList.iterator(); i.hasNext(); ) {
				Node node = (Node) i.next();
				String jarName = node.valueOf("@name");
				if (jarName.equals(""))
					throw new EclipseClasspathException("Could not get name of exported library");

				jarName = replaceSpecial(jarName);
				File jarFile = new File(jarName);
				if (!jarFile.isAbsolute()) {
					// Make relative to plugin directory<commit_msg>Minor improvement to error reporting<commit_after>/*
 * Generate a Java classpath from an Eclipse plugin.xml file
 * Copyright (C) 2004, 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.tools.eclipse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;

import org.dom4j.io.SAXReader;

/**
 * Starting from an Eclipse plugin, finds all required plugins
 * (in an Eclipse installation) and recursively finds the classpath
 * required to compile the original plugin.  Different Eclipse
 * releases will generally have different version numbers on the
 * plugins they contain, which makes this task slightly difficult.
 *
 * <p> Basically, this is a big complicated hack to allow compilation
 * of the FindBugs Eclipse plugin outside of the Eclipse workspace,
 * in a way that doesn't depend on any specific release of Eclipse.
 *
 * @author <NAME>
 */
public class EclipseClasspath {

	public static class EclipseClasspathException extends Exception {
		public EclipseClasspathException(String msg) {
			super(msg);
		}
	}

	/**
	 * A customized Reader for Eclipse plugin descriptor files.
	 * The problem is that Eclipse uses invalid XML in the form of
	 * directives like
	 * <pre>
	 * &lt;?eclipse version="3.0"?&gt;
	 * </pre>
	 * outside of the root element.  This Reader class
	 * filters out this crap.
	 */
	private static class EclipseXMLReader extends Reader {
		private BufferedReader reader;
		private LinkedList<String> lineList;

		public EclipseXMLReader(Reader reader) {
			this.reader = new BufferedReader(reader);
			this.lineList = new LinkedList<String>();
		}

		public int read(char[] cbuf, int off, int len) throws IOException {
			if (!fill())
				return -1;
			String line = lineList.getFirst();
			if (len > line.length())
				len = line.length();
			for (int i = 0; i < len; ++i)
				cbuf[i+off] = line.charAt(i);
			if (len == line.length())
				lineList.removeFirst();
			else
				lineList.set(0, line.substring(len));
			return len;
		}

		public void close() throws IOException {
			reader.close();
		}

		private boolean fill() throws IOException {
			if (!lineList.isEmpty())
				return true;

			String line;
			do {
				line = reader.readLine();
				if (line == null)
					return false;
			} while (isIllegal(line));
			lineList.add(line+"\n");
			return true;
		}

		private boolean isIllegal(String line) {
			return line.startsWith("<?eclipse");
		}
	}

	private class Plugin {
		private String directory;
		private Document document;
		private boolean isDependent;
		private String pluginId;
		private String pluginVersion;
		private List<String> requiredPluginIdList;
		private List<String> exportedLibraryList;

		public Plugin(String directory, Document document, boolean isDependent)
				throws DocumentException, EclipseClasspathException {
			this.directory = directory;
			this.document = document;
			this.isDependent = isDependent;

			// Get the plugin id
			Node plugin = document.selectSingleNode("/plugin");
			if (plugin == null)
				throw new EclipseClasspathException("No plugin node in plugin descriptor");
			pluginId = plugin.valueOf("@id");
			if (pluginId.equals(""))
				throw new EclipseClasspathException("Cannot determine plugin id in directory " + directory);
			//System.out.println("Plugin id is " + pluginId);
			pluginVersion = plugin.valueOf("@version");
			if (pluginVersion.equals(""))
				throw new EclipseClasspathException("Cannot determine plugin version");

			// Extract required plugins
			requiredPluginIdList = new LinkedList<String>();
			List requiredPluginNodeList = document.selectNodes("/plugin/requires/import");
			for (Iterator i = requiredPluginNodeList.iterator(); i.hasNext(); ) {
				Node node = (Node) i.next();
				String requiredPluginId = node.valueOf("@plugin");
				if (requiredPluginId.equals(""))
					throw new EclipseClasspathException("Import has no plugin id");
				//System.out.println(" Required plugin ==> " + requiredPluginId);
				requiredPluginIdList.add(requiredPluginId);
			}

			// Extract exported libraries
			exportedLibraryList = new LinkedList<String>();
			List exportedLibraryNodeList = document.selectNodes("/plugin/runtime/library");
			for (Iterator i = exportedLibraryNodeList.iterator(); i.hasNext(); ) {
				Node node = (Node) i.next();
				String jarName = node.valueOf("@name");
				if (jarName.equals(""))
					throw new EclipseClasspathException("Could not get name of exported library");

				jarName = replaceSpecial(jarName);
				File jarFile = new File(jarName);
				if (!jarFile.isAbsolute()) {
					// Make relative to plugin directory
{'fraction_non_alphanumeric': 0.10086304594194055, 'fraction_numerical': 0.0054049341818498825, 'mean_word_length': 3.9748482220294883, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6101045', 'n_tokens_mistral': 3629, 'n_tokens_neox': 3108, 'n_words': 1128}