KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > JDTCompilerAdapter


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 Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.core;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.lang.reflect.Constructor JavaDoc;
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Comparator JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.taskdefs.Javac;
29 import org.apache.tools.ant.taskdefs.compilers.DefaultCompilerAdapter;
30 import org.apache.tools.ant.types.Commandline;
31 import org.apache.tools.ant.types.Path;
32 import org.apache.tools.ant.types.Commandline.Argument;
33 import org.apache.tools.ant.util.JavaEnvUtils;
34 import org.eclipse.jdt.core.compiler.CharOperation;
35 import org.eclipse.jdt.internal.antadapter.AntAdapterMessages;
36 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
37 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
38 import org.eclipse.jdt.internal.compiler.util.Util;
39
40 /**
41  * Ant 1.5 compiler adapter for the Eclipse Java compiler. This adapter permits the
42  * Eclipse Java compiler to be used with the <code>javac</code> task in Ant scripts. In order
43  * to use it, just set the property <code>build.compiler</code> as follows:
44  * <p>
45  * <code>&lt;property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/&gt;</code>
46  * </p>
47  * <p>
48  * For more information on Ant check out the website at http://jakarta.apache.org/ant/ .
49  * </p>
50  *
51  * @since 2.0
52  */

53 public class JDTCompilerAdapter extends DefaultCompilerAdapter {
54     private static final char[] SEPARATOR_CHARS = new char[] { '/', '\\' };
55     private static final char[] ADAPTER_PREFIX = "#ADAPTER#".toCharArray(); //$NON-NLS-1$
56
private static final char[] ADAPTER_ENCODING = "ENCODING#".toCharArray(); //$NON-NLS-1$
57
private static final char[] ADAPTER_ACCESS = "ACCESS#".toCharArray(); //$NON-NLS-1$
58
private static String JavaDoc compilerClass = "org.eclipse.jdt.internal.compiler.batch.Main"; //$NON-NLS-1$
59
String JavaDoc logFileName;
60     Map JavaDoc customDefaultOptions;
61     private Map JavaDoc fileEncodings = null;
62     private Map JavaDoc dirEncodings = null;
63     private List JavaDoc accessRules = null;
64     
65     /**
66      * Performs a compile using the JDT batch compiler
67      * @throws BuildException if anything wrong happen during the compilation
68      * @return boolean true if the compilation is ok, false otherwise
69      */

70     public boolean execute() throws BuildException {
71         this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.usingJDTCompiler"), Project.MSG_VERBOSE); //$NON-NLS-1$
72
Commandline cmd = setupJavacCommand();
73
74         try {
75             Class JavaDoc c = Class.forName(compilerClass);
76             Constructor JavaDoc batchCompilerConstructor = c.getConstructor(new Class JavaDoc[] { PrintWriter JavaDoc.class, PrintWriter JavaDoc.class, Boolean.TYPE, Map JavaDoc.class});
77             Object JavaDoc batchCompilerInstance = batchCompilerConstructor.newInstance(new Object JavaDoc[] {new PrintWriter JavaDoc(System.out), new PrintWriter JavaDoc(System.err), Boolean.TRUE, this.customDefaultOptions});
78             Method JavaDoc compile = c.getMethod("compile", new Class JavaDoc[] {String JavaDoc[].class}); //$NON-NLS-1$
79
Object JavaDoc result = compile.invoke(batchCompilerInstance, new Object JavaDoc[] { cmd.getArguments()});
80             final boolean resultValue = ((Boolean JavaDoc) result).booleanValue();
81             if (!resultValue && this.logFileName != null) {
82                 this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.error.compilationFailed", this.logFileName)); //$NON-NLS-1$
83
}
84             return resultValue;
85         } catch (ClassNotFoundException JavaDoc cnfe) {
86             throw new BuildException(AntAdapterMessages.getString("ant.jdtadapter.error.cannotFindJDTCompiler")); //$NON-NLS-1$
87
} catch (Exception JavaDoc ex) {
88             throw new BuildException(ex);
89         }
90     }
91     
92     
93     protected Commandline setupJavacCommand() throws BuildException {
94         Commandline cmd = new Commandline();
95         this.customDefaultOptions = new CompilerOptions().getMap();
96         
97         Class JavaDoc javacClass = Javac.class;
98         
99         /*
100          * Read in the compiler arguments first since we might need to modify
101          * the classpath if any access rules were specified
102          */

103         String JavaDoc [] compilerArgs = processCompilerArguments(javacClass);
104
105         /*
106          * This option is used to never exit at the end of the ant task.
107          */

108         cmd.createArgument().setValue("-noExit"); //$NON-NLS-1$
109

110         if (this.bootclasspath != null) {
111             cmd.createArgument().setValue("-bootclasspath"); //$NON-NLS-1$
112
if (this.bootclasspath.size() != 0) {
113                 /*
114                  * Set the bootclasspath for the Eclipse compiler.
115                  */

116                 cmd.createArgument().setPath(this.bootclasspath);
117             } else {
118                 cmd.createArgument().setValue(Util.EMPTY_STRING);
119             }
120         }
121
122         Path classpath = new Path(this.project);
123
124        /*
125          * Eclipse compiler doesn't support -extdirs.
126          * It is emulated using the classpath. We add extdirs entries after the
127          * bootclasspath.
128          */

129         if (this.extdirs != null) {
130             cmd.createArgument().setValue("-extdirs"); //$NON-NLS-1$
131
cmd.createArgument().setPath(this.extdirs);
132         }
133
134         /*
135          * The java runtime is already handled, so we simply want to retrieve the
136          * ant runtime and the compile classpath.
137          */

138         classpath.append(getCompileClasspath());
139
140         // For -sourcepath, use the "sourcepath" value if present.
141
// Otherwise default to the "srcdir" value.
142
Path sourcepath = null;
143         
144         // retrieve the method getSourcepath() using reflect
145
// This is done to improve the compatibility to ant 1.5
146
Method JavaDoc getSourcepathMethod = null;
147         try {
148             getSourcepathMethod = javacClass.getMethod("getSourcepath", null); //$NON-NLS-1$
149
} catch(NoSuchMethodException JavaDoc e) {
150             // if not found, then we cannot use this method (ant 1.5)
151
}
152         Path compileSourcePath = null;
153         if (getSourcepathMethod != null) {
154             try {
155                 compileSourcePath = (Path) getSourcepathMethod.invoke(this.attributes, null);
156             } catch (IllegalAccessException JavaDoc e) {
157                 // should never happen
158
} catch (InvocationTargetException JavaDoc e) {
159                 // should never happen
160
}
161         }
162         if (compileSourcePath != null) {
163             sourcepath = compileSourcePath;
164         } else {
165             sourcepath = this.src;
166         }
167         classpath.append(sourcepath);
168         /*
169          * Set the classpath for the Eclipse compiler.
170          */

171         cmd.createArgument().setValue("-classpath"); //$NON-NLS-1$
172
createClasspathArgument(cmd, classpath);
173
174         final String JavaDoc javaVersion = JavaEnvUtils.getJavaVersion();
175         String JavaDoc memoryParameterPrefix = javaVersion.equals(JavaEnvUtils.JAVA_1_1) ? "-J-" : "-J-X";//$NON-NLS-1$//$NON-NLS-2$
176
if (this.memoryInitialSize != null) {
177             if (!this.attributes.isForkedJavac()) {
178                 this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.ignoringMemoryInitialSize"), Project.MSG_WARN); //$NON-NLS-1$
179
} else {
180                 cmd.createArgument().setValue(memoryParameterPrefix
181                                               + "ms" + this.memoryInitialSize); //$NON-NLS-1$
182
}
183         }
184
185         if (this.memoryMaximumSize != null) {
186             if (!this.attributes.isForkedJavac()) {
187                 this.attributes.log(AntAdapterMessages.getString("ant.jdtadapter.info.ignoringMemoryMaximumSize"), Project.MSG_WARN); //$NON-NLS-1$
188
} else {
189                 cmd.createArgument().setValue(memoryParameterPrefix
190                                               + "mx" + this.memoryMaximumSize); //$NON-NLS-1$
191
}
192         }
193
194         if (this.debug) {
195            // retrieve the method getSourcepath() using reflect
196
// This is done to improve the compatibility to ant 1.5
197
Method JavaDoc getDebugLevelMethod = null;
198             try {
199                 getDebugLevelMethod = javacClass.getMethod("getDebugLevel", null); //$NON-NLS-1$
200
} catch(NoSuchMethodException JavaDoc e) {
201                 // if not found, then we cannot use this method (ant 1.5)
202
// debug level is only available with ant 1.5.x
203
}
204             String JavaDoc debugLevel = null;
205             if (getDebugLevelMethod != null) {
206                 try {
207                     debugLevel = (String JavaDoc) getDebugLevelMethod.invoke(this.attributes, null);
208                 } catch (IllegalAccessException JavaDoc e) {
209                     // should never happen
210
} catch (InvocationTargetException JavaDoc e) {
211                     // should never happen
212
}
213             }
214             if (debugLevel != null) {
215                 this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
216                 this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
217                 this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.DO_NOT_GENERATE);
218                 if (debugLevel.length() != 0) {
219                     if (debugLevel.indexOf("vars") != -1) {//$NON-NLS-1$
220
this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
221                     }
222                     if (debugLevel.indexOf("lines") != -1) {//$NON-NLS-1$
223
this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
224                     }
225                     if (debugLevel.indexOf("source") != -1) {//$NON-NLS-1$
226
this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.GENERATE);
227                     }
228                 }
229             } else {
230                 this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
231                 this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
232                 this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.GENERATE);
233             }
234         } else {
235             this.customDefaultOptions.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE);
236             this.customDefaultOptions.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.DO_NOT_GENERATE);
237             this.customDefaultOptions.put(CompilerOptions.OPTION_SourceFileAttribute , CompilerOptions.DO_NOT_GENERATE);
238         }
239         
240         /*
241          * Handle the nowarn option. If none, then we generate all warnings.
242          */

