001: package tide.project;
002:
003: import java.util.*;
004: import java.io.*;
005: import java.util.zip.*;
006: import tide.classsyntax.JavaDocParser;
007: import tide.editor.completions.AttrCompletionItem;
008: import tide.classsyntax.ElementKind;
009: import java.lang.reflect.*;
010: import tide.sources.FileItem;
011:
012: /** Manages the sources and javadoc used in the completion.
013: * Currently: NO initialization time, scans on realtime when needed.
014: * TODO: allow docs directly in zip file... (like classes...)
015: * TODO: scan docs from source code...
016: */
017: public final class JavaDocManager {
018: final private List<File> javaDocRoots = new ArrayList<File>();
019: final private WeakHashMap<String, JDocRes> loadedDocs = new WeakHashMap<String, JDocRes>();
020:
021: // held in the project
022: JavaDocManager() {
023: }
024:
025: /** also clears the loaded docs, if any... (weak mapped)
026: */
027: public synchronized void setJavaDocRoots(List<File> roots) {
028: clearLoadedDocsCache();
029: this .javaDocRoots.clear();
030: this .javaDocRoots.addAll(roots);
031: }
032:
033: /** Can be called to liberate memory, however not really necessary since it is in a weak WeakHashMap
034: */
035: public synchronized void clearLoadedDocsCache() {
036: loadedDocs.clear();
037: }
038:
039: /** "Real time" search for javadoc in the roots.
040: */
041: public synchronized File getDocFile(String javaName,
042: boolean packageSummary) {
043: for (File jdr : javaDocRoots) {
044: File df = JavaDocParser.getJavaDocFile(javaName, jdr,
045: packageSummary);
046: if (df != null && df.exists())
047: return df;
048: }
049: return null;
050: }
051:
052: /** Maybe cached !
053: */
054: private synchronized JDocRes getDocFileContent(String javaName,
055: boolean packageSummary) // javax.swing.JFrame
056: {
057: // look in the cache
058: if (loadedDocs.containsKey(javaName))
059: return loadedDocs.get(javaName);
060:
061: for (File jdr : javaDocRoots) {
062: File df = JavaDocParser.getJavaDocFile(javaName, jdr,
063: packageSummary);
064: if (df != null && df.exists()) {
065: String cont = JavaDocParser.getHTMLContent(df);
066: JDocRes jr = new JDocRes(df, cont);
067: loadedDocs.put(javaName, jr);
068: return jr;
069: }
070: }
071: return null;
072: }
073:
074: /** Package summary.
075: */
076: public synchronized JDocRes getPackageSummary(String javaName) // javax.swing
077: {
078: JDocRes cont = getDocFileContent(javaName, true);
079: if (cont == null)
080: return JDocRes.notFound;
081:
082: return new JDocRes(cont.f, JavaDocParser
083: .getPackageSummaryFromCompleteContent(cont.part));
084: }
085:
086: /** real time search for javadoc in the roots
087: */
088: public synchronized JDocRes getClassSummary(String javaName) // javax.swing
089: {
090: JDocRes cont = getDocFileContent(javaName, false);
091: if (cont == null)
092: return JDocRes.notFound;
093:
094: return new JDocRes(cont.f, JavaDocParser
095: .getJavaDocClassDescription(cont.part, true)); // without head !
096: }
097:
098: /** supports fields, methods, ...
099: */
100: public synchronized JDocRes getJavaDocFor(Member member) {
101: //do not code defensive ! if(ai==null) return "<html> null item";
102: if (member instanceof Field) {
103: Field field = (Field) member;
104: JDocRes cont = this .getDocFileContent(field
105: .getDeclaringClass().getName(), false);
106: if (cont != null) {
107: return new JDocRes(cont.f, JavaDocParser
108: .getJavaDocDescription(field, cont.part));
109: }
110: } else if (member instanceof Method) {
111: Method field = (Method) member;
112: JDocRes cont = this .getDocFileContent(field
113: .getDeclaringClass().getName(), false);
114: if (cont != null) {
115: return new JDocRes(cont.f, JavaDocParser
116: .getJavaDocDescription(field, cont.part));
117: }
118: } else if (member instanceof Constructor) {
119: Constructor field = (Constructor) member;
120: JDocRes cont = this .getDocFileContent(field
121: .getDeclaringClass().getName(), false);
122: if (cont != null) {
123: return new JDocRes(cont.f, JavaDocParser
124: .getJavaDocDescription(field, cont.part));
125: }
126: }
127:
128: /*
129: if(ai.getKind()==ElementKind.Method)
130: {
131: if(ai.declaringClass!=null && ai.getMember() instanceof Method)
132: {
133: JDocRes cont = this.getDocFileContent(ai.declaringClass.getName(), false);
134: if(cont!=null)
135: {
136: return JavaDocParser.getJavaDocDescription((Method) ai.getMember(), cont);
137: }
138: }
139: }*/
140: return JDocRes.notFound;
141: }
142:
143: /** Supports fields, methods, ...
144: */
145: public synchronized JDocRes getJavaDocFor(AttrCompletionItem ai) {
146: //do not code defensive ! if(ai==null) return "<html> null item";
147: if (ai.getKind() == ElementKind.Field) {
148: if (ai.declaringClass != null
149: && ai.getMember() instanceof Field) {
150: JDocRes cont = this .getDocFileContent(ai.declaringClass
151: .getName(), false);
152: if (cont != null) {
153: return new JDocRes(cont.f, JavaDocParser
154: .getJavaDocDescription((Field) ai
155: .getMember(), cont.part));
156: }
157: }
158: }
159:
160: if (ai.getKind() == ElementKind.Method) {
161: if (ai.declaringClass != null
162: && ai.getMember() instanceof Method) {
163: JDocRes cont = this .getDocFileContent(ai.declaringClass
164: .getName(), false);
165: if (cont != null) {
166: return new JDocRes(cont.f, JavaDocParser
167: .getJavaDocDescription((Method) ai
168: .getMember(), cont.part));
169: }
170: }
171: }
172:
173: if (ai.getKind() == ElementKind.Class) {
174: String javaName = null;
175: if (ai.getMember() instanceof FileItem) {
176: FileItem fi = (FileItem) ai.getMember();
177: javaName = fi.getJavaName();
178: } else if (ai.getMember() instanceof Class) {
179: javaName = ((Class) ai.getMember()).getName();
180: }
181:
182: if (javaName != null) {
183: JDocRes cont = this .getDocFileContent(javaName, false);
184: if (cont != null) {
185: return new JDocRes(cont.f, JavaDocParser
186: .getJavaDocClassDescription(cont.part,
187: false));
188: }
189: }
190: }
191:
192: if (ai.getKind() == ElementKind.PackageNameFragment) {
193: String javaName = null;
194: if (ai.getMember() instanceof FileItem) {
195: FileItem fi = (FileItem) ai.getMember();
196: javaName = fi.getJavaName();
197: } else if (ai.getMember() instanceof Class) {
198: javaName = ((Class) ai.getMember()).getName();
199: }
200:
201: if (javaName != null) {
202: JDocRes cont = this .getDocFileContent(javaName, true);
203: if (cont != null) {
204: return new JDocRes(cont.f, JavaDocParser
205: .getJavaDocClassDescription(cont.part,
206: false));
207: }
208: }
209: }
210:
211: if (ai.getKind() == ElementKind.Constructor) {
212: if (ai.declaringClass != null
213: && ai.getMember() instanceof Constructor) {
214: JDocRes cont = this .getDocFileContent(ai.declaringClass
215: .getName(), false);
216: if (cont != null) {
217: return new JDocRes(cont.f, JavaDocParser
218: .getJavaDocDescription((Constructor) ai
219: .getMember(), cont.part));
220: }
221: }
222: }
223: return JDocRes.notFound;
224: }
225:
226: public synchronized void terminate() {
227: javaDocRoots.clear();
228: }
229:
230: }
|