1 19 20 package org.netbeans.modules.java.j2seplatform.libraries; 21 import java.beans.PropertyChangeEvent ; 22 23 import java.beans.PropertyChangeListener ; 24 25 26 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import javax.swing.event.ChangeEvent ; 34 35 import javax.swing.event.ChangeListener ; 36 37 import org.openide.ErrorManager; 38 import org.openide.util.WeakListeners; 39 import org.openide.filesystems.URLMapper; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileStateInvalidException; 42 import org.openide.filesystems.FileUtil; 43 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 44 import org.netbeans.api.project.libraries.Library; 45 import org.netbeans.api.project.libraries.LibraryManager; 46 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation; 47 import org.netbeans.modules.java.j2seplatform.platformdefinition.Util; 48 49 50 51 54 public class JavadocForBinaryQueryLibraryImpl implements JavadocForBinaryQueryImplementation { 55 56 private static int MAX_DEPTH = 3; 57 private final Map normalizedURLCache = new HashMap (); 58 59 60 public JavadocForBinaryQueryLibraryImpl() { 61 } 62 63 public JavadocForBinaryQuery.Result findJavadoc(final URL b) { 64 class R implements JavadocForBinaryQuery.Result, PropertyChangeListener { 65 66 private Library lib; 67 private ArrayList listeners; 68 private URL [] cachedRoots; 69 70 71 public R (Library lib) { 72 this.lib = lib; 73 this.lib.addPropertyChangeListener (WeakListeners.propertyChange(this,this.lib)); 74 } 75 76 public synchronized URL [] getRoots() { 77 if (this.cachedRoots == null) { 78 List l = lib.getContent(J2SELibraryTypeProvider.VOLUME_TYPE_JAVADOC); 79 List result = new ArrayList (); 80 for (Iterator it = l.iterator(); it.hasNext();) { 81 URL u = (URL ) it.next (); 82 result.add (getIndexFolder(u)); 83 } 84 this.cachedRoots = (URL [])result.toArray(new URL [result.size()]); 85 } 86 return this.cachedRoots; 87 } 88 89 public synchronized void addChangeListener(ChangeListener l) { 90 assert l != null : "Listener can not be null"; 91 if (this.listeners == null) { 92 this.listeners = new ArrayList (); 93 } 94 this.listeners.add (l); 95 } 96 97 public synchronized void removeChangeListener(ChangeListener l) { 98 assert l != null : "Listener can not be null"; 99 if (this.listeners == null) { 100 return; 101 } 102 this.listeners.remove (l); 103 } 104 105 public void propertyChange (PropertyChangeEvent event) { 106 if (Library.PROP_CONTENT.equals(event.getPropertyName())) { 107 synchronized (this) { 108 this.cachedRoots = null; 109 } 110 this.fireChange (); 111 } 112 } 113 114 private void fireChange () { 115 Iterator it = null; 116 synchronized (this) { 117 if (this.listeners == null) { 118 return; 119 } 120 it = ((ArrayList )this.listeners.clone()).iterator (); 121 } 122 ChangeEvent event = new ChangeEvent (this); 123 while (it.hasNext()) { 124 ((ChangeListener )it.next()).stateChanged(event); 125 } 126 } 127 } 128 129 boolean isNormalizedURL = isNormalizedURL(b); 130 LibraryManager lm = LibraryManager.getDefault(); 131 Library[] libs = lm.getLibraries(); 132 for (int i=0; i<libs.length; i++) { 133 String type = libs[i].getType(); 134 if (!J2SELibraryTypeProvider.LIBRARY_TYPE.equalsIgnoreCase(type)) { 135 continue; 136 } 137 List jars = libs[i].getContent(J2SELibraryTypeProvider.VOLUME_TYPE_CLASSPATH); Iterator it = jars.iterator(); 139 while (it.hasNext()) { 140 URL entry = (URL )it.next(); 141 URL normalizedEntry; 142 if (isNormalizedURL) { 143 normalizedEntry = getNormalizedURL(entry); 144 } 145 else { 146 normalizedEntry = entry; 147 } 148 if (normalizedEntry != null && normalizedEntry.equals(b)) { 149 return new R(libs[i]); 150 } 151 } 152 } 153 return null; 154 } 155 156 private URL getNormalizedURL (URL url) { 157 if (isNormalizedURL(url)) { 159 return url; 160 } 161 URL normalizedURL = (URL ) this.normalizedURLCache.get (url); 165 if (normalizedURL == null) { 166 FileObject fo = URLMapper.findFileObject(url); 167 if (fo != null) { 168 try { 169 normalizedURL = fo.getURL(); 170 this.normalizedURLCache.put (url, normalizedURL); 171 } catch (FileStateInvalidException e) { 172 ErrorManager.getDefault().notify(e); 173 } 174 } 175 } 176 return normalizedURL; 177 } 178 179 185 private static boolean isNormalizedURL (URL url) { 186 if ("jar".equals(url.getProtocol())) { url = FileUtil.getArchiveFile(url); 188 } 189 return "file".equals(url.getProtocol()); } 191 192 193 194 201 static boolean isValidLibraryJavadocRoot (final URL rootURL) { 202 assert rootURL != null && rootURL.toExternalForm().endsWith("/"); 203 final FileObject root = URLMapper.findFileObject(rootURL); 204 if (root == null) { 205 return false; 206 } 207 return findIndexFolder (root,1) != null; 208 } 209 210 216 private static URL getIndexFolder (URL rootURL) { 217 if (rootURL == null) { 218 return null; 219 } 220 FileObject root = URLMapper.findFileObject(rootURL); 221 if (root == null) { 222 return rootURL; 223 } 224 FileObject result = findIndexFolder (root,1); 225 try { 226 return result == null ? rootURL : result.getURL(); 227 } catch (FileStateInvalidException e) { 228 ErrorManager.getDefault().notify(e); 229 return rootURL; 230 } 231 } 232 233 private static FileObject findIndexFolder (FileObject fo, int depth) { 234 if (depth > MAX_DEPTH) { 235 return null; 236 } 237 if (fo.getFileObject("index-files",null)!=null || fo.getFileObject("index-all.html",null)!=null) { return fo; 239 } 240 FileObject[] children = fo.getChildren(); 241 for (int i=0; i< children.length; i++) { 242 if (children[i].isFolder()) { 243 FileObject result = findIndexFolder(children[i], depth+1); 244 if (result != null) { 245 return result; 246 } 247 } 248 } 249 return null; 250 } 251 252 } 253 | Popular Tags |