You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HtmlDecorator.java 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Mik Kersten port to AspectJ 1.1+ code base
  13. * ******************************************************************/
  14. package org.aspectj.tools.ajdoc;
  15. import java.io.BufferedReader;
  16. import java.io.DataInputStream;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.FileReader;
  20. import java.io.IOException;
  21. import java.io.StringReader;
  22. import java.util.ArrayList;
  23. import java.util.Hashtable;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.StringTokenizer;
  27. import org.aspectj.asm.AsmManager;
  28. import org.aspectj.asm.HierarchyWalker;
  29. import org.aspectj.asm.IProgramElement;
  30. import org.aspectj.asm.IRelationship;
  31. import org.aspectj.util.TypeSafeEnum;
  32. /**
  33. * @author Mik Kersten
  34. */
  35. class HtmlDecorator {
  36. private static final String POINTCUT_DETAIL = "Pointcut Detail";
  37. private static final String ADVICE_DETAIL = "Advice Detail";
  38. private static final String DECLARE_DETAIL = "Declare Detail";
  39. private static final String ADVICE_SUMMARY = "Advice Summary";
  40. private static final String POINTCUT_SUMMARY = "Pointcut Summary";
  41. private static final String DECLARE_SUMMARY = "Declare Summary";
  42. private static final String ITD_METHOD_SUMMARY = "Inter-Type Method Summary";
  43. private static final String ITD_FIELD_SUMMARY = "Inter-Type Field Summary";
  44. private static final String ITD_CONSTRUCTOR_SUMMARY = "Inter-Type Constructor Summary";
  45. static List visibleFileList = new ArrayList();
  46. static Hashtable declIDTable = null;
  47. static File rootDir = null;
  48. static String docVisibilityModifier;
  49. static void decorateHTMLFromInputFiles(AsmManager model, Hashtable table, File newRootDir, File[] inputFiles, String docModifier)
  50. throws IOException {
  51. rootDir = newRootDir;
  52. declIDTable = table;
  53. docVisibilityModifier = docModifier;
  54. for (int i = 0; i < inputFiles.length; i++) {
  55. decorateHTMLFromIPEs(getProgramElements(model, inputFiles[i].getCanonicalPath()), rootDir.getCanonicalPath()
  56. + Config.DIR_SEP_CHAR, docModifier, false);
  57. }
  58. }
  59. static void decorateHTMLFromIPEs(IProgramElement[] decls, String base, String docModifier, boolean exceededNestingLevel)
  60. throws IOException {
  61. if (decls != null) {
  62. for (int i = 0; i < decls.length; i++) {
  63. IProgramElement decl = decls[i];
  64. decorateHTMLFromIPE(decl, base, docModifier, exceededNestingLevel);
  65. }
  66. }
  67. }
  68. /**
  69. * Before attempting to decorate the HTML file we have to verify that it exists, which depends on the documentation visibility
  70. * specified to c.
  71. *
  72. * Depending on docModifier, can document - public: only public - protected: protected and public (default) - package: package
  73. * protected and public - private: everything
  74. */
  75. static void decorateHTMLFromIPE(IProgramElement decl, String base, String docModifier, boolean exceededNestingLevel)
  76. throws IOException {
  77. boolean nestedClass = false;
  78. if (decl.getKind().isType()) {
  79. boolean decorateFile = true;
  80. if (isAboveVisibility(decl)) {
  81. visibleFileList.add(decl.toSignatureString());
  82. String packageName = decl.getPackageName();
  83. String filename = "";
  84. if (packageName != null) {
  85. int index1 = base.lastIndexOf(Config.DIR_SEP_CHAR);
  86. int index2 = base.lastIndexOf(".");
  87. String currFileClass = "";
  88. if (index1 > -1 && index2 > 0 && index1 < index2) {
  89. currFileClass = base.substring(index1 + 1, index2);
  90. }
  91. // XXX only one level of nexting
  92. if (currFileClass.equals(decl.getDeclaringType())) {
  93. nestedClass = true;
  94. packageName = packageName.replace('.', '/');
  95. String newBase = "";
  96. if (base.lastIndexOf(Config.DIR_SEP_CHAR) > 0) {
  97. newBase = base.substring(0, base.lastIndexOf(Config.DIR_SEP_CHAR));
  98. }
  99. String signature = constructNestedTypeName(decl);
  100. filename = newBase + Config.DIR_SEP_CHAR + packageName + Config.DIR_SEP_CHAR + currFileClass + // "." +
  101. signature + ".html";
  102. } else {
  103. packageName = packageName.replace('.', '/');
  104. filename = base + packageName + Config.DIR_SEP_CHAR + decl.toSignatureString() + ".html";
  105. }
  106. } else {
  107. filename = base + decl.toSignatureString() + ".html";
  108. }
  109. if (!exceededNestingLevel) {
  110. decorateHTMLFile(new File(filename));
  111. } else {
  112. System.out.println("Warning: can not generate documentation for nested " + "inner class: "
  113. + decl.toSignatureString());
  114. }
  115. }
  116. }
  117. }
  118. private static String constructNestedTypeName(IProgramElement node) {
  119. if (node.getParent().getKind().isSourceFile()) {
  120. return node.getName();
  121. } else {
  122. String nodeName = "";
  123. if (node.getKind().isType())
  124. nodeName += '.' + node.getName();
  125. return constructNestedTypeName(node.getParent()) + nodeName;
  126. }
  127. }
  128. /**
  129. * Skips files that are public in the model but not public in the source, e.g. nested aspects.
  130. */
  131. static void decorateHTMLFile(File file) throws IOException {
  132. if (!file.exists())
  133. return;
  134. System.out.println("> Decorating " + file.getCanonicalPath() + "...");
  135. BufferedReader reader = new BufferedReader(new FileReader(file));
  136. StringBuffer fileContents = new StringBuffer();
  137. String line = reader.readLine();
  138. while (line != null) {
  139. fileContents.append(line + "\n");
  140. line = reader.readLine();
  141. }
  142. boolean isSecond = false;
  143. int index = 0;
  144. IProgramElement decl;
  145. while (true) {
  146. // ---this next part is an inlined procedure that returns two values---
  147. // ---the next declaration and the index at which that declaration's---
  148. // ---DeclID sits in the .html file ---
  149. String contents = fileContents.toString();
  150. int start = contents.indexOf(Config.DECL_ID_STRING, index);
  151. int end = contents.indexOf(Config.DECL_ID_TERMINATOR, index);
  152. if (start == -1)
  153. decl = null;
  154. else if (end == -1)
  155. throw new Error("Malformed DeclID.");
  156. else {
  157. String tid = contents.substring(start + Config.DECL_ID_STRING.length(), end);
  158. decl = (IProgramElement) declIDTable.get(tid);
  159. index = start;
  160. }
  161. // --- ---
  162. // --- ---
  163. if (decl == null)
  164. break;
  165. fileContents.delete(start, end + Config.DECL_ID_TERMINATOR.length());
  166. if (decl.getKind().isType()) {
  167. isSecond = true;
  168. String fullname = "";
  169. if (decl.getParent().getKind().equals(IProgramElement.Kind.ASPECT)
  170. || decl.getParent().getKind().equals(IProgramElement.Kind.CLASS)) {
  171. fullname += decl.getParent().toSignatureString().concat(".").concat(decl.toSignatureString());
  172. } else {
  173. fullname += decl.toSignatureString();
  174. }
  175. // only add aspect documentation if we're in the correct
  176. // file for the given IProgramElement
  177. if (file.getName().indexOf(fullname + ".html") != -1) {
  178. addAspectDocumentation(decl, fileContents, index);
  179. }
  180. } else {
  181. decorateMemberDocumentation(decl, fileContents, index);
  182. }
  183. // Change "Class" to "Aspect"
  184. // moved this here because then can use the IProgramElement.Kind
  185. // rather than checking to see if there's advice - this fixes
  186. // the case with an inner aspect not having the title "Aspect"
  187. if (decl.getKind().equals(IProgramElement.Kind.ASPECT) && file.getName().indexOf(decl.toSignatureString()) != -1) {
  188. // only want to change "Class" to "Aspect" if we're in the
  189. // file corresponding to the IProgramElement
  190. String fullname = "";
  191. if (decl.getParent().getKind().equals(IProgramElement.Kind.ASPECT)
  192. || decl.getParent().getKind().equals(IProgramElement.Kind.CLASS)) {
  193. fullname += decl.getParent().toSignatureString().concat(".").concat(decl.toSignatureString());
  194. } else {
  195. fullname += decl.toSignatureString();
  196. }
  197. if (file.getName().indexOf(fullname + ".html") == -1) {
  198. // we're still in the file for a parent IPE
  199. continue;
  200. }
  201. boolean br = true;
  202. int classStartIndex = fileContents.toString().indexOf("<BR>\nClass ");
  203. if (classStartIndex == -1) {
  204. classStartIndex = fileContents.toString().indexOf("<H2>\nClass ");
  205. br = false;
  206. }
  207. if (classStartIndex == -1) {
  208. // Java8 looks more like this:
  209. // <h2 title="Class A" class="title">Class A</h2>
  210. classStartIndex = fileContents.toString().indexOf("<h2 title=\"Class ");
  211. int classEndIndex = fileContents.toString().indexOf("</h2>", classStartIndex);
  212. if (classEndIndex != -1) {
  213. // Convert it to "<h2 title="Aspect A" class="title">Aspect A</h2>"
  214. String classLine = fileContents.toString().substring(classStartIndex, classEndIndex);
  215. String aspectLine = classLine.replaceAll("Class ","Aspect ");
  216. fileContents.delete(classStartIndex, classEndIndex);
  217. fileContents.insert(classStartIndex, aspectLine);
  218. }
  219. }
  220. else if (classStartIndex != -1) {
  221. int classEndIndex = fileContents.toString().indexOf("</H2>", classStartIndex);
  222. if (classStartIndex != -1 && classEndIndex != -1) {
  223. String classLine = fileContents.toString().substring(classStartIndex, classEndIndex);
  224. String aspectLine = "";
  225. if (br) {
  226. aspectLine += "<BR>\n" + "Aspect " + classLine.substring(11, classLine.length());
  227. } else {
  228. aspectLine += "<H2>\n" + "Aspect " + classLine.substring(11, classLine.length());
  229. }
  230. fileContents.delete(classStartIndex, classEndIndex);
  231. fileContents.insert(classStartIndex, aspectLine);
  232. }
  233. }
  234. int secondClassStartIndex = fileContents.toString().indexOf("class <B>");
  235. if (secondClassStartIndex != -1) {
  236. String name = decl.toSignatureString();
  237. int classEndIndex = fileContents.toString().indexOf(name + "</B><DT>");
  238. if (secondClassStartIndex != -1 && classEndIndex != -1) {
  239. StringBuffer sb = new StringBuffer(fileContents.toString().substring(secondClassStartIndex, classEndIndex));
  240. sb.replace(0, 5, "aspect");
  241. fileContents.delete(secondClassStartIndex, classEndIndex);
  242. fileContents.insert(secondClassStartIndex, sb.toString());
  243. }
  244. }
  245. else {
  246. // Java8:
  247. // <pre>static class <span class="typeNameLabel">ClassA.InnerAspect</span>
  248. classStartIndex = fileContents.toString().indexOf("class <span class=\"typeNameLabel\">");
  249. int classEndIndex = fileContents.toString().indexOf("</span>", classStartIndex);
  250. if (classEndIndex != -1) {
  251. // Convert it to "aspect <span class="typeNameLabel">ClassA.InnerAspect</span>"
  252. String classLine = fileContents.toString().substring(classStartIndex, classEndIndex);
  253. String aspectLine = "aspect"+fileContents.substring(classStartIndex+5,classEndIndex);
  254. fileContents.delete(classStartIndex, classEndIndex);
  255. fileContents.insert(classStartIndex, aspectLine);
  256. }
  257. }
  258. }
  259. }
  260. file.delete();
  261. FileOutputStream fos = new FileOutputStream(file);
  262. fos.write(fileContents.toString().getBytes());
  263. reader.close();
  264. fos.close();
  265. }
  266. static void addAspectDocumentation(IProgramElement node, StringBuffer fileBuffer, int index) {
  267. List pointcuts = new ArrayList();
  268. List advice = new ArrayList();
  269. List declares = new ArrayList();
  270. List methodsDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.INTER_TYPE_METHOD);
  271. if (methodsDeclaredOn != null && !methodsDeclaredOn.isEmpty()) {
  272. insertDeclarationsSummary(fileBuffer, methodsDeclaredOn, ITD_METHOD_SUMMARY, index);
  273. }
  274. List fieldsDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.INTER_TYPE_FIELD);
  275. if (fieldsDeclaredOn != null && !fieldsDeclaredOn.isEmpty()) {
  276. insertDeclarationsSummary(fileBuffer, fieldsDeclaredOn, ITD_FIELD_SUMMARY, index);
  277. }
  278. List constDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR);
  279. if (fieldsDeclaredOn != null && !constDeclaredOn.isEmpty()) {
  280. insertDeclarationsSummary(fileBuffer, constDeclaredOn, ITD_CONSTRUCTOR_SUMMARY, index);
  281. }
  282. for (Iterator it = node.getChildren().iterator(); it.hasNext();) {
  283. IProgramElement member = (IProgramElement) it.next();
  284. if (member.getKind().equals(IProgramElement.Kind.POINTCUT)) {
  285. pointcuts.add(member);
  286. } else if (member.getKind().equals(IProgramElement.Kind.ADVICE)) {
  287. advice.add(member);
  288. } else if (member.getKind().isDeclare() || member.getKind().isInterTypeMember()) {
  289. declares.add(member);
  290. }
  291. }
  292. if (declares.size() > 0) {
  293. insertDeclarationsDetails(fileBuffer, declares, DECLARE_DETAIL, index);
  294. insertDeclarationsSummary(fileBuffer, declares, DECLARE_SUMMARY, index);
  295. }
  296. if (pointcuts.size() > 0) {
  297. insertDeclarationsSummary(fileBuffer, pointcuts, POINTCUT_SUMMARY, index);
  298. insertDeclarationsDetails(fileBuffer, pointcuts, POINTCUT_DETAIL, index);
  299. }
  300. if (advice.size() > 0) {
  301. insertDeclarationsSummary(fileBuffer, advice, ADVICE_SUMMARY, index);
  302. insertDeclarationsDetails(fileBuffer, advice, ADVICE_DETAIL, index);
  303. }
  304. // add the 'aspect declarations' information against the type
  305. List parentsDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.DECLARE_PARENTS);
  306. if (parentsDeclaredOn != null && parentsDeclaredOn.size() > 0) {
  307. decorateDocWithRel(node, fileBuffer, index, parentsDeclaredOn, HtmlRelationshipKind.ASPECT_DECLARATIONS);
  308. }
  309. // add the 'annotated by' information against the type
  310. List annotatedBy = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE_INTER_TYPE, "annotated by");
  311. if (annotatedBy != null && annotatedBy.size() > 0) {
  312. decorateDocWithRel(node, fileBuffer, index, annotatedBy, HtmlRelationshipKind.ANNOTATED_BY);
  313. }
  314. // add the 'advised by' information against the type
  315. List advisedBy = StructureUtil.getTargets(node, IRelationship.Kind.ADVICE);
  316. if (advisedBy != null && advisedBy.size() > 0) {
  317. decorateDocWithRel(node, fileBuffer, index, advisedBy, HtmlRelationshipKind.ADVISED_BY);
  318. }
  319. }
  320. static void insertDeclarationsSummary(StringBuffer fileBuffer, List decls, String kind, int index) {
  321. if (!declsAboveVisibilityExist(decls))
  322. return;
  323. int insertIndex = findSummaryIndex(fileBuffer, index);
  324. // insert the head of the table
  325. String tableHead = "<!-- ======== " + kind.toUpperCase() + " ======= -->\n\n"
  326. + "<TABLE BORDER=\"1\" WIDTH=\"100%\" CELLPADDING=\"1\""
  327. + "CELLSPACING=\"0\"><TR><TD COLSPAN=2 BGCOLOR=\"#CCCCFF\">" + "<FONT SIZE=\"+2\"><B>" + kind
  328. + "</B></FONT></TD></TR>\n";
  329. fileBuffer.insert(insertIndex, tableHead);
  330. insertIndex += tableHead.length();
  331. // insert the body of the table
  332. for (int i = 0; i < decls.size(); i++) {
  333. IProgramElement decl = (IProgramElement) decls.get(i);
  334. if (isAboveVisibility(decl)) {
  335. // insert the table row accordingly
  336. String comment = generateSummaryComment(decl);
  337. String entry = "";
  338. if (kind.equals(ADVICE_SUMMARY)) {
  339. entry += "<TR><TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>" + generateSignatures(decl)
  340. + "</TT></A><BR>&nbsp;";
  341. if (!comment.equals("")) {
  342. entry += comment + "<P>";
  343. }
  344. entry += generateAffects(decl) + "</TD>" + "</TR><TD>\n";
  345. } else if (kind.equals(POINTCUT_SUMMARY)) {
  346. entry += "<TR><TD WIDTH=\"1%\">" + "<FONT SIZE=-1><TT>" + genAccessibility(decl) + "</TT></FONT>" + "</TD>\n"
  347. + "<TD>" + "<TT><A HREF=\"#" + generateHREFName(decl) + "\">" + decl.toLabelString()
  348. + "</A></TT><BR>&nbsp;";
  349. if (!comment.equals("")) {
  350. entry += comment + "<P>";
  351. }
  352. entry += "</TR></TD>\n";
  353. } else if (kind.equals(DECLARE_SUMMARY)) {
  354. entry += "<TR><TD WIDTH=\"1%\">" + "<FONT SIZE=-1><TT>" + generateModifierInformation(decl, false)
  355. + "</TT></FONT>" + "</TD>" + "<TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>"
  356. + decl.toLabelString() + "</TT></A><P>" + generateAffects(decl);
  357. } else if (kind.equals(ITD_FIELD_SUMMARY) || kind.equals(ITD_METHOD_SUMMARY)) {
  358. entry += "<TR><TD WIDTH=\"1%\">" + "<FONT SIZE=-1><TT>" + generateModifierInformation(decl, false)
  359. + "</TT></FONT>" + "</TD>" + "<TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>"
  360. + decl.toLabelString() + "</TT></A><P>" + generateDeclaredBy(decl);
  361. } else if (kind.equals(ITD_CONSTRUCTOR_SUMMARY)) {
  362. entry += "<TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>" + decl.toLabelString()
  363. + "</TT></A><P>" + generateDeclaredBy(decl);
  364. }
  365. // insert the entry
  366. fileBuffer.insert(insertIndex, entry);
  367. insertIndex += entry.length();
  368. }
  369. }
  370. // insert the end of the table
  371. String tableTail = "</TABLE><P>&nbsp;\n";
  372. fileBuffer.insert(insertIndex, tableTail);
  373. insertIndex += tableTail.length();
  374. }
  375. private static boolean declsAboveVisibilityExist(List decls) {
  376. boolean exist = false;
  377. for (Iterator it = decls.iterator(); it.hasNext();) {
  378. IProgramElement element = (IProgramElement) it.next();
  379. if (isAboveVisibility(element))
  380. exist = true;
  381. }
  382. return exist;
  383. }
  384. private static boolean isAboveVisibility(IProgramElement element) {
  385. IProgramElement.Accessibility acc = element.getAccessibility();
  386. if (docVisibilityModifier.equals("private")) {
  387. // show all classes and members
  388. return true;
  389. } else if (docVisibilityModifier.equals("package")) {
  390. // show package, protected and public classes and members
  391. return acc.equals(IProgramElement.Accessibility.PACKAGE) || acc.equals(IProgramElement.Accessibility.PROTECTED)
  392. || acc.equals(IProgramElement.Accessibility.PUBLIC);
  393. } else if (docVisibilityModifier.equals("protected")) {
  394. // show protected and public classes and members
  395. return acc.equals(IProgramElement.Accessibility.PROTECTED) || acc.equals(IProgramElement.Accessibility.PUBLIC);
  396. } else if (docVisibilityModifier.equals("public")) {
  397. // show public classes and members
  398. return acc.equals(IProgramElement.Accessibility.PUBLIC);
  399. }
  400. return false;
  401. }
  402. private static String genAccessibility(IProgramElement decl) {
  403. if (decl.getAccessibility().equals(IProgramElement.Accessibility.PACKAGE)) {
  404. return "(package private)";
  405. } else {
  406. return decl.getAccessibility().toString();
  407. }
  408. }
  409. static void insertDeclarationsDetails(StringBuffer fileBuffer, List decls, String kind, int index) {
  410. if (!declsAboveVisibilityExist(decls))
  411. return;
  412. int insertIndex = findDetailsIndex(fileBuffer, index);
  413. // insert the table heading
  414. String detailsHeading = "<P>&nbsp;\n" + "<!-- ======== " + kind.toUpperCase() + " SUMMARY ======= -->\n\n"
  415. + "<TABLE BORDER=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\" WIDTH=\"100%\">\n"
  416. + "<TR BGCOLOR=\"#CCCCFF\" CLASS=\"TableHeadingColor\">\n" + "<TD COLSPAN=1><FONT SIZE=\"+2\">\n" + "<B>" + kind
  417. + "</B></FONT></TD>\n" + "</TR>\n" + "</TABLE>";
  418. fileBuffer.insert(insertIndex, detailsHeading);
  419. insertIndex += detailsHeading.length();
  420. // insert the details
  421. for (int i = 0; i < decls.size(); i++) {
  422. IProgramElement decl = (IProgramElement) decls.get(i);
  423. if (isAboveVisibility(decl)) {
  424. String entry = "";
  425. // insert the table row accordingly
  426. entry += "<A NAME=\"" + generateHREFName(decl) + "\"><!-- --></A>\n";
  427. if (kind.equals(ADVICE_DETAIL)) {
  428. entry += "<H3>" + decl.getName() + "</H3><P>";
  429. entry += "<TT>" + generateSignatures(decl) + "</TT>\n" + "<P>" + generateDetailsComment(decl) + "<P>"
  430. + generateAffects(decl);
  431. } else if (kind.equals(POINTCUT_DETAIL)) {
  432. entry += "<H3>" + decl.toLabelString() + "</H3><P>" + generateDetailsComment(decl);
  433. } else if (kind.equals(DECLARE_DETAIL)) {
  434. entry += "<H3>" + decl.toLabelString() + "</H3><P>" + generateModifierInformation(decl, true);
  435. if (!decl.getKind().equals(IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR)) {
  436. entry += "&nbsp;&nbsp;";
  437. }
  438. // if we're not a declare statement then we need to generate the signature.
  439. // If we did this for declare statements we get two repeated lines
  440. if (!decl.getKind().isDeclare()) {
  441. String sigs = generateSignatures(decl);
  442. entry += sigs + "<P>";
  443. }
  444. entry += generateAffects(decl) + generateDetailsComment(decl);
  445. }
  446. // insert the entry
  447. if (i != decls.size() - 1) {
  448. entry += "<P><HR>\n";
  449. } else {
  450. entry += "<P>";
  451. }
  452. fileBuffer.insert(insertIndex, entry);
  453. insertIndex += entry.length();
  454. }
  455. }
  456. }
  457. /**
  458. * TODO: don't place the summary first.
  459. */
  460. static int findSummaryIndex(StringBuffer fileBuffer, int index) {
  461. String fbs = fileBuffer.toString();
  462. String MARKER_1 = "<!-- =========== FIELD SUMMARY =========== -->";
  463. String MARKER_2 = "<!-- ======== CONSTRUCTOR SUMMARY ======== -->";
  464. int index1 = fbs.indexOf(MARKER_1, index);
  465. int index2 = fbs.indexOf(MARKER_2, index);
  466. if (index1 < index2 && index1 != -1) {
  467. return index1;
  468. } else if (index2 != -1) {
  469. return index2;
  470. } else {
  471. return index;
  472. }
  473. }
  474. static int findDetailsIndex(StringBuffer fileBuffer, int index) {
  475. String fbs = fileBuffer.toString();
  476. String MARKER_1 = "<!-- ========= CONSTRUCTOR DETAIL ======== -->";
  477. String MARKER_2 = "<!-- ============ FIELD DETAIL =========== -->";
  478. String MARKER_3 = "<!-- ============ METHOD DETAIL ========== -->";
  479. int index1 = fbs.indexOf(MARKER_1, index);
  480. int index2 = fbs.indexOf(MARKER_2, index);
  481. int index3 = fbs.indexOf(MARKER_3, index);
  482. if (index1 != -1 && index1 < index2 && index1 < index3) {
  483. return index1;
  484. } else if (index2 != -1 && index2 < index1 && index2 < index3) {
  485. return index2;
  486. } else if (index3 != -1) {
  487. return index3;
  488. } else {
  489. return index;
  490. }
  491. }
  492. static void decorateDocWithRel(IProgramElement node, StringBuffer fileContentsBuffer, int index, List targets,
  493. HtmlRelationshipKind relKind) {
  494. if (targets != null && !targets.isEmpty()) {
  495. String adviceDoc = "<TABLE WIDTH=\"100%\" BGCOLOR=#FFFFFF><TR>"
  496. + "<TD width=\"15%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + relKind.toString() + "</font></b></td><td>";
  497. String relativePackagePath = getRelativePathFromHere(node.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR);
  498. List addedNames = new ArrayList();
  499. for (Iterator it = targets.iterator(); it.hasNext();) {
  500. Object o = it.next();
  501. IProgramElement currDecl = null;
  502. if (o instanceof String) {
  503. String currHandle = (String) o;
  504. currDecl = node.getModel().getHierarchy().findElementForHandle(currHandle);
  505. } else if (o instanceof IProgramElement) {
  506. currDecl = (IProgramElement) o;
  507. } else {
  508. return;
  509. }
  510. String packagePath = "";
  511. if (currDecl.getPackageName() != null && !currDecl.getPackageName().equals("")) {
  512. packagePath = currDecl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR;
  513. }
  514. String hrefName = "";
  515. String hrefLink = "";
  516. // Start the hRefLink with the relative path based on where
  517. // *this* type (i.e. the advised) is in the package structure.
  518. hrefLink = relativePackagePath + packagePath;
  519. if (currDecl.getPackageName() != null) {
  520. hrefName = currDecl.getPackageName().replace('.', '/');
  521. }
  522. // in the case of nested classes, in order for the links to work,
  523. // need to have the correct file name which is something of the
  524. // form parentClass.nestedAspect.html
  525. List names = new ArrayList();
  526. IProgramElement parent = currDecl;
  527. while (parent != null
  528. && parent.getParent() != null
  529. && (!parent.getParent().getKind().equals(IProgramElement.Kind.FILE_JAVA) && !parent.getParent().getKind()
  530. .equals(IProgramElement.Kind.FILE_ASPECTJ))) {
  531. parent = parent.getParent();
  532. names.add(parent.toLinkLabelString());
  533. }
  534. StringBuffer sbuff = new StringBuffer();
  535. for (int i = names.size() - 1; i >= 0; i--) {
  536. String element = (String) names.get(i);
  537. if (i == 0) {
  538. sbuff.append(element);
  539. } else {
  540. sbuff.append(element + ".");
  541. }
  542. }
  543. // use the currDecl.toLabelString rather than currDecl.getName()
  544. // because two distinct advice blocks can have the same
  545. // currDecl.getName() and wouldn't both appear in the ajdoc
  546. hrefName += Config.DIR_SEP_CHAR + sbuff.toString() + "." + currDecl.toLabelString();
  547. // need to replace " with quot; otherwise the links wont work
  548. // for 'matches declare' relationship
  549. StringBuffer sb = new StringBuffer(currDecl.toLabelString());
  550. int nextQuote = sb.toString().indexOf("\"");
  551. while (nextQuote != -1) {
  552. sb.deleteCharAt(nextQuote);
  553. sb.insert(nextQuote, "quot;");
  554. nextQuote = sb.toString().indexOf("\"");
  555. }
  556. hrefLink += sbuff.toString() + ".html" + "#" + sb.toString();
  557. if (!addedNames.contains(hrefName)) {
  558. adviceDoc = adviceDoc + "<A HREF=\"" + hrefLink + "\"><tt>" + hrefName.replace('/', '.') + "</tt></A>";
  559. if (it.hasNext())
  560. adviceDoc += ", ";
  561. addedNames.add(hrefName);
  562. }
  563. }
  564. adviceDoc += "</TR></TD></TABLE>\n";
  565. fileContentsBuffer.insert(index, adviceDoc);
  566. }
  567. }
  568. static void decorateMemberDocumentation(IProgramElement node, StringBuffer fileContentsBuffer, int index) {
  569. List targets = StructureUtil.getTargets(node, IRelationship.Kind.ADVICE);
  570. decorateDocWithRel(node, fileContentsBuffer, index, targets, HtmlRelationshipKind.ADVISED_BY);
  571. List warnings = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE, "matches declare");
  572. decorateDocWithRel(node, fileContentsBuffer, index, warnings, HtmlRelationshipKind.MATCHES_DECLARE);
  573. List softenedBy = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE, "softened by");
  574. decorateDocWithRel(node, fileContentsBuffer, index, softenedBy, HtmlRelationshipKind.SOFTENED_BY);
  575. List annotatedBy = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE_INTER_TYPE, "annotated by");
  576. decorateDocWithRel(node, fileContentsBuffer, index, annotatedBy, HtmlRelationshipKind.ANNOTATED_BY);
  577. }
  578. /**
  579. * pr119453 - adding "declared by" relationship
  580. */
  581. static String generateDeclaredBy(IProgramElement decl) {
  582. String entry = "<TABLE WIDTH=\"100%\" BGCOLOR=#FFFFFF><TR>"
  583. + "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + "&nbsp;Declared&nbsp;by:</b></font></td><td>";
  584. String relativePackagePath = getRelativePathFromHere(decl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR);
  585. if (decl != null && !StructureUtil.isAnonymous(decl.getParent())) {
  586. String packagePath = "";
  587. if (decl.getPackageName() != null && !decl.getPackageName().equals("")) {
  588. packagePath = decl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR;
  589. }
  590. String typeSignature = constructNestedTypeName(decl);
  591. String hrefName = packagePath + typeSignature;
  592. // The hrefLink needs to just be the corresponding aspect
  593. String hrefLink = relativePackagePath + packagePath + typeSignature + ".html";
  594. entry += "<A HREF=\"" + hrefLink + "\"><tt>" + hrefName.replace('/', '.') + "</tt></A>"; // !!! don't replace
  595. }
  596. entry += "</B></FONT></TD></TR></TABLE>\n</TR></TD>\n";
  597. return entry;
  598. }
  599. /**
  600. * TODO: probably want to make this the same for intros and advice.
  601. */
  602. static String generateAffects(IProgramElement decl) {
  603. List targets = null;
  604. if (decl.getKind().isDeclare() || decl.getKind().isInterTypeMember()) {
  605. targets = StructureUtil.getDeclareTargets(decl);
  606. } else {
  607. targets = StructureUtil.getTargets(decl, IRelationship.Kind.ADVICE);
  608. }
  609. if (targets == null)
  610. return "";
  611. String entry = "<TABLE WIDTH=\"100%\" BGCOLOR=#FFFFFF><TR>";
  612. IProgramElement.Kind kind = decl.getKind();
  613. if (kind.equals(IProgramElement.Kind.ADVICE)) {
  614. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.ADVISES.toString()
  615. + "</b></font></td><td>";
  616. } else if (kind.equals(IProgramElement.Kind.DECLARE_WARNING) || kind.equals(IProgramElement.Kind.DECLARE_ERROR)) {
  617. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.MATCHED_BY.toString()
  618. + "</b></font></td><td>";
  619. } else if (kind.isDeclareAnnotation()) {
  620. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.ANNOTATES.toString()
  621. + "</b></font></td><td>";
  622. } else if (kind.equals(IProgramElement.Kind.DECLARE_SOFT)) {
  623. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.SOFTENS.toString()
  624. + "</b></font></td><td>";
  625. } else {
  626. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.DECLARED_ON.toString()
  627. + "</b></font></td><td>";
  628. }
  629. String relativePackagePath = getRelativePathFromHere(decl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR);
  630. List addedNames = new ArrayList(); // for ensuring that we don't add duplciates
  631. for (Iterator it = targets.iterator(); it.hasNext();) {
  632. String currHandle = (String) it.next();
  633. IProgramElement currDecl = decl.getModel().getHierarchy().findElementForHandle(currHandle);
  634. if (currDecl.getKind().equals(IProgramElement.Kind.CODE)) {
  635. currDecl = currDecl.getParent(); // promote to enclosing
  636. }
  637. if (currDecl != null && !StructureUtil.isAnonymous(currDecl.getParent())) {
  638. String packagePath = "";
  639. if (currDecl.getPackageName() != null && !currDecl.getPackageName().equals("")) {
  640. packagePath = currDecl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR;
  641. }
  642. String typeSignature = constructNestedTypeName(currDecl);
  643. String hrefName = packagePath + typeSignature;
  644. // Start the hRefLink with the relative path based on where
  645. // *this* type (i.e. the advisor) is in the package structure.
  646. String hrefLink = relativePackagePath + packagePath + typeSignature + ".html";
  647. if (!currDecl.getKind().isType()) {
  648. hrefName += '.' + currDecl.getName();
  649. hrefLink += "#" + currDecl.toLabelString();
  650. }
  651. if (!addedNames.contains(hrefName)) {
  652. entry += "<A HREF=\"" + hrefLink + "\"><tt>" + hrefName.replace('/', '.') + "</tt></A>"; // !!! don't replace
  653. if (it.hasNext())
  654. entry += ", ";
  655. addedNames.add(hrefName);
  656. }
  657. }
  658. }
  659. entry += "</B></FONT></TD></TR></TABLE>\n</TR></TD>\n";
  660. return entry;
  661. }
  662. /**
  663. * Generates a relative directory path fragment that can be used to navigate "upwards" from the directory location implied by
  664. * the argument.
  665. *
  666. * @param packagePath
  667. * @return String consisting of multiple "../" parts, one for each component part of the input <code>packagePath</code>.
  668. */
  669. private static String getRelativePathFromHere(String packagePath) {
  670. StringBuffer result = new StringBuffer("");
  671. if (packagePath != null && (packagePath.indexOf("/") != -1)) {
  672. StringTokenizer sTok = new StringTokenizer(packagePath, "/", false);
  673. while (sTok.hasMoreTokens()) {
  674. sTok.nextToken(); // don't care about the token value
  675. result.append(".." + Config.DIR_SEP_CHAR);
  676. }// end while
  677. }// end if
  678. return result.toString();
  679. }
  680. /**
  681. * Generate the "public int"-type information about the given IProgramElement. Used when dealing with ITDs. To mirror the
  682. * behaviour of methods and fields in classes, if we're generating the summary information we don't want to include "public" if
  683. * the accessibility of the IProgramElement is public.
  684. *
  685. */
  686. private static String generateModifierInformation(IProgramElement decl, boolean isDetails) {
  687. String intro = "";
  688. if (decl.getKind().isDeclare()) {
  689. return intro + "</TT>";
  690. }
  691. if (isDetails || !decl.getAccessibility().equals(IProgramElement.Accessibility.PUBLIC)) {
  692. intro += "<TT>" + decl.getAccessibility().toString() + "&nbsp;";
  693. }
  694. if (decl.getKind().equals(IProgramElement.Kind.INTER_TYPE_FIELD)) {
  695. return intro + decl.getCorrespondingType() + "</TT>";
  696. } else if (decl.getKind().equals(IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR) && isDetails) {
  697. return intro + "</TT>";
  698. } else {
  699. return intro + decl.getCorrespondingType(true) + "</TT>";
  700. }
  701. }
  702. static String generateIntroductionSignatures(IProgramElement decl, boolean isDetails) {
  703. return "<not implemented>";
  704. }
  705. static String generateSignatures(IProgramElement decl) {
  706. return "<B>" + decl.toLabelString() + "</B>";
  707. }
  708. static String generateSummaryComment(IProgramElement decl) {
  709. String COMMENT_INDENT = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; // !!!
  710. String formattedComment = getFormattedComment(decl);
  711. int periodIndex = formattedComment.indexOf('.');
  712. if (formattedComment.equals("")) {
  713. return "";
  714. } else if (periodIndex != -1) {
  715. return COMMENT_INDENT + formattedComment.substring(0, periodIndex + 1);
  716. } else {
  717. return COMMENT_INDENT + formattedComment;
  718. }
  719. }
  720. static String generateDetailsComment(IProgramElement decl) {
  721. return getFormattedComment(decl);
  722. }
  723. static String generateHREFName(IProgramElement decl) {
  724. StringBuffer hrefLinkBuffer = new StringBuffer();
  725. char[] declChars = decl.toLabelString().toCharArray();
  726. for (int i = 0; i < declChars.length; i++) {
  727. if (declChars[i] == '"') {
  728. hrefLinkBuffer.append("quot;");
  729. } else {
  730. hrefLinkBuffer.append(declChars[i]);
  731. }
  732. }
  733. return hrefLinkBuffer.toString();
  734. }
  735. /**
  736. * Figure out the link relative to the package.
  737. */
  738. static String generateAffectsHREFLink(String declaringType) {
  739. String link = rootDir.getAbsolutePath() + "/" + declaringType + ".html";
  740. return link;
  741. }
  742. /**
  743. * This formats a comment according to the rules in the Java Langauge Spec: <I>The text of a docuemntation comment consists of
  744. * the characters between the /** that begins the comment and the 'star-slash' that ends it. The text is devided into one or
  745. * more lines. On each of these lines, the leading * characters are ignored; for lines other than the first, blanks and tabs
  746. * preceding the initial * characters are also discarded.</I>
  747. *
  748. * TODO: implement formatting or linking for tags.
  749. */
  750. static String getFormattedComment(IProgramElement decl) {
  751. String comment = decl.getFormalComment();
  752. if (comment == null)
  753. return "";
  754. String formattedComment = "";
  755. // strip the comment markers
  756. int startIndex = comment.indexOf("/**");
  757. int endIndex = comment.indexOf("*/");
  758. if (startIndex == -1) {
  759. startIndex = 0;
  760. } else {
  761. startIndex += 3;
  762. }
  763. if (endIndex == -1) {
  764. endIndex = comment.length();
  765. }
  766. comment = comment.substring(startIndex, endIndex);
  767. // string the leading whitespace and '*' characters at the beginning of each line
  768. BufferedReader reader = new BufferedReader(new StringReader(comment));
  769. try {
  770. for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  771. line = line.trim();
  772. for (int i = 0; i < line.length(); i++) {
  773. if (line.charAt(0) == '*') {
  774. line = line.substring(1, line.length());
  775. } else {
  776. break;
  777. }
  778. }
  779. // !!! remove any @see and @link tags from the line
  780. // int seeIndex = line.indexOf("@see");
  781. // int linkIndex = line.indexOf("@link");
  782. // if ( seeIndex != -1 ) {
  783. // line = line.substring(0, seeIndex) + line.substring(seeIndex);
  784. // }
  785. // if ( linkIndex != -1 ) {
  786. // line = line.substring(0, linkIndex) + line.substring(linkIndex);
  787. // }
  788. formattedComment += line;
  789. }
  790. } catch (IOException ioe) {
  791. throw new Error("Couldn't format comment for declaration: " + decl.getName());
  792. }
  793. return formattedComment;
  794. }
  795. static public IProgramElement[] getProgramElements(AsmManager model, String filename) {
  796. IProgramElement file = (IProgramElement) model.getHierarchy().findElementForSourceFile(filename);
  797. final List nodes = new ArrayList();
  798. HierarchyWalker walker = new HierarchyWalker() {
  799. public void preProcess(IProgramElement node) {
  800. IProgramElement p = (IProgramElement) node;
  801. if (accept(node))
  802. nodes.add(p);
  803. }
  804. };
  805. file.walk(walker);
  806. return (IProgramElement[]) nodes.toArray(new IProgramElement[nodes.size()]);
  807. }
  808. /**
  809. * Rejects anonymous kinds by checking if their name is an integer
  810. */
  811. static private boolean accept(IProgramElement node) {
  812. if (node.getKind().isType()) {
  813. boolean isAnonymous = StructureUtil.isAnonymous(node);
  814. return !node.getParent().getKind().equals(IProgramElement.Kind.METHOD) && !isAnonymous;
  815. } else {
  816. return !node.getKind().equals(IProgramElement.Kind.IMPORT_REFERENCE);
  817. }
  818. }
  819. /**
  820. * TypeSafeEnum for the entries which need to be put in the html doc
  821. */
  822. public static class HtmlRelationshipKind extends TypeSafeEnum {
  823. public HtmlRelationshipKind(String name, int key) {
  824. super(name, key);
  825. }
  826. public static HtmlRelationshipKind read(DataInputStream s) throws IOException {
  827. int key = s.readByte();
  828. switch (key) {
  829. case 1:
  830. return ADVISES;
  831. case 2:
  832. return ADVISED_BY;
  833. case 3:
  834. return MATCHED_BY;
  835. case 4:
  836. return MATCHES_DECLARE;
  837. case 5:
  838. return DECLARED_ON;
  839. case 6:
  840. return ASPECT_DECLARATIONS;
  841. case 7:
  842. return SOFTENS;
  843. case 8:
  844. return SOFTENED_BY;
  845. case 9:
  846. return ANNOTATES;
  847. case 10:
  848. return ANNOTATED_BY;
  849. case 11:
  850. return USES_POINTCUT;
  851. case 12:
  852. return POINTCUT_USED_BY;
  853. }
  854. throw new Error("weird relationship kind " + key);
  855. }
  856. public static final HtmlRelationshipKind ADVISES = new HtmlRelationshipKind("&nbsp;Advises:", 1);
  857. public static final HtmlRelationshipKind ADVISED_BY = new HtmlRelationshipKind("&nbsp;Advised&nbsp;by:", 2);
  858. public static final HtmlRelationshipKind MATCHED_BY = new HtmlRelationshipKind("&nbsp;Matched&nbsp;by:", 3);
  859. public static final HtmlRelationshipKind MATCHES_DECLARE = new HtmlRelationshipKind("&nbsp;Matches&nbsp;declare:", 4);
  860. public static final HtmlRelationshipKind DECLARED_ON = new HtmlRelationshipKind("&nbsp;Declared&nbsp;on:", 5);
  861. public static final HtmlRelationshipKind ASPECT_DECLARATIONS = new HtmlRelationshipKind("&nbsp;Aspect&nbsp;declarations:",
  862. 6);
  863. public static final HtmlRelationshipKind SOFTENS = new HtmlRelationshipKind("&nbsp;Softens:", 7);
  864. public static final HtmlRelationshipKind SOFTENED_BY = new HtmlRelationshipKind("&nbsp;Softened&nbsp;by:", 8);
  865. public static final HtmlRelationshipKind ANNOTATES = new HtmlRelationshipKind("&nbsp;Annotates:", 9);
  866. public static final HtmlRelationshipKind ANNOTATED_BY = new HtmlRelationshipKind("&nbsp;Annotated&nbsp;by:", 10);
  867. public static final HtmlRelationshipKind USES_POINTCUT = new HtmlRelationshipKind("&nbsp;Uses&nbsp;pointcut:", 11);
  868. public static final HtmlRelationshipKind POINTCUT_USED_BY = new HtmlRelationshipKind("&nbsp;Pointcut&nbsp;used&nbsp;by:",
  869. 12);
  870. }
  871. }