org.simpleframework.xml.Serializer Java Examples
The following examples show how to use
org.simpleframework.xml.Serializer.
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: NamespaceInheritanceTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testNamespace() throws Exception { Aaa parent = new Aaa(); Bbb child = new Bbb(); parent.bbb = child; Aaa grandchild = new Aaa(); child.aaa = grandchild; grandchild.bbb = new Bbb(); ByteArrayOutputStream tmp = new ByteArrayOutputStream(); Serializer serializer = new Persister(); serializer.write(parent, tmp); String result = new String(tmp.toByteArray()); System.out.println(result); assertElementHasAttribute(result, "/aaa", "xmlns", "namespace1"); assertElementHasAttribute(result, "/aaa/bbb", "xmlns", "namespace2"); assertElementHasAttribute(result, "/aaa/bbb/aaa", "xmlns", "namespace1"); assertElementHasAttribute(result, "/aaa/bbb/aaa/bbb", "xmlns", "namespace2"); assertElementHasNamespace(result, "/aaa", "namespace1"); assertElementHasNamespace(result, "/aaa/bbb", "namespace2"); assertElementHasNamespace(result, "/aaa/bbb/aaa", "namespace1"); assertElementHasNamespace(result, "/aaa/bbb/aaa/bbb", "namespace2"); }
Example #2
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 #3
Source File: Test4_2Test.java From simplexml with Apache License 2.0 | 6 votes |
public void testSerialization() throws Exception{ Serializer s = new Persister(); StringWriter sw = new StringWriter(); String serializedForm = "<test4>\n" + " <single-element class=\"org.simpleframework.xml.core.Test4_2Test$MyElementC\"/>\n" + "</test4>"; System.out.println(serializedForm); System.out.println(); Test4_2 o = s.read(Test4_2.class, serializedForm); //FIXME read ignores the class statement MyElementC ec = (MyElementC) o.element; sw.getBuffer().setLength(0); s.write(new Test4_2(new MyElementC()), sw); //FIXME it would be great, if this worked. Actually it works for ElementUnionLists. System.out.println(sw.toString()); System.out.println(); }
Example #4
Source File: NotificationConfigurator.java From oneops with Apache License 2.0 | 6 votes |
public void init() { InputStream is = this.getClass().getResourceAsStream("/notification.config.xml"); if (is == null) { logger.warn("Notification config file not found!"); return; } Serializer serializer = new Persister(); try { this.ruleList = serializer.read(NotificationRuleList.class, is); is.close(); } catch (Exception e) { logger.error("Read configuration file error!"); e.printStackTrace(); } if (ruleList != null && ruleList.getRules() != null) { ruleMap = new HashMap<>(); for (NotificationRule rule : ruleList.getRules()) { EventSource source = rule.getSource(); if (!ruleMap.containsKey(source)) { ruleMap.put(source, new ArrayList<>()); } ruleMap.get(source).add(rule); } configured = true; } }
Example #5
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 #6
Source File: DynamicMapOfAttributesTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testConverter() throws Exception { Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); StringWriter buffer = new StringWriter(); Car car = serializer.read(Car.class, SOURCE, false); assertNotNull(car); assertEquals(car.length, 3.3); assertEquals(car.color, "green"); assertEquals(car.nrOfDoors, 2); assertEquals(car.furtherAttributes.get("topSpeed"), "190"); assertEquals(car.furtherAttributes.get("brand"), "audi"); serializer.write(car, System.out); serializer.write(car, buffer); String text = buffer.toString(); assertElementExists(text, "/Car"); assertElementHasAttribute(text, "/Car", "length", "3.3"); assertElementHasAttribute(text, "/Car", "color", "green"); assertElementHasAttribute(text, "/Car", "nrOfDoors", "2"); assertElementHasAttribute(text, "/Car", "topSpeed", "190"); assertElementHasAttribute(text, "/Car", "brand", "audi"); }
Example #7
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 #8
Source File: UARTActivity.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void onRenameConfiguration(final String newName) { final boolean exists = databaseHelper.configurationExists(newName); if (exists) { Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show(); return; } final String oldName = configuration.getName(); configuration.setName(newName); try { final Format format = new Format(new HyphenStyle()); final Strategy strategy = new VisitorStrategy(new CommentVisitor()); final Serializer serializer = new Persister(strategy, format); final StringWriter writer = new StringWriter(); serializer.write(configuration, writer); final String xml = writer.toString(); databaseHelper.renameConfiguration(oldName, newName, xml); wearableSynchronizer.onConfigurationAddedOrEdited(preferences.getLong(PREFS_CONFIGURATION, 0), configuration); refreshConfigurations(); } catch (final Exception e) { Log.e(TAG, "Error while renaming configuration", e); } }
Example #9
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 #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: ConverterMapTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testMap() throws Exception { Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy); MapHolder holder = new MapHolder(); holder.put("a", "A"); holder.put("b", "B"); holder.put("c", "C"); holder.put("d", "D"); holder.put("e", "E"); holder.put("f", "F"); serializer.write(holder, System.out); validate(holder, serializer); }
Example #12
Source File: XmlDataLoader.java From Quiz with GNU General Public License v2.0 | 6 votes |
/** * Loads XML with quiz and returns {@link Quiz} object. * * @return quiz object * @throws Exception * when deserialization fails */ public Quiz loadXml() throws Exception { Resources resources = context.getResources(); String languageCode = getLanguageCode(resources); // Get XML name using reflection Field field = null; String prefix = context.getString(R.string.xml_prefix); try { field = R.raw.class.getField(prefix + languageCode); } catch (NoSuchFieldException e) { // If there is no language available use default field = R.raw.class.getField(prefix + context.getString(R.string.default_language)); } // Create InputSream from XML resource InputStream source = resources.openRawResource(field.getInt(null)); // Parse XML Serializer serializer = new Persister(); return serializer.read(Quiz.class, source); }
Example #13
Source File: EmptyMapEntryTest.java From simplexml with Apache License 2.0 | 6 votes |
/** * @param args the command line arguments */ public void testEmptyMapEntry() throws Exception { 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()); assertEquals(bug1.test1.data.get("key1"), bug2.test1.data.get("key1")); assertEquals(bug1.test1.data.get("key2"), bug2.test1.data.get("key2")); assertEquals(bug2.test1.data.get("key1"), "value1"); assertEquals(bug2.test1.data.get("key2"), "value1"); assertNull(bug2.test1.data.get("key3")); assertNull(bug2.test1.data.get(null)); validate(s, bug1); }
Example #14
Source File: UnionEmptyListBugTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testMapBug() throws Exception { Serializer serializer = new Persister(); ElementMapUnionBug element = new ElementMapUnionBug(); StringWriter writer = new StringWriter(); serializer.write(element, writer); String text = writer.toString(); assertElementExists(text, "/elementMapUnionBug"); assertElementDoesNotExist(text, "/elementMapUnionBug/string"); assertElementDoesNotExist(text, "/elementMapUnionBug/integer"); writer = new StringWriter(); writer = new StringWriter(); element.values.put("A", "string"); element.values.put("B", 1); serializer.write(element, writer); text = writer.toString(); System.out.println(text); assertElementExists(text, "/elementMapUnionBug/string"); assertElementHasValue(text, "/elementMapUnionBug/string/string[1]", "A"); assertElementHasValue(text, "/elementMapUnionBug/string/string[2]", "string"); assertElementExists(text, "/elementMapUnionBug/integer"); assertElementHasValue(text, "/elementMapUnionBug/integer/string", "B"); assertElementHasValue(text, "/elementMapUnionBug/integer/integer", "1"); }
Example #15
Source File: SimpleXmlParser.java From openkeepass with Apache License 2.0 | 6 votes |
public ByteArrayOutputStream toXml(Object objectToSerialize) { try { RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(Boolean.class, BooleanSimpleXmlAdapter.class); matcher.bind(GregorianCalendar.class, CalendarSimpleXmlAdapter.class); matcher.bind(UUID.class, UUIDSimpleXmlAdapter.class); matcher.bind(byte[].class, ByteSimpleXmlAdapter.class); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); TreeStrategyWithoutArrayLength strategy = new TreeStrategyWithoutArrayLength(); Serializer serializer = new Persister(strategy, matcher); serializer.write(objectToSerialize, outputStream); return outputStream; } catch (Exception e) { throw new KeePassDatabaseUnwriteableException("Could not serialize object to xml", e); } }
Example #16
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 #17
Source File: Test2Test.java From simplexml with Apache License 2.0 | 6 votes |
public void testException() throws Exception{ boolean failure = false; try { Serializer s = new Persister(); StringWriter sw = new StringWriter(); s.write(new Test2(new MyElementA(), new MyElementB()), sw); String serializedForm = sw.toString(); System.out.println(serializedForm); System.out.println(); Test2 o = s.read(Test2.class, serializedForm); sw.getBuffer().setLength(0); s.write(o, sw); System.out.println(sw.toString()); System.out.println(); }catch(Exception e) { e.printStackTrace(); failure = true; } assertTrue("Annotations are invalid as they use the same name", failure); }
Example #18
Source File: Test1_ReplaceTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testReplace() throws Exception{ Serializer s = new Persister(); StringWriter sw = new StringWriter(); s.write(new Test1A(null), sw); String serializedForm = sw.toString(); System.out.println(serializedForm); System.out.println(); Test1 o = s.read(Test1.class, serializedForm); sw.getBuffer().setLength(0); //Unnecessary constructor exception a write, note that this happens with the default constructor only (see above) // s.write(new Test1B(), sw); serializedForm = sw.toString(); System.out.println(serializedForm); // o = s.read(Test1.class, serializedForm); }
Example #19
Source File: SuperTypeTest.java From simplexml with Apache License 2.0 | 6 votes |
public void testSuperType() throws Exception { Map<String, String> clazMap = new HashMap<String, String> (); clazMap.put("subtype1", SubType1.class.getName()); clazMap.put("subtype2", SubType2.class.getName()); Visitor visitor = new ClassToNamespaceVisitor(false); Strategy strategy = new VisitorStrategy(visitor); Serializer serializer = new Persister(strategy); MyMap map = new MyMap(); SubType1 subtype1 = new SubType1(); SubType2 subtype2 = new SubType2(); StringWriter writer = new StringWriter(); subtype1.text = "subtype1"; subtype2.superType = subtype1; map.getInternalMap().put("one", subtype1); map.getInternalMap().put("two", subtype2); serializer.write(map, writer); serializer.write(map, System.out); serializer.read(MyMap.class, writer.toString()); }
Example #20
Source File: UARTActivity.java From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Saves the given configuration in the database. */ private void saveConfiguration() { final UartConfiguration configuration = this.configuration; try { final Format format = new Format(new HyphenStyle()); final Strategy strategy = new VisitorStrategy(new CommentVisitor()); final Serializer serializer = new Persister(strategy, format); final StringWriter writer = new StringWriter(); serializer.write(configuration, writer); final String xml = writer.toString(); databaseHelper.updateConfiguration(configuration.getName(), xml); wearableSynchronizer.onConfigurationAddedOrEdited(preferences.getLong(PREFS_CONFIGURATION, 0), configuration); } catch (final Exception e) { Log.e(TAG, "Error while creating a new configuration", e); } }
Example #21
Source File: UnionWithTypeOverridesTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testMyElementWithovOverrideC() throws Exception{ Serializer persister = new Persister(); OverrideTypeExample example = persister.read(OverrideTypeExample.class, SINGLE_ELEMENT_WITH_OVERRIDE_C); assertEquals(example.element.getClass(), MyElementC.class); StringWriter writer = new StringWriter(); persister.write(example, writer); persister.write(example, System.out); String text = writer.toString(); assertElementExists(text, "/test4/single-element"); assertElementHasAttribute(text, "/test4/single-element", "class", "org.simpleframework.xml.core.UnionWithTypeOverridesTest$MyElementC"); }
Example #22
Source File: MapTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testComplexMap() throws Exception { Serializer serializer = new Persister(); ComplexMap example = serializer.read(ComplexMap.class, COMPLEX_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); }
Example #23
Source File: InlineMapTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testPrimitiveAttributeValueMap() throws Exception { 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); }
Example #24
Source File: DeviceFacadeImpl.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
/** * Parse the XML representation of the commands of a device (which comes from * configuration) and return it as a list of {@link DeviceCommand} objects. * * @param xmlString the XML representation string of the device commands * * @return the list of device commands * @throws Exception if the XML could not be parsed */ private List<DeviceCommand> parseDeviceCommandsXML(String xmlString) throws Exception { List<DeviceCommand> deviceCommands = new ArrayList<>(); Serializer serializer = new Persister(); DeviceCommandList deviceCommandList = serializer.read(DeviceCommandList.class, xmlString); for (DeviceCommand deviceCommand : deviceCommandList.getDeviceCommands()) { deviceCommands.add(deviceCommand); } return deviceCommands; }
Example #25
Source File: UnionWithTypeOverridesTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testMyElementA() throws Exception{ Serializer persister = new Persister(); OverrideTypeExample example = persister.read(OverrideTypeExample.class, SINGLE_ELEMENT_A); assertEquals(example.element.getClass(), MyElementA.class); StringWriter writer = new StringWriter(); persister.write(example, writer); persister.write(example, System.out); String text = writer.toString(); assertElementExists(text, "/test4/single-elementA"); assertElementDoesNotHaveAttribute(text, "/test4/single-elementA", "class", "org.simpleframework.xml.core.UnionWithTypeOverridesTest$MyElementA"); }
Example #26
Source File: MapTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testPrimitiveMap() throws Exception { Serializer serializer = new Persister(); PrimitiveMap example = serializer.read(PrimitiveMap.class, PRIMITIVE_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 #27
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 #28
Source File: ValidateTest.java From simplexml with Apache License 2.0 | 5 votes |
public void testExtraElement() throws Exception { Serializer persister = new Persister(); boolean success = false; try { success = persister.validate(TextList.class, EXTRA_ELEMENT); } catch(Exception e) { e.printStackTrace(); success = true; } assertTrue(success); }
Example #29
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 #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); }