KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.tools.ant.taskdefs.MatchingTask;
27
28 // In Javadoc below: * == *
29
/** Preprocesses content of file replacing all conditional blocks.
30 * <PRE>
31 * /&#42;nbif someswitch
32 * // PART A
33 * nbelse&#42;/
34 * // PART B
35 * /&#42;nbend&#42;/
36 * </PRE>
37 * If <code>someswitch</code> is off, nothing will be changed, so part A will be
38 * commented-out and part B will be active. If <code>someswitch</code> is on,
39 * the code will be changed to:
40 * <PRE>
41 * /&#42;nbif someswitch&#42;/
42 * // PART A
43 * /&#42;nbelse
44 * // PART B
45 * /&#42;nbend&#42;/
46 * </PRE>
47 * So that part A is active and part B is commented-out.
48 * <p>You can also use a block without an else:
49 * <PRE>
50 * /&#42;nbif someswitch
51 * // PART A
52 * /&#42;nbend&#42;/
53 * </PRE>
54 * With the switch off, it will again be left as is, i.e. commented-out. With the switch
55 * on, you will get:
56 * <PRE>
57 * /&#42;nbif someswitch&#42;/
58 * // PART A
59 * /&#42;nbend&#42;/
60 * </PRE>
61 * where the interior section is now active.
62 * <p>Intent of this preprocessor is to permit incompatible API changes to made in source
63 * code, while creating a variant binary compatibility kit without the changes or with
64 * more conservative changes. It should <em>not</em> be used as a general-purpose Java
65 * preprocessor, we are not C++ programmers here!
66 * @author Jaroslav Tulach, Jesse Glick
67 * @deprecated No longer used.
68 */

