Java Code Examples for org.simpleframework.xml.core.Persister#read()

The following examples show how to use org.simpleframework.xml.core.Persister#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VisitorStrategyTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testStrategy() throws Exception {
   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);
   
   assertTrue(recover.map.containsKey("1"));
   assertTrue(recover.map.containsKey("2"));
   assertTrue(recover.items.contains("A"));
   assertTrue(recover.items.contains("B"));  
   
   validate(recover, persister);
}
 
Example 2
Source File: DefaultEmptyTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testDefaults() throws Exception {
   Persister persister = new Persister();
   DefaultExample example = persister.read(DefaultExample.class, SOURCE);
 
   assertEquals(example.name, "test");
   assertEquals(example.text, "some text");
   assertNotNull(example.stringList);
   assertNotNull(example.stringMap);
   assertNotNull(example.stringArray);
   
   persister.write(example, System.out);
   
   validate(persister, example);
   
   persister.write(new DefaultExample("name", "example text"), System.out);
}
 
Example 3
Source File: AnnotationTypeTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testAnnotationType() throws Exception {
   Strategy strategy = new AnnotationStrategy();
   Persister persister = new Persister(strategy);
   StringWriter writer = new StringWriter();
   AnnotationExample example = persister.read(AnnotationExample.class, SOURCE);
   
   persister.write(example, writer);
   
   String text = writer.toString();
   
   assertElementHasAttribute(text, "/annotationExample", "age", "10");
   assertElementHasAttribute(text, "/annotationExample/name", "key", "name");
   
   AnnotationExample result = persister.read(AnnotationExample.class, text);
   
   assertEquals(example.name, result.name);
   assertEquals(example.age, result.age);
   
   validate(result, persister);
}
 
Example 4
Source File: DateTransformTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testPersistence() throws Exception {
   long now = System.currentTimeMillis();
   Date date = new Date(now);      
   Persister persister = new Persister();
   DateExample example = new DateExample(date);
   StringWriter out = new StringWriter();
   
   assertEquals(example.attribute, date);
   assertEquals(example.element, date);
   assertEquals(example.array[0], date);
   assertEquals(example.list.get(0), date);
   assertEquals(example.list.get(1), date);
   
   persister.write(example, out);
   String text = out.toString();
   
   example = persister.read(DateExample.class, text);
   
   assertEquals(example.attribute, date);
   assertEquals(example.element, date);
   assertEquals(example.array[0], date);
   assertEquals(example.list.get(0), date);
   assertEquals(example.list.get(1), date);
   
   validate(example, persister);      
}
 
Example 5
Source File: FilterTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testEnvironmentFilter() throws Exception {
   Filter filter = new EnvironmentFilter(null);           
   Persister persister = new Persister(filter);
   Entry entry = persister.read(Entry.class, new StringReader(ENTRY));
   
   assertEquals(entry.number, 1234);
   assertEquals(entry.bool, true);
   assertEquals(entry.name, "${example.name}");
   assertEquals(entry.path, "${example.path}");

   Filter systemFilter = new SystemFilter();
   Filter environmentFilter = new EnvironmentFilter(systemFilter);
   Persister environmentPersister = new Persister(environmentFilter);
   Entry secondEntry = environmentPersister.read(Entry.class, new StringReader(ENTRY));
   
   assertEquals(secondEntry.number, 1234);
   assertEquals(secondEntry.bool, true);
   assertEquals(secondEntry.name, "some name");
   assertEquals(secondEntry.path, "/some/path");
   assertEquals(secondEntry.constant, "some constant");      
   
}
 
Example 6
Source File: SimpleExecutor.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public Duration write(TestRun test) throws Exception {
long start = System.currentTimeMillis();
   Persister persister = new Persister();
   Class schemaClass = test.getSchemaClass();
   Object result = persister.read(schemaClass, test.getSourceStream());
   
   // Perform once to build up schema cache
   if(test.isDebug()) {
      persister.write(result, System.out);
   }
   long startWrite = System.currentTimeMillis();
   
   for(int i = 0; i < test.getIterations(); i++) {
      persister.write(result, test.getResultWriter());        
   }
   return new Duration(start, startWrite, test.getIterations());
}
 
