MetamodelTreeView.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package de.jplag.emf.util;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Optional;
  7. import org.eclipse.emf.ecore.ENamedElement;
  8. import org.eclipse.emf.ecore.EObject;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import de.jplag.Token;
  12. import de.jplag.emf.MetamodelToken;
  13. /**
  14. * Simplistic tree view representation of an EMF metamodel.
  15. * @author Timur Saglam
  16. */
  17. public class MetamodelTreeView {
  18. private final File file;
  19. private int lineIndex;
  20. private int columnIndex;
  21. private final StringBuilder viewBuilder;
  22. private static final String INDENTATION = " ";
  23. public final Logger logger;
  24. /**
  25. * Creates a tree view for a metamodel.
  26. * @param file is the file where the metamodel is persisted.
  27. */
  28. public MetamodelTreeView(File file) {
  29. this.file = file;
  30. logger = LoggerFactory.getLogger(this.getClass());
  31. viewBuilder = new StringBuilder();
  32. }
  33. /**
  34. * Adds a token to the view, thus adding the index information to the token. Returns a new token enriched with the index
  35. * metadata.
  36. * @param token is the token to add.
  37. * @param treeDepth is the current containment tree depth, required for the indentation.
  38. */
  39. public MetamodelToken convertToMetadataEnrichedTokenAndAdd(MetamodelToken token, int treeDepth, String prefix) {
  40. int length = Token.NO_VALUE;
  41. int line = Token.NO_VALUE;
  42. int column = Token.NO_VALUE;
  43. Optional<EObject> optionalEObject = token.getEObject();
  44. if (optionalEObject.isPresent()) {
  45. EObject eObject = optionalEObject.get();
  46. if (prefix.isEmpty() && treeDepth > 0) {
  47. lineIndex++;
  48. columnIndex = 0;
  49. viewBuilder.append(System.lineSeparator());
  50. }
  51. String tokenText = token.getType().getDescription();
  52. if (eObject instanceof ENamedElement element) {
  53. tokenText = element.getName() + " : " + tokenText;
  54. }
  55. length = tokenText.length();
  56. if (prefix.isEmpty()) {
  57. for (int i = 0; i < treeDepth; i++) {
  58. viewBuilder.append(INDENTATION);
  59. columnIndex += INDENTATION.length();
  60. }
  61. viewBuilder.append(tokenText);
  62. } else {
  63. viewBuilder.append(prefix + tokenText);
  64. columnIndex += prefix.length();
  65. }
  66. line = lineIndex + 1;
  67. column = columnIndex + 1;
  68. columnIndex += tokenText.length();
  69. }
  70. return new MetamodelToken(token.getType(), token.getFile(), line, column, length, token.getEObject());
  71. }
  72. /**
  73. * Writes the tree view into a file.
  74. * @param suffix is the suffix of the file to be written.
  75. */
  76. public void writeToFile(String suffix) {
  77. File treeViewFile = new File(file, suffix);
  78. try (BufferedWriter writer = new BufferedWriter(new FileWriter(treeViewFile));) {
  79. if (!treeViewFile.createNewFile()) {
  80. logger.warn("Overwriting tree view file: {}", treeViewFile);
  81. }
  82. writer.append(viewBuilder.toString());
  83. } catch (IOException exception) {
  84. logger.error("Could not write tree view file!", exception);
  85. }
  86. }
  87. }