69 @Deprecated JavaDoc
70 public class Preprocess extends MatchingTask {
71     /** the format of begining of substitution */
72     private static final String JavaDoc F_BEGIN = "/*nbif"; // NOI18N
73
/** format of else statement */
74     private static final String JavaDoc F_ELSE = "nbelse*/"; // NOI18N
75
/** how to replace the else */
76     private static final String JavaDoc R_ELSE = "/*nbelse"; // NOI18N
77
/** format of end statement */
78     private static final String JavaDoc F_END = "/*nbend*/"; // NOI18N
79

80     
81     
82     /** source directory to scan files from */
83     private File src;
84     /** target directory */
85     private File dest;
86     /** copy all/copy modified */
87     private boolean copyAll = false;
88     /** switches to check in conditionals */
89     private List<Switch> switches = new LinkedList<Switch> ();
90     
91     /** Setter for the source directory to scan. */
92     public void setSrcDir (File f) {
93         src = f;
94     }
95     
96     /** Setter for the target directory to create processed files in.
97     */

98     public void setDestDir (File f) {
99         dest = f;
100     }
101     
102     /** Can be set to copy all files, if necessary.
103      * Default is not to copy a file unless it was actually modified.
104     * @param copyAll true if all files (even unmodified) should be copied
105     */

106     public void setCopyAll (boolean copyAll) {
107         this.copyAll = copyAll;
108     }
109
110     /** A switch to use as the test in preprocessor conditionals.
111      * Only switches explicitly listed here will be recognized.
112      */

113     public class Switch {
114         String JavaDoc name;
115         boolean on;
116         /** Set the name of the switch, which will be used in <code>nbif</code> tests. */
117         public void setName (String JavaDoc name) {
118             this.name = name;
119         }
120         /** Set whether the switch should be on or not. */
121         public void setOn (boolean on) {
122             this.on = on;
123         }
124     }
125
126     /** Add a conditional switch to control preprocessing. */
127     public Switch createSwitch () {
128         Switch s = new Switch ();
129         switches.add (s);
130         return s;
131     }
132     
133     public void execute () throws BuildException {
134         if (src == null || dest == null) {
135             throw new BuildException ("src and dest must be specified");
136         }
137         if (switches.isEmpty ()) {
138             throw new BuildException ("Useless to preprocess sources with no switches specified!");
139         }
140         
141         DirectoryScanner scanner = getDirectoryScanner (src);
142         scanner.scan ();
143         String JavaDoc[] files = scanner.getIncludedFiles ();
144         String JavaDoc message1 = "Processing " + files.length +
145             " file(s) from directory " + src + " to " + dest;
146         
147         StringBuffer JavaDoc message2 = new StringBuffer JavaDoc ("Switches:");
148         Set<String JavaDoc> ss = new HashSet<String JavaDoc> ();
149         for (Switch s: switches) {
150             if (s.on) {
151                 ss.add (s.name);
152                 message2.append (' ');
153                 message2.append (s.name);
154             } else {
155                 message2.append (" !");
156                 message2.append (s.name);
157             }
158         }
159         
160         try {
161             boolean shownMessages = false;
162             for (int i = 0; i < files.length; i++) {
163                 File src = new File (this.src, files[i]);
164                 File dest = new File (this.dest, files[i]);
165                 // Up-to-date check (note that this ignores changes in
166
// switches; for that you must clean first!):
167
if (dest.exists () && dest.lastModified () >= src.lastModified ()) {
168                     continue;
169                 }
170
171                 int size = (int)src.length () + 500;
172                 BufferedReader r = new BufferedReader (
173                     new FileReader (src), size
174                 );
175                 StringWriter w = new StringWriter (size);
176
177                 boolean modified = replace (r, w, ss);
178                 w.close ();
179                 r.close ();
180
181                 if ((modified || copyAll) && ! shownMessages) {
182                     shownMessages = true;
183                     log (message1);
184                     log (message2.toString ());
185                 }
186                 
187                 if (modified) {
188                     log ("Modified: " + files[i]);
189                 }
190
191                 if (modified || copyAll) {
192                     // the file has been modified
193

194                     // ensure the directories exists
195
File dir = dest.getParentFile ();
196                     dir.mkdirs ();
197                     
198                     Writer file = new FileWriter (dest);
199                     file.write (w.getBuffer().toString());
200                     file.close ();
201                 }
202             }
203         } catch (IOException ex) {
204             throw new BuildException (ex);
205         }
206     }
207     
208     
209     
210     
211 /*nbif test
212     public static void main (String[] args) throws IOException {
213         BufferedReader r = new BufferedReader (
214             new FileReader (args[0])
215         );
216         
217         BufferedWriter w = new BufferedWriter (
218             new OutputStreamWriter (System.out), System.getProperties ()
219         );
220         
221         replace (r, w);
222         
223         w.close ();
224     }
225 /*nbend*/

226     
227     
228     
229     
230     /** Reads a content of a file and produces replaces given lines
231      *
232      * @param r reader to read from
233      * @param w writer to write to
234      * @param props properties to be considered on
235      * @return true if the content of r has been modified
236      */

237     @SuppressWarnings JavaDoc("fallthrough")
238     private static boolean replace (
239         BufferedReader r, Writer w, Set props // Set<String>
240
) throws IOException {
241         boolean modified = false;
242         
243         int state = 0;
244         
245         for (;;) {
246             String JavaDoc line = r.readLine ();
247             if (line == null) {
248                 return modified;
249             }
250             
251             switch (state) {
252                 case 0: // regular text
253
if (line.trim ().startsWith (F_BEGIN)) {
254                         String JavaDoc rest = line.trim ().substring(F_BEGIN.length ()).trim ();
255                         if (props.contains (rest)) {
256                             // successful test, the content of line should
257
// be included
258
line += "*/"; // NOI18N
259
modified = true;
260                             state = 1;
261                         }
262                     }
263                     break;
264                 case 1: // waiting for the nbelse*/ statement
265
if (line.trim ().equals (F_ELSE)) {
266                         line = R_ELSE;
267                         modified = true; // redundant, for clarity
268
state = 2;
269                     }
270                     // FALLTHROUGH: OK to have a if-end block with no else!
271
case 2: // inside the else, waiting for end
272
if (line.trim ().equals (F_END)) {
273                         state = 0;
274                     }
275                     break;
276             }
277             
278             w.write (line);
279             w.write ('\n');
280         }
281     }
282 }
283
Popular Tags