2012-02-29 2 views
1

GWT의 문자열을 Object 구조로 구문 분석하려고합니다. 불행히도 나는 그것을 할 수 없다.Java (GWT)를 사용하여 이러한 문자열을 개체 구조로 구문 분석합니다.

예 문자열 : "node2""node5"

  • "node2" 2 자녀가 있습니다 : 아이들과 함께 "노드"될 수 "node3""node4"
  • 개체

    "(node1(node2(node3) (node4)) (node5))" 
    
    • "node1" 2 명의 아이들이있다 . 이에 대한 도움을 주시면 대단히 감사하겠습니다.

    +1

    이 GWT와는 아무 상관이있다. – Amadan

    +0

    @Amadan, 사실,하지만 OP는 JavaScript 객체 (연관 배열)로 끝나기를 원했고 다음 질문이 될 것이라고 생각합니다. * "Java에서 어떻게하면 좋을까요? (GWT 컴파일러가 JS 객체를 생성하도록) "*이 경우 GWT가 적합합니다. –

    +0

    @ user1241200, 왜 JS 용어로 생각하고 있습니까? 일반 Java로 코드를 모두 작성해야하며 GWT 컴파일러가 이것을 JS로 변환하는 방법에 대해 걱정할 필요가 없습니다. –

    답변

    3

    의사 코드를 제공 할 수 있습니다. 나는 다음과 같은 알고리즘이 효과가 있다고 생각하지만, 만약 당신이 그 문제를 발견한다면 알려주 길 바란다.

    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 
    

    부모를 추적하려면 스택을 사용할 수 있습니다.

    +2

    자바에서는 'StringTokenizer (str, "(" ", true)'를 사용하여 문자별로 문자 이동을 피할 수 있습니다. 재귀 (암시 적 스택)를 사용하여 부모를 좀 더 쉽게 추적 할 수도 있습니다. – Amadan

    +0

    @Amadan - +1 StringTokenizer에 대한 언급입니다. 그러나 그것은 레거시 클래스라는 것을 명심해야합니다. –

    +2

    그리고 asker가 GWT에서 StringTokenizer가 GWT에서 에뮬레이트되지 않았다는 점을 지적 할만한 가치가 있기 때문에 http://code.google.com/webtoolkit/doc/latest/RefJreEmulation.html –

    0

    저는 꽤 지루해 지므로 일부 코드를 작성했습니다.

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

    를 얻을 :

    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(); 
        } 
    
        } 
    

    당신은 그래서 같이 호출

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

    시간 내 주셔서 감사합니다 !! – nikhil3443

    관련 문제