1 19 20 package org.netbeans.modules.java.j2seplatform.platformdefinition; 21 import java.beans.PropertyChangeEvent ; 22 23 import java.beans.PropertyChangeListener ; 24 25 26 import java.net.MalformedURLException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.net.URL ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import javax.swing.event.ChangeEvent ; 33 34 import javax.swing.event.ChangeListener ; 35 import org.netbeans.api.java.classpath.ClassPath; 36 import org.netbeans.api.java.platform.JavaPlatform; 37 import org.netbeans.api.java.platform.JavaPlatformManager; 38 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 39 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation; 40 import org.openide.ErrorManager; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileStateInvalidException; 43 44 import org.openide.filesystems.URLMapper; 45 46 import org.openide.util.WeakListeners; 47 48 51 public class JavadocForBinaryQueryPlatformImpl implements JavadocForBinaryQueryImplementation { 52 53 private static final int STATE_ERROR = -1; 54 private static final int STATE_START = 0; 55 private static final int STATE_DOCS = 1; 56 private static final int STATE_LAN = 2; 57 private static final int STATE_API = 3; 58 private static final int STATE_INDEX = 4; 59 60 private static final String NAME_DOCS = "docs"; private static final String NAME_JA = "ja"; private static final String NAME_API = "api"; private static final String NAME_IDNEX ="index-files"; 65 66 public JavadocForBinaryQueryPlatformImpl() { 67 } 68 69 public JavadocForBinaryQuery.Result findJavadoc(final URL b) { 70 class R implements JavadocForBinaryQuery.Result, PropertyChangeListener { 71 72 private JavaPlatform platform; 73 private ArrayList listeners; 74 private URL [] cachedRoots; 75 76 public R (JavaPlatform plat) { 77 this.platform = plat; 78 this.platform.addPropertyChangeListener (WeakListeners.propertyChange(this,this.platform)); 79 } 80 81 public synchronized URL [] getRoots() { 82 if (this.cachedRoots == null) { 83 ArrayList l = new ArrayList (); 84 for (Iterator i2 = this.platform.getJavadocFolders().iterator(); i2.hasNext();) { 85 URL u = (URL )i2.next(); 86 l.add(getIndexFolder(u)); 87 } 88 this.cachedRoots = (URL [])l.toArray(new URL [l.size()]); 89 } 90 return this.cachedRoots; 91 } 92 93 public synchronized void addChangeListener(ChangeListener l) { 94 assert l != null : "Listener can not be null"; if (this.listeners == null) { 96 this.listeners = new ArrayList (); 97 } 98 this.listeners.add (l); 99 } 100 public synchronized void removeChangeListener(ChangeListener l) { 101 assert l != null : "Listener can not be null"; if (this.listeners == null) { 103 return; 104 } 105 this.listeners.remove (l); 106 } 107 108 public void propertyChange (PropertyChangeEvent event) { 109 if (JavaPlatform.PROP_JAVADOC_FOLDER.equals(event.getPropertyName())) { 110 synchronized (this) { 111 this.cachedRoots = null; 112 } 113 this.fireChange (); 114 } 115 } 116 117 private void fireChange () { 118 Iterator it = null; 119 synchronized (this) { 120 if (this.listeners == null) { 121 return; 122 } 123 it = ((ArrayList )this.listeners.clone()).iterator(); 124 } 125 ChangeEvent event = new ChangeEvent (this); 126 while (it.hasNext()) { 127 ((ChangeListener )it.next()).stateChanged(event); 128 } 129 } 130 131 } 132 JavaPlatformManager jpm = JavaPlatformManager.getDefault(); 133 JavaPlatform platforms[] = jpm.getInstalledPlatforms(); 134 for (int i=0; i<platforms.length; i++) { 135 JavaPlatform jp = platforms[i]; 136 Iterator it = jp.getBootstrapLibraries().entries().iterator(); 141 while (it.hasNext()) { 142 ClassPath.Entry entry = (ClassPath.Entry)it.next(); 143 if (b.equals(entry.getURL())) { 144 return new R (jp); 145 } 146 } 147 } 148 return null; 149 } 150 151 157 private static URL getIndexFolder (URL rootURL) { 158 if (rootURL == null) { 159 return null; 160 } 161 FileObject root = URLMapper.findFileObject(rootURL); 162 if (root == null) { 163 return rootURL; 164 } 165 FileObject result = findIndexFolder (root); 166 try { 167 return result == null ? rootURL : result.getURL(); 168 } catch (FileStateInvalidException e) { 169 ErrorManager.getDefault().notify (e); 170 return rootURL; 171 } 172 } 173 174 private static FileObject findIndexFolder (FileObject fo) { 175 int state = STATE_START; 176 while (state != STATE_ERROR && state != STATE_INDEX) { 177 switch (state) { 178 case STATE_START: 179 { 180 FileObject tmpFo = fo.getFileObject(NAME_DOCS); if (tmpFo != null) { 182 fo = tmpFo; 183 state = STATE_DOCS; 184 break; 185 } 186 tmpFo = fo.getFileObject(NAME_JA); if (tmpFo != null) { 188 fo = tmpFo; 189 state = STATE_LAN; 190 break; 191 192 } 193 tmpFo = fo.getFileObject(NAME_API); 194 if (tmpFo != null) { 195 fo = tmpFo; 196 state = STATE_API; 197 break; 198 } 199 fo = null; 200 state = STATE_ERROR; 201 break; 202 } 203 case STATE_DOCS: 204 { 205 FileObject tmpFo = fo.getFileObject(NAME_JA); 206 if (tmpFo != null) { 207 fo = tmpFo; 208 state = STATE_LAN; 209 break; 210 } 211 tmpFo = fo.getFileObject(NAME_API); 212 if (tmpFo != null) { 213 fo = tmpFo; 214 state = STATE_API; 215 break; 216 } 217 fo = null; 218 state = STATE_ERROR; 219 break; 220 } 221 case STATE_LAN: 222 { 223 FileObject tmpFo = fo.getFileObject(NAME_API); 224 if (tmpFo != null) { 225 fo = tmpFo; 226 state = STATE_API; 227 break; 228 } 229 fo = null; 230 state = STATE_ERROR; 231 break; 232 } 233 case STATE_API: 234 { 235 FileObject tmpFo = fo.getFileObject(NAME_IDNEX); 236 if (tmpFo !=null) { 237 state = STATE_INDEX; 238 break; 239 } 240 fo = null; 241 state = STATE_ERROR; 242 break; 243 } 244 } 245 } 246 return fo; 247 } 248 249 } 250 | Popular Tags |