Example 7
Source File: Example2.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public static void main(String[] list) throws Exception {
   Persister persister = new Persister();
   File file = new File("example2/example2.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.value);
}
 
Example 8
Source File: DateTransformTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testCyclicPersistence() throws Exception {
   long now = System.currentTimeMillis();
   Date date = new Date(now);
   CycleStrategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   DateExample example = new DateExample(date);
   StringWriter out = new StringWriter();
   
   assertEquals(example.attribute, date);
   assertEquals(example.element, date);
   assertEquals(example.array[0], date);
   assertEquals(example.list.get(0), date);
   assertEquals(example.list.get(1), date);
   
   persister.write(example, out);
   String text = out.toString();
   
   assertElementHasAttribute(text, "/dateExample", "id", "0");
   assertElementHasAttribute(text, "/dateExample/array", "id", "1");
   assertElementHasAttribute(text, "/dateExample/array/date", "id", "2");
   assertElementHasAttribute(text, "/dateExample/element", "reference", "2");
   assertElementHasAttribute(text, "/dateExample/list", "id", "3");
   
   example = persister.read(DateExample.class, text);
   
   assertEquals(example.attribute, date);
   assertEquals(example.element, date);
   assertEquals(example.array[0], date);
   assertEquals(example.list.get(0), date);
   assertEquals(example.list.get(1), date);
   
   validate(example, persister);      
}
 
Example 9
Source File: RegistryStrategyTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testConverter() throws Exception {
   Registry registry = new Registry();
   Strategy interceptor = new RegistryStrategy(registry);
   Persister persister = new Persister(interceptor);
   StringWriter writer = new StringWriter();
   PetShop shop = new PetShop();
   
   registry.bind(Cat.class, CatConverter.class);
   registry.bind(Dog.class, DogConverter.class);
           
   shop.addPet(new Cat("Kitty", 2));
   shop.addPet(new Dog("Lassie", 10));
   
   persister.write(shop, writer);
   persister.write(shop, System.out);
   
   String text = writer.toString();
   PetShop newShop = persister.read(PetShop.class, text);
   
   assertEquals("Lassie", newShop.getPet("Lassie").getName());
   assertEquals(10, newShop.getPet("Lassie").getAge());
   assertEquals("Kitty", newShop.getPet("Kitty").getName());
   assertEquals(2, newShop.getPet("Kitty").getAge());
   
   assertElementExists(text, "/petShop");
   assertElementExists(text, "/petShop/pets");
   assertElementExists(text, "/petShop/pets/pet[1]");
   assertElementExists(text, "/petShop/pets/pet[2]");
   assertElementDoesNotExist(text, "/petShop/pets/pet[3]");
   assertElementHasNamespace(text, "/petShop", "http://domain/a");
   assertElementHasNamespace(text, "/petShop/pets", "http://domain/b");
   assertElementHasNamespace(text, "/petShop/pets/pet[2]", null);
   assertElementHasAttribute(text, "/petShop/pets/pet[2]", "name", "Lassie");
   assertElementHasAttribute(text, "/petShop/pets/pet[2]", "age", "10");
   assertElementHasValue(text, "/petShop/pets/pet[1]/name", "Kitty");
   assertElementHasValue(text, "/petShop/pets/pet[1]/age", "2");
}
 
Example 10
Source File: StyleTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testCase() throws Exception {
   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);
   
   assertEquals(example.version, 1.0f);
   assertEquals(example.name, "example");
   assertEquals(example.URL, "http://domain.com/");
   
   writer.write(example, System.err);
   validate(example, reader); 
   validate(example, writer);
}
 
Example 11
Source File: SimpleExecutor.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public Duration read(TestRun test) throws Exception {
long start = System.currentTimeMillis();
   Persister persister = new Persister();
   Class schemaClass = test.getSchemaClass();
   
   // Perform once to build up schema cache
   Object result = persister.read(schemaClass, test.getSourceStream());        
   long startRead = System.currentTimeMillis();
   
   for(int i = 0; i < test.getIterations(); i++) {
      result = persister.read(schemaClass, test.getSourceStream());        
   }
   return new Duration(start, startRead, test.getIterations());
}
 
Example 12
Source File: Benchmark.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public static void main(String[] list) throws Exception {
   if(list.length < 1) {
      throw new IllegalStateException("File argument required");    
   }
   String source = list[0];
   File file = new File(source);
   
   if(!file.exists()) {
      throw new FileNotFoundException(source);
   }
   Persister persister = new Persister();
   TestExecution execution = persister.read(TestExecution.class, file);
   
   execution.execute();
}
 
