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.

BuildConfigModel.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * ******************************************************************/
  13. package org.aspectj.ajde.ui;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.StringTokenizer;
  18. /**
  19. * TODO: we have schitzophrenia between BuildConfigNode(s) and IProgramElement(s), fix.
  20. *
  21. * @author Mik Kersten
  22. */
  23. public class BuildConfigModel {
  24. private BuildConfigNode root = null;
  25. private String sourceFile;
  26. public BuildConfigModel(String sourceFile) {
  27. this.sourceFile = sourceFile;
  28. }
  29. /**
  30. * @param path java.io.File.separator delimited path
  31. * @return corresponding node if the path is found, the root otherwise
  32. */
  33. public BuildConfigNode getNodeForPath(String path) {
  34. BuildConfigNode upPathMatch = searchUpPaths(path);
  35. if (upPathMatch != null && upPathMatch != root) {
  36. return upPathMatch;
  37. } else {
  38. StringTokenizer st = new StringTokenizer(path, "/");
  39. return getNodeForPathHelper(st, root);
  40. }
  41. }
  42. private BuildConfigNode searchUpPaths(String path) {
  43. for (BuildConfigNode node : root.getChildren()) {
  44. if (node.getName().equals(path))
  45. return node;
  46. }
  47. return null;
  48. }
  49. private BuildConfigNode getNodeForPathHelper(StringTokenizer st, BuildConfigNode node) {
  50. BuildConfigNode parent = node;
  51. while (st.hasMoreElements()) {
  52. String pathItem = st.nextToken();
  53. for (BuildConfigNode element : node.getChildren()) {
  54. node = element;
  55. String childName = node.getName();
  56. if (childName.equals(pathItem)) {
  57. return getNodeForPathHelper(st, node);
  58. }
  59. }
  60. }
  61. return parent;
  62. }
  63. public List<BuildConfigNode> getActiveNodes(BuildConfigNode.Kind kind) {
  64. List<BuildConfigNode> nodes = new ArrayList<BuildConfigNode>();
  65. getActiveNodesHelper(root, kind, nodes);
  66. return nodes;
  67. }
  68. private void getActiveNodesHelper(BuildConfigNode node, BuildConfigNode.Kind kind, List<BuildConfigNode> nodes) {
  69. for (BuildConfigNode currNode : node.getChildren()) {
  70. if (currNode.getBuildConfigNodeKind().equals(kind) && currNode.isActive()) {
  71. nodes.add(currNode);
  72. }
  73. getActiveNodesHelper(currNode, kind, nodes);
  74. }
  75. }
  76. public String getSourceFile() {
  77. return sourceFile;
  78. }
  79. public void setSourceFile(String sourceFile) {
  80. this.sourceFile = sourceFile;
  81. }
  82. public BuildConfigNode getRoot() {
  83. return root;
  84. }
  85. public void setRoot(BuildConfigNode node) {
  86. root = node;
  87. }
  88. public BuildConfigNode findNodeForSourceLine(String sourceFilePath, int lineNumber) {
  89. BuildConfigNode node = findNodeForSourceLineHelper(root, sourceFilePath, lineNumber);
  90. return node;
  91. }
  92. private BuildConfigNode findNodeForSourceLineHelper(BuildConfigNode node, String sourceFilePath, int lineNumber) {
  93. if (matches(node, sourceFilePath, lineNumber) && !hasMoreSpecificChild(node, sourceFilePath, lineNumber)) {
  94. return node;
  95. }
  96. if (node != null && node.getChildren() != null) {
  97. for (Object element : node.getChildren()) {
  98. BuildConfigNode foundNode = findNodeForSourceLineHelper((BuildConfigNode) element, sourceFilePath, lineNumber);
  99. if (foundNode != null)
  100. return foundNode;
  101. }
  102. }
  103. return null;
  104. }
  105. private boolean matches(BuildConfigNode node, String sourceFilePath, int lineNumber) {
  106. try {
  107. return node != null
  108. && node.getSourceLocation() != null
  109. && node.getSourceLocation().getSourceFile().getCanonicalPath().equals(sourceFilePath)
  110. && ((node.getSourceLocation().getLine() <= lineNumber && node.getSourceLocation().getEndLine() >= lineNumber) || (lineNumber <= 1));
  111. } catch (IOException ioe) {
  112. return false;
  113. }
  114. }
  115. private boolean hasMoreSpecificChild(BuildConfigNode node, String sourceFilePath, int lineNumber) {
  116. for (BuildConfigNode child : node.getChildren()) {
  117. if (matches(child, sourceFilePath, lineNumber)) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. }