KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > SharabilityQueryImpl


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.spi.project.support.ant;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import org.netbeans.api.queries.SharabilityQuery;
28 import org.netbeans.spi.queries.SharabilityQueryImplementation;
29 import org.openide.util.WeakListeners;
30
31 /**
32  * Standard impl of {@link SharabilityQueryImplementation}.
33  * @author Jesse Glick
34  */

35 final class SharabilityQueryImpl implements SharabilityQueryImplementation, PropertyChangeListener JavaDoc {
36
37     private final AntProjectHelper h;
38     private final PropertyEvaluator eval;
39     private final String JavaDoc[] includes;
40     private final String JavaDoc[] excludes;
41     /** Absolute paths of directories or files to treat as sharable (except for the excludes). */
42     private String JavaDoc[] includePaths;
43     /** Absolute paths of directories or files to treat as not sharable. */
44     private String JavaDoc[] excludePaths;
45     
46     SharabilityQueryImpl(AntProjectHelper h, PropertyEvaluator eval, String JavaDoc[] includes, String JavaDoc[] excludes) {
47         this.h = h;
48         this.eval = eval;
49         this.includes = includes;
50         this.excludes = excludes;
51         computeFiles();
52         eval.addPropertyChangeListener(WeakListeners.propertyChange(this, eval));
53     }
54     
55     /** Compute the absolute paths which are and are not sharable. */
56     private void computeFiles() {
57         String JavaDoc[] _includePaths = computeFrom(includes);
58         String JavaDoc[] _excludePaths = computeFrom(excludes);
59         synchronized (this) {
60             includePaths = _includePaths;
61             excludePaths = _excludePaths;
62         }
63     }
64     
65     /** Compute a list of absolute paths based on some abstract names. */
66     private String JavaDoc[] computeFrom(String JavaDoc[] list) {
67         List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>(list.length);
68         for (String JavaDoc s : list) {
69             String JavaDoc val = eval.evaluate(s);
70             if (val != null) {
71                 File JavaDoc f = h.resolveFile(val);
72                 result.add(f.getAbsolutePath());
73             }
74         }
75         // XXX should remove overlaps somehow
76
return result.toArray(new String JavaDoc[result.size()]);
77     }
78     
79     public synchronized int getSharability(File JavaDoc file) {
80         String JavaDoc path = file.getAbsolutePath();
81         if (contains(path, excludePaths, false)) {
82             return SharabilityQuery.NOT_SHARABLE;
83         }
84         return contains(path, includePaths, false) ?
85             (contains(path, excludePaths, true) ? SharabilityQuery.MIXED : SharabilityQuery.SHARABLE) :
86             SharabilityQuery.UNKNOWN;
87     }
88     
89     /**
90      * Check whether a file path matches something in the supplied list.
91      * @param a file path to test
92      * @param list a list of file paths
93      * @param reverse if true, check if the file is an ancestor of some item; if false,
94      * check if some item is an ancestor of the file
95      * @return true if the file matches some item
96      */

97     private static boolean contains(String JavaDoc path, String JavaDoc[] list, boolean reverse) {
98         for (String JavaDoc s : list) {
99             if (path.equals(s)) {
100                 return true;
101             } else {
102                 if (reverse ? s.startsWith(path + File.separatorChar) : path.startsWith(s + File.separatorChar)) {
103                     return true;
104                 }
105             }
106         }
107         return false;
108     }
109     
110     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
111         computeFiles();
112     }
113     
114 }
115
Popular Tags