1 2 25 26 package org.aspectj.asm; 27 28 import java.io.File ; 29 import java.util.*; 30 import java.io.Serializable ; 31 32 35 public class StructureModel implements Serializable { 36 37 protected StructureNode root = null; 38 protected String configFile = null; 39 private Map fileMap = null; 40 public static final ProgramElementNode NO_STRUCTURE = new ProgramElementNode("<build to view structure>", ProgramElementNode.Kind.ERROR, null); 41 42 public StructureNode getRoot() { 43 return root; 44 } 45 46 public void setRoot(StructureNode root) { 47 this.root = root; 48 } 49 50 public Map getFileMap() { 51 return fileMap; 52 } 53 54 55 public void setFileMap(HashMap fileMap) { 56 this.fileMap = fileMap; 57 } 58 59 60 public boolean isValid() { 61 return root != null && fileMap != null; 62 } 63 64 68 public StructureNode findRootNodeForSourceFile(String sourceFilePath) { 69 if (!isValid() || sourceFilePath == null) { 70 return StructureModel.NO_STRUCTURE; 71 } else { 72 String correctedPath = sourceFilePath.replace('\\', '/'); 73 StructureNode node = (StructureNode)getFileMap().get(correctedPath); if (node != null) { 75 return node; 76 } else { 77 return createFileStructureNode(sourceFilePath); 78 } 79 } 80 } 81 82 89 public StructureNode findNodeForSourceLine(String sourceFilePath, int lineNumber) { 90 String correctedPath = sourceFilePath.replace('\\', '/'); 91 StructureNode node = findNodeForSourceLineHelper(root, correctedPath, lineNumber); 92 if (node != null) { 93 return node; 94 } else { 95 return createFileStructureNode(sourceFilePath); 96 } 97 } 98 99 private StructureNode createFileStructureNode(String sourceFilePath) { 100 String fileName = new File (sourceFilePath).getName(); 101 ProgramElementNode fileNode = new ProgramElementNode(fileName, ProgramElementNode.Kind.FILE_JAVA, null); 102 fileNode.setSourceLocation(new SourceLocation(sourceFilePath, 1, 1)); 103 fileNode.addChild(NO_STRUCTURE); 104 return fileNode; 105 } 106 107 108 private StructureNode findNodeForSourceLineHelper(StructureNode node, String sourceFilePath, int lineNumber) { 109 if (matches(node, sourceFilePath, lineNumber) 110 && !hasMoreSpecificChild(node, sourceFilePath, lineNumber)) { 111 return node; 112 } 113 114 if (node != null && node.getChildren() != null) { 115 for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) { 116 StructureNode foundNode = findNodeForSourceLineHelper( 117 (StructureNode)it.next(), 118 sourceFilePath, 119 lineNumber); 120 if (foundNode != null) return foundNode; 121 } 122 } 123 124 return null; 125 } 126 127 private boolean matches(StructureNode node, String sourceFilePath, int lineNumber) { 128 return node != null 129 && node.getSourceLocation() != null 130 && node.getSourceLocation().getSourceFilePath().equals(sourceFilePath) 131 && ((node.getSourceLocation().getLineNumber() <= lineNumber 132 && node.getSourceLocation().getEndLineNumber() >= lineNumber) 133 || 134 (lineNumber <= 1 135 && node instanceof ProgramElementNode 136 && ((ProgramElementNode)node).getProgramElementKind().isSourceFileKind()) 137 ); 138 } 139 140 private boolean hasMoreSpecificChild(StructureNode node, String sourceFilePath, int lineNumber) { 141 for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) { 142 ProgramElementNode child = (ProgramElementNode)it.next(); 143 if (matches(child, sourceFilePath, lineNumber)) return true; 144 } 145 return false; 146 } 147 148 public String getConfigFile() { 149 return configFile; 150 } 151 152 public void setConfigFile(String configFile) { 153 this.configFile = configFile; 154 } 155 156 } 157 158 | Popular Tags |