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 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
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 (Object o : fileSet) {
46 IProgramElement peNode = (IProgramElement) ((Map.Entry) o).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 (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);
104 // else if (nodeObject instanceof LinkNode) {
105 // LinkNode child = (LinkNode)nodeObject;
106 // printDecl(child.getProgramElementNode(), false);
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);
129 * @param structureNode can be a ProgramElementNode or a LinkNode
131 private void printDecl(IProgramElement node, boolean recurse) {
132 if (node == null || node.getSourceLocation() == null)
134 String kind = node.getKind().toString().toLowerCase();
136 print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
137 print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
138 print(kind + " "); // 2
141 String displayName = node.toString().replace('\"', ' ');
143 print("\"" + displayName + "\" ");
144 if (node.getSourceLocation().getSourceFile().getAbsolutePath() != null) {
145 print("\"" + fixFilename(node.getSourceLocation().getSourceFile().getAbsolutePath()) + "\""); // 4
149 if (node.getName() != null) {
150 print("\"" + node.getDeclaringType() + "\" "); // 5
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
177 Iterator<IProgramElement> it3 = node.getChildren().iterator();
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);
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
199 String fixFilename(String filename) {
200 return subst("\\\\", "\\", filename);
203 private void print(String string) {
205 writer.write(string + "\n");
206 } catch (IOException ioe) {
207 ioe.printStackTrace();
211 private String subst(String n, String o, String in) {
212 int pos = in.indexOf(o);
215 return in.substring(0, pos) + n + subst(n, o, (in.substring(pos + o.length())));
218 // private void lose(Error e) {
220 // print("(ERROR \"" + e.toString() + "\")");
222 // catch(Error ex) { }