简体   繁体   中英

Parse such a String into a object structure using Java (GWT)

I am trying to parse an string in GWT to an Object structure. Unfortunately I am not able to do it.

Example string:

"(node1(node2(node3) (node4)) (node5))"
  • "node1" has 2 children: "node2" and "node5"
  • "node2" has 2 children: "node3" and "node4"

Object can be "node", with children. Any help on this will be greatly appreciated.

I can provide you with a pseudo-code. I believe the following algorithm will work but please let me know if you spot anything wrong with it.

Create a root node called nodeRoot.
current_node = nodeRoot
For each char in the string:
    if the char is '('
        add a child node to current_node
        parse the string (after the '(' char) to fetch the name of the node
        current_node = the newly added child node
    else if the char is ')'
        current_node = parent of current_node

To keep track of parents, you can use a stack.

I'm pretty bored so I knocked together some code.

  private static class ObjectTree {
    private Set<ObjectTree> children = new LinkedHashSet();
    private ObjectTree parent = null;
    private String text = null;
    private int level = 0;

    public ObjectTree() {
      this(null);
    }

    public ObjectTree(ObjectTree parent) {
      this(parent, 0);
    }

    public ObjectTree(ObjectTree parent, int level) {
      this.parent = parent;
      this.level = level;
    }

    public void addChild(ObjectTree child) {
      children.add(child);
    }

    public void parse(String s) {
      int ix = s.indexOf("(");
      if (ix > 0)
        text = s.substring(0, ix);
      else if (ix <= 0)
        text = s;
      int iy = ix + 1;
      int count = 1;
      if (ix == -1)
        return;
      while (iy < s.length()) {
        if (s.charAt(iy) == ')')
          count--;
        else if (s.charAt(iy) == '(')
          count++;
        if (count == 0) {
          String newString = s.substring(ix + 1, iy);
          ObjectTree newChild = new ObjectTree(this, level + 1);
          addChild(newChild);
          newChild.parse(newString);
          ix = s.indexOf('(', iy);
          count++;
          iy = ix;
        }
        iy++;
      }

    }

    public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append(StringUtils.repeat("\t.", level)).append(text).append(" :\n");
      for (ObjectTree child : children) {
        sb.append(StringUtils.repeat("\t", level)).append("\t").append(child.toString()).append("\n");
      }
      if (!children.isEmpty())
        sb.append("\n");
      return sb.toString();
    }

  }

You call it like so:

ObjectTree root = new ObjectTree();
root.parse("(node1(node2(node3) (node4)) (node5))");
System.out.println(root.toString());

And get:

(node1(node2(node3) (node4)) (node5)) :
    .node1 :
        .   .node2 :
            .   .   .node3 :
            .   .   .node4 :
        .   .node5 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM