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

The following examples show how to use org.simpleframework.xml.Serializer#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: Test5Test.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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 2
Source File: MatcherTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
public void testStringMatcher() throws Exception {
   Matcher matcher = new ExampleStringMatcher();
   Serializer serializer = new Persister(matcher);
   EmptyStringExample original = new EmptyStringExample("", null);
   StringWriter writer = new StringWriter();
   
   serializer.write(original, writer);
   
   String text = writer.toString();
   System.out.println(text);
   
   EmptyStringExample recovered = serializer.read(EmptyStringExample.class, text);
   
   assertEquals(recovered.emptyValue, original.emptyValue);
   assertEquals(recovered.nullValue, original.nullValue);
}
 
Example 3
Source File: DeviceFacadeImpl.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 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: XmlDataLoader.java    From Quiz with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 5
Source File: MapNullTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testEmptyCompositeBlankKey() throws Exception {
   Serializer serializer = new Persister();
   ComplexMap value = serializer.read(ComplexMap.class, EMPTY_COMPOSITE_BLANK_KEY);     
   boolean valid = serializer.validate(ComplexMap.class, EMPTY_COMPOSITE_BLANK_KEY);
   
   assertTrue(valid);
   
   validate(value, serializer);      
}
 
Example 6
Source File: WrapperTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
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 7
Source File: MapTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testEntryMap() throws Exception {
   Serializer serializer = new Persister();
   EntryMap example = serializer.read(EntryMap.class, ENTRY_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 8
Source File: ContactEntryTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
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 9
Source File: DeviceClassFacadeImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Parse the XML representation of the commands of a device class (which comes
 * from configuration) and return it as a list of commands.
 *
 * @param xmlString the XML representation string of the device class commands
 *
 * @return the list of commands
 * @throws Exception if the XML could not be parsed
 */
private List<Command> parseXmlCommands(String xmlString) throws Exception {
  List<Command> commands = new ArrayList<>();

  Serializer serializer = new Persister();
  CommandList commandList = serializer.read(CommandList.class, xmlString);

  for (Command command : commandList.getCommands()) {
    commands.add(command);
  }

  return commands;
}
 
Example 10
Source File: VersionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testVersion2() throws Exception {           
   Serializer persister = new Persister();
   Example example = persister.read(Example2.class, VERSION_2);      
   
   assertTrue(example instanceof Example2);
   assertEquals(example.getValue(), "text value");
   assertEquals(example.version, 2.0);
   
   persister.write(example, System.out);
   
   assertEquals(example.getValue(), "text value");
   assertEquals(example.version, 2.0);
}
 
Example 11
Source File: MapNullTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testEmptyPrimitiveKey() throws Exception {
   Serializer serializer = new Persister();
   PrimitiveMap value = serializer.read(PrimitiveMap.class, EMPTY_PRIMITIVE_KEY);     
   boolean valid = serializer.validate(PrimitiveMap.class, EMPTY_PRIMITIVE_KEY);
   
   assertTrue(valid);
   
   validate(value, serializer);      
}
 
Example 12
Source File: Test1Test.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testParameterMismatch() throws Exception{
   Serializer s = new Persister();
   StringWriter sw = new StringWriter();
   s.write(new Test1("a", "b"), sw);      
   String serializedForm = sw.toString();
   System.out.println(serializedForm);
   Test1 o = s.read(Test1.class, serializedForm);
}
 
Example 13
Source File: FileUtil.java    From game-server with MIT License 5 votes vote down vote up
/**
 * 获取xml的实例
 *
 * @param <T>
 * @param path
 * @param fileName
 * @param configClass
 * @return
 */
public static <T> T getConfigXML(String path, String fileName, Class<T> configClass) {
    T ob = null;
    fileName = path + File.separatorChar + fileName;
    if (!new File(fileName).exists()) {
        return ob;
    }
    Serializer serializer = new Persister();
    try {
        ob = serializer.read(configClass, new File(fileName));
    } catch (Exception ex) {
        log.error("文件" + fileName + "配置有误", ex);
    }
    return ob;
}
 
Example 14
Source File: Test6Test.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testSerialize() throws Exception{
   Serializer s = new Persister();
   StringWriter sw = new StringWriter();
   s.write(new Test6(new MyElementA(), new MyElementB()), sw);    
   String serializedForm = sw.toString();
   System.out.println(serializedForm);
   System.out.println();
   Test6 o = s.read(Test6.class, serializedForm);
   assertTrue(o.isListConstructorUsed());
   sw.getBuffer().setLength(0);
   s.write(o, sw);
   System.out.println(sw.toString());
   System.out.println();
}
 
Example 15
Source File: UnionWithTypeOverridesTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
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 16
Source File: MixTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testMix() throws Exception {
   Serializer serializer = new Persister();
   MixExample example = new MixExample();
   StringWriter source = new StringWriter();
   
   example.setTime(new Date());
  // example.add("text");
  // example.add(1);
  // example.add(true);
  // example.add(new Entry("1", "example 1"));
  // example.add(new Entry("2", "example 2"));
  // example.put(new Entry("1", "key 1"), new Entry("1", "value 1"));
  // example.put("key 2", "value 2");
  // example.put("key 3", 3);
  // example.put("key 4", new Entry("4", "value 4"));
   
   serializer.write(example, System.out);
   serializer.write(example, source);   
   serializer.validate(MixExample.class, source.toString());
   
   MixExample other = serializer.read(MixExample.class, source.toString());
   
   serializer.write(other, System.out);
   
  // assertEquals(example.get(0), "text");
  // assertEquals(example.get(1), 1);      
  // assertEquals(example.get(2), true);
   
   validate(example, serializer);
}
 
Example 17
Source File: MapTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
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 18
Source File: SimpleConstructorInjectionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testConstructorInjection() throws Exception{
   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);
   System.out.println("message: "+example.getOptional());
}
 
Example 19
Source File: UnionWithTypeOverridesTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
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 20
Source File: MapTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
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);
}