1 package net.sourceforge.selfesteem.applet;
2
3 public class Parser {
4 public MyTreeNode parse(Source source) {
5 MyTreeNode root = new MyTreeNode(source.getParameter("line1"), 0);
6 MyTreeNode current = root;
7
8 int lines = Integer.parseInt(source.getParameter("lines"));
9 for (int i = 2; i < lines + 1; i++) {
10 String line = source.getParameter("line" + i);
11 int level = getLevel(line);
12 while (current.getLevel() > level - 1) {
13 current = (MyTreeNode) current.getParent();
14 }
15 while (current.getLevel() < level - 1) {
16 current = (MyTreeNode) current.getLastChild();
17 }
18 current.add(new MyTreeNode(line.substring(level), level));
19 }
20 return root;
21 }
22
23 private int getLevel(String line) {
24 for (int i = 0; i < line.length(); i++) {
25 if (line.charAt(i) != '-') {
26 return i;
27 }
28 }
29 return line.length();
30 }
31 }
This page was automatically generated by Maven