1 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 69 @Deprecated 70 public class Preprocess extends MatchingTask { 71 72 private static final String F_BEGIN = "/*nbif"; 74 private static final String F_ELSE = "nbelse*/"; 76 private static final String R_ELSE = "/*nbelse"; 78 private static final String F_END = "/*nbend*/"; 80 81 82 83 private File src; 84 85 private File dest; 86 87 private boolean copyAll = false; 88 89 private List<Switch> switches = new LinkedList<Switch> (); 90 91 92 public void setSrcDir (File f) { 93 src = f; 94 } 95 96 98 public void setDestDir (File f) { 99 dest = f; 100 } 101 102 106 public void setCopyAll (boolean copyAll) { 107 this.copyAll = copyAll; 108 } 109 110 113 public class Switch { 114 String name; 115 boolean on; 116 117 public void setName (String name) { 118 this.name = name; 119 } 120 121 public void setOn (boolean on) { 122 this.on = on; 123 } 124 } 125 126 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 [] files = scanner.getIncludedFiles (); 144 String message1 = "Processing " + files.length + 145 " file(s) from directory " + src + " to " + dest; 146 147 StringBuffer message2 = new StringBuffer ("Switches:"); 148 Set<String > ss = new HashSet<String > (); 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 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 194 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 226 227 228 229 230 237 @SuppressWarnings ("fallthrough") 238 private static boolean replace ( 239 BufferedReader r, Writer w, Set props ) throws IOException { 241 boolean modified = false; 242 243 int state = 0; 244 245 for (;;) { 246 String line = r.readLine (); 247 if (line == null) { 248 return modified; 249 } 250 251 switch (state) { 252 case 0: if (line.trim ().startsWith (F_BEGIN)) { 254 String rest = line.trim ().substring(F_BEGIN.length ()).trim (); 255 if (props.contains (rest)) { 256 line += "*/"; modified = true; 260 state = 1; 261 } 262 } 263 break; 264 case 1: if (line.trim ().equals (F_ELSE)) { 266 line = R_ELSE; 267 modified = true; state = 2; 269 } 270 case 2: 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 |