243         if (this.attributes.getNowarn()) {
244             // disable all warnings
245
Object JavaDoc[] entries = this.customDefaultOptions.entrySet().toArray();
246             for (int i = 0, max = entries.length; i < max; i++) {
247                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries[i];
248                 if (!(entry.getKey() instanceof String JavaDoc))
249                     continue;
250                 if (!(entry.getValue() instanceof String JavaDoc))
251                     continue;
252                 if (((String JavaDoc) entry.getValue()).equals(CompilerOptions.WARNING)) {
253                     this.customDefaultOptions.put(entry.getKey(), CompilerOptions.IGNORE);
254                 }
255             }
256             this.customDefaultOptions.put(CompilerOptions.OPTION_TaskTags, Util.EMPTY_STRING);
257             if (this.deprecation) {
258                 this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.WARNING);
259                 this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.ENABLED);
260                 this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.ENABLED);
261             }
262         } else if (this.deprecation) {
263             this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.WARNING);
264             this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.ENABLED);
265             this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.ENABLED);
266         } else {
267             this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecation, CompilerOptions.IGNORE);
268             this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationInDeprecatedCode, CompilerOptions.DISABLED);
269             this.customDefaultOptions.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.DISABLED);
270         }
271
272         /*
273          * destDir option.
274          */

