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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 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
  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<Map.Entry<String, IProgramElement>> fileSet = model.getHierarchy().getFileMapEntrySet();
  37. for (Map.Entry<String, IProgramElement> o : fileSet) {
  38. IProgramElement peNode = o.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 (IProgramElement child : node.getChildren()) {
  85. // this ignores relations on the compile unit
  86. // throw new RuntimeException("unimplemented");
  87. // if (nodeObject instanceof IProgramElement) {
  88. printDecl(child, true);
  89. // }
  90. // else if (nodeObject instanceof LinkNode) {
  91. // LinkNode child = (LinkNode)nodeObject;
  92. // printDecl(child.getProgramElementNode(), false);
  93. // }
  94. }
  95. print(") ");
  96. }
  97. // private void printDecls(IRelationship node) {
  98. // // for (Iterator it = node.getTargets().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 LinkNode) {
  103. // //// LinkNode child = (LinkNode)nodeObject;
  104. // //// if (//!child.getProgramElementNode().getKind().equals("stmnt") &&
  105. // //// !child.getProgramElementNode().getKind().equals("<undefined>")) {
  106. // //// printDecl(child.getProgramElementNode(), false);
  107. // ////// printDecl(child.getProgramElementNode(), false);
  108. // //// }
  109. // //// }
  110. // // }
  111. // }
  112. /**
  113. * @param structureNode can be a ProgramElementNode or a LinkNode
  114. */
  115. private void printDecl(IProgramElement node, boolean recurse) {
  116. if (node == null || node.getSourceLocation() == null)
  117. return;
  118. String kind = node.getKind().toString().toLowerCase();
  119. print("(");
  120. print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
  121. print("(" + node.getSourceLocation().getLine() + " . " + node.getSourceLocation().getColumn() + ") ");
  122. print(kind + " "); // 2
  123. // HACK:
  124. String displayName = node.toString().replace('\"', ' ');
  125. print("\"" + displayName + "\" ");
  126. if (node.getSourceLocation().getSourceFile().getAbsolutePath() != null) {
  127. print("\"" + fixFilename(node.getSourceLocation().getSourceFile().getAbsolutePath()) + "\""); // 4
  128. } else {
  129. print("nil");
  130. }
  131. if (node.getName() != null) {
  132. print("\"" + node.getDeclaringType() + "\" "); // 5
  133. } else {
  134. print("nil");
  135. }
  136. if (!recurse) {
  137. print("nil");
  138. print("nil");
  139. print("nil");
  140. } else {
  141. print("(");
  142. // if (node instanceof IProgramElement) {
  143. // java.util.List relations = ((IProgramElement)node).getRelations();
  144. // if (relations != null) {
  145. // for (Iterator it = relations.iterator(); it.hasNext(); ) {
  146. // IRelationship relNode = (IRelationship)it.next();
  147. // if (relNode.getKind() == IRelationship.Kind.ADVICE ||
  148. // relNode.getKind() == IRelationship.Kind.DECLARE) {
  149. // printDecls(relNode); // 6
  150. // }
  151. // }
  152. // }
  153. // }
  154. print(") ");
  155. print("(");
  156. print(") ");
  157. print("(");
  158. Iterator<IProgramElement> it3 = node.getChildren().iterator();
  159. if (it3.hasNext()) {
  160. while (it3.hasNext()) {
  161. // this ignores relations on the compile unit
  162. IProgramElement currNode = it3.next();
  163. if (// !currNode.isStmntKind() &&
  164. !currNode.getKind().equals("<undefined>")) {
  165. printDecl(currNode, true);
  166. }
  167. }
  168. }
  169. print(") ");
  170. }
  171. print(node.getKind().equals("class") ? "t " : "nil "); // 9
  172. // print(node.getKind().equals("introduction") ? "t " : "nil "); // 10
  173. print(node.getKind().equals("introduction") ? "nil " : "nil "); // 10
  174. print("nil "); // 11
  175. print("nil "); // 12
  176. print(")");
  177. }
  178. String fixFilename(String filename) {
  179. return subst("\\\\", "\\", filename);
  180. }
  181. private void print(String string) {
  182. try {
  183. writer.write(string + "\n");
  184. } catch (IOException ioe) {
  185. ioe.printStackTrace();
  186. }
  187. }
  188. private String subst(String n, String o, String in) {
  189. int pos = in.indexOf(o);
  190. if (pos == -1)
  191. return in;
  192. return in.substring(0, pos) + n + subst(n, o, (in.substring(pos + o.length())));
  193. }
  194. // private void lose(Error e) {
  195. // try {
  196. // print("(ERROR \"" + e.toString() + "\")");
  197. // }
  198. // catch(Error ex) { }
  199. // }
  200. }
  201. }