package org.jboss.as.console.client.util;

import com.gwtplatform.mvp.client.proxy.PlaceRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Heiko Braun
 * @date 2/17/11
 */
public class Places {

    public static List<PlaceRequest> fromString(String urlString)
    {
        List<PlaceRequest> places = new ArrayList<PlaceRequest>();

        // TODO: Tis is currently limited to a a parent/child hierarchy with depth 1
        if(urlString.contains("/"))
        {
            String[] parentChild = urlString.split("/");

            String parent = parentChild[0];
            places.add(new PlaceRequest(parent));

            String child = parentChild[1];
            if(child.contains(";"))
            {
                String[] split = child.split(";");
                String childPlace = split[0];
                String params[] = split[1].split("=");

                places.add(new PlaceRequest(childPlace).with(params[0], params[1]));
            }
            else
            {
                places.add(new PlaceRequest(child));
            }
        }

        return places;
    }
}

@@ -36,16 +36,59 @@
 .util;%0A%0A
+import com.allen_sauer.gwt.log.client.Log;%0A
 import c
@@ -370,159 +370,280 @@
  = n
-ew ArrayList%3CPlaceRequest%3E();%0A%0A        // TODO: Tis is currently limited to a a parent/child hierarchy with depth 1%0A        if(urlString.contains(%22/%22
+ull;%0A        try %7B%0A            places = new ArrayList%3CPlaceRequest%3E();%0A%0A            StringTokenizer tokenizer = new StringTokenizer(urlString, %22/%22);%0A            while(tokenizer.hasMoreTokens())%0A            %7B%0A                parseSingleToken(places, tokenizer.nextToken(
 ))
+;
 %0A   
@@ -639,33 +639,38 @@
 ken());%0A        
-%7B
+    %7D%0A
 %0A            Str
@@ -666,153 +666,448 @@
     
-    String%5B%5D parentChild = urlString.split(%22/%22);%0A%0A            String parent = parentChild%5B0%5D;
+%7D catch (Throwable e) %7B%0A            Log.error(%22Error parsing token: %22 + urlString);%0A        %7D%0A%0A        return places;%0A    %7D%0A%0A    private static void parseSingleToken(List%3CPlaceRequest%3E places, String token) %7B%0A%0A        if(token.contains(%22;%22)) // parametrized?
 %0A      
+  %7B%0A
       
-places.add(new PlaceRequest(parent));%0A%0A
+      StringTokenizer params = new StringTokenizer(token, %22;%22);%0A            PlaceRequest request = null;%0A            while(params.hasMoreTokens())%0A            %7B%0A    
     
@@ -1125,30 +1125,32 @@
 ing 
-child
+tok
  = par
-entChild%5B1%5D
+ams.nextToken()
 ;%0A  
@@ -1163,16 +1163,18 @@
     
-if(child
+    if(tok
 .con
@@ -1184,13 +1184,17 @@
 ns(%22
-;
+=
 %22))%0A
+    
     
@@ -1223,89 +1223,75 @@
     
-String%5B%5D split = child.split(%22;%22);%0A                String childPlace = split%5B0%5D;%0A
+    if(null==request) break;%0A%0A                    // parameter%0A    
     
@@ -1312,28 +1312,26 @@
 ring
+%5B%5D
  param
-s%5B%5D = split%5B1%5D
+eter = tok
 .spl
@@ -1339,17 +1339,20 @@
 t(%22=%22);%0A
-%0A
+    
         
@@ -1363,47 +1363,25 @@
     
-places.add(new PlaceRequest(childPlace)
+request = request
 .wit
@@ -1387,17 +1387,20 @@
 th(param
-s
+eter
 %5B0%5D, par
@@ -1405,14 +1405,16 @@
 aram
-s
+eter
 %5B1%5D)
-)
 ;%0A  
@@ -1415,34 +1415,42 @@
 %5D);%0A            
+  
-%7D%0A
+  %7D%0A    
             else
@@ -1450,16 +1450,20 @@
    else%0A
+    
         
@@ -1488,19 +1488,53 @@
     
-places.add(
+    // address%0A                    request = 
 new 
@@ -1550,17 +1550,19 @@
 est(
-child));%0A
+tok);%0A%0A    
     
@@ -1571,32 +1571,36 @@
       %7D%0A        
+    
 %7D%0A%0A        retur
@@ -1590,35 +1590,308 @@
  %7D%0A%0A        
-return places;
+    // exit, either wrong token or different formatter%0A            if(null==request)%0A                throw new IllegalArgumentException(%22Illegal token: %22+token);%0A%0A            places.add(request);%0A        %7D%0A        else%0A        %7B%0A            places.add(new PlaceRequest(token));%0A        %7D
 %0A    %7D%0A%7D%0A

Fix token to place request parsing: allow hierarchy depths > 2