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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 (String string : requiredStrings) {
  60. if (!containsString(htmlFile, string)) {
  61. missingStrings.add(string);
  62. }
  63. }
  64. return missingStrings;
  65. }
  66. /**
  67. * Checks whether the section of the html file contains the
  68. * required String
  69. *
  70. * @param htmlFile
  71. * @param requiredString
  72. * @param sectionHeader
  73. * @return true if the file contains the given string within the
  74. * required section or false otherwise (or if the file is null or
  75. * not an html file)
  76. * @throws Exception
  77. */
  78. public static boolean containsStringWithinSection(File htmlFile,
  79. String requiredString, String sectionHeader) throws Exception {
  80. if ((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html")) {
  81. return false;
  82. }
  83. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  84. String line = reader.readLine();
  85. while (line != null) {
  86. if (line.indexOf(sectionHeader) != -1) {
  87. String nextLine = reader.readLine();
  88. while (nextLine != null &&
  89. (nextLine.indexOf("========") == -1)) {
  90. if (nextLine.indexOf(requiredString) != -1) {
  91. reader.close();
  92. return true;
  93. }
  94. nextLine = reader.readLine();
  95. }
  96. reader.close();
  97. return false;
  98. }
  99. line = reader.readLine();
  100. }
  101. reader.close();
  102. return false;
  103. }
  104. /**
  105. * Returns those strings from the given array which aren't in the
  106. * ajdoc html file
  107. *
  108. * @param htmlFile
  109. * @param an array of requiredStrings
  110. * @param sectionHeader
  111. * @return List of those requiredStrings not found
  112. * @throws Exception
  113. */
  114. public static List<String> getMissingStringsInSection(File htmlFile,
  115. String[] requiredStrings, String sectionHeader) throws Exception {
  116. List<String> missingStrings = new ArrayList<String>();
  117. for (String string : requiredStrings) {
  118. if (!containsStringWithinSection(htmlFile, string, sectionHeader)) {
  119. missingStrings.add(string);
  120. }
  121. }
  122. return missingStrings;
  123. }
  124. /**
  125. * Returns whether the class data section has the expected
  126. * relationship and target i.e. have the relationships been
  127. * applied to the type.
  128. *
  129. * @param the ajdoc html file
  130. * @param the detail sectionHeader, for example "DECLARE DETAIL SUMMARY"
  131. * @param the source of the relationship, for example "Point()"
  132. * @param the relationship, for example HtmlDecorator.HtmlRelationshipKind.MATCHED_BY
  133. * @param the expected target, for example "HREF=\"../foo/Main.html#doIt()\""
  134. * @return true if the section contains the expected source/relationship/target,
  135. * false otherwise
  136. */
  137. public static boolean classDataSectionContainsRel(File htmlFile,
  138. HtmlDecorator.HtmlRelationshipKind relationship,
  139. String target) throws Exception {
  140. if (((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html"))) {
  141. return false;
  142. }
  143. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  144. String line = reader.readLine();
  145. while (line != null) {
  146. if (line.indexOf("START OF CLASS DATA") != -1) {
  147. // found the required class data section
  148. String subLine = reader.readLine();
  149. while(subLine != null
  150. && (subLine.indexOf("========") == -1)){
  151. int relIndex = subLine.indexOf(relationship.toString());
  152. int targetIndex = subLine.indexOf(target);
  153. if ((relIndex != -1) && (targetIndex != -1)) {
  154. reader.close();
  155. if (relIndex < targetIndex) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. subLine = reader.readLine();
  161. }
  162. reader.close();
  163. return false;
  164. }
  165. line = reader.readLine();
  166. }
  167. reader.close();
  168. return false;
  169. }
  170. /**
  171. * Returns whether the supplied source has the expected
  172. * relationship and target within the given detail section
  173. *
  174. * @param the ajdoc html file
  175. * @param the detail sectionHeader, for example "DECLARE DETAIL SUMMARY"
  176. * @param the source of the relationship, for example "Point()"
  177. * @param the relationship, for example HtmlDecorator.HtmlRelationshipKind.MATCHED_BY
  178. * @param the expected target, for example "HREF=\"../foo/Main.html#doIt()\""
  179. * @return true if the section contains the expected source/relationship/target,
  180. * false otherwise
  181. */
  182. public static boolean detailSectionContainsRel(File htmlFile,
  183. String sectionHeader, String source,
  184. HtmlDecorator.HtmlRelationshipKind relationship,
  185. String target) throws Exception {
  186. if (((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html"))) {
  187. return false;
  188. }
  189. if (sectionHeader.indexOf("DETAIL") == -1) {
  190. return false;
  191. }
  192. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  193. String line = reader.readLine();
  194. while (line != null) {
  195. if (line.indexOf(sectionHeader) != -1) {
  196. // found the required main section
  197. String nextLine = reader.readLine();
  198. while (nextLine != null && (nextLine.indexOf("========") == -1)) {
  199. // On JDK11 it looks like <a id="doIt()"> on earlier JDKs it can look like <a name="doit">
  200. if ((LangUtil.is11VMOrGreater() && nextLine.indexOf("ID=\""+source+"\"") != -1 || nextLine.indexOf("id=\""+source+"\"") != -1) ||
  201. nextLine.indexOf("NAME=\""+source+"\"") != -1 || nextLine.indexOf("name=\""+source+"\"") != -1) {
  202. // found the required subsection
  203. String subLine = reader.readLine();
  204. while(subLine != null
  205. && (subLine.indexOf("========") == -1)
  206. && (subLine.indexOf("NAME") == -1 && subLine.indexOf("name") == -1)) {
  207. int relIndex = subLine.indexOf(relationship.toString());
  208. int targetIndex = subLine.indexOf(target);
  209. if ((relIndex != -1) && (targetIndex != -1)) {
  210. reader.close();
  211. if (relIndex < targetIndex) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. subLine = reader.readLine();
  217. }
  218. reader.close();
  219. return false;
  220. }
  221. nextLine = reader.readLine();
  222. }
  223. reader.close();
  224. return false;
  225. }
  226. line = reader.readLine();
  227. }
  228. reader.close();
  229. return false;
  230. }
  231. /**
  232. * Returns whether the supplied source has the expected
  233. * relationship and target within the given summary section
  234. *
  235. * @param the ajdoc html file
  236. * @param the detail sectionHeader, for example "DECLARE SUMMARY"
  237. * @param the source of the relationship, for example "Point()"
  238. * @param the relationship, for example HtmlDecorator.HtmlRelationshipKind.MATCHED_BY
  239. * @param the expected target, for example "HREF=\"../foo/Main.html#doIt()\""
  240. * @return true if the section contains the expected source/relationship/target,
  241. * false otherwise
  242. */
  243. public static boolean summarySectionContainsRel(
  244. File htmlFile,
  245. String sectionHeader,
  246. String source,
  247. HtmlDecorator.HtmlRelationshipKind relationship,
  248. String target) throws Exception {
  249. if (((htmlFile == null) || !htmlFile.getAbsolutePath().endsWith("html"))) {
  250. return false;
  251. }
  252. if (sectionHeader.indexOf("SUMMARY") == -1) {
  253. return false;
  254. }
  255. BufferedReader reader = new BufferedReader(new FileReader(htmlFile));
  256. String line = reader.readLine();
  257. while (line != null) {
  258. if (line.indexOf(sectionHeader) != -1) {
  259. // found the required main section
  260. String nextLine = reader.readLine();
  261. while (nextLine != null && (nextLine.indexOf("========") == -1)) {
  262. if (nextLine.indexOf(source) != -1) {
  263. // found the required subsection
  264. String subLine = nextLine;
  265. while(subLine != null
  266. && (subLine.indexOf("========") == -1)
  267. && (subLine.indexOf("<TR BGCOLOR=\"white\" CLASS=\"TableRowColor\">") == -1)) {
  268. int relIndex = subLine.indexOf(relationship.toString());
  269. int targetIndex = subLine.indexOf(target);
  270. if ((relIndex != -1) && (targetIndex != -1)) {
  271. reader.close();
  272. if (relIndex < targetIndex) {
  273. return true;
  274. }
  275. return false;
  276. }
  277. subLine = reader.readLine();
  278. }
  279. reader.close();
  280. return false;
  281. }
  282. nextLine = reader.readLine();
  283. }
  284. reader.close();
  285. return false;
  286. }
  287. line = reader.readLine();
  288. }
  289. reader.close();
  290. return false;
  291. }
  292. }