KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.DirectoryScanner;
36 import org.apache.tools.ant.Project;
37 import org.apache.tools.ant.types.EnumeratedAttribute;
38 import org.apache.tools.ant.types.FileSet;
39
40 /** A special fileset permitting exclusions based on CVS characteristics.
41  * @author Jesse Glick
42  */

43 public class CvsFileSet extends FileSet {
44     
45     /** Filtering mode for CVS filesets.
46      */

47     public static final class Mode extends EnumeratedAttribute {
48         public String JavaDoc[] getValues() {
49             return new String JavaDoc[] {
50                 "controlled",
51                 "uncontrolled",
52                 "text",
53                 "binary",
54             };
55         }
56     }
57     
58     private String JavaDoc mode = "controlled";
59     
60     /** Set filtering mode.
61      * @param m The mode to use:
62      * <dl>
63      * <dt><code>controlled</code> (default)
64      * <dd>Only files under CVS control are matched.
65      * <dt><code>uncontrolled</code>
66      * <dd>Only files not under CVS control are matched.
67      * <dt><code>text</code>
68      * <dd>Only files under CVS control and marked as textual are matched.
69      * <dt><code>binary</code>
70      * <dd>Only files under CVS control and marked as binary are matched.
71      * </dl>
72      */

73     public void setMode(Mode m) {
74         mode = m.getValue();
75     }
76     
77     public DirectoryScanner getDirectoryScanner(Project proj) throws BuildException {
78         DirectoryScanner scan = new CvsDirectoryScanner();
79         setupDirectoryScanner(scan, proj);
80         scan.scan();
81         return scan;
82     }
83     
84     private static final List JavaDoc<Set JavaDoc<String JavaDoc>> NO_ENTRIES = new ArrayList JavaDoc<Set JavaDoc<String JavaDoc>>();
85     static {
86         NO_ENTRIES.add(Collections.<String JavaDoc>emptySet());
87         NO_ENTRIES.add(Collections.<String JavaDoc>emptySet());
88     }
89         
90     private class CvsDirectoryScanner extends DirectoryScanner {
91         
92         // Map from dirs to parsed CVS/Entries, being set of text and binary filenames
93
private final Map JavaDoc<File JavaDoc,List JavaDoc/*2*/<Set JavaDoc<String JavaDoc>>> entries = new HashMap JavaDoc<File JavaDoc,List JavaDoc<Set JavaDoc<String JavaDoc>>>(100);
94         
95         protected boolean isIncluded(String JavaDoc name) throws BuildException {
96             if (! super.isIncluded(name)) return false;
97             File JavaDoc f = new File JavaDoc(getBasedir(), name);
98             if (! f.exists()) throw new IllegalStateException JavaDoc();
99             if (!f.isFile()) {
100                 // Need to say it is included so that <delete includeemptydirs="true"> will work as expected:
101
return true;
102             }
103             List JavaDoc<Set JavaDoc<String JavaDoc>> entries = loadEntries(f.getParentFile());
104             Set JavaDoc<String JavaDoc> text = entries.get(0);
105             Set JavaDoc<String JavaDoc> binary = entries.get(1);
106             String JavaDoc bname = f.getName();
107             if (mode.equals("controlled")) {
108                 return text.contains(bname) || binary.contains(bname);
109             } else if (mode.equals("uncontrolled")) {
110                 return ! text.contains(bname) && ! binary.contains(bname);
111             } else if (mode.equals("text")) {
112                 return text.contains(bname);
113             } else if (mode.equals("binary")) {
114                 return binary.contains(bname);
115             } else {
116                 throw new IllegalStateException JavaDoc(mode);
117             }
118         }
119         
120         private List JavaDoc<Set JavaDoc<String JavaDoc>> loadEntries(File JavaDoc dir) throws BuildException {
121             List JavaDoc<Set JavaDoc<String JavaDoc>> tb = entries.get(dir);
122             if (tb == null) {
123                 File JavaDoc efile = new File JavaDoc(new File JavaDoc(dir, "CVS"), "Entries");
124                 if (efile.exists()) {
125                     tb = new ArrayList JavaDoc<Set JavaDoc<String JavaDoc>>();
126                     tb.add(new HashSet JavaDoc<String JavaDoc>(10));
127                     tb.add(new HashSet JavaDoc<String JavaDoc>(10));
128                     try {
129                         Reader JavaDoc r = new FileReader JavaDoc(efile);
130                         try {
131                             BufferedReader JavaDoc buf = new BufferedReader JavaDoc(r);
132                             String JavaDoc line;
133                             int lineNumber = 0;
134                             while ((line = buf.readLine()) != null) {
135                                 lineNumber++;
136                                 if (line.startsWith("/")) {
137                                     line = line.substring(1);
138                                     int idx = line.indexOf('/');
139                                     String JavaDoc name = line.substring(0, idx);
140                                     idx = line.lastIndexOf('/');
141                                     line = line.substring(0, idx);
142                                     idx = line.lastIndexOf('/');
143                                     String JavaDoc subst = line.substring(idx + 1);
144                                     if (subst.equals("-kb")) {
145                                         tb.get(1).add(name);
146                                     } else {
147                                         // Usually "", but occasionally "-ko", "-kv", etc.
148
tb.get(0).add(name);
149                                     }
150                                 }
151                             }
152                         } finally {
153                             r.close();
154                         }
155                     } catch (IOException JavaDoc ioe) {
156                         throw new BuildException("While reading " + efile, ioe);
157                     }
158                 } else {
159                     tb = NO_ENTRIES;
160                 }
161                 entries.put(dir, tb);
162             }
163             return tb;
164         }
165         
166         protected boolean couldHoldIncluded(String JavaDoc name) {
167             if (! super.couldHoldIncluded(name)) return false;
168             // Do not look into CVS meta directories.
169
return ! name.endsWith(File.separatorChar + "CVS");
170         }
171         
172     }
173     
174 }
175
Popular Tags