@Test public void testSimpleXmlHandling() throws Exception { // From bytes. Persister p = new Persister(); Example e = p.read(Example.class, new ByteArrayInputStream(utf8Xml)); assertThat(e.text).isEqualTo(" \u0096 "); // round trip. ByteArrayOutputStream baos = new ByteArrayOutputStream(); p.write(e, baos); e = p.read(Example.class, new ByteArrayInputStream(utf8Xml)); assertThat(e.text).isEqualTo(" \u0096 "); }
public void testXML() throws Exception { @Root @Namespace class C1 { @Element private int i; @Element private int j; } C1 c1 = new C1(); Persister serializer = new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>")); StringWriter sw = new StringWriter(); serializer.write(c1, sw); System.out.println(sw.getBuffer().toString()); String str = "<c1><i>1</i><j>2</j></c1>"; serializer.read(c1, str); }
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); }
/** Save attributes to an XML file. */ static void saveAttributes(IPath filenameHint, AttributeValueSets attributes) { final IPath pathHint = filenameHint.isAbsolute() ? filenameHint : FileDialogs.recallPath(REMEMBER_DIRECTORY).append(filenameHint); final Path saveLocation = FileDialogs.openSaveXML(pathHint); if (saveLocation != null) { try { final Persister persister = new Persister(new Format(2)); persister.write(attributes, saveLocation.toFile()); } catch (Exception e) { Utils.showError( new Status( IStatus.ERROR, WorkbenchCorePlugin.PLUGIN_ID, "An error occurred while saving attributes.", e)); } FileDialogs.rememberDirectory(REMEMBER_DIRECTORY, saveLocation); } }
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) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (file != null) { if (!file.delete()) { log.warn("error deleting tmp file"); } } } return ret; }