📄 eclipseclasspath.java
字号:
/* * 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.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;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.StringTokenizer;import java.util.jar.Attributes;import java.util.jar.Manifest;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 David Hovemeyer */public class EclipseClasspath { public static class EclipseClasspathException extends Exception { public EclipseClasspathException(String msg) { super(msg); } public EclipseClasspathException(String msg, Throwable e) { super(msg, e); } } /** * A customized Reader for Eclipse plugin descriptor files. * The problem is that Eclipse uses invalid XML in the form of * directives like * <pre> * <?eclipse version="3.0"?> * </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 boolean isDependent; private String pluginId; private String pluginVersion; private List<String> requiredPluginIdList; private List<String> exportedLibraryList; public Plugin(String directory, boolean isDependent) throws DocumentException, EclipseClasspathException, IOException { this.directory = directory; this.isDependent = isDependent; this.requiredPluginIdList = new LinkedList<String>(); this.exportedLibraryList = new LinkedList<String>(); // Figure out whether this is an old-style (Eclipse 2.1.x) // or new-style (3.0, OSGI-based) plugin. boolean oldStyle = false; Document document = null; File pluginDescriptorFile = new File(directory + File.separator + "plugin.xml"); if (pluginDescriptorFile.isFile()) { SAXReader reader = new SAXReader(); document = reader.read(new EclipseXMLReader(new FileReader(pluginDescriptorFile))); Node plugin = document.selectSingleNode("/plugin"); if (plugin == null) throw new EclipseClasspathException("No plugin node in plugin descriptor"); oldStyle = !plugin.valueOf("@id").equals(""); } // Get the plugin id if (oldStyle) { parseOldPluginDescriptor(directory, document, isDependent); } else { parseNewPluginDescriptor(directory, isDependent); } } public String getDirectory() { return directory; } public boolean isDependent() { return isDependent; } public String getId() { return pluginId; } public String getVersion() { return pluginVersion; } public Iterator<String> requiredPluginIdIterator() { return requiredPluginIdList.iterator(); } public Iterator<String> exportedLibraryIterator() { return exportedLibraryList.iterator(); } private void parseOldPluginDescriptor(String directory, Document document, boolean isDependent) throws DocumentException, EclipseClasspathException { // In Eclipse 2.1.x, all of the information we need // is in plugin.xml. Node plugin = document.selectSingleNode("/plugin"); pluginId = plugin.valueOf("@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 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 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 jarFile = new File(directory, jarName); } exportedLibraryList.add(jarFile.getPath()); } } private void parseNewPluginDescriptor(String directory, boolean isDependent) throws DocumentException, EclipseClasspathException { // In Eclipse 3.x, we need to parse the plugin's MANIFEST.MF BufferedInputStream in = null; try { String manifestFileName = directory + File.separator + "META-INF/MANIFEST.MF"; in = new BufferedInputStream(new FileInputStream(manifestFileName)); Manifest manifest = new Manifest(in); Attributes attributes = manifest.getMainAttributes(); // Get the plugin id
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -