blob: f664c416df6d489b960165b29cfdf2d7900ab270 [file] [log] [blame]
The Android Open Source Project88b60792009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import com.sun.javadoc.*;
18
19import org.clearsilver.HDF;
20
21import java.util.*;
22import java.io.*;
23import java.lang.reflect.Proxy;
24import java.lang.reflect.Array;
25import java.lang.reflect.InvocationHandler;
26import java.lang.reflect.InvocationTargetException;
27import java.lang.reflect.Method;
28
29public class DroidDoc
30{
31 private static final String SDK_CONSTANT_ANNOTATION = "android.annotation.SdkConstant";
32 private static final String SDK_CONSTANT_TYPE_ACTIVITY_ACTION = "android.annotation.SdkConstant.SdkConstantType.ACTIVITY_INTENT_ACTION";
33 private static final String SDK_CONSTANT_TYPE_BROADCAST_ACTION = "android.annotation.SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION";
34 private static final String SDK_CONSTANT_TYPE_SERVICE_ACTION = "android.annotation.SdkConstant.SdkConstantType.SERVICE_INTENT_ACTION";
35 private static final String SDK_CONSTANT_TYPE_CATEGORY = "android.annotation.SdkConstant.SdkConstantType.INTENT_CATEGORY";
36 private static final String SDK_WIDGET_ANNOTATION = "android.annotation.Widget";
37 private static final String SDK_LAYOUT_ANNOTATION = "android.annotation.Layout";
38
39 private static final int TYPE_NONE = 0;
40 private static final int TYPE_WIDGET = 1;
41 private static final int TYPE_LAYOUT = 2;
42 private static final int TYPE_LAYOUT_PARAM = 3;
43
44 public static final int SHOW_PUBLIC = 0x00000001;
45 public static final int SHOW_PROTECTED = 0x00000003;
46 public static final int SHOW_PACKAGE = 0x00000007;
47 public static final int SHOW_PRIVATE = 0x0000000f;
48 public static final int SHOW_HIDDEN = 0x0000001f;
49
50 public static int showLevel = SHOW_PROTECTED;
51
52 public static final String javadocDir = "reference/";
53 public static String htmlExtension;
54
55 public static RootDoc root;
56 public static ArrayList<String[]> mHDFData = new ArrayList<String[]>();
57 public static Map<Character,String> escapeChars = new HashMap<Character,String>();
58 public static String title = "";
59
60 public static boolean checkLevel(int level)
61 {
62 return (showLevel & level) == level;
63 }
64
65 public static boolean checkLevel(boolean pub, boolean prot, boolean pkgp,
66 boolean priv, boolean hidden)
67 {
68 int level = 0;
69 if (hidden && !checkLevel(SHOW_HIDDEN)) {
70 return false;
71 }
72 if (pub && checkLevel(SHOW_PUBLIC)) {
73 return true;
74 }
75 if (prot && checkLevel(SHOW_PROTECTED)) {
76 return true;
77 }
78 if (pkgp && checkLevel(SHOW_PACKAGE)) {
79 return true;
80 }
81 if (priv && checkLevel(SHOW_PRIVATE)) {
82 return true;
83 }
84 return false;
85 }
86
87 public static boolean start(RootDoc r)
88 {
89 String keepListFile = null;
90 String proofreadFile = null;
91 String todoFile = null;
92 String sdkValuePath = null;
93 ArrayList<SampleCode> sampleCodes = new ArrayList<SampleCode>();
94 String stubsDir = null;
95 //Create the dependency graph for the stubs directory
96 boolean apiXML = false;
97 String apiFile = null;
98 String debugStubsFile = "";
99 HashSet<String> stubPackages = null;
100
101 root = r;
102
103 String[][] options = r.options();
104 for (String[] a: options) {
105 if (a[0].equals("-d")) {
106 ClearPage.outputDir = a[1];
107 }
108 else if (a[0].equals("-templatedir")) {
109 ClearPage.addTemplateDir(a[1]);
110 }
111 else if (a[0].equals("-hdf")) {
112 mHDFData.add(new String[] {a[1], a[2]});
113 }
114 else if (a[0].equals("-toroot")) {
115 ClearPage.toroot = a[1];
116 }
117 else if (a[0].equals("-samplecode")) {
118 sampleCodes.add(new SampleCode(a[1], a[2], a[3]));
119 }
120 else if (a[0].equals("-htmldir")) {
121 ClearPage.htmlDir = a[1];
122 }
123 else if (a[0].equals("-title")) {
124 DroidDoc.title = a[1];
125 }
126 else if (a[0].equals("-werror")) {
127 Errors.setWarningsAreErrors(true);
128 }
129 else if (a[0].equals("-error") || a[0].equals("-warning")
130 || a[0].equals("-hide")) {
131 try {
132 int level = -1;
133 if (a[0].equals("-error")) {
134 level = Errors.ERROR;
135 }
136 else if (a[0].equals("-warning")) {
137 level = Errors.WARNING;
138 }
139 else if (a[0].equals("-hide")) {
140 level = Errors.HIDDEN;
141 }
142 Errors.setErrorLevel(Integer.parseInt(a[1]), level);
143 }
144 catch (NumberFormatException e) {
145 // already printed below
146 return false;
147 }
148 }
149 else if (a[0].equals("-keeplist")) {
150 keepListFile = a[1];
151 }
152 else if (a[0].equals("-proofread")) {
153 proofreadFile = a[1];
154 }
155 else if (a[0].equals("-todo")) {
156 todoFile = a[1];
157 }
158 else if (a[0].equals("-public")) {
159 showLevel = SHOW_PUBLIC;
160 }
161 else if (a[0].equals("-protected")) {
162 showLevel = SHOW_PROTECTED;
163 }
164 else if (a[0].equals("-package")) {
165 showLevel = SHOW_PACKAGE;
166 }
167 else if (a[0].equals("-private")) {
168 showLevel = SHOW_PRIVATE;
169 }
170 else if (a[0].equals("-hidden")) {
171 showLevel = SHOW_HIDDEN;
172 }
173 else if (a[0].equals("-stubs")) {
174 stubsDir = a[1];
175 }
176 else if (a[0].equals("-stubpackages")) {
177 stubPackages = new HashSet();
178 for (String pkg: a[1].split(":")) {
179 stubPackages.add(pkg);
180 }
181 }
182 else if (a[0].equals("-sdkvalues")) {
183 sdkValuePath = a[1];
184 }
185 else if (a[0].equals("-apixml")) {
186 apiXML = true;
187 apiFile = a[1];
188 }
189 }
190
191 // read some prefs from the template
192 if (!readTemplateSettings()) {
193 return false;
194 }
195
196 // Set up the data structures
197 Converter.makeInfo(r);
198
199 // Files for proofreading
200 if (proofreadFile != null) {
201 Proofread.initProofread(proofreadFile);
202 }
203 if (todoFile != null) {
204 TodoFile.writeTodoFile(todoFile);
205 }
206
207 // HTML Pages
208 if (ClearPage.htmlDir != null) {
209 writeHTMLPages();
210 }
211
212 // Navigation tree
213 NavTree.writeNavTree(javadocDir);
214
215 // Packages Pages
216 writePackages(javadocDir
217 + (ClearPage.htmlDir!=null
218 ? "packages" + htmlExtension
219 : "index" + htmlExtension));
220
221 // Classes
222 writeClassLists();
223 writeClasses();
224 writeHierarchy();
225 // writeKeywords();
226
227 // Lists for JavaScript
228 writeLists();
229 if (keepListFile != null) {
230 writeKeepList(keepListFile);
231 }
232
233 // Sample Code
234 for (SampleCode sc: sampleCodes) {
235 sc.write();
236 }
237
238 // Index page
239 writeIndex();
240
241 Proofread.finishProofread(proofreadFile);
242
243 // Stubs
244 if (stubsDir != null) {
245 Stubs.writeStubs(stubsDir, apiXML, apiFile, stubPackages);
246 }
247
248 if (sdkValuePath != null) {
249 writeSdkValues(sdkValuePath);
250 }
251
252 Errors.printErrors();
253 return !Errors.hadError;
254 }
255
256 private static void writeIndex() {
257 HDF data = makeHDF();
258 ClearPage.write(data, "index.cs", javadocDir + "index" + htmlExtension);
259 }
260
261 private static boolean readTemplateSettings()
262 {
263 HDF data = makeHDF();
264 htmlExtension = data.getValue("template.extension", ".html");
265 int i=0;
266 while (true) {
267 String k = data.getValue("template.escape." + i + ".key", "");
268 String v = data.getValue("template.escape." + i + ".value", "");
269 if ("".equals(k)) {
270 break;
271 }
272 if (k.length() != 1) {
273 System.err.println("template.escape." + i + ".key must have a length of 1: " + k);
274 return false;
275 }
276 escapeChars.put(k.charAt(0), v);
277 i++;
278 }
279 return true;
280 }
281
282 public static String escape(String s) {
283 if (escapeChars.size() == 0) {
284 return s;
285 }
286 StringBuffer b = null;
287 int begin = 0;
288 final int N = s.length();
289 for (int i=0; i<N; i++) {
290 char c = s.charAt(i);
291 String mapped = escapeChars.get(c);
292 if (mapped != null) {
293 if (b == null) {
294 b = new StringBuffer(s.length() + mapped.length());
295 }
296 if (begin != i) {
297 b.append(s.substring(begin, i));
298 }
299 b.append(mapped);
300 begin = i+1;
301 }
302 }
303 if (b != null) {
304 if (begin != N) {
305 b.append(s.substring(begin, N));
306 }
307 return b.toString();
308 }
309 return s;
310 }
311
312 public static void setPageTitle(HDF data, String title)
313 {
314 String s = title;
315 if (DroidDoc.title.length() > 0) {
316 s += " - " + DroidDoc.title;
317 }
318 data.setValue("page.title", s);
319 }
320
321 public static LanguageVersion languageVersion()
322 {
323 return LanguageVersion.JAVA_1_5;
324 }
325
326 public static int optionLength(String option)
327 {
328 if (option.equals("-d")) {
329 return 2;
330 }
331 if (option.equals("-templatedir")) {
332 return 2;
333 }
334 if (option.equals("-hdf")) {
335 return 3;
336 }
337 if (option.equals("-toroot")) {
338 return 2;
339 }
340 if (option.equals("-samplecode")) {
341 return 4;
342 }
343 if (option.equals("-htmldir")) {
344 return 2;
345 }
346 if (option.equals("-title")) {
347 return 2;
348 }
349 if (option.equals("-werror")) {
350 return 1;
351 }
352 if (option.equals("-hide")) {
353 return 2;
354 }
355 if (option.equals("-warning")) {
356 return 2;
357 }
358 if (option.equals("-error")) {
359 return 2;
360 }
361 if (option.equals("-keeplist")) {
362 return 2;
363 }
364 if (option.equals("-proofread")) {
365 return 2;
366 }
367 if (option.equals("-todo")) {
368 return 2;
369 }
370 if (option.equals("-public")) {
371 return 1;
372 }
373 if (option.equals("-protected")) {
374 return 1;
375 }
376 if (option.equals("-package")) {
377 return 1;
378 }
379 if (option.equals("-private")) {
380 return 1;
381 }
382 if (option.equals("-hidden")) {
383 return 1;
384 }
385 if (option.equals("-stubs")) {
386 return 2;
387 }
388 if (option.equals("-stubpackages")) {
389 return 2;
390 }
391 if (option.equals("-sdkvalues")) {
392 return 2;
393 }
394 if (option.equals("-apixml")) {
395 return 2;
396 }
397 return 0;
398 }
399
400 public static boolean validOptions(String[][] options, DocErrorReporter r)
401 {
402 for (String[] a: options) {
403 if (a[0].equals("-error") || a[0].equals("-warning")
404 || a[0].equals("-hide")) {
405 try {
406 Integer.parseInt(a[1]);
407 }
408 catch (NumberFormatException e) {
409 r.printError("bad -" + a[0] + " value must be a number: "
410 + a[1]);
411 return false;
412 }
413 }
414 }
415
416 return true;
417 }
418
419 public static HDF makeHDF()
420 {
421 HDF data = new HDF();
422
423 for (String[] p: mHDFData) {
424 data.setValue(p[0], p[1]);
425 }
426
427 try {
428 for (String p: ClearPage.hdfFiles) {
429 data.readFile(p);
430 }
431 }
432 catch (IOException e) {
433 throw new RuntimeException(e);
434 }
435
436 return data;
437 }
438
439 public static HDF makePackageHDF()
440 {
441 HDF data = makeHDF();
442 ClassInfo[] classes = Converter.rootClasses();
443
444 SortedMap<String, PackageInfo> sorted = new TreeMap<String, PackageInfo>();
445 for (ClassInfo cl: classes) {
446 PackageInfo pkg = cl.containingPackage();
447 String name;
448 if (pkg == null) {
449 name = "";
450 } else {
451 name = pkg.name();
452 }
453 sorted.put(name, pkg);
454 }
455
456 int i = 0;
457 for (String s: sorted.keySet()) {
458 PackageInfo pkg = sorted.get(s);
459
460 if (pkg.isHidden()) {
461 continue;
462 }
463 Boolean allHidden = true;
464 int pass = 0;
465 ClassInfo[] classesToCheck = null;
466 while (pass < 5 ) {
467 switch(pass) {
468 case 0:
469 classesToCheck = pkg.ordinaryClasses();
470 break;
471 case 1:
472 classesToCheck = pkg.enums();
473 break;
474 case 2:
475 classesToCheck = pkg.errors();
476 break;
477 case 3:
478 classesToCheck = pkg.exceptions();
479 break;
480 case 4:
481 classesToCheck = pkg.interfaces();
482 break;
483 default:
484 System.err.println("Error reading package: " + pkg.name());
485 break;
486 }
487 for (ClassInfo cl : classesToCheck) {
488 if (!cl.isHidden()) {
489 allHidden = false;
490 break;
491 }
492 }
493 if (!allHidden) {
494 break;
495 }
496 pass++;
497 }
498 if (allHidden) {
499 continue;
500 }
501
502 data.setValue("reference", "true");
503 data.setValue("docs.packages." + i + ".name", s);
504 data.setValue("docs.packages." + i + ".link", pkg.htmlPage());
505 TagInfo.makeHDF(data, "docs.packages." + i + ".shortDescr",
506 pkg.firstSentenceTags());
507 i++;
508 }
509
510 return data;
511 }
512
513 public static void writeDirectory(File dir, String relative)
514 {
515 File[] files = dir.listFiles();
516 int i, count = files.length;
517 for (i=0; i<count; i++) {
518 File f = files[i];
519 if (f.isFile()) {
520 String templ = relative + f.getName();
521 int len = templ.length();
522 if (len > 3 && ".cs".equals(templ.substring(len-3))) {
523 HDF data = makeHDF();
524 String filename = templ.substring(0,len-3) + htmlExtension;
525 ClearPage.write(data, templ, filename);
526 }
527 else if (len > 3 && ".jd".equals(templ.substring(len-3))) {
528 String filename = templ.substring(0,len-3) + htmlExtension;
529 DocFile.writePage(f.getAbsolutePath(), relative, filename);
530 }
531 else {
532 ClearPage.copyFile(f, templ);
533 }
534 }
535 else if (f.isDirectory()) {
536 writeDirectory(f, relative + f.getName() + "/");
537 }
538 }
539 }
540
541 public static void writeHTMLPages()
542 {
543 File f = new File(ClearPage.htmlDir);
544 if (!f.isDirectory()) {
545 System.err.println("htmlDir not a directory: " + ClearPage.htmlDir);
546 }
547 writeDirectory(f, "");
548 }
549
550 public static void writeLists()
551 {
552 HDF data = makeHDF();
553
554 ClassInfo[] classes = Converter.rootClasses();
555
556 SortedMap<String, Object> sorted = new TreeMap<String, Object>();
557 for (ClassInfo cl: classes) {
558 if (cl.isHidden()) {
559 continue;
560 }
561 sorted.put(cl.qualifiedName(), cl);
562 PackageInfo pkg = cl.containingPackage();
563 String name;
564 if (pkg == null) {
565 name = "";
566 } else {
567 name = pkg.name();
568 }
569 sorted.put(name, pkg);
570 }
571
572 int i = 0;
573 for (String s: sorted.keySet()) {
574 data.setValue("docs.pages." + i + ".id" , ""+i);
575 data.setValue("docs.pages." + i + ".label" , s);
576
577 Object o = sorted.get(s);
578 if (o instanceof PackageInfo) {
579 PackageInfo pkg = (PackageInfo)o;
580 data.setValue("docs.pages." + i + ".link" , pkg.htmlPage());
581 data.setValue("docs.pages." + i + ".type" , "package");
582 }
583 else if (o instanceof ClassInfo) {
584 ClassInfo cl = (ClassInfo)o;
585 data.setValue("docs.pages." + i + ".link" , cl.htmlPage());
586 data.setValue("docs.pages." + i + ".type" , "class");
587 }
588 i++;
589 }
590
591 ClearPage.write(data, "lists.cs", javadocDir + "lists.js");
592 }
593
594 public static void cantStripThis(ClassInfo cl, HashSet<ClassInfo> notStrippable) {
595 if (!notStrippable.add(cl)) {
596 // slight optimization: if it already contains cl, it already contains
597 // all of cl's parents
598 return;
599 }
600 ClassInfo supr = cl.superclass();
601 if (supr != null) {
602 cantStripThis(supr, notStrippable);
603 }
604 for (ClassInfo iface: cl.interfaces()) {
605 cantStripThis(iface, notStrippable);
606 }
607 }
608
609 private static String getPrintableName(ClassInfo cl) {
610 ClassInfo containingClass = cl.containingClass();
611 if (containingClass != null) {
612 // This is an inner class.
613 String baseName = cl.name();
614 baseName = baseName.substring(baseName.lastIndexOf('.') + 1);
615 return getPrintableName(containingClass) + '$' + baseName;
616 }
617 return cl.qualifiedName();
618 }
619
620 /**
621 * Writes the list of classes that must be present in order to
622 * provide the non-hidden APIs known to javadoc.
623 *
624 * @param filename the path to the file to write the list to
625 */
626 public static void writeKeepList(String filename) {
627 HashSet<ClassInfo> notStrippable = new HashSet<ClassInfo>();
628 ClassInfo[] all = Converter.allClasses();
629 Arrays.sort(all); // just to make the file a little more readable
630
631 // If a class is public and not hidden, then it and everything it derives
632 // from cannot be stripped. Otherwise we can strip it.
633 for (ClassInfo cl: all) {
634 if (cl.isPublic() && !cl.isHidden()) {
635 cantStripThis(cl, notStrippable);
636 }
637 }
638 PrintStream stream = null;
639 try {
640 stream = new PrintStream(filename);
641 for (ClassInfo cl: notStrippable) {
642 stream.println(getPrintableName(cl));
643 }
644 }
645 catch (FileNotFoundException e) {
646 System.err.println("error writing file: " + filename);
647 }
648 finally {
649 if (stream != null) {
650 stream.close();
651 }
652 }
653 }
654
655 private static PackageInfo[] sVisiblePackages = null;
656 public static PackageInfo[] choosePackages() {
657 if (sVisiblePackages != null) {
658 return sVisiblePackages;
659 }
660
661 ClassInfo[] classes = Converter.rootClasses();
662 SortedMap<String, PackageInfo> sorted = new TreeMap<String, PackageInfo>();
663 for (ClassInfo cl: classes) {
664 PackageInfo pkg = cl.containingPackage();
665 String name;
666 if (pkg == null) {
667 name = "";
668 } else {
669 name = pkg.name();
670 }
671 sorted.put(name, pkg);
672 }
673
674 ArrayList<PackageInfo> result = new ArrayList();
675
676 for (String s: sorted.keySet()) {
677 PackageInfo pkg = sorted.get(s);
678
679 if (pkg.isHidden()) {
680 continue;
681 }
682 Boolean allHidden = true;
683 int pass = 0;
684 ClassInfo[] classesToCheck = null;
685 while (pass < 5 ) {
686 switch(pass) {
687 case 0:
688 classesToCheck = pkg.ordinaryClasses();
689 break;
690 case 1:
691 classesToCheck = pkg.enums();
692 break;
693 case 2:
694 classesToCheck = pkg.errors();
695 break;
696 case 3:
697 classesToCheck = pkg.exceptions();
698 break;
699 case 4:
700 classesToCheck = pkg.interfaces();
701 break;
702 default:
703 System.err.println("Error reading package: " + pkg.name());
704 break;
705 }
706 for (ClassInfo cl : classesToCheck) {
707 if (!cl.isHidden()) {
708 allHidden = false;
709 break;
710 }
711 }
712 if (!allHidden) {
713 break;
714 }
715 pass++;
716 }
717 if (allHidden) {
718 continue;
719 }
720
721 result.add(pkg);
722 }
723
724 sVisiblePackages = result.toArray(new PackageInfo[result.size()]);
725 return sVisiblePackages;
726 }
727
728 public static void writePackages(String filename)
729 {
730 HDF data = makePackageHDF();
731
732 int i = 0;
733 for (PackageInfo pkg: choosePackages()) {
734 writePackage(pkg);
735
736 data.setValue("docs.packages." + i + ".name", pkg.name());
737 data.setValue("docs.packages." + i + ".link", pkg.htmlPage());
738 TagInfo.makeHDF(data, "docs.packages." + i + ".shortDescr",
739 pkg.firstSentenceTags());
740
741 i++;
742 }
743
744 setPageTitle(data, "Package Index");
745
746 TagInfo.makeHDF(data, "root.descr",
747 Converter.convertTags(root.inlineTags(), null));
748
749 ClearPage.write(data, "packages.cs", filename);
750 ClearPage.write(data, "package-list.cs", javadocDir + "package-list");
751
752 Proofread.writePackages(filename,
753 Converter.convertTags(root.inlineTags(), null));
754 }
755
756 public static void writePackage(PackageInfo pkg)
757 {
758 // these this and the description are in the same directory,
759 // so it's okay
760 HDF data = makePackageHDF();
761
762 String name = pkg.name();
763
764 data.setValue("package.name", name);
765 data.setValue("package.descr", "...description...");
766
767 makeClassListHDF(data, "package.interfaces",
768 ClassInfo.sortByName(pkg.interfaces()));
769 makeClassListHDF(data, "package.classes",
770 ClassInfo.sortByName(pkg.ordinaryClasses()));
771 makeClassListHDF(data, "package.enums",
772 ClassInfo.sortByName(pkg.enums()));
773 makeClassListHDF(data, "package.exceptions",
774 ClassInfo.sortByName(pkg.exceptions()));
775 makeClassListHDF(data, "package.errors",
776 ClassInfo.sortByName(pkg.errors()));
777 TagInfo.makeHDF(data, "package.shortDescr",
778 pkg.firstSentenceTags());
779 TagInfo.makeHDF(data, "package.descr", pkg.inlineTags());
780
781 String filename = pkg.htmlPage();
782 setPageTitle(data, name);
783 ClearPage.write(data, "package.cs", filename);
784
785 filename = pkg.fullDescriptionHtmlPage();
786 setPageTitle(data, name + " Details");
787 ClearPage.write(data, "package-descr.cs", filename);
788
789 Proofread.writePackage(filename, pkg.inlineTags());
790 }
791
792 public static void writeClassLists()
793 {
794 int i;
795 HDF data = makePackageHDF();
796
797 ClassInfo[] classes = PackageInfo.filterHidden(
798 Converter.convertClasses(root.classes()));
799 if (classes.length == 0) {
800 return ;
801 }
802
803 Sorter[] sorted = new Sorter[classes.length];
804 for (i=0; i<sorted.length; i++) {
805 ClassInfo cl = classes[i];
806 String name = cl.name();
807 sorted[i] = new Sorter(name, cl);
808 }
809
810 Arrays.sort(sorted);
811
812 // make a pass and resolve ones that have the same name
813 int firstMatch = 0;
814 String lastName = sorted[0].label;
815 for (i=1; i<sorted.length; i++) {
816 String s = sorted[i].label;
817 if (!lastName.equals(s)) {
818 if (firstMatch != i-1) {
819 // there were duplicates
820 for (int j=firstMatch; j<i; j++) {
821 PackageInfo pkg = ((ClassInfo)sorted[j].data).containingPackage();
822 if (pkg != null) {
823 sorted[j].label = sorted[j].label + " (" + pkg.name() + ")";
824 }
825 }
826 }
827 firstMatch = i;
828 lastName = s;
829 }
830 }
831
832 // and sort again
833 Arrays.sort(sorted);
834
835 for (i=0; i<sorted.length; i++) {
836 String s = sorted[i].label;
837 ClassInfo cl = (ClassInfo)sorted[i].data;
838 char first = Character.toUpperCase(s.charAt(0));
839 cl.makeShortDescrHDF(data, "docs.classes." + first + '.' + i);
840 }
841
842 setPageTitle(data, "Class Index");
843 ClearPage.write(data, "classes.cs", javadocDir + "classes" + htmlExtension);
844 }
845
846 // we use the word keywords because "index" means something else in html land
847 // the user only ever sees the word index
848/* public static void writeKeywords()
849 {
850 ArrayList<KeywordEntry> keywords = new ArrayList<KeywordEntry>();
851
852 ClassInfo[] classes = PackageInfo.filterHidden(Converter.convertClasses(root.classes()));
853
854 for (ClassInfo cl: classes) {
855 cl.makeKeywordEntries(keywords);
856 }
857
858 HDF data = makeHDF();
859
860 Collections.sort(keywords);
861
862 int i=0;
863 for (KeywordEntry entry: keywords) {
864 String base = "keywords." + entry.firstChar() + "." + i;
865 entry.makeHDF(data, base);
866 i++;
867 }
868
869 setPageTitle(data, "Index");
870 ClearPage.write(data, "keywords.cs", javadocDir + "keywords" + htmlExtension);
871 } */
872
873 public static void writeHierarchy()
874 {
875 ClassInfo[] classes = Converter.rootClasses();
876 ArrayList<ClassInfo> info = new ArrayList<ClassInfo>();
877 for (ClassInfo cl: classes) {
878 if (!cl.isHidden()) {
879 info.add(cl);
880 }
881 }
882 HDF data = makePackageHDF();
883 Hierarchy.makeHierarchy(data, info.toArray(new ClassInfo[info.size()]));
884 setPageTitle(data, "Class Hierarchy");
885 ClearPage.write(data, "hierarchy.cs", javadocDir + "hierarchy" + htmlExtension);
886 }
887
888 public static void writeClasses()
889 {
890 ClassInfo[] classes = Converter.rootClasses();
891
892 for (ClassInfo cl: classes) {
893 HDF data = makePackageHDF();
894 if (!cl.isHidden()) {
895 writeClass(cl, data);
896 }
897 }
898 }
899
900 public static void writeClass(ClassInfo cl, HDF data)
901 {
902 cl.makeHDF(data);
903
904 setPageTitle(data, cl.name());
905 ClearPage.write(data, "class.cs", cl.htmlPage());
906
907 Proofread.writeClass(cl.htmlPage(), cl);
908 }
909
910 public static void makeClassListHDF(HDF data, String base,
911 ClassInfo[] classes)
912 {
913 for (int i=0; i<classes.length; i++) {
914 ClassInfo cl = classes[i];
915 if (!cl.isHidden()) {
916 cl.makeShortDescrHDF(data, base + "." + i);
917 }
918 }
919 }
920
921 public static String linkTarget(String source, String target)
922 {
923 String[] src = source.split("/");
924 String[] tgt = target.split("/");
925
926 int srclen = src.length;
927 int tgtlen = tgt.length;
928
929 int same = 0;
930 while (same < (srclen-1)
931 && same < (tgtlen-1)
932 && (src[same].equals(tgt[same]))) {
933 same++;
934 }
935
936 String s = "";
937
938 int up = srclen-same-1;
939 for (int i=0; i<up; i++) {
940 s += "../";
941 }
942
943
944 int N = tgtlen-1;
945 for (int i=same; i<N; i++) {
946 s += tgt[i] + '/';
947 }
948 s += tgt[tgtlen-1];
949
950 return s;
951 }
952
953 /**
954 * Returns true if the given element has an @hide annotation.
955 */
956 private static boolean hasHideAnnotation(Doc doc) {
957 return doc.getRawCommentText().indexOf("@hide") != -1;
958 }
959
960 /**
961 * Returns true if the given element is hidden.
962 */
963 private static boolean isHidden(Doc doc) {
964 // Methods, fields, constructors.
965 if (doc instanceof MemberDoc) {
966 return hasHideAnnotation(doc);
967 }
968
969 // Classes, interfaces, enums, annotation types.
970 if (doc instanceof ClassDoc) {
971 ClassDoc classDoc = (ClassDoc) doc;
972
973 // Check the containing package.
974 if (hasHideAnnotation(classDoc.containingPackage())) {
975 return true;
976 }
977
978 // Check the class doc and containing class docs if this is a
979 // nested class.
980 ClassDoc current = classDoc;
981 do {
982 if (hasHideAnnotation(current)) {
983 return true;
984 }
985
986 current = current.containingClass();
987 } while (current != null);
988 }
989
990 return false;
991 }
992
993 /**
994 * Filters out hidden elements.
995 */
996 private static Object filterHidden(Object o, Class<?> expected) {
997 if (o == null) {
998 return null;
999 }
1000
1001 Class type = o.getClass();
1002 if (type.getName().startsWith("com.sun.")) {
1003 // TODO: Implement interfaces from superclasses, too.
1004 return Proxy.newProxyInstance(type.getClassLoader(),
1005 type.getInterfaces(), new HideHandler(o));
1006 } else if (o instanceof Object[]) {
1007 Class<?> componentType = expected.getComponentType();
1008 Object[] array = (Object[]) o;
1009 List<Object> list = new ArrayList<Object>(array.length);
1010 for (Object entry : array) {
1011 if ((entry instanceof Doc) && isHidden((Doc) entry)) {
1012 continue;
1013 }
1014 list.add(filterHidden(entry, componentType));
1015 }
1016 return list.toArray(
1017 (Object[]) Array.newInstance(componentType, list.size()));
1018 } else {
1019 return o;
1020 }
1021 }
1022
1023 /**
1024 * Filters hidden elements out of method return values.
1025 */
1026 private static class HideHandler implements InvocationHandler {
1027
1028 private final Object target;
1029
1030 public HideHandler(Object target) {
1031 this.target = target;
1032 }
1033
1034 public Object invoke(Object proxy, Method method, Object[] args)
1035 throws Throwable {
1036 String methodName = method.getName();
1037 if (args != null) {
1038 if (methodName.equals("compareTo") ||
1039 methodName.equals("equals") ||
1040 methodName.equals("overrides") ||
1041 methodName.equals("subclassOf")) {
1042 args[0] = unwrap(args[0]);
1043 }
1044 }
1045
1046 if (methodName.equals("getRawCommentText")) {
1047 return filterComment((String) method.invoke(target, args));
1048 }
1049
1050 // escape "&" in disjunctive types.
1051 if (proxy instanceof Type && methodName.equals("toString")) {
1052 return ((String) method.invoke(target, args))
1053 .replace("&", "&amp;");
1054 }
1055
1056 try {
1057 return filterHidden(method.invoke(target, args),
1058 method.getReturnType());
1059 } catch (InvocationTargetException e) {
1060 throw e.getTargetException();
1061 }
1062 }
1063
1064 private String filterComment(String s) {
1065 if (s == null) {
1066 return null;
1067 }
1068
1069 s = s.trim();
1070
1071 // Work around off by one error
1072 while (s.length() >= 5
1073 && s.charAt(s.length() - 5) == '{') {
1074 s += "&nbsp;";
1075 }
1076
1077 return s;
1078 }
1079
1080 private static Object unwrap(Object proxy) {
1081 if (proxy instanceof Proxy)
1082 return ((HideHandler)Proxy.getInvocationHandler(proxy)).target;
1083 return proxy;
1084 }
1085 }
1086
1087 public static String scope(Scoped scoped) {
1088 if (scoped.isPublic()) {
1089 return "public";
1090 }
1091 else if (scoped.isProtected()) {
1092 return "protected";
1093 }
1094 else if (scoped.isPackagePrivate()) {
1095 return "";
1096 }
1097 else if (scoped.isPrivate()) {
1098 return "private";
1099 }
1100 else {
1101 throw new RuntimeException("invalid scope for object " + scoped);
1102 }
1103 }
1104
1105 /**
1106 * Collect the values used by the Dev tools and write them in files packaged with the SDK
1107 * @param output the ouput directory for the files.
1108 */
1109 private static void writeSdkValues(String output) {
1110 ArrayList<String> activityActions = new ArrayList<String>();
1111 ArrayList<String> broadcastActions = new ArrayList<String>();
1112 ArrayList<String> serviceActions = new ArrayList<String>();
1113 ArrayList<String> categories = new ArrayList<String>();
1114
1115 ArrayList<ClassInfo> layouts = new ArrayList<ClassInfo>();
1116 ArrayList<ClassInfo> widgets = new ArrayList<ClassInfo>();
1117 ArrayList<ClassInfo> layoutParams = new ArrayList<ClassInfo>();
1118
1119 ClassInfo[] classes = Converter.allClasses();
1120
1121 // Go through all the fields of all the classes, looking SDK stuff.
1122 for (ClassInfo clazz : classes) {
1123
1124 // first check constant fields for the SdkConstant annotation.
1125 FieldInfo[] fields = clazz.allSelfFields();
1126 for (FieldInfo field : fields) {
1127 Object cValue = field.constantValue();
1128 if (cValue != null) {
1129 AnnotationInstanceInfo[] annotations = field.annotations();
1130 if (annotations.length > 0) {
1131 for (AnnotationInstanceInfo annotation : annotations) {
1132 if (SDK_CONSTANT_ANNOTATION.equals(annotation.type().qualifiedName())) {
1133 AnnotationValueInfo[] values = annotation.elementValues();
1134 if (values.length > 0) {
1135 String type = values[0].valueString();
1136 if (SDK_CONSTANT_TYPE_ACTIVITY_ACTION.equals(type)) {
1137 activityActions.add(cValue.toString());
1138 } else if (SDK_CONSTANT_TYPE_BROADCAST_ACTION.equals(type)) {
1139 broadcastActions.add(cValue.toString());
1140 } else if (SDK_CONSTANT_TYPE_SERVICE_ACTION.equals(type)) {
1141 serviceActions.add(cValue.toString());
1142 } else if (SDK_CONSTANT_TYPE_CATEGORY.equals(type)) {
1143 categories.add(cValue.toString());
1144 }
1145 }
1146 break;
1147 }
1148 }
1149 }
1150 }
1151 }
1152
1153 // Now check the class for @Widget or if its in the android.widget package
1154 // (unless the class is hidden or abstract, or non public)
1155 if (clazz.isHidden() == false && clazz.isPublic() && clazz.isAbstract() == false) {
1156 boolean annotated = false;
1157 AnnotationInstanceInfo[] annotations = clazz.annotations();
1158 if (annotations.length > 0) {
1159 for (AnnotationInstanceInfo annotation : annotations) {
1160 if (SDK_WIDGET_ANNOTATION.equals(annotation.type().qualifiedName())) {
1161 widgets.add(clazz);
1162 annotated = true;
1163 break;
1164 } else if (SDK_LAYOUT_ANNOTATION.equals(annotation.type().qualifiedName())) {
1165 layouts.add(clazz);
1166 annotated = true;
1167 break;
1168 }
1169 }
1170 }
1171
1172 if (annotated == false) {
1173 // lets check if this is inside android.widget
1174 PackageInfo pckg = clazz.containingPackage();
1175 String packageName = pckg.name();
1176 if ("android.widget".equals(packageName) ||
1177 "android.view".equals(packageName)) {
1178 // now we check what this class inherits either from android.view.ViewGroup
1179 // or android.view.View, or android.view.ViewGroup.LayoutParams
1180 int type = checkInheritance(clazz);
1181 switch (type) {
1182 case TYPE_WIDGET:
1183 widgets.add(clazz);
1184 break;
1185 case TYPE_LAYOUT:
1186 layouts.add(clazz);
1187 break;
1188 case TYPE_LAYOUT_PARAM:
1189 layoutParams.add(clazz);
1190 break;
1191 }
1192 }
1193 }
1194 }
1195 }
1196
1197 // now write the files, whether or not the list are empty.
1198 // the SDK built requires those files to be present.
1199
1200 Collections.sort(activityActions);
1201 writeValues(output + "/activity_actions.txt", activityActions);
1202
1203 Collections.sort(broadcastActions);
1204 writeValues(output + "/broadcast_actions.txt", broadcastActions);
1205
1206 Collections.sort(serviceActions);
1207 writeValues(output + "/service_actions.txt", serviceActions);
1208
1209 Collections.sort(categories);
1210 writeValues(output + "/categories.txt", categories);
1211
1212 // before writing the list of classes, we do some checks, to make sure the layout params
1213 // are enclosed by a layout class (and not one that has been declared as a widget)
1214 for (int i = 0 ; i < layoutParams.size();) {
1215 ClassInfo layoutParamClass = layoutParams.get(i);
1216 ClassInfo containingClass = layoutParamClass.containingClass();
1217 if (containingClass == null || layouts.indexOf(containingClass) == -1) {
1218 layoutParams.remove(i);
1219 } else {
1220 i++;
1221 }
1222 }
1223
1224 writeClasses(output + "/widgets.txt", widgets, layouts, layoutParams);
1225 }
1226
1227 /**
1228 * Writes a list of values into a text files.
1229 * @param pathname the absolute os path of the output file.
1230 * @param values the list of values to write.
1231 */
1232 private static void writeValues(String pathname, ArrayList<String> values) {
1233 FileWriter fw = null;
1234 BufferedWriter bw = null;
1235 try {
1236 fw = new FileWriter(pathname, false);
1237 bw = new BufferedWriter(fw);
1238
1239 for (String value : values) {
1240 bw.append(value).append('\n');
1241 }
1242 } catch (IOException e) {
1243 // pass for now
1244 } finally {
1245 try {
1246 if (bw != null) bw.close();
1247 } catch (IOException e) {
1248 // pass for now
1249 }
1250 try {
1251 if (fw != null) fw.close();
1252 } catch (IOException e) {
1253 // pass for now
1254 }
1255 }
1256 }
1257
1258 /**
1259 * Writes the widget/layout/layout param classes into a text files.
1260 * @param pathname the absolute os path of the output file.
1261 * @param widgets the list of widget classes to write.
1262 * @param layouts the list of layout classes to write.
1263 * @param layoutParams the list of layout param classes to write.
1264 */
1265 private static void writeClasses(String pathname, ArrayList<ClassInfo> widgets,
1266 ArrayList<ClassInfo> layouts, ArrayList<ClassInfo> layoutParams) {
1267 FileWriter fw = null;
1268 BufferedWriter bw = null;
1269 try {
1270 fw = new FileWriter(pathname, false);
1271 bw = new BufferedWriter(fw);
1272
1273 // write the 3 types of classes.
1274 for (ClassInfo clazz : widgets) {
1275 writeClass(bw, clazz, 'W');
1276 }
1277 for (ClassInfo clazz : layoutParams) {
1278 writeClass(bw, clazz, 'P');
1279 }
1280 for (ClassInfo clazz : layouts) {
1281 writeClass(bw, clazz, 'L');
1282 }
1283 } catch (IOException e) {
1284 // pass for now
1285 } finally {
1286 try {
1287 if (bw != null) bw.close();
1288 } catch (IOException e) {
1289 // pass for now
1290 }
1291 try {
1292 if (fw != null) fw.close();
1293 } catch (IOException e) {
1294 // pass for now
1295 }
1296 }
1297 }
1298
1299 /**
1300 * Writes a class name and its super class names into a {@link BufferedWriter}.
1301 * @param writer the BufferedWriter to write into
1302 * @param clazz the class to write
1303 * @param prefix the prefix to put at the beginning of the line.
1304 * @throws IOException
1305 */
1306 private static void writeClass(BufferedWriter writer, ClassInfo clazz, char prefix)
1307 throws IOException {
1308 writer.append(prefix).append(clazz.qualifiedName());
1309 ClassInfo superClass = clazz;
1310 while ((superClass = superClass.superclass()) != null) {
1311 writer.append(' ').append(superClass.qualifiedName());
1312 }
1313 writer.append('\n');
1314 }
1315
1316 /**
1317 * Checks the inheritance of {@link ClassInfo} objects. This method return
1318 * <ul>
1319 * <li>{@link #TYPE_LAYOUT}: if the class extends <code>android.view.ViewGroup</code></li>
1320 * <li>{@link #TYPE_WIDGET}: if the class extends <code>android.view.View</code></li>
1321 * <li>{@link #TYPE_LAYOUT_PARAM}: if the class extends <code>android.view.ViewGroup$LayoutParams</code></li>
1322 * <li>{@link #TYPE_NONE}: in all other cases</li>
1323 * </ul>
1324 * @param clazz the {@link ClassInfo} to check.
1325 */
1326 private static int checkInheritance(ClassInfo clazz) {
1327 if ("android.view.ViewGroup".equals(clazz.qualifiedName())) {
1328 return TYPE_LAYOUT;
1329 } else if ("android.view.View".equals(clazz.qualifiedName())) {
1330 return TYPE_WIDGET;
1331 } else if ("android.view.ViewGroup.LayoutParams".equals(clazz.qualifiedName())) {
1332 return TYPE_LAYOUT_PARAM;
1333 }
1334
1335 ClassInfo parent = clazz.superclass();
1336 if (parent != null) {
1337 return checkInheritance(parent);
1338 }
1339
1340 return TYPE_NONE;
1341 }
1342}