1 /* *******************************************************************
2 * Copyright (c) 1999-2001 Xerox Corporation,
3 * 2002 Palo Alto Research Center, Incorporated (PARC).
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
11 * PARC initial implementation
12 * ******************************************************************/
14 package org.aspectj.ajdt.internal.core.builder;
16 import java.io.BufferedWriter;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.util.Iterator;
24 import org.aspectj.asm.AsmManager;
25 import org.aspectj.asm.IProgramElement;
30 public class EmacsStructureModelManager {
32 private static final String EXTERN_FILE_SUFFIX = ".ajesym";
34 public EmacsStructureModelManager() {
38 public void externalizeModel(AsmManager model) {
39 if (!model.getHierarchy().isValid())
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);
49 } catch (IOException ioe) {
50 ioe.printStackTrace();
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);
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);
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;
72 writer = new BufferedWriter(new FileWriter(new File(fileName)));
73 new SExpressionPrinter(writer).printDecls(node);
79 } catch (IOException e) {
86 * This class was not written in an OO style.
88 private static class SExpressionPrinter {
90 private BufferedWriter writer = null;
92 public SExpressionPrinter(BufferedWriter writer) {
96 private void printDecls(IProgramElement node) {
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);
106 // else if (nodeObject instanceof LinkNode) {
107 // LinkNode child = (LinkNode)nodeObject;
108 // printDecl(child.getProgramElementNode(), false);
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);
131 * @param structureNode can be a ProgramElementNode or a LinkNode
133 private void printDecl(IProgramElement node, boolean recurse) {
134 if (node == null || node.getSourceLocation() == null)
136 String kind = node.getKind().toString().toLowerCase();
138 print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
139 print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
140 print(kind + " "); // 2
143 String displayName = node.toString().replace('\"', ' ');
145 print("\"" + displayName + "\" ");
146 if (node.getSourceLocation().getSourceFile().getAbsolutePath() != null) {
147 print("\"" + fixFilename(node.getSourceLocation().getSourceFile().getAbsolutePath()) + "\""); // 4
151 if (node.getName() != null) {
152 print("\"" + node.getDeclaringType() + "\" "); // 5
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
179 Iterator<IProgramElement> it3 = node.getChildren().iterator();
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);
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
204 String fixFilename(String filename) {
205 return subst("\\\\", "\\", filename);
208 private void print(String string) {
210 writer.write(string + "\n");
211 } catch (IOException ioe) {
212 ioe.printStackTrace();
216 private String subst(String n, String o, String in) {
217 int pos = in.indexOf(o);
220 return in.substring(0, pos) + n + subst(n, o, (in.substring(pos + o.length())));
223 // private void lose(Error e) {
225 // print("(ERROR \"" + e.toString() + "\")");
227 // catch(Error ex) { }