| 1 2 25 26 package org.aspectj.asm.views; 27 28 import java.util.*; 29 import java.io.*; 30 import org.aspectj.asm.associations.*; 32 import org.aspectj.asm.*; 33 import org.aspectj.compiler.crosscuts.AspectJCompiler; 34 36 39 public class EmacsViewManager extends StructureModelManager { 40 41 private static final String 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 69 private void dumpStructureToFile(ProgramElementNode node) throws IOException { 70 String sourceName = node.getSourceLocation().getSourceFilePath(); 71 String 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) {} } 83 } 84 } 85 86 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 Object 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 Object nodeObject = it.next(); 117 if (nodeObject instanceof LinkNode) { 118 LinkNode child = (LinkNode)nodeObject; 119 if ( !child.getProgramElementNode().getKind().equals("<undefined>")) { 121 printDecl(child.getProgramElementNode(), false); 122 } 124 } 125 } 126 } 127 128 131 private void printDecl(ProgramElementNode node, boolean recurse) { 132 String 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 + " "); 138 String displayName = node.toString().replace('\"', ' '); 140 141 print("\"" + displayName + "\" "); 142 if (node.getSourceLocation().getSourceFilePath() != null) { 143 print("\"" + fixFilename(node.getSourceLocation().getSourceFilePath()) + "\""); } else { 145 print("nil"); 146 } 147 if (node.getSignature() != null) { 148 print("\"" + node.getDeclaringType() + "\" "); } 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 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); } 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 Object nodeObject = it3.next(); 180 if (nodeObject instanceof ProgramElementNode) { 181 ProgramElementNode currNode = (ProgramElementNode)nodeObject; 182 if ( !currNode.getKind().equals("<undefined>")) { 184 printDecl(currNode, true); 185 } 186 } 187 } 188 } 189 print(") "); 190 } 191 192 print(node.getKind().equals("class") ? "t " : "nil "); print(node.getKind().equals("introduction") ? "nil " : "nil "); print("nil "); print("nil "); print(")"); 198 } 199 200 String fixFilename(String filename) { 201 return subst("\\\\", "\\", filename); 202 } 203 204 private void print(String string) { 205 try { 206 writer.write(string + "\n"); 207 } catch (IOException ioe) { 208 ioe.printStackTrace(); 209 } 210 } 211 212 private String subst(String n, String o, String 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 e) { 222 try { 223 print("(ERROR \"" + e.toString() + "\")"); 224 } 225 catch(Error ex) { } 226 } 227 } 228 } 229 230 | Popular Tags |