275         if (this.destDir != null) {
276             cmd.createArgument().setValue("-d"); //$NON-NLS-1$
277
cmd.createArgument().setFile(this.destDir.getAbsoluteFile());
278         }
279
280         /*
281          * verbose option
282          */

283         if (this.verbose) {
284             cmd.createArgument().setValue("-verbose"); //$NON-NLS-1$
285
}
286
287         /*
288          * failnoerror option
289          */

290         if (!this.attributes.getFailonerror()) {
291             cmd.createArgument().setValue("-proceedOnError"); //$NON-NLS-1$
292
}
293
294         /*
295          * target option.
296          */

297         if (this.target != null) {
298             this.customDefaultOptions.put(CompilerOptions.OPTION_TargetPlatform, this.target);
299         }
300
301         /*
302          * source option
303          */

304         String JavaDoc source = this.attributes.getSource();
305         if (source != null) {
306             this.customDefaultOptions.put(CompilerOptions.OPTION_Source, source);
307         }
308
309         /*
310          * encoding option
311          */

312         if (this.encoding != null) {
313             cmd.createArgument().setValue("-encoding"); //$NON-NLS-1$
314
cmd.createArgument().setValue(this.encoding);
315         }
316
317         if (compilerArgs != null) {
318             /*
319              * Add extra argument on the command line
320              */

321             final int length = compilerArgs.length;
322             if (length != 0) {
323                 for (int i = 0, max = length; i < max; i++) {
324                     String JavaDoc arg = compilerArgs[i];
325                     if (this.logFileName == null && "-log".equals(arg) && ((i + 1) < max)) { //$NON-NLS-1$
326
this.logFileName = compilerArgs[i + 1];
327                     }
328                     cmd.createArgument().setValue(arg);
329                 }
330             }
331         }
332         /*
333          * Eclipse compiler doesn't have a -sourcepath option. This is
334          * handled through the javac task that collects all source files in
335          * srcdir option.
336          */

