Advertisement
Guest User

Untitled

a guest
Jun 4th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. package com.company.parsing;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import java.util.OptionalInt;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.IntStream;
  10.  
  11. public class SampleMe {
  12. public static final String TEXT = "# Sport\n" +
  13. "common text\n" +
  14. "## Tennis\n" +
  15. "### Table\n" +
  16. "### Ping Pong\n" +
  17. "commontext\n" +
  18. "# Dance";
  19. public static void main(String[] args) throws IOException {
  20. List<String> lines = Arrays.stream(TEXT.split("\n")).collect(Collectors.toList());
  21. lines.add(0, "Name of file");
  22. lines = lines.stream().map(s -> s.replace('#', ' ')).collect(Collectors.toList());
  23. Node root = new Node(lines.get(0));
  24. root.parent = null;
  25. Node currentNode = root;
  26. List<String> headers = new ArrayList<>();
  27. for (int i = 1; i < lines.size(); i++) {
  28. if (!lines.get(i).startsWith(" ")) {
  29. currentNode.name += "\n" + diff(currentNode.name) + lines.get(i);
  30. continue;
  31. }
  32. int cCount = lines.get(i).length() - lines.get(i).trim().length();
  33. int pCount = currentNode.name.length() - currentNode.name.trim().length();
  34. Node node = new Node(lines.get(i));
  35. if (cCount > pCount) { //if spaces are more than previous add child node
  36. node.setIndex(1);
  37. node.parent = currentNode;
  38. node.aIndex = node.parent.aIndex == null ? String.valueOf(node.getIndex()) : node.parent.aIndex + "." + node.getIndex();
  39. node.name = node.name + " " + node.aIndex;
  40. currentNode.children.add(node);
  41. currentNode = node;
  42. headers.add(node.name);
  43. } else if (cCount == pCount) {//if spaces are same add node on same level
  44. node.setIndex(currentNode.getIndex() + 1);
  45. currentNode.parent.children.add(node);
  46. node.parent = currentNode.parent;
  47. node.aIndex = node.parent.aIndex == null ? String.valueOf(node.getIndex()) : node.parent.aIndex + "." + node.getIndex();
  48. node.name = node.name + " " + node.aIndex;
  49. currentNode = node;
  50. headers.add(node.name);
  51. } else {//if spaces are less then add node to parent of parent
  52. node.setIndex(currentNode.parent.getIndex() + 1);
  53. currentNode.parent.parent.children.add(node);
  54. node.parent = currentNode.parent.parent;
  55. node.aIndex = node.parent.aIndex == null ? String.valueOf(node.getIndex()) : node.parent.aIndex + "." + node.getIndex();
  56. node.name = node.name + " " + node.aIndex;
  57. currentNode = node;
  58. headers.add(node.name);
  59. }
  60. }
  61. headers.forEach(System.out::println);
  62. System.out.println();
  63. recursiveDeep(root);
  64. }
  65.  
  66. private static String diff(String s) {
  67. OptionalInt first = IntStream.range(0, s.length()).filter(i -> s.charAt(i) != ' ').findFirst();
  68. return " ".repeat(Math.max(0, first.getAsInt()));
  69. }
  70.  
  71. public static void recursiveDeep(Node root) {
  72. for (int i = 0; i < root.children.size(); i++) {
  73. Node node = root.children.get(i);
  74. System.out.println(node.name);
  75. if (!node.children.isEmpty()) {
  76. recursiveDeep(node);
  77. }
  78. }
  79. }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement