You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmacsStructureModelManager.java 7.1KB

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