337         logAndAddFilesToCompile(cmd);
338         return cmd;
339     }
340     
341     /**
342      * Get the compiler arguments
343      * @param javacClass
344      * @return String[] the array of arguments
345      */

346     private String JavaDoc[] processCompilerArguments(Class JavaDoc javacClass) {
347         // retrieve the method getCurrentCompilerArgs() using reflect
348
// This is done to improve the compatibility to ant 1.5
349
Method JavaDoc getCurrentCompilerArgsMethod = null;
350         try {
351             getCurrentCompilerArgsMethod = javacClass.getMethod("getCurrentCompilerArgs", null); //$NON-NLS-1$
352
} catch (NoSuchMethodException JavaDoc e) {
353             // if not found, then we cannot use this method (ant 1.5)
354
// debug level is only available with ant 1.5.x
355
}
356         String JavaDoc[] compilerArgs = null;
357         if (getCurrentCompilerArgsMethod != null) {
358             try {
359                 compilerArgs = (String JavaDoc[]) getCurrentCompilerArgsMethod.invoke(this.attributes, null);
360             } catch (IllegalAccessException JavaDoc e) {
361                 // should never happen
362
} catch (InvocationTargetException JavaDoc e) {
363                 // should never happen
364
}
365         }
366         //check the compiler arguments for anything requiring extra processing
367
if (compilerArgs != null) checkCompilerArgs(compilerArgs);
368         return compilerArgs;
369     }
370     /**
371      * check the compiler arguments.
372      * Extract from files specified using @, lines marked with ADAPTER_PREFIX
373      * These lines specify information that needs to be interpreted by us.
374      * @param args compiler arguments to process
375      */

