KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
23 import java.util.*;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.Task;
28 import org.apache.tools.ant.taskdefs.Copy;
29 import org.apache.tools.ant.types.FileSet;
30
31 /** Copies content of subdirectories of defined name from set of directories
32  * to a certain location.
33  *
34  * @author Jesse Glick, Rudolf Balada
35  *
36  * Copied and changed from NbMerge.java
37  */

38 public class SimpleMerge extends Task {
39     
40     private File JavaDoc dest;
41     private List<String JavaDoc> modules = new ArrayList<String JavaDoc>();
42     private List<File JavaDoc> topdirs = new ArrayList<File JavaDoc>();
43     private List<String JavaDoc> subdirs = new ArrayList<String JavaDoc>();
44     
45     /** Target directory to unpack to (top of IDE installation). */
46     public void setDest (File JavaDoc f) {
47         dest = f;
48     }
49     
50     /** Comma-separated list of modules to include. */
51     public void setModules (String JavaDoc s) {
52         StringTokenizer tok = new StringTokenizer (s, ", ");
53         modules = new ArrayList<String JavaDoc>();
54         while (tok.hasMoreTokens ())
55             modules.add(tok.nextToken ());
56     }
57     
58     /** Set the top directory.
59      * There should be subdirectories under this for each named module.
60      */

61     public void setTopdir (File JavaDoc t) {
62         topdirs.add (t);
63     }
64
65     /** Nested topdir addition. */
66     public class Topdir {
67         /** Path to an extra topdir. */
68         public void setPath (File JavaDoc t) {
69             topdirs.add (t);
70         }
71     }
72     /** Add a nested topdir.
73      * If there is more than one topdir total, build products
74      * may be taken from any of them, including from multiple places
75      * for the same module. (Later topdirs may override build
76      * products in earlier topdirs.)
77      */

78     public Topdir createTopdir () {
79         return new Topdir ();
80     }
81
82     /** Set the subdirectory.
83      * There should be these subdirectories in each named module.
84      */

85     public void setSubdir (String JavaDoc t) {
86         subdirs.add (t);
87     }
88
89     /** Nested subdir addition. */
90     public class Subdir {
91         /** Path to an extra Subdir. */
92         public void setPath (String JavaDoc t) {
93             subdirs.add (t);
94         }
95     }
96     /** Add a nested subdir.
97      * If there is more than one subdir total, build products
98      * may be taken from any of them, including from multiple places
99      * for the same module. (Later subdirs may override build
100      * products in earlier subdirs.)
101      */

102     public Subdir createSubdir () {
103         return new Subdir ();
104     }
105
106     public void execute () throws BuildException {
107         if (topdirs.isEmpty ()) {
108             throw new BuildException("You must set at least one topdir attribute", getLocation());
109         }
110         
111         if (subdirs.isEmpty ()) {
112             throw new BuildException("You must set at least one subdir attribute", getLocation());
113         }
114
115         log ( "Starting merge to " + dest.getAbsolutePath() );
116         for (File JavaDoc topdir : topdirs) {
117             for (String JavaDoc module : modules) {
118                 for (String JavaDoc sdir : subdirs) {
119                     File JavaDoc subdir = new File JavaDoc(new File JavaDoc(topdir, module), sdir);
120                     if (!subdir.exists()) {
121                         log("Dir " + subdir + " does not exist, skipping...", Project.MSG_WARN);
122                         continue;
123                     }
124                     Copy copy = (Copy) getProject().createTask("copy");
125                     FileSet fs = new FileSet();
126                     fs.setDir(subdir);
127                     copy.addFileset(fs);
128                     copy.setTodir(dest);
129                     copy.setIncludeEmptyDirs(true);
130                     copy.init();
131                     copy.setLocation(getLocation());
132                     copy.execute();
133                 }
134             }
135         }
136         log ( "Merge finished" );
137     }
138 }
139
Popular Tags