KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > Postprocess


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.apache.tools.ant.*;
26
27 /** Changes content of a binary file. Usually used to change already compiled
28 * bytecode to contain, for example, a different method name. This is one way
29 * to cause two methods with the same name and arguments
30 * to differ in return type.
31 * <p>Differs little from the standard <code>&lt;replace&gt;</code> task,
32 * though a little more customized for binary files.
33 *
34 * @author Jaroslav Tulach
35 * @deprecated No longer used.
36 */

37 @Deprecated JavaDoc
38 public class Postprocess extends Task {
39     /** file to post process */
40     private File file;
41     /** string to replace */
42     private String JavaDoc oldString;
43     /** string to replace with */
44     private String JavaDoc newString;
45     /** minimum number of occurrences of the string */
46     private int min = 0;
47     /** maximum number of occurrences of the string */
48     private int max = 1;
49     
50     /** Set the file to work on.
51      * @param f the file
52      */

53     public void setFile (File f) {
54         this.file = f;
55     }
56     
57     /** Set the string to search for
58      */

59     public void setOld (String JavaDoc s) {
60         this.oldString = s;
61     }
62     
63     /** Set new string.
64      */

65     public void setNew (String JavaDoc s) {
66         this.newString = s;
67     }
68     
69     /** Sets the minimum number of string occurrences
70      */

71     public void setMin (int m) {
72         this.min = m;
73     }
74     
75     /** Sets the maximum number of string occurrences
76      */

77     public void setMax (int m) {
78         this.max = m;
79     }
80     
81     public void execute () throws BuildException {
82         if (file == null) {
83             throw new BuildException ("A file must be specified"); // NOI18N
84
}
85         
86         if (
87             oldString == null || newString == null ||
88             oldString.length() != newString.length()
89         ) {
90             throw new BuildException ("New and old strings must be specified and they must have the same length"); // NOI18N
91
}
92         
93         try {
94             int len = (int)file.length();
95             byte[] b = new byte[len];
96             FileInputStream is = new FileInputStream (file);
97             try {
98                 if (is.read(b) != len) throw new BuildException("Failed to read whole file", getLocation());
99             } finally {
100                 is.close ();
101             }
102             
103             int cnt = replaceString (b);
104             
105             if (cnt < min || cnt > max) {
106                 throw new BuildException ("String " + oldString + " found " + cnt + " times, that is out of min/max range"); // NOI18N
107
}
108             
109             if (cnt > 0) {
110                 log ("Replaced `" + oldString + "' by `" + newString + "' " + cnt + " times in " + file);
111                 FileOutputStream os = new FileOutputStream (file);
112                 try {
113                     os.write (b);
114                 } finally {
115                     os.close ();
116                 }
117             }
118             
119         } catch (IOException ex) {
120             throw new BuildException (ex);
121         }
122     }
123     
124     
125     /** Scans the array and replaces the occurences of oldString by newString
126      * @param b the array
127      * @return the number of replaces
128      */

129     private int replaceString (byte[] b) {
130         
131         try {
132             
133             // This encoding is unadorned 8-bit.
134
String JavaDoc arr = new String JavaDoc (b, "ISO8859_1");
135             int cnt = 0;
136             int pos = -1;
137             
138             byte[] newbytes = newString.getBytes("ISO8859_1");
139             if (newbytes.length != oldString.getBytes("ISO8859_1").length) {
140                 throw new BuildException("Strings to replace must be equal in length", getLocation());
141             }
142             while ((pos = arr.indexOf(oldString, pos + 1)) != -1) {
143                 System.arraycopy(newbytes, 0, b, pos, newbytes.length);
144                 cnt++;
145             }
146             
147             return cnt;
148             
149         } catch (UnsupportedEncodingException e) {
150             throw new BuildException("Error replacing text", e, getLocation());
151         }
152     }
153         
154 }
155
Popular Tags