]> source.dussan.org Git - aspectj.git/blob
ed0b9aa69dd178fac85b1f13287fdacd04e7ca01
[aspectj.git] /
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 v 2.0
7  * which accompanies this distribution and is available at
8  * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
9  *
10  * Contributors:
11  *     PARC     initial implementation
12  * ******************************************************************/
13
14 package org.aspectj.ajdt.internal.core.builder;
15
16 import java.io.BufferedWriter;
17 import java.io.File;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Set;
23
24 import org.aspectj.asm.AsmManager;
25 import org.aspectj.asm.IProgramElement;
26
27 /**
28  * @author Mik Kersten
29  */
30 public class EmacsStructureModelManager {
31
32         private static final String EXTERN_FILE_SUFFIX = ".ajesym";
33
34         public EmacsStructureModelManager() {
35                 super();
36         }
37
38         public void externalizeModel(AsmManager model) {
39                 if (!model.getHierarchy().isValid())
40                         return;
41
42                 try {
43                         // Set fileSet = StructureModelManager.INSTANCE.getStructureModel().getFileMap().entrySet();
44                         Set fileSet = model.getHierarchy().getFileMapEntrySet();
45                         for (Object o : fileSet) {
46                                 IProgramElement peNode = (IProgramElement) ((Map.Entry) o).getValue();
47                                 dumpStructureToFile(peNode);
48                         }
49                 } catch (IOException ioe) {
50                         ioe.printStackTrace();
51                 }
52         }
53
54         // private void dumpStructureToFile(ProgramElementNode node) throws IOException {
55         // String sourceName = node.getSourceLocation().getSourceFilePath();
56         // String fileName = sourceName.substring(0, sourceName.lastIndexOf(".")) + EXTERN_FILE_SUFFIX;
57         // BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName)));
58         // new SExpressionPrinter(writer).printDecls(node);
59         // writer.flush();
60         // }
61
62         private void dumpStructureToFile(IProgramElement node) throws IOException {
63                 String s = node.getKind().toString();
64                 if (!(s.equals(IProgramElement.Kind.FILE_ASPECTJ.toString()) || s.equals(IProgramElement.Kind.FILE_JAVA.toString()))) {
65                         throw new IllegalArgumentException("externalize file, not " + node);
66                 }
67                 // source files have source locations
68                 String sourceName = node.getSourceLocation().getSourceFile().getAbsolutePath();
69                 String fileName = sourceName.substring(0, sourceName.lastIndexOf(".")) + EXTERN_FILE_SUFFIX;
70                 BufferedWriter writer = null;
71                 try {
72                         writer = new BufferedWriter(new FileWriter(new File(fileName)));
73                         new SExpressionPrinter(writer).printDecls(node);
74                         writer.flush();
75                 } finally {
76                         if (writer != null) {
77                                 try {
78                                         writer.close();
79                                 } catch (IOException e) {
80                                 } // ignore
81                         }
82                 }
83         }
84
85         /**
86          * This class was not written in an OO style.
87          */
88         private static class SExpressionPrinter {
89
90                 private BufferedWriter writer = null;
91
92                 public SExpressionPrinter(BufferedWriter writer) {
93                         this.writer = writer;
94                 }
95
96                 private void printDecls(IProgramElement node) {
97                         print("(");
98                         for (IProgramElement child : node.getChildren()) {
99                                 // this ignores relations on the compile unit
100                                 // throw new RuntimeException("unimplemented");
101                                 // if (nodeObject instanceof IProgramElement) {
102                                 printDecl(child, true);
103                                 // }
104                                 // else if (nodeObject instanceof LinkNode) {
105                                 // LinkNode child = (LinkNode)nodeObject;
106                                 // printDecl(child.getProgramElementNode(), false);
107                                 // }
108                         }
109                         print(") ");
110                 }
111
112                 // private void printDecls(IRelationship node) {
113                 // // for (Iterator it = node.getTargets().iterator(); it.hasNext(); ) {
114                 // // // this ignores relations on the compile unit
115                 // // Object nodeObject = it.next();
116                 // // throw new RuntimeException("unimplemented");
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(IProgramElement node, boolean recurse) {
132                         if (node == null || node.getSourceLocation() == null)
133                                 return;
134                         String kind = node.getKind().toString().toLowerCase();
135                         print("(");
136                         print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
137                         print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
138                         print(kind + " "); // 2
139
140                         // HACK:
141                         String displayName = node.toString().replace('\"', ' ');
142
143                         print("\"" + displayName + "\" ");
144                         if (node.getSourceLocation().getSourceFile().getAbsolutePath() != null) {
145                                 print("\"" + fixFilename(node.getSourceLocation().getSourceFile().getAbsolutePath()) + "\""); // 4
146                         } else {
147                                 print("nil");
148                         }
149                         if (node.getName() != null) {
150                                 print("\"" + node.getDeclaringType() + "\" "); // 5
151                         } else {
152                                 print("nil");
153                         }
154
155                         if (!recurse) {
156                                 print("nil");
157                                 print("nil");
158                                 print("nil");
159                         } else {
160                                 print("(");
161                                 // if (node instanceof IProgramElement) {
162                                 // java.util.List relations = ((IProgramElement)node).getRelations();
163                                 // if (relations != null) {
164                                 // for (Iterator it = relations.iterator(); it.hasNext(); ) {
165                                 // IRelationship relNode = (IRelationship)it.next();
166                                 // if (relNode.getKind() == IRelationship.Kind.ADVICE ||
167                                 // relNode.getKind() == IRelationship.Kind.DECLARE) {
168                                 // printDecls(relNode); // 6
169                                 // }
170                                 // }
171                                 // }
172                                 // }
173                                 print(") ");
174                                 print("(");
175                                 print(") ");
176                                 print("(");
177                                 Iterator<IProgramElement> it3 = node.getChildren().iterator();
178                                 if (it3.hasNext()) {
179                                         while (it3.hasNext()) {
180                                                 // this ignores relations on the compile unit
181                                                 IProgramElement currNode = it3.next();
182                                                 if (// !currNode.isStmntKind() &&
183                                                 !currNode.getKind().equals("<undefined>")) {
184                                                         printDecl(currNode, true);
185                                                 }
186                                         }
187                                 }
188                                 print(") ");
189                         }
190
191                         print(node.getKind().equals("class") ? "t " : "nil "); // 9
192                         // print(node.getKind().equals("introduction") ? "t " : "nil "); // 10
193                         print(node.getKind().equals("introduction") ? "nil " : "nil "); // 10
194                         print("nil "); // 11
195                         print("nil "); // 12
196                         print(")");
197                 }
198
199                 String fixFilename(String filename) {
200                         return subst("\\\\", "\\", filename);
201                 }
202
203                 private void print(String string) {
204                         try {
205                                 writer.write(string + "\n");
206                         } catch (IOException ioe) {
207                                 ioe.printStackTrace();
208                         }
209                 }
210
211                 private String subst(String n, String o, String in) {
212                         int pos = in.indexOf(o);
213                         if (pos == -1)
214                                 return in;
215                         return in.substring(0, pos) + n + subst(n, o, (in.substring(pos + o.length())));
216                 }
217
218                 // private void lose(Error e) {
219                 // try {
220                 // print("(ERROR \"" + e.toString() + "\")");
221                 // }
222                 // catch(Error ex) { }
223                 // }
224         }
225 }