KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > asm > StructureModel


1
2 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3  *
4  * This file is part of the IDE support for the AspectJ(tm)
5  * programming language; see http://aspectj.org
6  *
7  * The contents of this file are subject to the Mozilla Public License
8  * Version 1.1 (the "License"); you may not use this file except in
9  * compliance with the License. You may obtain a copy of the License at
10  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is AspectJ.
18  *
19  * The Initial Developer of the Original Code is Xerox Corporation. Portions
20  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
21  * All Rights Reserved.
22  *
23  * Contributor(s):
24  */

25
26 package org.aspectj.asm;
27
28 import java.io.File JavaDoc;
29 import java.util.*;
30 import java.io.Serializable JavaDoc;
31
32 /**
33  * @author Mik Kersten
34  */

35 public class StructureModel implements Serializable JavaDoc {
36     
37     protected StructureNode root = null;
38     protected String JavaDoc 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     /**
65      * @param sourceFilePath modified to '/' delimited path for consistency
66      * @return a new structure node for the file if it was not found in the model
67      */

68     public StructureNode findRootNodeForSourceFile(String JavaDoc sourceFilePath) {
69         if (!isValid() || sourceFilePath == null) {
70             return StructureModel.NO_STRUCTURE;
71         } else {
72             String JavaDoc correctedPath = sourceFilePath.replace('\\', '/');
73             StructureNode node = (StructureNode)getFileMap().get(correctedPath);//findFileNode(filePath, model);
74
if (node != null) {
75                 return node;
76             } else {
77                 return createFileStructureNode(sourceFilePath);
78             }
79         }
80     }
81
82     /**
83      * Never returns null
84      *
85      * @param sourceFilePath modified to '/' delimited path for consistency
86      * @param lineNumber if 0 or 1 the corresponding file node will be returned
87      * @return a new structure node for the file if it was not found in the model
88      */

89     public StructureNode findNodeForSourceLine(String JavaDoc sourceFilePath, int lineNumber) {
90         String JavaDoc 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 JavaDoc sourceFilePath) {
100         String JavaDoc fileName = new File JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getConfigFile() {
149         return configFile;
150     }
151
152     public void setConfigFile(String JavaDoc configFile) {
153         this.configFile = configFile;
154     }
155
156 }
157
158
Popular Tags