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.fs.World;
19  import net.oneandone.sushi.metadata.Instance;
20  import net.oneandone.sushi.metadata.Type;
21  import net.oneandone.sushi.metadata.reflect.ReflectSchema;
22  
23  public class MetadataSample {
24      private static final World WORLD = new World();
25      /** Serialize object to xml and load the result back into an object */
26      public static void main(String[] args) throws Exception {
27          Instance<Obj> instance;
28          Obj obj;
29          
30          instance = TYPE.loadXml(WORLD.memoryNode("<obj><number>2</number><string>str</string></obj>"));
31          obj = instance.get();
32          System.out.println("object:\n" + obj);
33          obj.number = 3;
34          System.out.println("xml:\n" + instance.toXml());
35      }
36      
37      private static final Type TYPE = new ReflectSchema(WORLD).type(Obj.class);
38      
39      public static class Obj {
40          public int number;
41          public String string;
42          
43          public Obj() {
44              this(0, "");
45          }
46  
47          public Obj(int number, String string) {
48              this.number = number;
49              this.string = string;
50          }
51          
52          @Override
53          public String toString() {
54              return "number=" + number + ",string=" + string;
55          }
56      }
57  }