View Javadoc
1   /**
2    * Copyright 1&1 Internet AG, https://github.com/1and1/
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.oneandone.sushi;
17  
18  import net.oneandone.sushi.csv.Csv;
19  import net.oneandone.sushi.csv.Format;
20  import net.oneandone.sushi.csv.View;
21  import net.oneandone.sushi.fs.World;
22  import net.oneandone.sushi.metadata.Instance;
23  import net.oneandone.sushi.metadata.Type;
24  import net.oneandone.sushi.metadata.reflect.ReflectSchema;
25  
26  import java.util.Arrays;
27  
28  public class CsvSample {
29      private static final World WORLD = new World();
30      private static final Type TYPE = new ReflectSchema(WORLD).type(All.class);
31      
32      /** Serialize object to xml and load the result back into an object */
33      public static void main(String[] args) throws Exception {
34          All all;
35          View view;
36          Csv csv;
37          Instance<All> instance;
38          
39          all = new All();
40  
41          System.out.println("object:\n" + all);
42          
43          instance = TYPE.instance(all);
44          csv = new Csv(new Format());
45          view = View.fromXml(WORLD.memoryNode("<view>" +
46                  "  <scope>items</scope>" +
47                  "  <field><name>Id</name><path>id</path></field>" +
48                  "  <field><name>String</name><path>string</path></field>" +
49                  "</view>"));
50          instance.exportCsv(view, csv, "7", "2");
51          System.out.println("orig\n" + csv);
52          csv.get(2).get(1).set(0, "two");
53          instance.importCsv(view, csv);
54          System.out.println("modified\n" + csv);
55      }
56      
57      public static class All {
58          // TOOD: doesn't work for Lists because the static type is used
59          public final Item[] items = { new Item(2, "zwei"), new Item(7, "sieben") };
60          
61          @Override
62          public String toString() {
63              return Arrays.asList(items).toString();
64          }
65      }
66      
67      public static class Item {
68          public final int id;
69          public final String string;
70          
71          public Item() {
72              this(0, "");
73          }
74  
75          public Item(int id, String string) {
76              this.id = id;
77              this.string = string;
78          }
79          
80          @Override
81          public String toString() {
82              return "id=" + id + "+string=" + string;
83          }
84      }
85  }