blob: 811a6687ffa315919896f2d7d478edb81a2c0483 [file] [log] [blame]
david_williams29ca4b12005-09-04 02:34:32 +00001/*******************************************************************************
2 * Copyright (c) 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.wst.sse.unittests.minortools;
12
13import java.io.File;
14import java.io.FileOutputStream;
15import java.io.IOException;
16
17import org.eclipse.wst.xml.core.tests.util.CommonXML;
18import org.w3c.dom.Document;
19import org.w3c.dom.Node;
20import org.w3c.dom.NodeList;
21import org.xml.sax.SAXException;
22
23
24
25/**
26 * Modifies plugin.xml and fragment.xml files to not require specific versions
27 * of their plugin dependencies.
28 *
29 * @author nitin
30 */
31public class VersionRemover {
32
33 char[] charbuff = new char[2048];
34 StringBuffer s = null;
35
36 public VersionRemover() {
37 super();
38 }
39
40
41
42 public static void main(String[] args) {
43 if (args.length < 1)
44 new VersionRemover().visit(new File("d:/target"));
45 else
46 new VersionRemover().visit(new File(args[0]));
47 }
48
49
50
51 protected void visit(File file) {
52 // Skip directories like org.eclipse.*, org.apache.*, and org.junit.*
53 if (file.isDirectory() && !file.getName().startsWith("org.eclipse.") && !file.getName().startsWith("org.apache") && !file.getName().startsWith("org.junit")) {
54 String[] contents = file.list();
55 for (int i = 0; i < contents.length; i++)
56 visit(new File(file.getAbsolutePath() + '/' + contents[i]));
57 }
58 else {
59 fixupFile(file);
60 }
61 }
62
63 protected void fixupFile(File file) {
64 // only load and fixup files named plugin.xml or fragment.xml under eclipse\plugins\XXXXXXX.*
65 if (!(file.getName().equalsIgnoreCase("plugin.xml") || file.getName().equalsIgnoreCase("fragment.xml")) || file.getAbsolutePath().indexOf("eclipse\\plugins\\XXXXXXX.") == -1)
66 return;
67 // System.out.println(file.getAbsolutePath());
68 try {
69 Document doc = CommonXML.getDocumentBuilder().parse(file);
70 NodeList imports = null;
71 if (file.getName().equalsIgnoreCase("plugin.xml"))
72 imports = doc.getElementsByTagName("import");
73 else if (file.getName().equalsIgnoreCase("fragment.xml"))
74 imports = doc.getElementsByTagName("fragment");
75 boolean changed = false;
76 for (int i = 0; i < imports.getLength(); i++) {
77 Node importNode = imports.item(i);
78 if (importNode.getNodeName().equalsIgnoreCase("import") && importNode.getAttributes().getNamedItem("version") != null) {
79 changed = true;
80 importNode.getAttributes().removeNamedItem("version");
81 }
82 if (importNode.getAttributes().getNamedItem("plugin-version") != null) {
83 changed = true;
84 importNode.getAttributes().removeNamedItem("plugin-version");
85 }
86 if (importNode.getAttributes().getNamedItem("match") != null) {
87 importNode.getAttributes().removeNamedItem("match");
88 changed = true;
89 }
90 }
91 if (changed) {
92 FileOutputStream ostream = new FileOutputStream(file.getAbsolutePath());
93 CommonXML.serialize(doc, ostream);
94 ostream.close();
95 System.out.println("Modified " + file.getAbsolutePath());
96 }
97 }
98 catch (SAXException e) {
99 System.err.println(file.getPath() + ": " + e);
100 }
101 catch (IOException e) {
102 System.err.println(file.getPath() + ": " + e);
103 }
104 }
105}