Example 13
Source File: Example6.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public static void main(String[] list) throws Exception {
   Persister persister = new Persister();
   File file = new File("example6/example6.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.a);
   System.out.println(example.b);
   
   persister.write(example, System.out);
}
 
Example 14
Source File: Example8.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public static void main(String[] list) throws Exception {
   Persister persister = new Persister();
   File file = new File("example8/example8.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.b);
   System.out.println(example.c);
   
   persister.write(example, System.out);
}
 
Example 15
Source File: Example4.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public static void main(String[] list) throws Exception {
   Persister persister = new Persister();
   File file = new File("example4/example4.xml");
   Point example = persister.read(Point.class, file);
   
   System.out.println(example.x);
   System.out.println(example.y);
}
 
Example 16
Source File: Example5.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public static void main(String[] list) throws Exception {
   Persister persister = new Persister();
   File file = new File("example5/example5.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.a);
   System.out.println(example.b);
   
   persister.write(example, System.out);
}
 
Example 17
Source File: Example1.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public static void main(String[] list) throws Exception {
   Persister persister = new Persister();
   File file = new File("example1/example1.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.x);
   System.out.println(example.y);
}
 
Example 18
Source File: CaseTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testCase() throws Exception {
   Persister persister = new Persister();
   CaseExample example = persister.read(CaseExample.class, SOURCE);
   
   assertEquals(example.version, 1.0f);
   assertEquals(example.name, "example");
   assertEquals(example.URL, "http://domain.com/");
   
   validate(example, persister);
}
 
Example 19
Source File: RegistryCycleStrategyTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void testCycle() throws Exception {
   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);
   registry.bind(Dog.class, DogConverter.class);

   Pet kitty = new Cat("Kitty", 10);
   Pet lassie = new Dog("Lassie", 7);
   Pet ben = new Dog("Ben", 8);
   
   bucket.addPet(kitty);
   bucket.addPet(lassie);
   bucket.addPet(ben);
   bucket.addPet(lassie);
   bucket.addPet(kitty);
   
   persister.write(bucket, writer);
   persister.write(bucket, System.out);

   String text = writer.toString();
   PetBucket copy = persister.read(PetBucket.class, text);
   
   assertEquals(copy.getPets().get(0), bucket.getPets().get(0));
   assertEquals(copy.getPets().get(1), bucket.getPets().get(1));
   assertEquals(copy.getPets().get(2), bucket.getPets().get(2));
   assertEquals(copy.getPets().get(3), bucket.getPets().get(3));
   assertEquals(copy.getPets().get(4), bucket.getPets().get(4));
   
   assertTrue(copy.getPets().get(0) == copy.getPets().get(4)); // cycle
   assertTrue(copy.getPets().get(1) == copy.getPets().get(3)); // cycle

   assertElementExists(text, "/petBucket");
   assertElementExists(text, "/petBucket/pet");
   assertElementHasAttribute(text, "/petBucket", "id", "0");
   assertElementHasAttribute(text, "/petBucket/pet[1]", "id", "1");
   assertElementHasAttribute(text, "/petBucket/pet[2]", "id", "2");
   assertElementHasAttribute(text, "/petBucket/pet[3]", "id", "3");
   assertElementHasAttribute(text, "/petBucket/pet[4]", "reference", "2");
   assertElementHasAttribute(text, "/petBucket/pet[5]", "reference", "1");
   assertElementHasValue(text, "/petBucket/pet[1]/name", "Kitty");
   assertElementHasValue(text, "/petBucket/pet[1]/age", "10");
   assertElementHasAttribute(text, "/petBucket/pet[1]", "class", Cat.class.getName());
   assertElementHasAttribute(text, "/petBucket/pet[2]", "class", Dog.class.getName());
}
 
Example 20
Source File: AnnotationCycleStrategyTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void testCycle() throws Exception {
   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.setPrimary(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);
   
   assertEquals(copy.getEntries().get(0), list.getEntries().get(0));
   assertEquals(copy.getEntries().get(1), list.getEntries().get(1));
   assertEquals(copy.getEntries().get(2), list.getEntries().get(2));
   assertEquals(copy.getEntries().get(3), list.getEntries().get(3));
   assertEquals(copy.getEntries().get(4), list.getEntries().get(4));
   
   assertTrue(copy.getEntries().get(2) == copy.getEntries().get(4)); // cycle
   assertTrue(copy.getEntries().get(1) == copy.getEntries().get(3)); // cycle
   
   assertElementExists(text, "/entryListExample");
   assertElementExists(text, "/entryListExample/entry[1]");
   assertElementExists(text, "/entryListExample/entry[1]/name");
   assertElementExists(text, "/entryListExample/entry[1]/value");
   assertElementHasValue(text, "/entryListExample/entry[1]/name", "A");
   assertElementHasValue(text, "/entryListExample/entry[1]/value", "a");
   assertElementExists(text, "/entryListExample/entry[2]/name");
   assertElementExists(text, "/entryListExample/entry[2]/value");
   assertElementHasValue(text, "/entryListExample/entry[2]/name", "B");
   assertElementHasValue(text, "/entryListExample/entry[2]/value", "b");
   assertElementExists(text, "/entryListExample/entry[3]/name");
   assertElementExists(text, "/entryListExample/entry[3]/value");
   assertElementHasValue(text, "/entryListExample/entry[3]/name", "C");
   assertElementHasValue(text, "/entryListExample/entry[3]/value", "c");
   assertElementExists(text, "/entryListExample/entry[4]");
   assertElementExists(text, "/entryListExample/entry[5]");
   assertElementHasAttribute(text, "/entryListExample/entry[4]", "reference", "2"); // cycle
   assertElementHasAttribute(text, "/entryListExample/entry[5]", "reference", "3"); // cycle
   assertElementExists(text, "/entryListExample/primary");
   assertElementHasAttribute(text, "/entryListExample/primary", "name", "PRIMARY"); // other converter
   assertElementHasAttribute(text, "/entryListExample/primary", "value", "primary"); // other converter
}