KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > asm > views > EmacsViewManager


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.views;
27
28 import java.util.*;
29 import java.io.*;
30 //import org.aspectj.tools.ide.*;
31
import org.aspectj.asm.associations.*;
32 import org.aspectj.asm.*;
33 import org.aspectj.compiler.crosscuts.AspectJCompiler;
34 //import org.aspectj.ajde.compiler.AjdeCompiler;
35

36 /**
37  * @author Mik Kersten
38  */

39 public class EmacsViewManager extends StructureModelManager {
40
41     private static final String JavaDoc EXTERN_FILE_SUFFIX = ".ajesym";
42
43     public EmacsViewManager() {
44         super();
45     }
46
47     public void externalizeStructureView(final AspectJCompiler ajdeCompiler) {
48         super.buildStructureModel(ajdeCompiler);
49         if (!model.isValid()) return;
50         try {
51             Set fileSet = model.getFileMap().entrySet();
52             for (Iterator it = fileSet.iterator(); it.hasNext(); ) {
53                 ProgramElementNode peNode = (ProgramElementNode)((Map.Entry)it.next()).getValue();
54                 dumpStructureToFile(peNode);
55             }
56         } catch (IOException ioe) {
57             ioe.printStackTrace();
58         }
59     }
60
61 // private void dumpStructureToFile(ProgramElementNode node) throws IOException {
62
// String sourceName = node.getSourceLocation().getSourceFilePath();
63
// String fileName = sourceName.substring(0, sourceName.lastIndexOf(".")) + EXTERN_FILE_SUFFIX;
64
// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName)));
65
// new SExpressionPrinter(writer).printDecls(node);
66
// writer.flush();
67
// }
68

69     private void dumpStructureToFile(ProgramElementNode node) throws IOException {
70         String JavaDoc sourceName = node.getSourceLocation().getSourceFilePath();
71         String JavaDoc fileName = sourceName.substring(0, sourceName.lastIndexOf(".")) + EXTERN_FILE_SUFFIX;
72         BufferedWriter writer = null;
73         try {
74             writer = new BufferedWriter(new FileWriter(new File(fileName)));
75             new SExpressionPrinter(writer).printDecls(node);
76             writer.flush();
77         } finally {
78             if (writer != null) {
79                 try {
80                     writer.close();
81                 } catch (IOException e) {} // ignore
82
}
83         }
84     }
85
86     /**
87      * This class was not written in an OO style.
88      */

89     private static class SExpressionPrinter {
90
91         private BufferedWriter writer = null;
92
93         public SExpressionPrinter(BufferedWriter writer) {
94             this.writer = writer;
95         }
96
97         private void printDecls(ProgramElementNode node) {
98             print("(");
99             for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
100                 // this ignores relations on the compile unit
101
Object JavaDoc nodeObject = it.next();
102                 if (nodeObject instanceof ProgramElementNode) {
103                     ProgramElementNode child = (ProgramElementNode)nodeObject;
104                     printDecl(child, true);
105                 } else if (nodeObject instanceof LinkNode) {
106                     LinkNode child = (LinkNode)nodeObject;
107                     printDecl(child.getProgramElementNode(), false);
108                 }
109             }
110             print(") ");
111         }
112
113         private void printDecls(RelationNode node) {
114             for (Iterator it = node.getChildren().iterator(); it.hasNext(); ) {
115                 // this ignores relations on the compile unit
116
Object JavaDoc nodeObject = it.next();
117                 if (nodeObject instanceof LinkNode) {
118                     LinkNode child = (LinkNode)nodeObject;
119                     if (//!child.getProgramElementNode().getKind().equals("stmnt") &&
120
!child.getProgramElementNode().getKind().equals("<undefined>")) {
121                         printDecl(child.getProgramElementNode(), false);
122 // printDecl(child.getProgramElementNode(), false);
123
}
124                 }
125             }
126         }
127
128         /**
129          * @param structureNode can be a ProgramElementNode or a LinkNode
130          */

131         private void printDecl(ProgramElementNode node, boolean recurse) {
132             String JavaDoc kind = node.getKind().toLowerCase();
133             print("(");
134             print("(" + node.getSourceLocation().getLineNumber() + " . " + node.getSourceLocation().getColumnNumber() + ") ");
135             print("(" + node.getSourceLocation().getLineNumber() + " . " + node.getSourceLocation().getColumnNumber() + ") ");
136             print(kind + " "); //2
137

138             // HACK:
139
String JavaDoc displayName = node.toString().replace('\"', ' ');
140
141             print("\"" + displayName + "\" ");
142             if (node.getSourceLocation().getSourceFilePath() != null) {
143                 print("\"" + fixFilename(node.getSourceLocation().getSourceFilePath()) + "\""); //4
144
} else {
145                 print("nil");
146             }
147             if (node.getSignature() != null) {
148                 print("\"" + node.getDeclaringType() + "\" "); //5
149
} else {
150                 print("nil");
151             }
152
153             if (!recurse) {
154                 print("nil");
155                 print("nil");
156                 print("nil");
157             } else {
158                 print("(");
159                 if (node instanceof ProgramElementNode) {
160                     java.util.List JavaDoc relations = ((ProgramElementNode)node).getRelations();
161                     if (relations != null) {
162                         for (Iterator it = relations.iterator(); it.hasNext(); ) {
163                             RelationNode relNode = (RelationNode)it.next();
164                             if (relNode.getRelation().getAssociationName().equals(Advice.NAME) ||
165                                 relNode.getRelation().getAssociationName().equals(Introduction.NAME)) {
166                                 printDecls(relNode); // 6
167
}
168                         }
169                     }
170                 }
171                 print(") ");
172                 print("(");
173                 print(") ");
174                 print("(");
175                 Iterator it3 = node.getChildren().iterator();
176                 if (it3.hasNext()) {
177                     while (it3.hasNext()) {
178                         // this ignores relations on the compile unit
179
Object JavaDoc nodeObject = it3.next();
180                         if (nodeObject instanceof ProgramElementNode) {
181                             ProgramElementNode currNode = (ProgramElementNode)nodeObject;
182                             if (//!currNode.isStmntKind() &&
183
!currNode.getKind().equals("<undefined>")) {
184                                 printDecl(currNode, true);
185                             }
186                         }
187                     }
188                 }
189                 print(") ");
190             }
191
192             print(node.getKind().equals("class") ? "t " : "nil "); // 9
193
// print(node.getKind().equals("introduction") ? "t " : "nil "); // 10
194
print(node.getKind().equals("introduction") ? "nil " : "nil "); // 10
195
print("nil "); // 11
196
print("nil "); // 12
197
print(")");
198         }
199
200         String JavaDoc fixFilename(String JavaDoc filename) {
201             return subst("\\\\", "\\", filename);
202         }
203
204         private void print(String JavaDoc string) {
205             try {
206                 writer.write(string + "\n");
207             } catch (IOException ioe) {
208                 ioe.printStackTrace();
209             }
210         }
211
212         private String JavaDoc subst(String JavaDoc n, String JavaDoc o, String JavaDoc in) {
213             int pos = in.indexOf(o);
214             if (pos == -1)
215                 return in;
216             return in.substring(0, pos) +
217                    n +
218                    subst(n, o, (in.substring(pos + o.length())));
219         }
220
221         private void lose(Error JavaDoc e) {
222             try {
223                 print("(ERROR \"" + e.toString() + "\")");
224             }
225             catch(Error JavaDoc ex) { }
226         }
227     }
228 }
229
230
Popular Tags