DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Eclipse PropertyTester Finder

07.23.2009
| 2871 views |
  • submit to reddit
        // description of your code here
Prints all Eclipse Property Testers, as an HTML table. You have to specify the eclipse directory to search it.

public class PropertyTestFinder {

	static DocumentBuilder builder;

	public static void main(String[] args) throws Exception {

		if (args.length == 0 || !new File(args[0]).isDirectory()) {
			System.out.println("Usage Searcher <eclipse-dir>");
		} else {
			builder  = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			String dir = args[0];
			System.out.println("<table>"
					+ "<tr>"
					+ "<td align='center'><b>Namespace</b></td>"
					+ "<td align='center'><b>Properties</b></td>"
					+ "<td align='center'><b>Type</b></td>"
					+ "<td align='center'><b>Class</b></td>"
					+ "<td align='center'><b>Location</b></td>"
					+ "</tr>");
			findString(new File(dir));
			System.out.println("</table>");
		}
	}

	public static void findString(File dir) throws Exception {
		File[] files = dir.listFiles();
		if (files != null) {
			for (File file : files) {
				if (file.isDirectory())
					findString(file);
				else {
					if (!file.canRead()) {
						System.out.println("Can not read " + file);
					} else {
						if (file.getName().endsWith(".zip") || file.getName().endsWith(".jar")) {
							ZipInputStream zipIS = new ZipInputStream(new FileInputStream(file));
							BufferedReader reader = new BufferedReader(new InputStreamReader(zipIS));
							ZipEntry entry;
							try {
								while ((entry = zipIS.getNextEntry()) != null) {
									if (entry.getName().endsWith("plugin.xml")) {
										StringBuilder sb = new StringBuilder();
										String line;
										while ((line = reader.readLine()) != null) {
											sb.append(line + " ");
										}
										ByteArrayInputStream in = new ByteArrayInputStream(sb.toString().getBytes());
										Document doc = builder.parse(in);
										check(doc.getDocumentElement(), file.getName());
									}
								}
							} catch (Exception ex) {
								ex.printStackTrace();
							}
							zipIS.close();
						} else {
							if (file.getName().endsWith("plugin.xml")) {
								Document doc = builder.parse(new FileInputStream(file));
								check(doc.getDocumentElement(), file.getParentFile().getName());
							}
						}
					}
				}
			}
		}
	}

	public static void check(Element element, String location) {
		if (element.getNodeName().equals("propertyTester")) {
			String type = element.getAttribute("type");
			type = type.substring(type.lastIndexOf('.') + 1);
			String clazz = element.getAttribute("class");
			clazz = clazz.substring(clazz.lastIndexOf('.') + 1);
			System.out.println("<tr>"
					+ "<td>" + element.getAttribute("namespace") + "</td>"
					+ "<td>" + element.getAttribute("properties").replace(",", ", ") + "</td>"
					+ "<td>" + type + "</td>"
					+ "<td>" + clazz + "</td>"
					+ "<td>" + location + "</td>"
					+ "</tr>");
		}
		NodeList children = element.getChildNodes();
		for (int i=0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child instanceof Element) {
				check((Element) child, location);
			}
		}
	}
}