title: How to replace StringTokenzier(..., true) in GWT?
body: <p>I am looking for a GWT compatible replacement for a StringTokenzier which includes the delimiter. The task cannot be solved by regular expressions because the grammar is not context free.</p>
  
  <p>Example: Extract the first level of a generic type definition. So for <code>List&lt;String&gt;, Map&lt;Integer, Map&lt;Character, Boolean&gt;&gt;, Set&lt;List&lt;Double&gt;&gt;</code>, I want a list with three items. <code>List&lt;String&gt;</code> and <code>Map&lt;Integer, Map&lt;Character, Boolean&gt;&gt;</code> and <code>Set&lt;List&lt;Double&gt;&gt;</code></p>
  
  <p>Stripped down example code:</p>
  
  <pre><code>private static List&lt;String&gt; extractFirstLevel(String type) {
      List&lt;String&gt; res = new LinkedList&lt;String&gt;();
      StringTokenizer st = new StringTokenizer(type, "&lt;&gt;,", true);
      int nesting = 0;        // we are only interested in nesting 0
      String lastToken = "";
      while (st.hasMoreTokens()) {
          String token = st.nextToken();
          if (token.equals("&lt;")) {
              nesting++;  // ignore till matching &gt;, but keep track of additional &lt;
              lastToken = lastToken + "&lt;";
          } else if (token.equals("&gt;")) {
              nesting--;  // up one level
              lastToken = lastToken + "&gt;";
          } else if (token.equals(",")) {
              if (nesting == 0) {  // we are interested in the top level
                  res.add(lastToken);
                  lastToken = "";
              } else { // this is a , inside a &lt; &gt;, so we are not interested
                  lastToken = lastToken + ", ";
              }
          } else {
              lastToken = lastToken + token.trim();
          }
      }
      res.add(lastToken);
      return res;
  }
  </code></pre>