1 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 22 public class PluginVersionReplaceTask extends Task { 23 private static final String PLUGIN_START_TAG = "<plugin"; private static final String FRAGMENT_START_TAG = "<fragment"; private static final String COMMENT_START_TAG = "<!--"; private static final String COMMENT_END_TAG = "-->"; private static final String VERSION = "version"; private static final String BACKSLASH = "\""; 30 private String pluginFilePath; 32 private boolean plugin = true; 33 private String newVersion; 34 35 39 public void setPluginFilePath(String path) { 40 pluginFilePath = path; 41 } 42 43 47 public void setVersionNumber(String qualifier) { 48 newVersion = qualifier; 49 } 50 51 55 public void setInput(String input) { 56 if (input.equalsIgnoreCase("fragment.xml")) plugin = false; 58 } 59 60 public void execute() { 61 StringBuffer buffer = null; 62 try { 63 buffer = readFile(new File(pluginFilePath)); 64 } catch (IOException e) { 65 throw new BuildException(e); 66 } 67 68 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, ">"); 84 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 int endVersionWord = versionAttr + VERSION.length(); 96 while (Character.isWhitespace(buffer.charAt(endVersionWord)) && endVersionWord < endPlugin) { 97 endVersionWord++; 98 } 99 if (endVersionWord > endPlugin) return; 101 102 if (buffer.charAt(endVersionWord) != '=') { 103 startPlugin = endVersionWord; 104 continue; 105 } 106 107 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"); w.write(buffer.toString()); 117 w.close(); 118 } catch (FileNotFoundException e) { 119 } catch (IOException e) { 121 throw new BuildException(e); 122 } 123 } 124 125 private int scan(StringBuffer buf, int start, String targetName) { 126 return scan(buf, start, new String [] {targetName}); 127 } 128 129 private int scan(StringBuffer buf, int start, String [] 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 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 readFile(File targetName) throws IOException { 143 InputStreamReader reader = new InputStreamReader(new BufferedInputStream(new FileInputStream(targetName)), "UTF-8"); StringBuffer result = new StringBuffer (); 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 } 159 } 160 return result; 161 } 162 } 163 | Popular Tags |