376     private void checkCompilerArgs(String JavaDoc[] args) {
377         for (int i = 0; i < args.length; i++) {
378             if (args[i].charAt(0) == '@') {
379                 try {
380                     char[] content = Util.getFileCharContent(new File JavaDoc(args[i].substring(1)), null);
381                     int offset = 0;
382                     int prefixLength = ADAPTER_PREFIX.length;
383                     while ((offset = CharOperation.indexOf(ADAPTER_PREFIX, content, true, offset)) > -1) {
384                         int start = offset + prefixLength;
385                         int end = CharOperation.indexOf('\n', content, start);
386                         if (end == -1)
387                             end = content.length;
388                         while (CharOperation.isWhitespace(content[end])) {
389                             end--;
390                         }
391                         
392                         // end is inclusive, but in the API end is exclusive
393
if (CharOperation.equals(ADAPTER_ENCODING, content, start, start + ADAPTER_ENCODING.length)) {
394                             CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, end + 1);
395                             // file or folder level custom encoding
396
start += ADAPTER_ENCODING.length;
397                             int encodeStart = CharOperation.lastIndexOf('[', content, start, end);
398                             if (start < encodeStart && encodeStart < end) {
399                                 boolean isFile = CharOperation.equals(SuffixConstants.SUFFIX_java, content, encodeStart - 5, encodeStart, false);
400
401                                 String JavaDoc str = String.valueOf(content, start, encodeStart - start);
402                                 String JavaDoc enc = String.valueOf(content, encodeStart, end - encodeStart + 1);
403                                 if (isFile) {
404                                     if (fileEncodings == null)
405                                         fileEncodings = new HashMap JavaDoc();
406                                     //use File to translate the string into a path with the correct File.seperator
407
fileEncodings.put(str, enc);
408                                 } else {
409                                     if (dirEncodings == null)
410                                         dirEncodings = new HashMap JavaDoc();
411                                     dirEncodings.put(str, enc);
412                                 }
413                             }
414                         } else if (CharOperation.equals(ADAPTER_ACCESS, content, start, start + ADAPTER_ACCESS.length)) {
415                             // access rules for the classpath
416
start += ADAPTER_ACCESS.length;
417                             int accessStart = CharOperation.indexOf('[', content, start, end);
418                             CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, accessStart);
419                             if (start < accessStart && accessStart < end) {
420                                 String JavaDoc path = String.valueOf(content, start, accessStart - start);
421                                 String JavaDoc access = String.valueOf(content, accessStart, end - accessStart + 1);
422                                 if (accessRules == null)
423                                     accessRules = new ArrayList JavaDoc();
424                                 accessRules.add(path);
425                                 accessRules.add(access);
426                             }
427                         }
428                         offset = end;
429                     }
430                 } catch (IOException JavaDoc e) {
431                     //ignore
432
}
433             }
434         }
435
436     }
437     
438     /**
439      * Copy the classpath to the command line with access rules included.
440      * @param cmd the given command line
441      * @param classpath the given classpath entry
442      */

443     private void createClasspathArgument(Commandline cmd, Path classpath) {
444         Argument arg = cmd.createArgument();
445         final String JavaDoc[] pathElements = classpath.list();
446
447         // empty path return empty string
448
if (pathElements.length == 0) {
449             arg.setValue(Util.EMPTY_STRING);
450             return;
451         }
452
453         // no access rules, can set the path directly
454
if (accessRules == null) {
455             arg.setPath(classpath);
456             return;
457         }
458
459         int rulesLength = accessRules.size();
460         String JavaDoc[] rules = (String JavaDoc[]) accessRules.toArray(new String JavaDoc[rulesLength]);
461         int nextRule = 0;
462         final StringBuffer JavaDoc result = new StringBuffer JavaDoc();
463
464         //access rules are expected in the same order as the classpath, but there could
465
//be elements in the classpath not in the access rules or access rules not in the classpath
466
for (int i = 0, max = pathElements.length; i < max; i++) {
467             if (i > 0)
468                 result.append(File.pathSeparatorChar);
469             String JavaDoc pathElement = pathElements[i];
470             result.append(pathElement);
471             //the rules list is [path, rule, path, rule, ...]
472
for (int j = nextRule; j < rulesLength; j += 2) {
473                 String JavaDoc rule = rules[j];
474                 if (pathElement.endsWith(rule)) {
475                     result.append(rules[j + 1]);
476                     nextRule = j + 2;
477                     break;
478                 }
479                 // if the path doesn't match, it could be due to a trailing file separatorChar in the rule
480
if (rule.endsWith(File.separator)) {
481                     // rule ends with the File.separator, but pathElement might not
482
// otherwise it would match on the first endsWith
483
int ruleLength = rule.length();
484                     if (pathElement.regionMatches(false, pathElement.length() - ruleLength + 1, rule, 0, ruleLength - 1)) {
485                         result.append(rules[j + 1]);
486                         nextRule = j + 2;
487                         break;
488                     }
489                 } else if (pathElement.endsWith(File.separator)) {
490                     // rule doesn't end with the File.separator, but pathElement might
491
int ruleLength = rule.length();
492                     if (pathElement.regionMatches(false, pathElement.length() - ruleLength - 1, rule, 0, ruleLength)) {
493                         result.append(rules[j + 1]);
494                         nextRule = j + 2;
495                         break;
496                     }
497                 }
498             }
499         }
500
501         arg.setValue(result.toString());
502     }
503     /**
504      * Modified from base class, Logs the compilation parameters, adds the files
505      * to compile and logs the &quot;niceSourceList&quot;
506      * Appends encoding information at the end of arguments
507      *
508      * @param cmd the given command line
509      */

