KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > platformdefinition > JavadocForBinaryQueryPlatformImpl


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.modules.java.j2seplatform.platformdefinition;
21 import java.beans.PropertyChangeEvent JavaDoc;
22
23 import java.beans.PropertyChangeListener JavaDoc;
24
25
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URISyntaxException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33
34 import javax.swing.event.ChangeListener JavaDoc;
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 /**
49  * Implementation of Javadoc query for the platform.
50  */

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 JavaDoc NAME_DOCS = "docs"; //NOI18N
61
private static final String JavaDoc NAME_JA = "ja"; //NOI18N
62
private static final String JavaDoc NAME_API = "api"; //NOI18N
63
private static final String JavaDoc NAME_IDNEX ="index-files"; //NOI18N
64

65     /** Default constructor for lookup. */
66     public JavadocForBinaryQueryPlatformImpl() {
67     }
68     
69     public JavadocForBinaryQuery.Result findJavadoc(final URL JavaDoc b) {
70         class R implements JavadocForBinaryQuery.Result, PropertyChangeListener JavaDoc {
71
72             private JavaPlatform platform;
73             private ArrayList JavaDoc listeners;
74             private URL JavaDoc[] 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 JavaDoc[] getRoots() {
82                 if (this.cachedRoots == null) {
83                     ArrayList JavaDoc l = new ArrayList JavaDoc();
84                     for (Iterator JavaDoc i2 = this.platform.getJavadocFolders().iterator(); i2.hasNext();) {
85                         URL JavaDoc u = (URL JavaDoc)i2.next();
86                         l.add(getIndexFolder(u));
87                     }
88                     this.cachedRoots = (URL JavaDoc[])l.toArray(new URL JavaDoc[l.size()]);
89                 }
90                 return this.cachedRoots;
91             }
92
93             public synchronized void addChangeListener(ChangeListener JavaDoc l) {
94                 assert l != null : "Listener can not be null"; //NOI18N
95
if (this.listeners == null) {
96                     this.listeners = new ArrayList JavaDoc ();
97                 }
98                 this.listeners.add (l);
99             }
100             public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
101                 assert l != null : "Listener can not be null"; //NOI18N
102
if (this.listeners == null) {
103                     return;
104                 }
105                 this.listeners.remove (l);
106             }
107             
108             public void propertyChange (PropertyChangeEvent JavaDoc 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 JavaDoc it = null;
119                 synchronized (this) {
120                     if (this.listeners == null) {
121                         return;
122                     }
123                     it = ((ArrayList JavaDoc)this.listeners.clone()).iterator();
124                 }
125                 ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
126                 while (it.hasNext()) {
127                     ((ChangeListener JavaDoc)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 //Not valid assumption: May change in the future result should be returned, since the result is live.
137
// if (jp.getJavadocFolders().size() == 0) {
138
// continue;
139
// }
140
Iterator JavaDoc 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     /**
152      * Search for the actual root of the Javadoc containing the index-all.html or
153      * index-files. In case when it is not able to find it, it returns the given Javadoc folder/file.
154      * @param URL Javadoc folder/file
155      * @return URL either the URL of folder containg the index or the given parameter if the index was not found.
156      */

157     private static URL JavaDoc getIndexFolder (URL JavaDoc 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); //NOI18N
181
if (tmpFo != null) {
182                             fo = tmpFo;
183                             state = STATE_DOCS;
184                             break;
185                         }
186                         tmpFo = fo.getFileObject(NAME_JA); //NOI18N
187
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