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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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<String> visibleFileList = new ArrayList<String>();
  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. if (classStartIndex == -1) {
  250. // Java7: 464604
  251. // <pre>public class <span class="strong">Azpect</span>
  252. classStartIndex = fileContents.toString().indexOf("class <span class=\"strong\">");
  253. }
  254. int classEndIndex = fileContents.toString().indexOf("</span>", classStartIndex);
  255. if (classEndIndex != -1) {
  256. // Convert it to "aspect <span class="typeNameLabel">ClassA.InnerAspect</span>"
  257. String classLine = fileContents.toString().substring(classStartIndex, classEndIndex);
  258. String aspectLine = "aspect"+fileContents.substring(classStartIndex+5,classEndIndex);
  259. fileContents.delete(classStartIndex, classEndIndex);
  260. fileContents.insert(classStartIndex, aspectLine);
  261. }
  262. }
  263. }
  264. }
  265. file.delete();
  266. FileOutputStream fos = new FileOutputStream(file);
  267. fos.write(fileContents.toString().getBytes());
  268. reader.close();
  269. fos.close();
  270. }
  271. static void addAspectDocumentation(IProgramElement node, StringBuffer fileBuffer, int index) {
  272. List<IProgramElement> pointcuts = new ArrayList<IProgramElement>();
  273. List<IProgramElement> advice = new ArrayList<IProgramElement>();
  274. List<IProgramElement> declares = new ArrayList<IProgramElement>();
  275. List<IProgramElement> methodsDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.INTER_TYPE_METHOD);
  276. if (methodsDeclaredOn != null && !methodsDeclaredOn.isEmpty()) {
  277. insertDeclarationsSummary(fileBuffer, methodsDeclaredOn, ITD_METHOD_SUMMARY, index);
  278. }
  279. List<IProgramElement> fieldsDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.INTER_TYPE_FIELD);
  280. if (fieldsDeclaredOn != null && !fieldsDeclaredOn.isEmpty()) {
  281. insertDeclarationsSummary(fileBuffer, fieldsDeclaredOn, ITD_FIELD_SUMMARY, index);
  282. }
  283. List<IProgramElement> constDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR);
  284. if (fieldsDeclaredOn != null && !constDeclaredOn.isEmpty()) {
  285. insertDeclarationsSummary(fileBuffer, constDeclaredOn, ITD_CONSTRUCTOR_SUMMARY, index);
  286. }
  287. for (Iterator<IProgramElement> it = node.getChildren().iterator(); it.hasNext();) {
  288. IProgramElement member = (IProgramElement) it.next();
  289. if (member.getKind().equals(IProgramElement.Kind.POINTCUT)) {
  290. pointcuts.add(member);
  291. } else if (member.getKind().equals(IProgramElement.Kind.ADVICE)) {
  292. advice.add(member);
  293. } else if (member.getKind().isDeclare() || member.getKind().isInterTypeMember()) {
  294. declares.add(member);
  295. }
  296. }
  297. if (declares.size() > 0) {
  298. insertDeclarationsDetails(fileBuffer, declares, DECLARE_DETAIL, index);
  299. insertDeclarationsSummary(fileBuffer, declares, DECLARE_SUMMARY, index);
  300. }
  301. if (pointcuts.size() > 0) {
  302. insertDeclarationsSummary(fileBuffer, pointcuts, POINTCUT_SUMMARY, index);
  303. insertDeclarationsDetails(fileBuffer, pointcuts, POINTCUT_DETAIL, index);
  304. }
  305. if (advice.size() > 0) {
  306. insertDeclarationsSummary(fileBuffer, advice, ADVICE_SUMMARY, index);
  307. insertDeclarationsDetails(fileBuffer, advice, ADVICE_DETAIL, index);
  308. }
  309. // add the 'aspect declarations' information against the type
  310. List<IProgramElement> parentsDeclaredOn = StructureUtil.getDeclareInterTypeTargets(node, IProgramElement.Kind.DECLARE_PARENTS);
  311. if (parentsDeclaredOn != null && parentsDeclaredOn.size() > 0) {
  312. decorateDocWithRel(node, fileBuffer, index, parentsDeclaredOn, HtmlRelationshipKind.ASPECT_DECLARATIONS);
  313. }
  314. // add the 'annotated by' information against the type
  315. List<String> annotatedBy = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE_INTER_TYPE, "annotated by");
  316. if (annotatedBy != null && annotatedBy.size() > 0) {
  317. decorateDocWithRel(node, fileBuffer, index, annotatedBy, HtmlRelationshipKind.ANNOTATED_BY);
  318. }
  319. // add the 'advised by' information against the type
  320. List<String> advisedBy = StructureUtil.getTargets(node, IRelationship.Kind.ADVICE);
  321. if (advisedBy != null && advisedBy.size() > 0) {
  322. decorateDocWithRel(node, fileBuffer, index, advisedBy, HtmlRelationshipKind.ADVISED_BY);
  323. }
  324. }
  325. static void insertDeclarationsSummary(StringBuffer fileBuffer, List decls, String kind, int index) {
  326. if (!declsAboveVisibilityExist(decls))
  327. return;
  328. int insertIndex = findSummaryIndex(fileBuffer, index);
  329. // insert the head of the table
  330. String tableHead = "<!-- ======== " + kind.toUpperCase() + " ======= -->\n\n"
  331. + "<TABLE BORDER=\"1\" WIDTH=\"100%\" CELLPADDING=\"1\""
  332. + "CELLSPACING=\"0\"><TR><TD COLSPAN=2 BGCOLOR=\"#CCCCFF\">" + "<FONT SIZE=\"+2\"><B>" + kind
  333. + "</B></FONT></TD></TR>\n";
  334. fileBuffer.insert(insertIndex, tableHead);
  335. insertIndex += tableHead.length();
  336. // insert the body of the table
  337. for (int i = 0; i < decls.size(); i++) {
  338. IProgramElement decl = (IProgramElement) decls.get(i);
  339. if (isAboveVisibility(decl)) {
  340. // insert the table row accordingly
  341. String comment = generateSummaryComment(decl);
  342. String entry = "";
  343. if (kind.equals(ADVICE_SUMMARY)) {
  344. entry += "<TR><TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>" + generateSignatures(decl)
  345. + "</TT></A><BR>&nbsp;";
  346. if (!comment.equals("")) {
  347. entry += comment + "<P>";
  348. }
  349. entry += generateAffects(decl) + "</TD>" + "</TR><TD>\n";
  350. } else if (kind.equals(POINTCUT_SUMMARY)) {
  351. entry += "<TR><TD WIDTH=\"1%\">" + "<FONT SIZE=-1><TT>" + genAccessibility(decl) + "</TT></FONT>" + "</TD>\n"
  352. + "<TD>" + "<TT><A HREF=\"#" + generateHREFName(decl) + "\">" + decl.toLabelString()
  353. + "</A></TT><BR>&nbsp;";
  354. if (!comment.equals("")) {
  355. entry += comment + "<P>";
  356. }
  357. entry += "</TR></TD>\n";
  358. } else if (kind.equals(DECLARE_SUMMARY)) {
  359. entry += "<TR><TD WIDTH=\"1%\">" + "<FONT SIZE=-1><TT>" + generateModifierInformation(decl, false)
  360. + "</TT></FONT>" + "</TD>" + "<TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>"
  361. + decl.toLabelString() + "</TT></A><P>" + generateAffects(decl);
  362. } else if (kind.equals(ITD_FIELD_SUMMARY) || kind.equals(ITD_METHOD_SUMMARY)) {
  363. entry += "<TR><TD WIDTH=\"1%\">" + "<FONT SIZE=-1><TT>" + generateModifierInformation(decl, false)
  364. + "</TT></FONT>" + "</TD>" + "<TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>"
  365. + decl.toLabelString() + "</TT></A><P>" + generateDeclaredBy(decl);
  366. } else if (kind.equals(ITD_CONSTRUCTOR_SUMMARY)) {
  367. entry += "<TD>" + "<A HREF=\"#" + generateHREFName(decl) + "\">" + "<TT>" + decl.toLabelString()
  368. + "</TT></A><P>" + generateDeclaredBy(decl);
  369. }
  370. // insert the entry
  371. fileBuffer.insert(insertIndex, entry);
  372. insertIndex += entry.length();
  373. }
  374. }
  375. // insert the end of the table
  376. String tableTail = "</TABLE><P>&nbsp;\n";
  377. fileBuffer.insert(insertIndex, tableTail);
  378. insertIndex += tableTail.length();
  379. }
  380. private static boolean declsAboveVisibilityExist(List decls) {
  381. boolean exist = false;
  382. for (Iterator it = decls.iterator(); it.hasNext();) {
  383. IProgramElement element = (IProgramElement) it.next();
  384. if (isAboveVisibility(element))
  385. exist = true;
  386. }
  387. return exist;
  388. }
  389. private static boolean isAboveVisibility(IProgramElement element) {
  390. IProgramElement.Accessibility acc = element.getAccessibility();
  391. if (docVisibilityModifier.equals("private")) {
  392. // show all classes and members
  393. return true;
  394. } else if (docVisibilityModifier.equals("package")) {
  395. // show package, protected and public classes and members
  396. return acc.equals(IProgramElement.Accessibility.PACKAGE) || acc.equals(IProgramElement.Accessibility.PROTECTED)
  397. || acc.equals(IProgramElement.Accessibility.PUBLIC);
  398. } else if (docVisibilityModifier.equals("protected")) {
  399. // show protected and public classes and members
  400. return acc.equals(IProgramElement.Accessibility.PROTECTED) || acc.equals(IProgramElement.Accessibility.PUBLIC);
  401. } else if (docVisibilityModifier.equals("public")) {
  402. // show public classes and members
  403. return acc.equals(IProgramElement.Accessibility.PUBLIC);
  404. }
  405. return false;
  406. }
  407. private static String genAccessibility(IProgramElement decl) {
  408. if (decl.getAccessibility().equals(IProgramElement.Accessibility.PACKAGE)) {
  409. return "(package private)";
  410. } else {
  411. return decl.getAccessibility().toString();
  412. }
  413. }
  414. static void insertDeclarationsDetails(StringBuffer fileBuffer, List decls, String kind, int index) {
  415. if (!declsAboveVisibilityExist(decls))
  416. return;
  417. int insertIndex = findDetailsIndex(fileBuffer, index);
  418. // insert the table heading
  419. String detailsHeading = "<P>&nbsp;\n" + "<!-- ======== " + kind.toUpperCase() + " SUMMARY ======= -->\n\n"
  420. + "<TABLE BORDER=\"1\" CELLPADDING=\"3\" CELLSPACING=\"0\" WIDTH=\"100%\">\n"
  421. + "<TR BGCOLOR=\"#CCCCFF\" CLASS=\"TableHeadingColor\">\n" + "<TD COLSPAN=1><FONT SIZE=\"+2\">\n" + "<B>" + kind
  422. + "</B></FONT></TD>\n" + "</TR>\n" + "</TABLE>";
  423. fileBuffer.insert(insertIndex, detailsHeading);
  424. insertIndex += detailsHeading.length();
  425. // insert the details
  426. for (int i = 0; i < decls.size(); i++) {
  427. IProgramElement decl = (IProgramElement) decls.get(i);
  428. if (isAboveVisibility(decl)) {
  429. String entry = "";
  430. // insert the table row accordingly
  431. entry += "<A NAME=\"" + generateHREFName(decl) + "\"><!-- --></A>\n";
  432. if (kind.equals(ADVICE_DETAIL)) {
  433. entry += "<H3>" + decl.getName() + "</H3><P>";
  434. entry += "<TT>" + generateSignatures(decl) + "</TT>\n" + "<P>" + generateDetailsComment(decl) + "<P>"
  435. + generateAffects(decl);
  436. } else if (kind.equals(POINTCUT_DETAIL)) {
  437. entry += "<H3>" + decl.toLabelString() + "</H3><P>" + generateDetailsComment(decl);
  438. } else if (kind.equals(DECLARE_DETAIL)) {
  439. entry += "<H3>" + decl.toLabelString() + "</H3><P>" + generateModifierInformation(decl, true);
  440. if (!decl.getKind().equals(IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR)) {
  441. entry += "&nbsp;&nbsp;";
  442. }
  443. // if we're not a declare statement then we need to generate the signature.
  444. // If we did this for declare statements we get two repeated lines
  445. if (!decl.getKind().isDeclare()) {
  446. String sigs = generateSignatures(decl);
  447. entry += sigs + "<P>";
  448. }
  449. entry += generateAffects(decl) + generateDetailsComment(decl);
  450. }
  451. // insert the entry
  452. if (i != decls.size() - 1) {
  453. entry += "<P><HR>\n";
  454. } else {
  455. entry += "<P>";
  456. }
  457. fileBuffer.insert(insertIndex, entry);
  458. insertIndex += entry.length();
  459. }
  460. }
  461. }
  462. /**
  463. * TODO: don't place the summary first.
  464. */
  465. static int findSummaryIndex(StringBuffer fileBuffer, int index) {
  466. String fbs = fileBuffer.toString();
  467. String MARKER_1 = "<!-- =========== FIELD SUMMARY =========== -->";
  468. String MARKER_2 = "<!-- ======== CONSTRUCTOR SUMMARY ======== -->";
  469. int index1 = fbs.indexOf(MARKER_1, index);
  470. int index2 = fbs.indexOf(MARKER_2, index);
  471. if (index1 < index2 && index1 != -1) {
  472. return index1;
  473. } else if (index2 != -1) {
  474. return index2;
  475. } else {
  476. return index;
  477. }
  478. }
  479. static int findDetailsIndex(StringBuffer fileBuffer, int index) {
  480. String fbs = fileBuffer.toString();
  481. String MARKER_1 = "<!-- ========= CONSTRUCTOR DETAIL ======== -->";
  482. String MARKER_2 = "<!-- ============ FIELD DETAIL =========== -->";
  483. String MARKER_3 = "<!-- ============ METHOD DETAIL ========== -->";
  484. int index1 = fbs.indexOf(MARKER_1, index);
  485. int index2 = fbs.indexOf(MARKER_2, index);
  486. int index3 = fbs.indexOf(MARKER_3, index);
  487. if (index1 != -1 && index1 < index2 && index1 < index3) {
  488. return index1;
  489. } else if (index2 != -1 && index2 < index1 && index2 < index3) {
  490. return index2;
  491. } else if (index3 != -1) {
  492. return index3;
  493. } else {
  494. return index;
  495. }
  496. }
  497. static void decorateDocWithRel(IProgramElement node, StringBuffer fileContentsBuffer, int index, List targets,
  498. HtmlRelationshipKind relKind) {
  499. if (targets != null && !targets.isEmpty()) {
  500. String adviceDoc = "<TABLE WIDTH=\"100%\" BGCOLOR=#FFFFFF><TR>"
  501. + "<TD width=\"15%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + relKind.toString() + "</font></b></td><td>";
  502. String relativePackagePath = getRelativePathFromHere(node.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR);
  503. List addedNames = new ArrayList();
  504. for (Iterator it = targets.iterator(); it.hasNext();) {
  505. Object o = it.next();
  506. IProgramElement currDecl = null;
  507. if (o instanceof String) {
  508. String currHandle = (String) o;
  509. currDecl = node.getModel().getHierarchy().findElementForHandle(currHandle);
  510. } else if (o instanceof IProgramElement) {
  511. currDecl = (IProgramElement) o;
  512. } else {
  513. return;
  514. }
  515. String packagePath = "";
  516. if (currDecl.getPackageName() != null && !currDecl.getPackageName().equals("")) {
  517. packagePath = currDecl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR;
  518. }
  519. String hrefName = "";
  520. String hrefLink = "";
  521. // Start the hRefLink with the relative path based on where
  522. // *this* type (i.e. the advised) is in the package structure.
  523. hrefLink = relativePackagePath + packagePath;
  524. if (currDecl.getPackageName() != null) {
  525. hrefName = currDecl.getPackageName().replace('.', '/');
  526. }
  527. // in the case of nested classes, in order for the links to work,
  528. // need to have the correct file name which is something of the
  529. // form parentClass.nestedAspect.html
  530. List names = new ArrayList();
  531. IProgramElement parent = currDecl;
  532. while (parent != null
  533. && parent.getParent() != null
  534. && (!parent.getParent().getKind().equals(IProgramElement.Kind.FILE_JAVA) && !parent.getParent().getKind()
  535. .equals(IProgramElement.Kind.FILE_ASPECTJ))) {
  536. parent = parent.getParent();
  537. names.add(parent.toLinkLabelString());
  538. }
  539. StringBuffer sbuff = new StringBuffer();
  540. for (int i = names.size() - 1; i >= 0; i--) {
  541. String element = (String) names.get(i);
  542. if (i == 0) {
  543. sbuff.append(element);
  544. } else {
  545. sbuff.append(element + ".");
  546. }
  547. }
  548. // use the currDecl.toLabelString rather than currDecl.getName()
  549. // because two distinct advice blocks can have the same
  550. // currDecl.getName() and wouldn't both appear in the ajdoc
  551. hrefName += Config.DIR_SEP_CHAR + sbuff.toString() + "." + currDecl.toLabelString();
  552. // need to replace " with quot; otherwise the links wont work
  553. // for 'matches declare' relationship
  554. StringBuffer sb = new StringBuffer(currDecl.toLabelString());
  555. int nextQuote = sb.toString().indexOf("\"");
  556. while (nextQuote != -1) {
  557. sb.deleteCharAt(nextQuote);
  558. sb.insert(nextQuote, "quot;");
  559. nextQuote = sb.toString().indexOf("\"");
  560. }
  561. hrefLink += sbuff.toString() + ".html" + "#" + sb.toString();
  562. if (!addedNames.contains(hrefName)) {
  563. adviceDoc = adviceDoc + "<A HREF=\"" + hrefLink + "\"><tt>" + hrefName.replace('/', '.') + "</tt></A>";
  564. if (it.hasNext())
  565. adviceDoc += ", ";
  566. addedNames.add(hrefName);
  567. }
  568. }
  569. adviceDoc += "</TR></TD></TABLE>\n";
  570. fileContentsBuffer.insert(index, adviceDoc);
  571. }
  572. }
  573. static void decorateMemberDocumentation(IProgramElement node, StringBuffer fileContentsBuffer, int index) {
  574. List<String> targets = StructureUtil.getTargets(node, IRelationship.Kind.ADVICE);
  575. decorateDocWithRel(node, fileContentsBuffer, index, targets, HtmlRelationshipKind.ADVISED_BY);
  576. List<String> warnings = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE, "matches declare");
  577. decorateDocWithRel(node, fileContentsBuffer, index, warnings, HtmlRelationshipKind.MATCHES_DECLARE);
  578. List<String> softenedBy = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE, "softened by");
  579. decorateDocWithRel(node, fileContentsBuffer, index, softenedBy, HtmlRelationshipKind.SOFTENED_BY);
  580. List<String> annotatedBy = StructureUtil.getTargets(node, IRelationship.Kind.DECLARE_INTER_TYPE, "annotated by");
  581. decorateDocWithRel(node, fileContentsBuffer, index, annotatedBy, HtmlRelationshipKind.ANNOTATED_BY);
  582. }
  583. /**
  584. * pr119453 - adding "declared by" relationship
  585. */
  586. static String generateDeclaredBy(IProgramElement decl) {
  587. String entry = "<TABLE WIDTH=\"100%\" BGCOLOR=#FFFFFF><TR>"
  588. + "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + "&nbsp;Declared&nbsp;by:</b></font></td><td>";
  589. String relativePackagePath = getRelativePathFromHere(decl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR);
  590. if (decl != null && !StructureUtil.isAnonymous(decl.getParent())) {
  591. String packagePath = "";
  592. if (decl.getPackageName() != null && !decl.getPackageName().equals("")) {
  593. packagePath = decl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR;
  594. }
  595. String typeSignature = constructNestedTypeName(decl);
  596. String hrefName = packagePath + typeSignature;
  597. // The hrefLink needs to just be the corresponding aspect
  598. String hrefLink = relativePackagePath + packagePath + typeSignature + ".html";
  599. entry += "<A HREF=\"" + hrefLink + "\"><tt>" + hrefName.replace('/', '.') + "</tt></A>"; // !!! don't replace
  600. }
  601. entry += "</B></FONT></TD></TR></TABLE>\n</TR></TD>\n";
  602. return entry;
  603. }
  604. /**
  605. * TODO: probably want to make this the same for intros and advice.
  606. */
  607. static String generateAffects(IProgramElement decl) {
  608. List targets = null;
  609. if (decl.getKind().isDeclare() || decl.getKind().isInterTypeMember()) {
  610. targets = StructureUtil.getDeclareTargets(decl);
  611. } else {
  612. targets = StructureUtil.getTargets(decl, IRelationship.Kind.ADVICE);
  613. }
  614. if (targets == null)
  615. return "";
  616. String entry = "<TABLE WIDTH=\"100%\" BGCOLOR=#FFFFFF><TR>";
  617. IProgramElement.Kind kind = decl.getKind();
  618. if (kind.equals(IProgramElement.Kind.ADVICE)) {
  619. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.ADVISES.toString()
  620. + "</b></font></td><td>";
  621. } else if (kind.equals(IProgramElement.Kind.DECLARE_WARNING) || kind.equals(IProgramElement.Kind.DECLARE_ERROR)) {
  622. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.MATCHED_BY.toString()
  623. + "</b></font></td><td>";
  624. } else if (kind.isDeclareAnnotation()) {
  625. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.ANNOTATES.toString()
  626. + "</b></font></td><td>";
  627. } else if (kind.equals(IProgramElement.Kind.DECLARE_SOFT)) {
  628. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.SOFTENS.toString()
  629. + "</b></font></td><td>";
  630. } else {
  631. entry += "<TD width=\"10%\" bgcolor=\"#FFD8B0\"><B><FONT COLOR=000000>" + HtmlRelationshipKind.DECLARED_ON.toString()
  632. + "</b></font></td><td>";
  633. }
  634. String relativePackagePath = getRelativePathFromHere(decl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR);
  635. List addedNames = new ArrayList(); // for ensuring that we don't add duplciates
  636. for (Iterator it = targets.iterator(); it.hasNext();) {
  637. String currHandle = (String) it.next();
  638. IProgramElement currDecl = decl.getModel().getHierarchy().findElementForHandle(currHandle);
  639. if (currDecl.getKind().equals(IProgramElement.Kind.CODE)) {
  640. currDecl = currDecl.getParent(); // promote to enclosing
  641. }
  642. if (currDecl != null && !StructureUtil.isAnonymous(currDecl.getParent())) {
  643. String packagePath = "";
  644. if (currDecl.getPackageName() != null && !currDecl.getPackageName().equals("")) {
  645. packagePath = currDecl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR;
  646. }
  647. String typeSignature = constructNestedTypeName(currDecl);
  648. String hrefName = packagePath + typeSignature;
  649. // Start the hRefLink with the relative path based on where
  650. // *this* type (i.e. the advisor) is in the package structure.
  651. String hrefLink = relativePackagePath + packagePath + typeSignature + ".html";
  652. if (!currDecl.getKind().isType()) {
  653. hrefName += '.' + currDecl.getName();
  654. hrefLink += "#" + currDecl.toLabelString();
  655. }
  656. if (!addedNames.contains(hrefName)) {
  657. entry += "<A HREF=\"" + hrefLink + "\"><tt>" + hrefName.replace('/', '.') + "</tt></A>"; // !!! don't replace
  658. if (it.hasNext())
  659. entry += ", ";
  660. addedNames.add(hrefName);
  661. }
  662. }
  663. }
  664. entry += "</B></FONT></TD></TR></TABLE>\n</TR></TD>\n";
  665. return entry;
  666. }
  667. /**
  668. * Generates a relative directory path fragment that can be used to navigate "upwards" from the directory location implied by
  669. * the argument.
  670. *
  671. * @param packagePath
  672. * @return String consisting of multiple "../" parts, one for each component part of the input <code>packagePath</code>.
  673. */
  674. private static String getRelativePathFromHere(String packagePath) {
  675. StringBuffer result = new StringBuffer("");
  676. if (packagePath != null && (packagePath.indexOf("/") != -1)) {
  677. StringTokenizer sTok = new StringTokenizer(packagePath, "/", false);
  678. while (sTok.hasMoreTokens()) {
  679. sTok.nextToken(); // don't care about the token value
  680. result.append(".." + Config.DIR_SEP_CHAR);
  681. }// end while
  682. }// end if
  683. return result.toString();
  684. }
  685. /**
  686. * Generate the "public int"-type information about the given IProgramElement. Used when dealing with ITDs. To mirror the
  687. * behaviour of methods and fields in classes, if we're generating the summary information we don't want to include "public" if
  688. * the accessibility of the IProgramElement is public.
  689. *
  690. */
  691. private static String generateModifierInformation(IProgramElement decl, boolean isDetails) {
  692. String intro = "";
  693. if (decl.getKind().isDeclare()) {
  694. return intro + "</TT>";
  695. }
  696. if (isDetails || !decl.getAccessibility().equals(IProgramElement.Accessibility.PUBLIC)) {
  697. intro += "<TT>" + decl.getAccessibility().toString() + "&nbsp;";
  698. }
  699. if (decl.getKind().equals(IProgramElement.Kind.INTER_TYPE_FIELD)) {
  700. return intro + decl.getCorrespondingType() + "</TT>";
  701. } else if (decl.getKind().equals(IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR) && isDetails) {
  702. return intro + "</TT>";
  703. } else {
  704. return intro + decl.getCorrespondingType(true) + "</TT>";
  705. }
  706. }
  707. static String generateIntroductionSignatures(IProgramElement decl, boolean isDetails) {
  708. return "<not implemented>";
  709. }
  710. static String generateSignatures(IProgramElement decl) {
  711. return "<B>" + decl.toLabelString() + "</B>";
  712. }
  713. static String generateSummaryComment(IProgramElement decl) {
  714. String COMMENT_INDENT = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; // !!!
  715. String formattedComment = getFormattedComment(decl);
  716. int periodIndex = formattedComment.indexOf('.');
  717. if (formattedComment.equals("")) {
  718. return "";
  719. } else if (periodIndex != -1) {
  720. return COMMENT_INDENT + formattedComment.substring(0, periodIndex + 1);
  721. } else {
  722. return COMMENT_INDENT + formattedComment;
  723. }
  724. }
  725. static String generateDetailsComment(IProgramElement decl) {
  726. return getFormattedComment(decl);
  727. }
  728. static String generateHREFName(IProgramElement decl) {
  729. StringBuffer hrefLinkBuffer = new StringBuffer();
  730. char[] declChars = decl.toLabelString().toCharArray();
  731. for (int i = 0; i < declChars.length; i++) {
  732. if (declChars[i] == '"') {
  733. hrefLinkBuffer.append("quot;");
  734. } else {
  735. hrefLinkBuffer.append(declChars[i]);
  736. }
  737. }
  738. return hrefLinkBuffer.toString();
  739. }
  740. /**
  741. * Figure out the link relative to the package.
  742. */
  743. static String generateAffectsHREFLink(String declaringType) {
  744. String link = rootDir.getAbsolutePath() + "/" + declaringType + ".html";
  745. return link;
  746. }
  747. /**
  748. * This formats a comment according to the rules in the Java Langauge Spec: <I>The text of a docuemntation comment consists of
  749. * the characters between the /** that begins the comment and the 'star-slash' that ends it. The text is devided into one or
  750. * more lines. On each of these lines, the leading * characters are ignored; for lines other than the first, blanks and tabs
  751. * preceding the initial * characters are also discarded.</I>
  752. *
  753. * TODO: implement formatting or linking for tags.
  754. */
  755. static String getFormattedComment(IProgramElement decl) {
  756. String comment = decl.getFormalComment();
  757. if (comment == null)
  758. return "";
  759. String formattedComment = "";
  760. // strip the comment markers
  761. int startIndex = comment.indexOf("/**");
  762. int endIndex = comment.indexOf("*/");
  763. if (startIndex == -1) {
  764. startIndex = 0;
  765. } else {
  766. startIndex += 3;
  767. }
  768. if (endIndex == -1) {
  769. endIndex = comment.length();
  770. }
  771. comment = comment.substring(startIndex, endIndex);
  772. // string the leading whitespace and '*' characters at the beginning of each line
  773. BufferedReader reader = new BufferedReader(new StringReader(comment));
  774. try {
  775. for (String line = reader.readLine(); line != null; line = reader.readLine()) {
  776. line = line.trim();
  777. for (int i = 0; i < line.length(); i++) {
  778. if (line.charAt(0) == '*') {
  779. line = line.substring(1, line.length());
  780. } else {
  781. break;
  782. }
  783. }
  784. // !!! remove any @see and @link tags from the line
  785. // int seeIndex = line.indexOf("@see");
  786. // int linkIndex = line.indexOf("@link");
  787. // if ( seeIndex != -1 ) {
  788. // line = line.substring(0, seeIndex) + line.substring(seeIndex);
  789. // }
  790. // if ( linkIndex != -1 ) {
  791. // line = line.substring(0, linkIndex) + line.substring(linkIndex);
  792. // }
  793. formattedComment += line;
  794. }
  795. } catch (IOException ioe) {
  796. throw new Error("Couldn't format comment for declaration: " + decl.getName());
  797. }
  798. return formattedComment;
  799. }
  800. static public IProgramElement[] getProgramElements(AsmManager model, String filename) {
  801. IProgramElement file = (IProgramElement) model.getHierarchy().findElementForSourceFile(filename);
  802. final List nodes = new ArrayList();
  803. HierarchyWalker walker = new HierarchyWalker() {
  804. public void preProcess(IProgramElement node) {
  805. IProgramElement p = (IProgramElement) node;
  806. if (accept(node))
  807. nodes.add(p);
  808. }
  809. };
  810. file.walk(walker);
  811. return (IProgramElement[]) nodes.toArray(new IProgramElement[nodes.size()]);
  812. }
  813. /**
  814. * Rejects anonymous kinds by checking if their name is an integer
  815. */
  816. static private boolean accept(IProgramElement node) {
  817. if (node.getKind().isType()) {
  818. boolean isAnonymous = StructureUtil.isAnonymous(node);
  819. return !node.getParent().getKind().equals(IProgramElement.Kind.METHOD) && !isAnonymous;
  820. } else {
  821. return !node.getKind().equals(IProgramElement.Kind.IMPORT_REFERENCE);
  822. }
  823. }
  824. /**
  825. * TypeSafeEnum for the entries which need to be put in the html doc
  826. */
  827. public static class HtmlRelationshipKind extends TypeSafeEnum {
  828. public HtmlRelationshipKind(String name, int key) {
  829. super(name, key);
  830. }
  831. public static HtmlRelationshipKind read(DataInputStream s) throws IOException {
  832. int key = s.readByte();
  833. switch (key) {
  834. case 1:
  835. return ADVISES;
  836. case 2:
  837. return ADVISED_BY;
  838. case 3:
  839. return MATCHED_BY;
  840. case 4:
  841. return MATCHES_DECLARE;
  842. case 5:
  843. return DECLARED_ON;
  844. case 6:
  845. return ASPECT_DECLARATIONS;
  846. case 7:
  847. return SOFTENS;
  848. case 8:
  849. return SOFTENED_BY;
  850. case 9:
  851. return ANNOTATES;
  852. case 10:
  853. return ANNOTATED_BY;
  854. case 11:
  855. return USES_POINTCUT;
  856. case 12:
  857. return POINTCUT_USED_BY;
  858. }
  859. throw new Error("weird relationship kind " + key);
  860. }
  861. public static final HtmlRelationshipKind ADVISES = new HtmlRelationshipKind("&nbsp;Advises:", 1);
  862. public static final HtmlRelationshipKind ADVISED_BY = new HtmlRelationshipKind("&nbsp;Advised&nbsp;by:", 2);
  863. public static final HtmlRelationshipKind MATCHED_BY = new HtmlRelationshipKind("&nbsp;Matched&nbsp;by:", 3);
  864. public static final HtmlRelationshipKind MATCHES_DECLARE = new HtmlRelationshipKind("&nbsp;Matches&nbsp;declare:", 4);
  865. public static final HtmlRelationshipKind DECLARED_ON = new HtmlRelationshipKind("&nbsp;Declared&nbsp;on:", 5);
  866. public static final HtmlRelationshipKind ASPECT_DECLARATIONS = new HtmlRelationshipKind("&nbsp;Aspect&nbsp;declarations:",
  867. 6);
  868. public static final HtmlRelationshipKind SOFTENS = new HtmlRelationshipKind("&nbsp;Softens:", 7);
  869. public static final HtmlRelationshipKind SOFTENED_BY = new HtmlRelationshipKind("&nbsp;Softened&nbsp;by:", 8);
  870. public static final HtmlRelationshipKind ANNOTATES = new HtmlRelationshipKind("&nbsp;Annotates:", 9);
  871. public static final HtmlRelationshipKind ANNOTATED_BY = new HtmlRelationshipKind("&nbsp;Annotated&nbsp;by:", 10);
  872. public static final HtmlRelationshipKind USES_POINTCUT = new HtmlRelationshipKind("&nbsp;Uses&nbsp;pointcut:", 11);
  873. public static final HtmlRelationshipKind POINTCUT_USED_BY = new HtmlRelationshipKind("&nbsp;Pointcut&nbsp;used&nbsp;by:",
  874. 12);
  875. }
  876. }