510     protected void logAndAddFilesToCompile(Commandline cmd) {
511         attributes.log("Compilation " + cmd.describeArguments(), //$NON-NLS-1$
512
Project.MSG_VERBOSE);
513
514         StringBuffer JavaDoc niceSourceList = new StringBuffer JavaDoc("File"); //$NON-NLS-1$
515
if (compileList.length != 1) {
516             niceSourceList.append("s"); //$NON-NLS-1$
517
}
518         niceSourceList.append(" to be compiled:"); //$NON-NLS-1$
519
niceSourceList.append(lSep);
520
521         String JavaDoc[] encodedFiles = null, encodedDirs = null;
522         int encodedFilesLength = 0, encodedDirsLength = 0;
523         if (fileEncodings != null) {
524             encodedFilesLength = fileEncodings.size();
525             encodedFiles = new String JavaDoc[encodedFilesLength];
526             fileEncodings.keySet().toArray(encodedFiles);
527         }
528         if (dirEncodings != null) {
529             encodedDirsLength = dirEncodings.size();
530             encodedDirs = new String JavaDoc[encodedDirsLength];
531             dirEncodings.keySet().toArray(encodedDirs);
532             //we need the directories sorted, longest first,since sub directories can
533
//override encodings for their parent directories
534
Comparator JavaDoc comparator = new Comparator JavaDoc() {
535                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
536                     return ((String JavaDoc) o2).length() - ((String JavaDoc) o1).length();
537                 }
538             };
539             Arrays.sort(encodedDirs, comparator);
540         }
541
542         for (int i = 0; i < compileList.length; i++) {
543             String JavaDoc arg = compileList[i].getAbsolutePath();
544             boolean encoded = false;
545             if (encodedFiles != null) {
546                 //check for file level custom encoding
547
for (int j = 0; j < encodedFilesLength; j++) {
548                     if (arg.endsWith(encodedFiles[j])) {
549                         //found encoding, remove it from the list to speed things up next time around
550
arg = arg + (String JavaDoc) fileEncodings.get(encodedFiles[j]);
551                         if (j < encodedFilesLength - 1) {
552                             System.arraycopy(encodedFiles, j + 1, encodedFiles, j, encodedFilesLength - j - 1);
553                         }
554                         encodedFiles[--encodedFilesLength] = null;
555                         encoded = true;
556                         break;
557                     }
558                 }
559             }
560             if (!encoded && encodedDirs != null) {
561                 //check folder level custom encoding
562
for (int j = 0; j < encodedDirsLength; j++) {
563                     if (arg.lastIndexOf(encodedDirs[j]) != -1) {
564                         arg = arg + (String JavaDoc) dirEncodings.get(encodedDirs[j]);
565                         break;
566                     }
567                 }
568             }
569             cmd.createArgument().setValue(arg);
570             niceSourceList.append(" " + arg + lSep); //$NON-NLS-1$
571
}
572
573         attributes.log(niceSourceList.toString(), Project.MSG_VERBOSE);
574     }
575 }
576
Popular Tags