The Persister object is used to provide an implementation of a serializer. This : the Serializer interface and enables objects to be persisted and loaded from various sources. This implementation makes use of Filter objects to replace template variables within the source XML document. It is fully thread safe and can be shared by multiple threads without concerns.

Deserialization is performed by passing an XML schema class into one of the read methods along with the source of an XML stream. The read method then reads the contents of the XML stream and builds the object using annotations within the XML schema class.

Serialization is performed by passing an object and an XML stream into one of the write methods. The serialization process will use the class of the provided object as the schema class. The object is traversed and all fields are marshalled to the result stream.

Inheritance: Serializer
 public void TestPersistence() {
    String[] list = new String[] { "one", "two", "three", "four"};
    Persister persister = new Persister();
    StringArrayExample example = new StringArrayExample(list);
    StringWriter out = new StringWriter();
    AssertEquals(example.attribute[0], "one");
    AssertEquals(example.attribute[1], "two");
    AssertEquals(example.attribute[2], "three");
    AssertEquals(example.attribute[3], "four");
    AssertEquals(example.element[0], "one");
    AssertEquals(example.element[1], "two");
    AssertEquals(example.element[2], "three");
    AssertEquals(example.element[3], "four");
    AssertEquals(example.list.get(0)[0], "one");
    AssertEquals(example.list.get(0)[1], "two");
    AssertEquals(example.list.get(0)[2], "three");
    AssertEquals(example.list.get(0)[3], "four");
    AssertEquals(example.array[0][0], "one");
    AssertEquals(example.array[0][1], "two");
    AssertEquals(example.array[0][2], "three");
    AssertEquals(example.array[0][3], "four");
    persister.write(example, out);
    String text = out.toString();
    System.err.println(text);
    example = persister.read(StringArrayExample.class, text);
Exemplo n.º 2
0
 public void TestPrimitiveMap() {
    PrimitiveInlineMap map = new PrimitiveInlineMap();
    Serializer serializer = new Persister();
    map.map.put("a", new BigDecimal(1.1));
    map.map.put("b", new BigDecimal(2.2));
    validate(map, serializer);
 }
    //public void SetName(String name) {
    //   this.name = name;
    //}
 public void TestDefaultWithParameters() {
    Persister persister = new Persister();
    DefaultTestClass type = new DefaultTestClass();
    type.foo = 100;
    persister.write(type, System.out);
    validate(type, persister);
 }
Exemplo n.º 4
0
 public void TestConverter() {
    Registry registry = new Registry();
    Strategy interceptor = new RegistryStrategy(registry);
    Persister persister = new Persister(interceptor);
    StringWriter writer = new StringWriter();
    PetShop shop = new PetShop();
    registry.bind(Dog.class, DogConverter.class)
    //public List<Pet> GetPets() {
    //   return list;
    //}
 public void TestCycle() {
    Registry registry = new Registry();
    CycleStrategy inner = new CycleStrategy();
    RegistryStrategy strategy = new RegistryStrategy(registry, inner);
    Persister persister = new Persister(strategy);
    PetBucket bucket = new PetBucket();
    StringWriter writer = new StringWriter();
    registry.bind(Cat.class, CatConverter.class);
 public void TestConstructorInjection() {
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
          "<MessageWrapper>" +
          "<necessary>test</necessary>" +
          "<optional/>" +
          "</MessageWrapper>";
    Serializer serializer = new Persister();
    Message example = serializer.read(Message.class, xml);
Exemplo n.º 7
0
 public void TestPrimitiveAttributeValueMap() {
    PrimitiveInlineAttributeValueMap map = new PrimitiveInlineAttributeValueMap();
    Serializer serializer = new Persister();
    map.map.put("a", new BigDecimal(1.1));
    map.map.put("b", new BigDecimal(2.2));
    map.map.put(null, new BigDecimal(3.3));
    map.map.put("d", null);
    validate(map, serializer);
 }
Exemplo n.º 8
0
 public void TestEnumSet() {
    Persister persister = new Persister();
    EnumSetExample example = new EnumSetExample();
    example.Add(Qualification.BEGINNER);
    example.Add(Qualification.EXPERT);
    assertTrue(example.Contains(Qualification.BEGINNER));
    assertTrue(example.Contains(Qualification.EXPERT));
    assertFalse(example.Contains(Qualification.GURU));
    persister.write(example, System.out);
    validate(persister, example);
 }
Exemplo n.º 9
0
 /// <summary>
 /// </summary>
 /// <param name="args">
 /// the command line arguments
 /// </param>
 public void TestEmptyMapEntry() {
     Strategy resolver = new CycleStrategy("id", "ref");
     Serializer s = new Persister(resolver);
     StringWriter w = new StringWriter();
     SimpleBug1 bug1 = new SimpleBug1();
     AssertEquals(bug1.test1.data.get("key1"), "value1");
     AssertEquals(bug1.test1.data.get("key2"), "value1");
     AssertEquals(bug1.test1.data.get("key3"), "");
     AssertEquals(bug1.test1.data.get(""), "");
     s.write(bug1, w);
     System.err.println(w.toString());
     SimpleBug1 bug2 = s.read(SimpleBug1.class, w.toString());
 public void TestDefaultWithTransientErrors() {
    Persister persister = new Persister();
    DefaultTestClassWithInvalidTransient type = new DefaultTestClassWithInvalidTransient();
    type.foo = 100;
    bool failure = false;
    try {
       persister.write(type, System.out);
    }catch(Exception e) {
       e.printStackTrace();
       failure=true;
    }
    assertTrue("Annotation on a method which is not a property succeeded", failure);
 }
Exemplo n.º 11
0
 public void TestStrategy() {
    Visitor visitor = new ClassToNamespaceVisitor();
    Strategy strategy = new VisitorStrategy(visitor);
    Persister persister = new Persister(strategy);
    VisitorExample item = new VisitorExample();
    StringWriter writer = new StringWriter();
    item.Put("1", "ONE");
    item.Put("2", "TWO");
    item.Add("A");
    item.Add("B");
    persister.write(item, writer);
    String text = writer.toString();
    System.out.println(text);
    VisitorExample recover = persister.read(VisitorExample.class, text);
Exemplo n.º 12
0
 public void TestReplaceParent() {
     Persister persister = new Persister();
     Set<String> children = new HashSet<String>();
     RealParent parent = new RealParent(children);
     children.add("Tom");
     children.add("Dick");
     children.add("Harry");
     StringWriter writer = new StringWriter();
     persister.write(parent, writer);
     String text = writer.toString();
     System.out.println(text);
     AssertEquals(text.indexOf("Tom"), -1);
     AssertEquals(text.indexOf("Dick"), -1);
     AssertEquals(text.indexOf("Harry"), -1);
     validate(persister, parent);
 }
Exemplo n.º 13
0
 public void TestStackOverflow() {
    StringBuilder builder = new StringBuilder();
    Persister persister = new Persister();
    builder.append("<delivery>");
    bool isNewBenefit = true;
    for(int i = 0; i < ITERATIONS; i++) {
       String text = null;
       if(isNewBenefit) {
          text = NEW_BENEFIT;
       } else {
          text = BENEFIT_MUTATION;
       }
       isNewBenefit = !isNewBenefit ;
       builder.append(text);
    }
    builder.append("</delivery>");
    Delivery delivery = persister.read(Delivery.class, builder.toString());
Exemplo n.º 14
0
 public void TestMissingGenerics() {
     MissingGenerics example = new MissingGenerics();
     Persister persister = new Persister();
     Dictionary map = example.Map;
     map.put("a", "A");
     map.put("b", "B");
     map.put("c", "C");
     map.put("d", "D");
     map.put("e", "E");
     List list = example.List;
     list.add("1");
     list.add("2");
     list.add("3");
     list.add("4");
     list.add("5");
     StringWriter out = new StringWriter();
     persister.write(example, out);
     String text = out.toString();
     MissingGenerics recovered = persister.read(MissingGenerics.class, text);
    //public List<Entry> GetEntries() {
    //   return list;
    //}
 public void TestCycle() {
    CycleStrategy inner = new CycleStrategy();
    AnnotationStrategy strategy = new AnnotationStrategy(inner);
    Persister persister = new Persister(strategy);
    EntryListExample list = new EntryListExample();
    StringWriter writer = new StringWriter();
    Entry a = new Entry("A", "a");
    Entry b = new Entry("B", "b");
    Entry c = new Entry("C", "c");
    Entry primary = new Entry("PRIMARY", "primary");
    list.Primary = primary;
    list.AddEntry(a);
    list.AddEntry(b);
    list.AddEntry(c);
    list.AddEntry(b);
    list.AddEntry(c);
    persister.write(list, writer);
    persister.write(list, System.out);
    String text = writer.toString();
    EntryListExample copy = persister.read(EntryListExample.class, text);
 public void TestNamespacePrefix() {
    AaaWithPrefix parent = new AaaWithPrefix();
    BbbWithPrefix child = new BbbWithPrefix();
    parent.bbb = child;
    AaaWithPrefix grandchild = new AaaWithPrefix();
    child.aaa = grandchild;
    grandchild.bbb = new BbbWithPrefix();
    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
    Serializer serializer = new Persister();
    serializer.write(parent, tmp);
    String result = new String(tmp.toByteArray());
    System.out.println(result);
    assertElementHasAttribute(result, "/aaaWithPrefix", "xmlns:aaa", "namespace1");
    assertElementHasAttribute(result, "/aaaWithPrefix/bbb", "xmlns:bbb", "namespace2");
    assertElementDoesNotHaveAttribute(result, "/aaaWithPrefix/bbb/aaa", "xmlns:aaa", "namespace1");
    assertElementDoesNotHaveAttribute(result, "/aaaWithPrefix/bbb/aaa/bbb", "xmlns:bbb", "namespace2");
    assertElementHasNamespace(result, "/aaaWithPrefix", "namespace1");
    assertElementHasNamespace(result, "/aaaWithPrefix/bbb", "namespace2");
    assertElementHasNamespace(result, "/aaaWithPrefix/bbb/aaa", "namespace1");
    assertElementHasNamespace(result, "/aaaWithPrefix/bbb/aaa/bbb", "namespace2");
 }
Exemplo n.º 17
0
    //public String GetContent() {
    //   return text;
    //}
    //public void SetContent(String text) {
    //   this.text = text;
    //}
 public void TestStatic() {
    Persister persister = new Persister();
    Document document = new Document("Secret Document");
    Section section = new Section("Introduction");
    Paragraph first = new Paragraph();
    Paragraph second = new Paragraph();
    Paragraph third = new Paragraph();
    first.setContent("First paragraph of document");
    second.setContent("Second paragraph in the document");
    third.setContent("Third and readonly paragraph");
    section.Add(first);
    section.Add(second);
    section.Add(third);
    document.Add(section);
    persister.write(document, System.out);
    validate(persister, document);
 }
Exemplo n.º 18
0
 public void TestIndent() {
    Persister serializer = new Persister(new Format(5));
    Contact contact = serializer.read(Contact.class, EXAMPLE);
Exemplo n.º 19
0
 public void SetUp() {
    serializer = new Persister();
 }
Exemplo n.º 20
0
 public void SetUp() {
    persister = new Persister();
 }
Exemplo n.º 21
0
 public void TestExamplePrimitiveCollection() {
    Serializer serializer = new Persister();
    ExamplePrimitiveCollection list = serializer.read(ExamplePrimitiveCollection.class, PRIMITIVE_LIST);
Exemplo n.º 22
0
 public void TestExampleInlineCollection() {
    Serializer serializer = new Persister();
    ExampleInlineCollection list = serializer.read(ExampleInlineCollection.class, INLINE_LIST);
Exemplo n.º 23
0
 public void TestVersionMissing() {
    Serializer persister = new Persister();
    bool success = false;
    try {
       success = persister.validate(TextList.class, VERSION_MISSING);
Exemplo n.º 24
0
 	public void SetUp() {
 	   serializer = new Persister(new Format(4, "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"));
 	}
Exemplo n.º 25
0
 public void TestProperties() {
    Persister persister = new Persister();
    Properties properties = persister.read(Properties.class, SOURCE);
Exemplo n.º 26
0
 public void TestEntryMap() {
    Serializer serializer = new Persister();
    EntryMap example = serializer.read(EntryMap.class, ENTRY_MAP);
Exemplo n.º 27
0
 public void SetUp() {
    persister = new Persister(new CycleStrategy("id", "ref"));
 }
Exemplo n.º 28
0
 public void TestScope() {
    Persister persister = new Persister();
    StringWriter writer = new StringWriter();
    A example = persister.read(A.class, SOURCE);
Exemplo n.º 29
0
 public void TestStrictMode() {
    bool failure = false;
    try {
       Persister persister = new Persister();
       ExampleObjectWithAddress object = persister.read(ExampleObjectWithAddress.class, SOURCE);
Exemplo n.º 30
0
 public void TestCase() {
    Style style = new HyphenStyle();
    Format format = new Format(style);
    Persister writer = new Persister(format);
    Persister reader = new Persister();
    CaseExample example = reader.read(CaseExample.class, SOURCE);