KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > queries > BinaryForSourceQuery


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.queries;
20
21 import java.net.URL JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Set JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.netbeans.spi.java.queries.BinaryForSourceQueryImplementation;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileStateInvalidException;
29 import org.openide.filesystems.URLMapper;
30 import org.openide.util.Exceptions;
31 import org.openide.util.Lookup;
32
33 /**
34  *
35  * The query is used for finding binaries for sources,
36  * this is intended to be the inverse of the SourceForBinaryQuery.
37  * @see BinaryForSourceQueryImplementation
38  * @see SourceForBinaryQuery
39  * @since org.netbeans.api.java/1 1.12
40  * @author Tomas Zezula
41  *
42  */

43 public final class BinaryForSourceQuery {
44     
45     
46     /** Creates a new instance of BInaryForSOurceQuery */
47     private BinaryForSourceQuery() {
48     }
49     
50     /**
51      * Returns the binary root for given source root.
52      * @param sourceRoot the source path root.
53      * @return a result object encapsulating the answer (never null)
54      */

55     public static Result findBinaryRoots (final URL JavaDoc sourceRoot) {
56        assert sourceRoot != null;
57        for (BinaryForSourceQueryImplementation impl : Lookup.getDefault().lookupAll(BinaryForSourceQueryImplementation.class)) {
58            BinaryForSourceQuery.Result result = impl.findBinaryRoots (sourceRoot);
59            if (result != null) {
60                return result;
61            }
62        }
63        return new DefaultResult (sourceRoot);
64     }
65     
66     /**
67      * Result of finding binaries, encapsulating the answer as well as the
68      * ability to listen to it.
69      */

70     public static interface Result {
71         
72         /**
73          * Get the binary roots.
74          * @return array of roots of compiled classes (may be empty but not null)
75          */

76         URL JavaDoc[] getRoots();
77         
78         /**
79          * Add a listener to changes in the roots.
80          * @param l a listener to add
81          */

82         void addChangeListener(ChangeListener JavaDoc l);
83         
84         /**
85          * Remove a listener to changes in the roots.
86          * @param l a listener to remove
87          */

88         void removeChangeListener(ChangeListener JavaDoc l);
89     }
90     
91     private static class DefaultResult implements Result {
92         
93         private final URL JavaDoc sourceRoot;
94         
95         DefaultResult (final URL JavaDoc sourceRoot) {
96             this.sourceRoot = sourceRoot;
97         }
98     
99         public URL JavaDoc[] getRoots() {
100             FileObject fo = URLMapper.findFileObject(sourceRoot);
101             if (fo == null) {
102                 return new URL JavaDoc[0];
103             }
104             ClassPath exec = ClassPath.getClassPath(fo, ClassPath.EXECUTE);
105             if (exec == null) {
106                 return new URL JavaDoc[0];
107             }
108             Set JavaDoc<URL JavaDoc> result = new HashSet JavaDoc<URL JavaDoc>();
109             for (ClassPath.Entry e : exec.entries()) {
110                 FileObject[] roots = SourceForBinaryQuery.findSourceRoots(e.getURL()).getRoots();
111                 for (FileObject root : roots) {
112                     try {
113                         if (sourceRoot.equals (root.getURL())) {
114                             result.add (e.getURL());
115                         }
116                     } catch (FileStateInvalidException fsie) {
117                         Exceptions.printStackTrace(fsie);
118                     }
119                 }
120             }
121             return result.toArray(new URL JavaDoc[result.size()]);
122         }
123
124         public void addChangeListener(ChangeListener JavaDoc l) {
125         }
126
127         public void removeChangeListener(ChangeListener JavaDoc l) {
128         }
129     }
130 }
131
Popular Tags