@Override public void onRenameConfiguration(final String newName) { final boolean exists = mDatabaseHelper.configurationExists(newName); if (exists) { Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show(); return; } final String oldName = mConfiguration.getName(); mConfiguration.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(mConfiguration, writer); final String xml = writer.toString(); mDatabaseHelper.renameConfiguration(oldName, newName, xml); mWearableSynchronizer.onConfigurationAddedOrEdited(mPreferences.getLong(PREFS_CONFIGURATION, 0), mConfiguration); refreshConfigurations(); } catch (final Exception e) { Log.e(TAG, "Error while renaming configuration", e); } }
/** * Saves the given configuration in the database. */ private void saveConfiguration() { final UartConfiguration configuration = mConfiguration; 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(); mDatabaseHelper.updateConfiguration(configuration.getName(), xml); mWearableSynchronizer.onConfigurationAddedOrEdited(mPreferences.getLong(PREFS_CONFIGURATION, 0), configuration); } catch (final Exception e) { Log.e(TAG, "Error while creating a new configuration", e); } }
private static <T> void writeList(Serializer ser, OutputStream os, String listElement, List<T> list) throws Exception { Format format = new Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); OutputNode doc = NodeBuilder.write(new OutputStreamWriter(os, UTF_8), format); OutputNode root = doc.getChild("root"); root.setComment(BACKUP_COMMENT); OutputNode listNode = root.getChild(listElement); if (list != null) { for (T t : list) { try { ser.write(t, listNode); } catch (Exception e) { log.debug("Exception While writing node of type: " + t.getClass(), e); } } } root.commit(); }
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); }
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); }
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()); }
public void testSimplePath() throws Exception { Expression expression = new PathParser("path1/path2/path3/path4", new ClassType(PathParserTest.class), new Format()); assertEquals(expression.getFirst(), "path1"); assertEquals(expression.getPrefix(), null); assertEquals(expression.getLast(), "path4"); assertEquals(expression.toString(), "path1/path2/path3/path4"); assertTrue(expression.isPath()); assertFalse(expression.isAttribute()); expression = expression.getPath(1); assertEquals(expression.getFirst(), "path2"); assertEquals(expression.getPrefix(), null); assertEquals(expression.getLast(), "path4"); assertEquals(expression.toString(), "path2/path3/path4"); assertTrue(expression.isPath()); assertFalse(expression.isAttribute()); }
public void testAttribute() throws Exception { Expression expression = new PathParser("./some[3]/path[2]/to/parse/@attribute", new ClassType(PathParserTest.class), new Format()); assertEquals(expression.getFirst(), "some"); assertEquals(expression.getIndex(), 3); assertEquals(expression.getLast(), "attribute"); assertEquals(expression.toString(), "some[3]/path[2]/to/parse/@attribute"); assertTrue(expression.isPath()); assertTrue(expression.isAttribute()); expression = expression.getPath(2); assertEquals(expression.getFirst(), "to"); assertEquals(expression.getIndex(), 1); assertEquals(expression.getLast(), "attribute"); assertEquals(expression.toString(), "to/parse/@attribute"); assertTrue(expression.isPath()); assertTrue(expression.isAttribute()); expression = expression.getPath(2); assertEquals(expression.getFirst(), "attribute"); assertEquals(expression.getIndex(), 1); assertEquals(expression.getLast(), "attribute"); assertEquals(expression.toString(), "@attribute"); assertFalse(expression.isPath()); assertTrue(expression.isAttribute()); }
public void testOtherStyle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); OtherCaseExample example = new OtherCaseExample("a", "b"); Persister persister = new Persister(format); StringWriter writer = new StringWriter(); boolean exception = false; try { persister.write(example, writer); }catch(Exception e) { e.printStackTrace(); exception = true; } System.out.println(writer.toString()); assertFalse("No exception should be thrown with the elements are not the same name", exception); }
public void testConverter() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Strategy cycle = new CycleStrategy(); Strategy strategy = new AnnotationStrategy(cycle); Persister persister = new Persister(strategy, format); List<ConverterDecorationExample> list = new ArrayList<ConverterDecorationExample>(); List<NormalExample> normal = new ArrayList<NormalExample>(); ConverterDecoration example = new ConverterDecoration(list, normal); ConverterDecorationExample duplicate = new ConverterDecorationExample("duplicate"); NormalExample normalDuplicate = new NormalExample("duplicate"); list.add(duplicate); list.add(new ConverterDecorationExample("a")); list.add(new ConverterDecorationExample("b")); list.add(new ConverterDecorationExample("c")); list.add(duplicate); list.add(new ConverterDecorationExample("d")); list.add(duplicate); normal.add(normalDuplicate); normal.add(new NormalExample("1")); normal.add(new NormalExample("2")); normal.add(normalDuplicate); persister.write(example, System.err); }
public void testConverterWithPathInHyphenStyle() throws Exception { Style style = new HyphenStyle(); Format format = new Format(style); Strategy strategy = new AnnotationStrategy(); Persister persister = new Persister(strategy, format); ServerDetails primary = new ServerDetails("host1.blah.com", 4567, "PRIMARY"); ServerDetails secondary = new ServerDetails("host2.foo.com", 4567, "SECONDARY"); ServerDetailsReference reference = new ServerDetailsReference(primary, secondary); StringWriter writer = new StringWriter(); persister.write(reference, writer); System.out.println(writer); ServerDetailsReference recovered = persister.read(ServerDetailsReference.class, writer.toString()); assertEquals(recovered.getPrimary().getHost(), reference.getPrimary().getHost()); assertEquals(recovered.getPrimary().getPort(), reference.getPrimary().getPort()); assertEquals(recovered.getPrimary().getName(), reference.getPrimary().getName()); assertEquals(recovered.getSecondary().getHost(), reference.getSecondary().getHost()); assertEquals(recovered.getSecondary().getPort(), reference.getSecondary().getPort()); assertEquals(recovered.getSecondary().getName(), reference.getSecondary().getName()); }
public void testConverterWithPathInCamelStyle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Strategy strategy = new AnnotationStrategy(); Persister persister = new Persister(strategy, format); ServerDetails primary = new ServerDetails("host1.blah.com", 4567, "PRIMARY"); ServerDetails secondary = new ServerDetails("host2.foo.com", 4567, "SECONDARY"); ServerDetailsReference reference = new ServerDetailsReference(primary, secondary); StringWriter writer = new StringWriter(); persister.write(reference, writer); System.out.println(writer); ServerDetailsReference recovered = persister.read(ServerDetailsReference.class, writer.toString()); assertEquals(recovered.getPrimary().getHost(), reference.getPrimary().getHost()); assertEquals(recovered.getPrimary().getPort(), reference.getPrimary().getPort()); assertEquals(recovered.getPrimary().getName(), reference.getPrimary().getName()); assertEquals(recovered.getSecondary().getHost(), reference.getSecondary().getHost()); assertEquals(recovered.getSecondary().getPort(), reference.getSecondary().getPort()); assertEquals(recovered.getSecondary().getName(), reference.getSecondary().getName()); }
public void testLargeList() throws Exception { Context context = getContext(); Group group = getGroup(); Type type = new ClassType(CompositeListUnionTest.class); List<Shape> list = new ArrayList<Shape>(); Expression expression = new PathParser("some/path", type, new Format()); CompositeListUnion union = new CompositeListUnion( context, group, expression, type); InputNode node = NodeBuilder.read(new StringReader(LARGE_SOURCE)); for(InputNode next = node.getNext(); next != null; next = node.getNext()) { union.read(next, list); } OutputNode output = NodeBuilder.write(new PrintWriter(System.err), new Format(3)); OutputNode parent = output.getChild("parent"); union.write(parent, list); }
public void testCamelCaseStyle() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Persister persister = new Persister(format); UnionListExample example = persister.read(UnionListExample.class, CAMEL_CASE_SOURCE); List<Entry> entry = example.list; assertEquals(entry.size(), 4); assertEquals(entry.get(0).getClass(), IntegerEntry.class); assertEquals(entry.get(0).foo(), 111); assertEquals(entry.get(1).getClass(), DoubleEntry.class); assertEquals(entry.get(1).foo(), 222.0); assertEquals(entry.get(2).getClass(), StringEntry.class); assertEquals(entry.get(2).foo(), "A"); assertEquals(entry.get(3).getClass(), StringEntry.class); assertEquals(entry.get(3).foo(), "B"); assertEquals(example.entry.getClass(), IntegerEntry.class); assertEquals(example.entry.foo(), 777); persister.write(example, System.out); validate(persister, example); }
public void testHyphenStyle() throws Exception { Style style = new HyphenStyle(); Format format = new Format(style); Persister persister = new Persister(format); UnionListExample example = persister.read(UnionListExample.class, HYPHEN_SOURCE); List<Entry> entry = example.list; assertEquals(entry.size(), 4); assertEquals(entry.get(0).getClass(), IntegerEntry.class); assertEquals(entry.get(0).foo(), 111); assertEquals(entry.get(1).getClass(), DoubleEntry.class); assertEquals(entry.get(1).foo(), 222.0); assertEquals(entry.get(2).getClass(), StringEntry.class); assertEquals(entry.get(2).foo(), "A"); assertEquals(entry.get(3).getClass(), StringEntry.class); assertEquals(entry.get(3).foo(), "B"); assertEquals(example.entry.getClass(), IntegerEntry.class); assertEquals(example.entry.foo(), 777); persister.write(example, System.out); validate(persister, example); }
/** * Testing * * @param args * @throws Exception */ public static void main(String... args) throws Exception { System.out.println("Start: " + new Date()); WebApp webApp = new WebApp(); webApp.setVersion("2.5"); WelcomeFileList welcomeFileList = new WelcomeFileList(); webApp.setWelcomeFileList(welcomeFileList); List<String> welcomeFiles = new ArrayList<>(); welcomeFiles.add("index.html"); welcomeFileList.setWelcomeFiles(welcomeFiles); Serializer serializer = new Persister(new Format(4, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("C:/Users/mark/web.xml"), "UTF-8"); serializer.write(webApp, writer); writer.close(); System.out.println(" End: " + new Date()); }
protected String convertSQLToXML( TGSqlParser sqlparser ) { StringBuffer buffer = new StringBuffer( ); TStatementList stmts = sqlparser.getSqlstatements( ); for ( int i = 0; i < stmts.size( ); i++ ) { direct_sql_statement directSqlStatement = new direct_sql_statement( ); convertStmtToModel( stmts.get( i ), directSqlStatement.getDirectly_executable_statement( ) ); try { Serializer serializer = new Persister( new Format( "<?xml version=\"1.0\" encoding= \"UTF-8\" ?>" ) ); StringWriter sw = new StringWriter( ); serializer.write( directSqlStatement, sw ); sw.close( ); buffer.append( sw.toString( ) ); } catch ( Exception e ) { e.printStackTrace( ); } } return buffer.toString( ); }
@Test public void deveLerXMLDeAcordoComOPadraoEstabelecido() throws Exception { final String xmlRecebido = "<retDistDFeInt versao=\"1.00\" xmlns=\"http://www.portalfiscal.inf.br/cte\"><tpAmb>2</tpAmb><verAplic>1.0.0_1709261815</verAplic><cStat>138</cStat><xMotivo>documento localizado.</xMotivo><dhResp>2017-09-26T18:15:01</dhResp><ultNSU>000000000000001</ultNSU><maxNSU>000000000000001</maxNSU><loteDistDFeInt><docZip NSU=\"000000000000001\" schema=\"procCTe_v2.00.xsd\">xXxXxXxX</docZip></loteDistDFeInt></retDistDFeInt>"; final CTDistribuicaoIntRetorno retornoLido = new Persister(new DFRegistryMatcher(), new Format(0)).read(CTDistribuicaoIntRetorno.class, xmlRecebido); final CTDistribuicaoIntRetorno retorno = new CTDistribuicaoIntRetorno(); retorno.setAmbiente(DFAmbiente.HOMOLOGACAO); retorno.setCodigoStatusReposta("138"); retorno.setDataHoraResposta("2017-09-26T18:15:01"); retorno.setMotivo("documento localizado."); retorno.setMaximoNSU("000000000000001"); retorno.setUltimoNSU("000000000000001"); retorno.setVersao("1.00"); retorno.setVersaoAplicativo("1.0.0_1709261815"); retorno.setLote(new CTDistribuicaoDFeLote().setDocZip(Arrays.asList(new CTDistribuicaoDocumentoZip().setNsu("000000000000001").setSchema("procCTe_v2.00.xsd").setValue("xXxXxXxX")))); Assert.assertEquals(retornoLido.toString(), retorno.toString()); }
@Before public void setUp() { Format format = new Format(0, null, new HyphenStyle(), Verbosity.HIGH); Persister persister = new Persister(format); Retrofit retrofit = new Retrofit.Builder() .baseUrl(server.url("/")) .addConverterFactory(SimpleXmlConverterFactory.create(persister)) .build(); service = retrofit.create(Service.class); }