KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > tasks > PluginVersionReplaceTask


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.build.tasks;
12
13 import java.io.*;
14 import org.apache.tools.ant.BuildException;
15 import org.apache.tools.ant.Task;
16
17 /**
18  * Internal task.
19  * This task aims at replacing the generic ids used into a plugin.xml by another value.
20  * @since 3.0
21  */

22 public class PluginVersionReplaceTask extends Task {
23     private static final String JavaDoc PLUGIN_START_TAG = "<plugin"; //$NON-NLS-1$
24
private static final String JavaDoc FRAGMENT_START_TAG = "<fragment"; //$NON-NLS-1$
25
private static final String JavaDoc COMMENT_START_TAG = "<!--"; //$NON-NLS-1$
26
private static final String JavaDoc COMMENT_END_TAG = "-->"; //$NON-NLS-1$
27
private static final String JavaDoc VERSION = "version";//$NON-NLS-1$
28
private static final String JavaDoc BACKSLASH = "\""; //$NON-NLS-1$
29

30     //Path of the file where we are replacing the values
31
private String JavaDoc pluginFilePath;
32     private boolean plugin = true;
33     private String JavaDoc newVersion;
34
35     /**
36      * The location of a fragment.xml or plugin.xml file
37      * @param path
38      */

39     public void setPluginFilePath(String JavaDoc path) {
40         pluginFilePath = path;
41     }
42
43     /**
44      * Set the new version.
45      * @param qualifier the version that will be set in the manifest file.
46      */

47     public void setVersionNumber(String JavaDoc qualifier) {
48         newVersion = qualifier;
49     }
50
51     /**
52      * Set the type of the file.
53      * @param input
54      */

55     public void setInput(String JavaDoc input) {
56         if (input.equalsIgnoreCase("fragment.xml")) //$NON-NLS-1$
57
plugin = false;
58     }
59
60     public void execute() {
61         StringBuffer JavaDoc buffer = null;
62         try {
63             buffer = readFile(new File(pluginFilePath));
64         } catch (IOException e) {
65             throw new BuildException(e);
66         }
67
68         //Find the word plugin or fragment
69
int startPlugin = scan(buffer, 0, plugin ? PLUGIN_START_TAG : FRAGMENT_START_TAG);
70         int startComment = scan(buffer, 0, COMMENT_START_TAG);
71         int endComment = startComment > -1 ? scan(buffer, startComment, COMMENT_END_TAG) : -1;
72
73         while (startComment != -1 && startPlugin > startComment && startPlugin < endComment) {
74             startPlugin = scan(buffer, endComment, plugin ? PLUGIN_START_TAG : FRAGMENT_START_TAG);
75             startComment = scan(buffer, endComment, COMMENT_START_TAG);
76             endComment = startComment > -1 ? scan(buffer, startComment, COMMENT_END_TAG) : -1;
77         }
78
79         if (startPlugin == -1)
80             return;
81
82         int endPlugin = scan(buffer, startPlugin + 1, ">"); //$NON-NLS-1$
83

84         //Find the version tag in the plugin header
85
boolean versionFound = false;
86         while (!versionFound) {
87             int versionAttr = scan(buffer, startPlugin, VERSION);
88             if (versionAttr == -1 || versionAttr > endPlugin)
89                 return;
90             if (!Character.isWhitespace(buffer.charAt(versionAttr - 1))) {
91                 startPlugin = versionAttr + VERSION.length();
92                 continue;
93             }
94             //Verify that the word version found is the actual attribute
95
int endVersionWord = versionAttr + VERSION.length();
96             while (Character.isWhitespace(buffer.charAt(endVersionWord)) && endVersionWord < endPlugin) {
97                 endVersionWord++;
98             }
99             if (endVersionWord > endPlugin) //version has not been found
100
return;
101
102             if (buffer.charAt(endVersionWord) != '=') {
103                 startPlugin = endVersionWord;
104                 continue;
105             }
106
107             //Version has been found, extract the version id and replace it
108
int startVersionId = scan(buffer, versionAttr + 1, BACKSLASH);
109             int endVersionId = scan(buffer, startVersionId + 1, BACKSLASH);
110
111             buffer.replace(startVersionId + 1, endVersionId, newVersion);
112             versionFound = true;
113         }
114         try {
115             OutputStreamWriter w = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(pluginFilePath)), "UTF-8"); //$NON-NLS-1$
116
w.write(buffer.toString());
117             w.close();
118         } catch (FileNotFoundException e) {
119             // ignore
120
} catch (IOException e) {
121             throw new BuildException(e);
122         }
123     }
124
125     private int scan(StringBuffer JavaDoc buf, int start, String JavaDoc targetName) {
126         return scan(buf, start, new String JavaDoc[] {targetName});
127     }
128
129     private int scan(StringBuffer JavaDoc buf, int start, String JavaDoc[] targets) {
130         for (int i = start; i < buf.length(); i++) {
131             for (int j = 0; j < targets.length; j++) {
132                 if (i < buf.length() - targets[j].length()) {
133                     String JavaDoc match = buf.substring(i, i + targets[j].length());
134                     if (targets[j].equalsIgnoreCase(match))
135                         return i;
136                 }
137             }
138         }
139         return -1;
140     }
141
142     private StringBuffer JavaDoc readFile(File targetName) throws IOException {
143         InputStreamReader reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(targetName)), "UTF-8"); //$NON-NLS-1$
144
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
145         char[] buf = new char[4096];
146         int count;
147         try {
148             count = reader.read(buf, 0, buf.length);
149             while (count != -1) {
150                 result.append(buf, 0, count);
151                 count = reader.read(buf, 0, buf.length);
152             }
153         } finally {
154             try {
155                 reader.close();
156             } catch (IOException e) {
157                 // ignore exceptions here
158
}
159         }
160         return result;
161     }
162 }
163
Popular Tags