1 19 package org.netbeans.api.java.queries; 20 21 import java.net.URL ; 22 import java.util.HashSet ; 23 import java.util.Set ; 24 import javax.swing.event.ChangeListener ; 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 43 public final class BinaryForSourceQuery { 44 45 46 47 private BinaryForSourceQuery() { 48 } 49 50 55 public static Result findBinaryRoots (final URL 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 70 public static interface Result { 71 72 76 URL [] getRoots(); 77 78 82 void addChangeListener(ChangeListener l); 83 84 88 void removeChangeListener(ChangeListener l); 89 } 90 91 private static class DefaultResult implements Result { 92 93 private final URL sourceRoot; 94 95 DefaultResult (final URL sourceRoot) { 96 this.sourceRoot = sourceRoot; 97 } 98 99 public URL [] getRoots() { 100 FileObject fo = URLMapper.findFileObject(sourceRoot); 101 if (fo == null) { 102 return new URL [0]; 103 } 104 ClassPath exec = ClassPath.getClassPath(fo, ClassPath.EXECUTE); 105 if (exec == null) { 106 return new URL [0]; 107 } 108 Set <URL > result = new HashSet <URL >(); 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 [result.size()]); 122 } 123 124 public void addChangeListener(ChangeListener l) { 125 } 126 127 public void removeChangeListener(ChangeListener l) { 128 } 129 } 130 } 131 | Popular Tags |