]> source.dussan.org Git - aspectj.git/blob
3322ac30871921260a797afeeb6288666f59f5ea
[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 v1.0
7  * which accompanies this distribution and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
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 (Iterator it = fileSet.iterator(); it.hasNext();) {
46                                 IProgramElement peNode = (IProgramElement) ((Map.Entry) it.next()).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 (Iterator it = node.getChildren().iterator(); it.hasNext();) {
99                                 // this ignores relations on the compile unit
100                                 Object nodeObject = it.next();
101                                 // throw new RuntimeException("unimplemented");
102                                 // if (nodeObject instanceof IProgramElement) {
103                                 IProgramElement child = (IProgramElement) nodeObject;
104                                 printDecl(child, true);
105                                 // }
106                                 // else if (nodeObject instanceof LinkNode) {
107                                 // LinkNode child = (LinkNode)nodeObject;
108                                 // printDecl(child.getProgramElementNode(), false);
109                                 // }
110                         }
111                         print(") ");
112                 }
113
114                 // private void printDecls(IRelationship node) {
115                 // // for (Iterator it = node.getTargets().iterator(); it.hasNext(); ) {
116                 // // // this ignores relations on the compile unit
117                 // // Object nodeObject = it.next();
118                 // // throw new RuntimeException("unimplemented");
119                 // //// if (nodeObject instanceof LinkNode) {
120                 // //// LinkNode child = (LinkNode)nodeObject;
121                 // //// if (//!child.getProgramElementNode().getKind().equals("stmnt") &&
122                 // //// !child.getProgramElementNode().getKind().equals("<undefined>")) {
123                 // //// printDecl(child.getProgramElementNode(), false);
124                 // ////// printDecl(child.getProgramElementNode(), false);
125                 // //// }
126                 // //// }
127                 // // }
128                 // }
129
130                 /**
131                  * @param structureNode can be a ProgramElementNode or a LinkNode
132                  */
133                 private void printDecl(IProgramElement node, boolean recurse) {
134                         if (node == null || node.getSourceLocation() == null)
135                                 return;
136                         String kind = node.getKind().toString().toLowerCase();
137                         print("(");
138                         print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
139                         print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
140                         print(kind + " "); // 2
141
142                         // HACK:
143                         String displayName = node.toString().replace('\"', ' ');
144
145                         print("\"" + displayName + "\" ");
146                         if (node.getSourceLocation().getSourceFile().getAbsolutePath() != null) {
147                                 print("\"" + fixFilename(node.getSourceLocation().getSourceFile().getAbsolutePath()) + "\""); // 4
148                         } else {
149                                 print("nil");
150                         }
151                         if (node.getName() != null) {
152                                 print("\"" + node.getDeclaringType() + "\" "); // 5
153                         } else {
154                                 print("nil");
155                         }
156
157                         if (!recurse) {
158                                 print("nil");
159                                 print("nil");
160                                 print("nil");
161                         } else {
162                                 print("(");
163                                 // if (node instanceof IProgramElement) {
164                                 // java.util.List relations = ((IProgramElement)node).getRelations();
165                                 // if (relations != null) {
166                                 // for (Iterator it = relations.iterator(); it.hasNext(); ) {
167                                 // IRelationship relNode = (IRelationship)it.next();
168                                 // if (relNode.getKind() == IRelationship.Kind.ADVICE ||
169                                 // relNode.getKind() == IRelationship.Kind.DECLARE) {
170                                 // printDecls(relNode); // 6
171                                 // }
172                                 // }
173                                 // }
174                                 // }
175                                 print(") ");
176                                 print("(");
177                                 print(") ");
178                                 print("(");
179                                 Iterator<IProgramElement> it3 = node.getChildren().iterator();
180                                 if (it3.hasNext()) {
181                                         while (it3.hasNext()) {
182                                                 // this ignores relations on the compile unit
183                                                 Object nodeObject = it3.next();
184                                                 if (nodeObject instanceof IProgramElement) {
185                                                         IProgramElement currNode = (IProgramElement) nodeObject;
186                                                         if (// !currNode.isStmntKind() &&
187                                                         !currNode.getKind().equals("<undefined>")) {
188                                                                 printDecl(currNode, true);
189                                                         }
190                                                 }
191                                         }
192                                 }
193                                 print(") ");
194                         }
195
196                         print(node.getKind().equals("class") ? "t " : "nil "); // 9
197                         // print(node.getKind().equals("introduction") ? "t " : "nil "); // 10
198                         print(node.getKind().equals("introduction") ? "nil " : "nil "); // 10
199                         print("nil "); // 11
200                         print("nil "); // 12
201                         print(")");
202                 }
203
204                 String fixFilename(String filename) {
205                         return subst("\\\\", "\\", filename);
206                 }
207
208                 private void print(String string) {
209                         try {
210                                 writer.write(string + "\n");
211                         } catch (IOException ioe) {
212                                 ioe.printStackTrace();
213                         }
214                 }
215
216                 private String subst(String n, String o, String in) {
217                         int pos = in.indexOf(o);
218                         if (pos == -1)
219                                 return in;
220                         return in.substring(0, pos) + n + subst(n, o, (in.substring(pos + o.length())));
221                 }
222
223                 // private void lose(Error e) {
224                 // try {
225                 // print("(ERROR \"" + e.toString() + "\")");
226                 // }
227                 // catch(Error ex) { }
228                 // }
229         }
230 }