Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.parsing;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.OptionalInt;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class SampleMe {
- public static final String TEXT = "# Sport\n" +
- "common text\n" +
- "## Tennis\n" +
- "### Table\n" +
- "### Ping Pong\n" +
- "commontext\n" +
- "# Dance";
- public static void main(String[] args) throws IOException {
- List<String> lines = Arrays.stream(TEXT.split("\n")).collect(Collectors.toList());
- lines.add(0, "Name of file");
- lines = lines.stream().map(s -> s.replace('#', ' ')).collect(Collectors.toList());
- Node root = new Node(lines.get(0));
- root.parent = null;
- Node currentNode = root;
- List<String> headers = new ArrayList<>();
- for (int i = 1; i < lines.size(); i++) {
- if (!lines.get(i).startsWith(" ")) {
- currentNode.name += "\n" + diff(currentNode.name) + lines.get(i);
- continue;
- }
- int cCount = lines.get(i).length() - lines.get(i).trim().length();
- int pCount = currentNode.name.length() - currentNode.name.trim().length();
- Node node = new Node(lines.get(i));
- if (cCount > pCount) { //if spaces are more than previous add child node
- node.setIndex(1);
- node.parent = currentNode;
- node.aIndex = node.parent.aIndex == null ? String.valueOf(node.getIndex()) : node.parent.aIndex + "." + node.getIndex();
- node.name = node.name + " " + node.aIndex;
- currentNode.children.add(node);
- currentNode = node;
- headers.add(node.name);
- } else if (cCount == pCount) {//if spaces are same add node on same level
- node.setIndex(currentNode.getIndex() + 1);
- currentNode.parent.children.add(node);
- node.parent = currentNode.parent;
- node.aIndex = node.parent.aIndex == null ? String.valueOf(node.getIndex()) : node.parent.aIndex + "." + node.getIndex();
- node.name = node.name + " " + node.aIndex;
- currentNode = node;
- headers.add(node.name);
- } else {//if spaces are less then add node to parent of parent
- node.setIndex(currentNode.parent.getIndex() + 1);
- currentNode.parent.parent.children.add(node);
- node.parent = currentNode.parent.parent;
- node.aIndex = node.parent.aIndex == null ? String.valueOf(node.getIndex()) : node.parent.aIndex + "." + node.getIndex();
- node.name = node.name + " " + node.aIndex;
- currentNode = node;
- headers.add(node.name);
- }
- }
- headers.forEach(System.out::println);
- System.out.println();
- recursiveDeep(root);
- }
- private static String diff(String s) {
- OptionalInt first = IntStream.range(0, s.length()).filter(i -> s.charAt(i) != ' ').findFirst();
- return " ".repeat(Math.max(0, first.getAsInt()));
- }
- public static void recursiveDeep(Node root) {
- for (int i = 0; i < root.children.size(); i++) {
- Node node = root.children.get(i);
- System.out.println(node.name);
- if (!node.children.isEmpty()) {
- recursiveDeep(node);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement