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

The following examples show how to use org.simpleframework.xml.core.Persister#write() . 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: CombinedStrategyTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testCombinationStrategyWithStyle() throws Exception {
   Registry registry = new Registry();
   AnnotationStrategy annotationStrategy = new AnnotationStrategy();
   RegistryStrategy registryStrategy = new RegistryStrategy(registry, annotationStrategy);
   Style style = new HyphenStyle();
   Format format = new Format(style);
   Persister persister = new Persister(registryStrategy, format);
   CombinationExample example = new CombinationExample(1, 2, 3);
   StringWriter writer = new StringWriter();
   
   registry.bind(Item.class, RegistryItemConverter.class);
   persister.write(example, writer);
   
   String text = writer.toString();
   System.out.println(text);
   
   assertElementExists(text, "/combination-example/item/value");
   assertElementHasValue(text, "/combination-example/item/value", "1");
   assertElementHasValue(text, "/combination-example/item/type", RegistryItemConverter.class.getName());
   assertElementExists(text, "/combination-example/overridden-item");
   assertElementHasAttribute(text, "/combination-example/overridden-item", "value", "2");
   assertElementHasAttribute(text, "/combination-example/overridden-item", "type", AnnotationItemConverter.class.getName());
   assertElementExists(text, "/combination-example/extended-item");
   assertElementHasAttribute(text, "/combination-example/extended-item", "value", "3");
   assertElementHasAttribute(text, "/combination-example/extended-item", "type", ExtendedItemConverter.class.getName());      
}
 
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: 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 4
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 5
Source File: RegistryMatcherTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testMatcher() throws Exception {
   RegistryMatcher matcher = new RegistryMatcher();
   Transform<C> transform = new CTransform();
   matcher.bind(A.class, ATransform.class);
   matcher.bind(B.class, BTransform.class);
   matcher.bind(C.class, transform);
   Transform<A> a = matcher.match(A.class);
   Transform<B> b = matcher.match(B.class);
   Transform<C> c = matcher.match(C.class);
   A ia = a.read("A");
   B ib = b.read("B");
   C ic = c.read("C");
   assertEquals(ia.getValue(), "A");
   assertEquals(ib.getValue(), "B");
   assertEquals(ic.getValue(), "C");
   Persister persister = new Persister(matcher);
   Example example = new Example(ia, ib, ic);
   StringWriter writer = new StringWriter();
   persister.write(example, writer);
   String text = writer.toString();
   assertElementExists(text, "/example");
   assertElementExists(text, "/example/a");
   assertElementHasValue(text, "/example/a", "A");
   assertElementHasAttribute(text, "/example", "b", "B");
   assertElementHasAttribute(text, "/example", "c", "C");
}
 
Example 6
Source File: Example11.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("example11/example11.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.value);
   
   persister.write(example, System.out);
}
 
Example 7
Source File: CommentTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testComment() throws Exception {
   Visitor visitor = new CommentVisitor();
   Strategy strategy = new VisitorStrategy(visitor);
   Persister persister = new Persister(strategy);
   CommentExample example = new CommentExample();
   
   example.name = "Some Name";
   example.value = "A value to use";
   example.price = 9.99;
   
   persister.write(example, System.out);
}
 
Example 8
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 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: EmailTemplateServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public String exportTemplateAsXml(String key, Locale locale) {
	
	EmailTemplate template = getEmailTemplate(key, locale);
	Persister persister = new Persister();
	File file = null;
	String ret = null;
	try {
		file = File.createTempFile("emailtemplate", "xml");
		persister.write(template, file);
		//read the data
		ret = readFile(file.getAbsolutePath());
	} catch (Exception e) {
		log.warn( "Error creating or writing to file", e );
	}
	finally {
		if (file != null) {
			if (!file.delete()) {
				log.warn("error deleting tmp file");
			}
		}
		
	}
	
	
	
	
	return ret;
}
 
Example 11
Source File: NamespaceTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testNamespace() throws Exception {
   Persister persister = new Persister();
   Employer employer = new Employer("Spam Soft", "Sesame Street", true, 1000);
   Profession job = new Profession("Software Engineer", 10, 12, employer);
   Person person = new Person("John Doe", "Person", 30, job);
   
   persister.write(person, System.out);
   
   validate(persister, person);
}
 
Example 12
Source File: SerializeRandomListOfObjectsTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testSerializeRandomList() throws Exception {
   List list = new ArrayList();
   list.add("x");
   list.add(10);
   list.add(new Date());
   ListArgument argument = new ListArgument(list);
   Persister persister = new Persister();
   persister.write(argument, System.out);
   
}
 
Example 13
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 14
Source File: Example10.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("example10/example10.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.list);
   
   persister.write(example, System.out);
}
 
Example 15
Source File: Example9.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("example9/example9.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.name);
   
   persister.write(example, System.out);
}
 
Example 16
Source File: Example7.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("example7/example7.xml");
   Example example = persister.read(Example.class, file);
   
   System.out.println(example.x);
   
   persister.write(example, System.out);
}
 
Example 17
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 18
Source File: StringArrayTransformTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void testPersistence() throws Exception {
   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);
   
   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");
   
   validate(example, persister);      
}
 
Example 19
Source File: PrimitiveArrayTransformTest.java    From simplexml with Apache License 2.0 4 votes vote down vote up
public void testCyclicPersistence() throws Exception {
   int[] list = new int[] { 1, 2, 3, 4 };
   CycleStrategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   IntegerArrayExample example = new IntegerArrayExample(list);
   StringWriter out = new StringWriter();
   
   assertEquals(example.attribute[0], 1);
   assertEquals(example.attribute[1], 2);
   assertEquals(example.attribute[2], 3);
   assertEquals(example.attribute[3], 4);
   assertEquals(example.element[0], 1);
   assertEquals(example.element[1], 2);
   assertEquals(example.element[2], 3);
   assertEquals(example.element[3], 4);      
   assertEquals(example.list.get(0)[0], 1);
   assertEquals(example.list.get(0)[1], 2);
   assertEquals(example.list.get(0)[2], 3);
   assertEquals(example.list.get(0)[3], 4);
   assertEquals(example.array[0][0], 1);
   assertEquals(example.array[0][1], 2);
   assertEquals(example.array[0][2], 3);
   assertEquals(example.array[0][3], 4);
   
   persister.write(example, out);
   String text = out.toString();
   
   assertElementHasAttribute(text, "/integerArrayExample", "id", "0");
   assertElementHasAttribute(text, "/integerArrayExample/element", "id", "1");
   assertElementHasAttribute(text, "/integerArrayExample/list", "id", "2");
   assertElementHasAttribute(text, "/integerArrayExample/array", "id", "3");
   assertElementHasAttribute(text, "/integerArrayExample/list/int", "reference", "1");
   assertElementHasAttribute(text, "/integerArrayExample/array/int", "reference", "1");      
   assertElementHasValue(text, "/integerArrayExample/element", "1, 2, 3, 4");
   
   validate(example, persister);
   
   example = new IntegerArrayExample(null);
   out = new StringWriter();
   persister.write(example, out);
   text = out.toString();
   
   validate(example, persister);
   
   example = persister.read(IntegerArrayExample.class, text);
   
   assertEquals(example.attribute, null);
   assertEquals(example.element, null);     
   assertEquals(example.list.size(), 0);
   assertEquals(example.array[0], null);
   
   assertElementHasAttribute(text, "/integerArrayExample", "id", "0");
   assertElementHasAttribute(text, "/integerArrayExample/list", "id", "1");
   assertElementHasAttribute(text, "/integerArrayExample/array", "id", "2");
}
 
Example 20
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());
}