org.simpleframework.xml.core.Persister Java Examples
The following examples show how to use
org.simpleframework.xml.core.Persister.
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: AnnotationTypeTest.java From simplexml with Apache License 2.0 | 6 votes |
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 #2
Source File: HideEnclosingConverterTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testWrapper() throws Exception{ Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); Entry entry = new Entry("name", "value"); EntryHolder holder = new EntryHolder(entry, "test", 10); StringWriter writer = new StringWriter(); serializer.write(holder, writer); System.out.println(writer.toString()); serializer.read(EntryHolder.class, writer.toString()); System.err.println(writer.toString()); String sourceXml = writer.toString(); assertElementExists(sourceXml, "/entryHolder"); assertElementHasAttribute(sourceXml, "/entryHolder", "code", "10"); assertElementExists(sourceXml, "/entryHolder/entry"); assertElementExists(sourceXml, "/entryHolder/entry/name"); assertElementHasValue(sourceXml, "/entryHolder/entry/name", "name"); assertElementExists(sourceXml, "/entryHolder/entry/value"); assertElementHasValue(sourceXml, "/entryHolder/entry/value", "value"); assertElementExists(sourceXml, "/entryHolder/name"); assertElementHasValue(sourceXml, "/entryHolder/name", "test"); }
Example #3
Source File: DeviceFacadeImpl.java From c2mon with GNU Lesser General Public License v3.0 | 6 votes |
/** * Parse the XML representation of the properties of a device (which comes * from configuration) and return it as a list of {@link DeviceProperty} * objects. * * @param xmlString the XML representation string of the device properties * * @return the list of device properties * @throws Exception if the XML could not be parsed */ private List<DeviceProperty> parseDevicePropertiesXML(String xmlString) throws Exception { List<DeviceProperty> deviceProperties = new ArrayList<>(); Serializer serializer = new Persister(); DevicePropertyList devicePropertyList = serializer.read(DevicePropertyList.class, xmlString); for (DeviceProperty deviceProperty : devicePropertyList.getDeviceProperties()) { // Remove all whitespace and control characters if (deviceProperty.getValue() != null) { deviceProperty.setValue(deviceProperty.getValue().replaceAll("[\u0000-\u001f]", "").trim()); } deviceProperties.add(deviceProperty); } return deviceProperties; }
Example #4
Source File: AlarmValueImpl.java From c2mon with GNU Lesser General Public License v3.0 | 6 votes |
public static AlarmValueImpl fromXml(final String xml) throws Exception { AlarmValueImpl alarmVal = null; StringReader sr = null; Serializer serializer = new Persister(new AnnotationStrategy()); try { sr = new StringReader(xml); alarmVal = serializer.read(AlarmValueImpl.class, new StringReader(xml), false); } finally { if (sr != null) { sr.close(); } } return alarmVal; }
Example #5
Source File: DateTransformTest.java From simplexml with Apache License 2.0 | 6 votes |
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 #6
Source File: Test5Test.java From simplexml with Apache License 2.0 | 6 votes |
public void testSerialize() throws Exception{ Serializer s = new Persister(); StringWriter sw = new StringWriter(); //FIXME serialization is ok s.write(new Test5(new MyElementA(), new MyElementA(), new MyElementB()), sw); String serializedForm = sw.toString(); System.out.println(serializedForm); System.out.println(); //FIXME but no idea what is happening Test5 o = s.read(Test5.class, serializedForm); sw.getBuffer().setLength(0); s.write(o, sw); System.out.println(sw.toString()); System.out.println(); sw.getBuffer().setLength(0); }
Example #7
Source File: RegistryMatcherTest.java From simplexml with Apache License 2.0 | 6 votes |
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 #8
Source File: FilterTest.java From simplexml with Apache License 2.0 | 6 votes |
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 #9
Source File: SimpleExecutor.java From simplexml with Apache License 2.0 | 6 votes |
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 #10
Source File: RegistryConverterTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testPersonConverter() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Registry registry = new Registry(); Person person = new Person("Niall", 30); RegistryStrategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy, format); Converter converter = new PersonConverter(serializer); StringWriter writer = new StringWriter(); registry.bind(Person.class, converter); PersonProfile profile = new PersonProfile(person); serializer.write(profile, writer); System.out.println(writer.toString()); PersonProfile read = serializer.read(PersonProfile.class, writer.toString()); assertEquals(read.person.name, "Niall"); assertEquals(read.person.age, 30); }
Example #11
Source File: IppAttributeProvider.java From cups4j with GNU Lesser General Public License v3.0 | 6 votes |
private IppAttributeProvider() { try { InputStream tagListStream = IIppAttributeProvider.class.getClassLoader().getResourceAsStream(TAG_LIST_FILENAME); InputStream attListStream = IIppAttributeProvider.class.getClassLoader().getResourceAsStream( ATTRIBUTE_LIST_FILENAME); Serializer serializer = new Persister(); TagList tList = serializer.read(TagList.class, tagListStream); tagList = tList.getTag(); AttributeList aList = serializer.read(AttributeList.class, attListStream); attributeGroupList = aList.getAttributeGroup(); } catch (Exception e) { throw new RuntimeException(e); } }
Example #12
Source File: OrderTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testSerializationOrder() throws Exception { Serializer serializer = new Persister(); OrderExample example = new OrderExample("first", "second", "third", "fourth", 1, 2, 3.0); StringWriter writer = new StringWriter(); serializer.write(example, writer); validate(example, serializer); String text = writer.toString(); assertTrue(text.indexOf("first") < text.indexOf("second")); assertTrue(text.indexOf("second") < text.indexOf("third")); assertTrue(text.indexOf("third") < text.indexOf("fourth")); assertTrue(text.indexOf("one") < text.indexOf("two")); assertTrue(text.indexOf("two") < text.indexOf("three")); example = new OrderExample("1st", "2nd", "3rd", "4th", 10, 20, 30.0); validate(example, serializer); }
Example #13
Source File: RegistryConverterCycleTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testCycle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Registry registry = new Registry(); Address address = new Address("An Address"); Person person = new Person(address, "Niall", 30); CycleStrategy referencer = new CycleStrategy(); RegistryStrategy strategy = new RegistryStrategy(registry, referencer); Serializer serializer = new Persister(strategy, format); Converter converter = new PersonConverter(serializer); Club club = new Club(address); club.addMember(person); registry.bind(Person.class, converter); serializer.write(club, System.out); }
Example #14
Source File: VisitorStrategyTest.java From simplexml with Apache License 2.0 | 6 votes |
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 #15
Source File: MapCycleTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testPrimitiveMap() throws Exception { Strategy strategy = new CycleStrategy(); Serializer serializer = new Persister(strategy); PrimitiveMap example = serializer.read(PrimitiveMap.class, PRIMITIVE_MAP); assertEquals(1.0, example.getValue("one")); assertEquals(1.0, example.getValue("two")); assertEquals(4.0, example.getValue("three")); validate(example, serializer); }
Example #16
Source File: MapCycleTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testComplexMap() throws Exception { Strategy strategy = new CycleStrategy(); Serializer serializer = new Persister(strategy); ComplexMap example = serializer.read(ComplexMap.class, COMPLEX_MAP); assertEquals("example 2", example.getValue(new CompositeKey("name 1", "address 1"))); assertEquals("example 2", example.getValue(new CompositeKey("name 3", "address 3"))); assertEquals("example 4", example.getValue(new CompositeKey("name 4", "address 4"))); validate(example, serializer); }
Example #17
Source File: MapTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testStringMap() throws Exception { Serializer serializer = new Persister(); StringMap example = serializer.read(StringMap.class, STRING_MAP); assertEquals("example 1", example.getValue("a")); assertEquals("example 2", example.getValue("b")); assertEquals("example 3", example.getValue("c")); assertEquals("example 4", example.getValue("d")); validate(example, serializer); }
Example #18
Source File: MapTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testPrimitiveValueOverrideMap() throws Exception { Serializer serializer = new Persister(); PrimitiveValueOverrideMap example = serializer.read(PrimitiveValueOverrideMap.class, PRIMITIVE_VALUE_OVERRIDE_MAP); assertEquals(new BigDecimal("1.0"), example.getValue("one")); assertEquals(new BigDecimal("2.0"), example.getValue("two")); assertEquals(new BigDecimal("3.0"), example.getValue("three")); assertEquals(new BigDecimal("4.0"), example.getValue("four")); validate(example, serializer); }
Example #19
Source File: AnnotationConverterTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testAnnotationConversion() throws Exception { Strategy strategy = new TreeStrategy(); Strategy converter = new AnnotationStrategy(strategy); Serializer serializer = new Persister(converter); StringWriter writer = new StringWriter(); FarmExample example = serializer.read(FarmExample.class, SOURCE); assertEquals(example.getCow().getName(), "Bull"); assertEquals(example.getCow().getAge(), 4); assertEquals(example.getCow().getLegs(), 4); assertEquals(example.getChicken().getName(), "Hen"); assertEquals(example.getChicken().getAge(), 1); assertEquals(example.getChicken().getLegs(), 2); serializer.write(example, System.out); serializer.write(example, writer); String text = writer.toString(); assertElementHasAttribute(text, "/farmExample/chicken", "name", "Hen"); assertElementHasAttribute(text, "/farmExample/chicken", "age", "1"); assertElementHasAttribute(text, "/farmExample/chicken", "legs", "2"); assertElementHasAttribute(text, "/farmExample/cow", "name", "Bull"); assertElementHasAttribute(text, "/farmExample/cow", "age", "4"); assertElementHasAttribute(text, "/farmExample/cow", "legs", "4"); }
Example #20
Source File: ValidateTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testNameMissing() throws Exception { Serializer persister = new Persister(); boolean success = false; try { success = persister.validate(TextList.class, NAME_MISSING); } catch(Exception e) { e.printStackTrace(); success = true; } assertTrue(success); }
Example #21
Source File: AnnotationStrategyTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testAnnotationStrategy() throws Exception { Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); StringWriter writer = new StringWriter(); FarmExample example = serializer.read(FarmExample.class, SOURCE); example.getTime().add(10); example.getTime().add(11); example.getTime().add(12); assertEquals(example.getCow().getName(), "Bull"); assertEquals(example.getCow().getAge(), 4); assertEquals(example.getCow().getLegs(), 4); assertEquals(example.getChicken().getName(), "Hen"); assertEquals(example.getChicken().getAge(), 1); assertEquals(example.getChicken().getLegs(), 2); serializer.write(example, System.out); serializer.write(example, writer); String text = writer.toString(); assertElementExists(text, "/farmExample/chicken"); assertElementExists(text, "/farmExample/cow"); assertElementHasNamespace(text, "/farmExample/chicken", "http://www.domain.com/test"); assertElementDoesNotHaveNamespace(text, "/farmExample/cow", "http://www.domain.com/test"); assertElementHasAttribute(text, "/farmExample/chicken", "name", "Hen"); assertElementHasAttribute(text, "/farmExample/chicken", "age", "1"); assertElementHasAttribute(text, "/farmExample/chicken", "legs", "2"); assertElementHasAttribute(text, "/farmExample/cow", "name", "Bull"); assertElementHasAttribute(text, "/farmExample/cow", "age", "4"); assertElementHasAttribute(text, "/farmExample/cow", "legs", "4"); }
Example #22
Source File: ContactEntryTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testContact() throws Exception { Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); EntryList list = new EntryList("Other", "Value"); StringWriter writer = new StringWriter(); list.getList().add(new Entry("a", "A")); list.getList().add(new Entry("b", "B")); list.getList().add(new Entry("c", "C")); list.getOtherList().add(new Entry("1", "ONE")); list.getOtherList().add(new Entry("2", "TWO")); list.getOtherList().add(new Entry("3", "THREE")); serializer.write(list, writer); String text = writer.toString(); EntryList copy = serializer.read(EntryList.class, text); assertEquals(copy.getList().get(0).getName(), list.getList().get(0).getName()); assertEquals(copy.getList().get(0).getValue(), list.getList().get(0).getValue()); assertEquals(copy.getList().get(1).getName(), list.getList().get(1).getName()); assertEquals(copy.getList().get(1).getValue(), list.getList().get(1).getValue()); assertEquals(copy.getList().get(2).getName(), list.getList().get(2).getName()); assertEquals(copy.getList().get(2).getValue(), list.getList().get(2).getValue()); assertEquals(copy.getOtherList().get(0).getName(), list.getOtherList().get(0).getName()); assertEquals(copy.getOtherList().get(0).getValue(), list.getOtherList().get(0).getValue()); assertEquals(copy.getOtherList().get(1).getName(), list.getOtherList().get(1).getName()); assertEquals(copy.getOtherList().get(1).getValue(), list.getOtherList().get(1).getValue()); assertEquals(copy.getOtherList().get(2).getName(), list.getOtherList().get(2).getName()); assertEquals(copy.getOtherList().get(2).getValue(), list.getOtherList().get(2).getValue()); System.out.println(text); }
Example #23
Source File: MapNullTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testEmptyPrimitiveBlankValue() throws Exception { Serializer serializer = new Persister(); PrimitiveMap value = serializer.read(PrimitiveMap.class, EMPTY_PRIMITIVE_BLANK_VALUE); boolean valid = serializer.validate(PrimitiveMap.class, EMPTY_PRIMITIVE_BLANK_VALUE); assertTrue(valid); validate(value, serializer); }
Example #24
Source File: CommentTest.java From simplexml with Apache License 2.0 | 5 votes |
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 #25
Source File: MapNullTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testEmptyCompositeValue() throws Exception { Serializer serializer = new Persister(); ComplexMap value = serializer.read(ComplexMap.class, EMPTY_COMPOSITE_VALUE); boolean valid = serializer.validate(ComplexMap.class, EMPTY_COMPOSITE_VALUE); assertTrue(valid); validate(value, serializer); }
Example #26
Source File: WrapperTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testWrapper() throws Exception{ Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); Entry entry = new Entry("name", "value"); Wrapper wrapper = new Wrapper(entry); WrapperExample example = new WrapperExample(wrapper); WrapperConverter converter = new WrapperConverter(serializer); StringWriter writer = new StringWriter(); registry.bind(Wrapper.class, converter); serializer.write(example, writer); serializer.read(WrapperExample.class, writer.toString()); System.err.println(writer.toString()); }
Example #27
Source File: ValidateTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testTextMissing() throws Exception { Serializer persister = new Persister(); boolean success = false; try { success = persister.validate(TextList.class, TEXT_MISSING); } catch(Exception e) { e.printStackTrace(); success = true; } assertTrue(success); }
Example #28
Source File: SimpleExecutor.java From simplexml with Apache License 2.0 | 5 votes |
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 #29
Source File: MapNullTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testEmptyCompositeBlankValue() throws Exception { Serializer serializer = new Persister(); ComplexMap value = serializer.read(ComplexMap.class, EMPTY_COMPOSITE_BLANK_VALUE); boolean valid = serializer.validate(ComplexMap.class, EMPTY_COMPOSITE_BLANK_VALUE); assertTrue(valid); validate(value, serializer); }
Example #30
Source File: MapTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testComplexValueKeyOverrideMap() throws Exception { Serializer serializer = new Persister(); ComplexValueKeyOverrideMap example = serializer.read(ComplexValueKeyOverrideMap.class, COMPLEX_VALUE_KEY_OVERRIDE_MAP); assertEquals("example 1", example.getValue(new CompositeKey("name 1", "address 1"))); assertEquals("example 2", example.getValue(new CompositeKey("name 2", "address 2"))); assertEquals("example 3", example.getValue(new CompositeKey("name 3", "address 3"))); assertEquals("example 4", example.getValue(new CompositeKey("name 4", "address 4"))); validate(example, serializer); }