title: Generate tree structure from csv
body: <p>I have scratched my head over this problem for a while now.  I am basically trying to generate a tree hierarchy from a set of CSV data.  The CSV data is not necessarily ordered.  This is like something as follows:</p>
  
  <pre><code>Header: Record1,Record2,Value1,Value2
  Row: A,XX,22,33
  Row: A,XX,777,888
  Row: A,YY,33,11
  Row: B,XX,12,0
  Row: A,YY,13,23
  Row: B,YY,44,98
  </code></pre>
  
  <p>I am trying to make the way the grouping is performed as flexible as possible.  The simplest for of  grouping would to do it for Record1 and Record2 with the Value1 and Value2 stored under Record2 so that we get the following output:</p>
  
  <pre><code>Record1
      Record2
          Value1 Value2
  </code></pre>
  
  <p>Which would be:</p>
  
  <pre><code>A
      XX
          22,33
          777,888
      YY
          33,11
          13,23
  B
      XX
          12,0
      YY
          44,98 
  </code></pre>
  
  <p>I am storing my group settings in a List at present - which I don't know if this is hindering my thoughts.  This list contains a hierarchy of the groups for example:</p>
  
  <pre><code>Record1 (SchemaGroup)
      .column = Record1
      .columns = null
      .childGroups =
          Record2 (SchemaGroup)
              .column = Record1
              .columns = Value1 (CSVColumnInformation), Value2 (CSVColumnInformation)
              .childGroups = null
  </code></pre>
  
  <p>The code for this looks like as follows:</p>
  
  <pre><code>private class SchemaGroup {
      private SchemaGroupType type = SchemaGroupType.StaticText;  // default to text
      private String text;
      private CSVColumnInformation column = null;
      private List&lt;SchemaGroup&gt; childGroups = new ArrayList&lt;SchemaGroup&gt;();
      private List&lt;CSVColumnInformation&gt; columns = new ArrayList&lt;CSVColumnInformation&gt;();
  }
  
  
  private enum SchemaGroupType {
      /** Allow fixed text groups to be added */
      StaticText,
      /** Related to a column with common value */
      ColumnGroup
  }
  </code></pre>
  
  <p>I am stuggling producing an algorithm for this, trying to think of the underlying structure to use.  At present I am parsing the CSV top to bottom, using my own wrapper class:</p>
  
  <pre><code>CSVParser csv = new CSVParser(content);
  String[] line;
  while((line = csv.readLine()) != null ) {
      ...
  }
  </code></pre>
  
  <p>I am just trying to kick start my coding brain.</p>
  
  <p>Any thoughts?</p>