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.

AjdocOutputChecker.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /********************************************************************
  2. * Copyright (c) 2005 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution and is available at
  6. * http://eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - iniital version
  10. *******************************************************************/
  11. package org.aspectj.tools.ajdoc;
  12. import java.io.BufferedReader;
  13. import java.io.File;
  14. import java.io.FileReader;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import org.aspectj.util.LangUtil;
  18. /**
  19. * Helper class to check whether the ajdoc contains the expected
  20. * information.
  21. */
  22. public class AjdocOutputChecker {
  23. /**
  24. * Checks whether the given html file contains the required String.
  25. *
  26. * @param htmlFile
  27. * @param requiredString
  28. * @return true if the file contains the given string or
  29. * false otherwise (or if the file is null or not an html file)
  30. * @throws Exception
  31. */
  32. public static boolean containsString(File htmlFile,
  33. String requiredString) throws Exception {
  34. if ((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html")) {
  35. return false;
  36. }
  37. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  38. String line = reader.readLine();
  39. while (line != null) {
  40. if (line.indexOf(requiredString) != -1) {
  41. reader.close();
  42. return true;
  43. }
  44. line = reader.readLine();
  45. }
  46. reader.close();
  47. return false;
  48. }
  49. /**
  50. * Returns those strings from the given array which aren't in the html file.
  51. *
  52. * @param htmlFile
  53. * @param an array of requiredStrings
  54. * @return a List of those strings not found
  55. * @throws Exception
  56. */
  57. public static List<String> getMissingStringsInFile(File htmlFile, String[] requiredStrings) throws Exception {
  58. List<String> missingStrings = new ArrayList<String>();
  59. for (int i = 0; i < requiredStrings.length; i++) {
  60. String string = requiredStrings[i];
  61. if (!containsString(htmlFile, string)) {
  62. missingStrings.add(string);
  63. }
  64. }
  65. return missingStrings;
  66. }
  67. /**
  68. * Checks whether the section of the html file contains the
  69. * required String
  70. *
  71. * @param htmlFile
  72. * @param requiredString
  73. * @param sectionHeader
  74. * @return true if the file contains the given string within the
  75. * required section or false otherwise (or if the file is null or
  76. * not an html file)
  77. * @throws Exception
  78. */
  79. public static boolean containsStringWithinSection(File htmlFile,
  80. String requiredString, String sectionHeader) throws Exception {
  81. if ((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html")) {
  82. return false;
  83. }
  84. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  85. String line = reader.readLine();
  86. while (line != null) {
  87. if (line.indexOf(sectionHeader) != -1) {
  88. String nextLine = reader.readLine();
  89. while (nextLine != null &&
  90. (nextLine.indexOf("========") == -1)) {
  91. if (nextLine.indexOf(requiredString) != -1) {
  92. reader.close();
  93. return true;
  94. }
  95. nextLine = reader.readLine();
  96. }
  97. reader.close();
  98. return false;
  99. }
  100. line = reader.readLine();
  101. }
  102. reader.close();
  103. return false;
  104. }
  105. /**
  106. * Returns those strings from the given array which aren't in the
  107. * ajdoc html file
  108. *
  109. * @param htmlFile
  110. * @param an array of requiredStrings
  111. * @param sectionHeader
  112. * @return List of those requiredStrings not found
  113. * @throws Exception
  114. */
  115. public static List /*String*/ getMissingStringsInSection(File htmlFile,
  116. String[] requiredStrings, String sectionHeader) throws Exception {
  117. List missingStrings = new ArrayList();
  118. for (int i = 0; i < requiredStrings.length; i++) {
  119. String string = requiredStrings[i];
  120. if (!containsStringWithinSection(htmlFile,string,sectionHeader)) {
  121. missingStrings.add(string);
  122. }
  123. }
  124. return missingStrings;
  125. }
  126. /**
  127. * Returns whether the class data section has the expected
  128. * relationship and target i.e. have the relationships been
  129. * applied to the type.
  130. *
  131. * @param the ajdoc html file
  132. * @param the detail sectionHeader, for example "DECLARE DETAIL SUMMARY"
  133. * @param the source of the relationship, for example "Point()"
  134. * @param the relationship, for example HtmlDecorator.HtmlRelationshipKind.MATCHED_BY
  135. * @param the expected target, for example "HREF=\"../foo/Main.html#doIt()\""
  136. * @return true if the section contains the expected source/relationship/target,
  137. * false otherwise
  138. */
  139. public static boolean classDataSectionContainsRel(File htmlFile,
  140. HtmlDecorator.HtmlRelationshipKind relationship,
  141. String target) throws Exception {
  142. if (((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html"))) {
  143. return false;
  144. }
  145. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  146. String line = reader.readLine();
  147. while (line != null) {
  148. if (line.indexOf("START OF CLASS DATA") != -1) {
  149. // found the required class data section
  150. String subLine = reader.readLine();
  151. while(subLine != null
  152. && (subLine.indexOf("========") == -1)){
  153. int relIndex = subLine.indexOf(relationship.toString());
  154. int targetIndex = subLine.indexOf(target);
  155. if ((relIndex != -1) && (targetIndex != -1)) {
  156. reader.close();
  157. if (relIndex < targetIndex) {
  158. return true;
  159. }
  160. return false;
  161. }
  162. subLine = reader.readLine();
  163. }
  164. reader.close();
  165. return false;
  166. }
  167. line = reader.readLine();
  168. }
  169. reader.close();
  170. return false;
  171. }
  172. /**
  173. * Returns whether the supplied source has the expected
  174. * relationship and target within the given detail section
  175. *
  176. * @param the ajdoc html file
  177. * @param the detail sectionHeader, for example "DECLARE DETAIL SUMMARY"
  178. * @param the source of the relationship, for example "Point()"
  179. * @param the relationship, for example HtmlDecorator.HtmlRelationshipKind.MATCHED_BY
  180. * @param the expected target, for example "HREF=\"../foo/Main.html#doIt()\""
  181. * @return true if the section contains the expected source/relationship/target,
  182. * false otherwise
  183. */
  184. public static boolean detailSectionContainsRel(File htmlFile,
  185. String sectionHeader, String source,
  186. HtmlDecorator.HtmlRelationshipKind relationship,
  187. String target) throws Exception {
  188. if (((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html"))) {
  189. return false;
  190. }
  191. if (sectionHeader.indexOf("DETAIL") == -1) {
  192. return false;
  193. }
  194. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  195. String line = reader.readLine();
  196. while (line != null) {
  197. if (line.indexOf(sectionHeader) != -1) {
  198. // found the required main section
  199. String nextLine = reader.readLine();
  200. while (nextLine != null && (nextLine.indexOf("========") == -1)) {
  201. // On JDK11 it looks like <a id="doIt()"> on earlier JDKs it can look like <a name="doit">
  202. if ((LangUtil.is11VMOrGreater() && nextLine.indexOf("ID=\""+source+"\"") != -1 || nextLine.indexOf("id=\""+source+"\"") != -1) ||
  203. nextLine.indexOf("NAME=\""+source+"\"") != -1 || nextLine.indexOf("name=\""+source+"\"") != -1) {
  204. // found the required subsection
  205. String subLine = reader.readLine();
  206. while(subLine != null
  207. && (subLine.indexOf("========") == -1)
  208. && (subLine.indexOf("NAME") == -1 && subLine.indexOf("name") == -1)) {
  209. int relIndex = subLine.indexOf(relationship.toString());
  210. int targetIndex = subLine.indexOf(target);
  211. if ((relIndex != -1) && (targetIndex != -1)) {
  212. reader.close();
  213. if (relIndex < targetIndex) {
  214. return true;
  215. }
  216. return false;
  217. }
  218. subLine = reader.readLine();
  219. }
  220. reader.close();
  221. return false;
  222. }
  223. nextLine = reader.readLine();
  224. }
  225. reader.close();
  226. return false;
  227. }
  228. line = reader.readLine();
  229. }
  230. reader.close();
  231. return false;
  232. }
  233. /**
  234. * Returns whether the supplied source has the expected
  235. * relationship and target within the given summary section
  236. *
  237. * @param the ajdoc html file
  238. * @param the detail sectionHeader, for example "DECLARE SUMMARY"
  239. * @param the source of the relationship, for example "Point()"
  240. * @param the relationship, for example HtmlDecorator.HtmlRelationshipKind.MATCHED_BY
  241. * @param the expected target, for example "HREF=\"../foo/Main.html#doIt()\""
  242. * @return true if the section contains the expected source/relationship/target,
  243. * false otherwise
  244. */
  245. public static boolean summarySectionContainsRel(
  246. File htmlFile,
  247. String sectionHeader,
  248. String source,
  249. HtmlDecorator.HtmlRelationshipKind relationship,
  250. String target) throws Exception {
  251. if (((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html"))) {
  252. return false;
  253. }
  254. if (sectionHeader.indexOf("SUMMARY") == -1) {
  255. return false;
  256. }
  257. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  258. String line = reader.readLine();
  259. while (line != null) {
  260. if (line.indexOf(sectionHeader) != -1) {
  261. // found the required main section
  262. String nextLine = reader.readLine();
  263. while (nextLine != null && (nextLine.indexOf("========") == -1)) {
  264. if (nextLine.indexOf(source) != -1) {
  265. // found the required subsection
  266. String subLine = nextLine;
  267. while(subLine != null
  268. && (subLine.indexOf("========") == -1)
  269. && (subLine.indexOf("<TR BGCOLOR=\"white\" CLASS=\"TableRowColor\">") == -1)) {
  270. int relIndex = subLine.indexOf(relationship.toString());
  271. int targetIndex = subLine.indexOf(target);
  272. if ((relIndex != -1) && (targetIndex != -1)) {
  273. reader.close();
  274. if (relIndex < targetIndex) {
  275. return true;
  276. }
  277. return false;
  278. }
  279. subLine = reader.readLine();
  280. }
  281. reader.close();
  282. return false;
  283. }
  284. nextLine = reader.readLine();
  285. }
  286. reader.close();
  287. return false;
  288. }
  289. line = reader.readLine();
  290. }
  291. reader.close();
  292. return false;
  293. }
  294. }