@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); } }
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()); }
public void testPrimitiveErasure() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureExample<Double> example = new ErasureExample<Double>(); example.addItem("a", 2.0); example.addItem("b", 1.2); example.addItem("c", 5.4); example.addDoubleGeneric(7.8, 8.7); example.addDoubleGeneric(1.2, 2.1); example.addDoubleGeneric(3.1, 1.3); persister.write(example, System.out); validate(example, persister); }
public void testEnumErasure() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureExample<ErasureEnum> example = new ErasureExample<ErasureEnum>(); example.addItem("a", ErasureEnum.A); example.addItem("b", ErasureEnum.B); example.addItem("c", ErasureEnum.C); example.addDoubleGeneric(ErasureEnum.A, ErasureEnum.B); example.addDoubleGeneric(ErasureEnum.B, ErasureEnum.C); example.addDoubleGeneric(ErasureEnum.C, ErasureEnum.D); persister.write(example, System.out); validate(example, persister); }
public void testErasureWithMapAttributeIllegalExample() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureWithMapAttributeIllegalExample<ErasureEnum> example = new ErasureWithMapAttributeIllegalExample<ErasureEnum>(); boolean failure = false; example.addItem(ErasureEnum.A, "a"); example.addItem(ErasureEnum.B, "b"); example.addItem(ErasureEnum.C, "c"); try { persister.write(example, System.out); }catch(Exception e) { e.printStackTrace(); failure = true; } assertTrue("Attribute should not be possible with an erased key", failure); }
public void testTypeCallback() throws Exception { TypeDecorator decorator = new TypeDecorator(); Strategy strategy = new VisitorStrategy(decorator); Persister persister = new Persister(strategy); decorator.register(Double.class, "double"); decorator.register(Integer.class, "int"); decorator.register(Float.class, "float"); decorator.register(Byte.class, "byte"); decorator.register(String.class, "string"); Example example = new Example(); example.s = "text"; StringWriter out = new StringWriter(); persister.write(example, out); Example other = persister.read(Example.class, out.toString()); assertEquals(other.d, example.d); System.out.println(out); }
@Override public void onNewConfiguration(final String name, final boolean duplicate) { final boolean exists = mDatabaseHelper.configurationExists(name); if (exists) { Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show(); return; } UartConfiguration configuration = mConfiguration; if (!duplicate) configuration = new UartConfiguration(); configuration.setName(name); 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(); final long id = mDatabaseHelper.addConfiguration(name, xml); mWearableSynchronizer.onConfigurationAddedOrEdited(id, configuration); refreshConfigurations(); selectConfiguration(mConfigurationsAdapter.getItemPosition(id)); } catch (final Exception e) { Log.e(TAG, "Error while creating a new configuration", e); } }
/** * Converts the old configuration, stored in preferences, into the first XML configuration and saves it to the database. * If there is already any configuration in the database this method does nothing. */ private void ensureFirstConfiguration(final DatabaseHelper mDatabaseHelper) { // This method ensures that the "old", single configuration has been saved to the database. if (mDatabaseHelper.getConfigurationsCount() == 0) { final UartConfiguration configuration = new UartConfiguration(); configuration.setName("First configuration"); final Command[] commands = configuration.getCommands(); for (int i = 0; i < 9; ++i) { final String cmd = mPreferences.getString(PREFS_BUTTON_COMMAND + i, null); if (cmd != null) { final Command command = new Command(); command.setCommand(cmd); command.setActive(mPreferences.getBoolean(PREFS_BUTTON_ENABLED + i, false)); command.setEol(0); // default one command.setIconIndex(mPreferences.getInt(PREFS_BUTTON_ICON + i, 0)); commands[i] = command; } } 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.addConfiguration(configuration.getName(), xml); } catch (final Exception e) { Log.e(TAG, "Error while creating default configuration", e); } } }
public void testVisitor() throws Exception { Style style = new CamelCaseStyle(); Format format = new Format(style); Visitor visitor = new RenameVisitor(); VisitorStrategy strategy = new VisitorStrategy(visitor); Persister renamePersister = new Persister(strategy); Persister normalPersister = new Persister(format); RenameExample example = new RenameExample("example name", "example value"); StringWriter renameWriter = new StringWriter(); StringWriter normalWriter = new StringWriter(); renamePersister.write(example, renameWriter); normalPersister.write(example, normalWriter); String renameResult = renameWriter.toString(); String normalResult = normalWriter.toString(); System.out.println(renameResult); System.out.println(normalResult); assertElementExists(renameResult, "/RENAMEEXAMPLE"); assertElementExists(renameResult, "/RENAMEEXAMPLE/NAME"); assertElementExists(renameResult, "/RENAMEEXAMPLE/VALUE"); assertElementHasValue(renameResult, "/RENAMEEXAMPLE/NAME", "example name"); assertElementHasValue(renameResult, "/RENAMEEXAMPLE/VALUE", "example value"); assertElementExists(normalResult, "/RenameExample"); assertElementExists(normalResult, "/RenameExample/Name"); assertElementExists(normalResult, "/RenameExample/Value"); assertElementHasValue(normalResult, "/RenameExample/Name", "example name"); assertElementHasValue(normalResult, "/RenameExample/Value", "example value"); }
public void testErasure() throws Exception { Visitor visitor = new ClassToNamespaceVisitor(); Strategy strategy = new VisitorStrategy(visitor); Persister persister = new Persister(strategy); ErasureExample<ErasureItem> example = new ErasureExample<ErasureItem>(); StringWriter writer = new StringWriter(); example.addItem("a", new ErasureItem("A", "1")); example.addItem("b", new ErasureItem("B", "2")); example.addItem("c", new ErasureItem("C", "3")); example.addDoubleGeneric(new ErasureItem("1", "1"), new ErasureItem("A", "1")); example.addDoubleGeneric(new ErasureItem("2", "2"), new ErasureItem("B", "2")); example.addDoubleGeneric(new ErasureItem("3", "3"), new ErasureItem("C", "3")); persister.write(example, writer); String text = writer.toString(); System.out.println(text); ErasureExample<ErasureItem> exampleCopy = persister.read(ErasureExample.class, text); assertEquals(exampleCopy.getItem("a").name, "A"); assertEquals(exampleCopy.getItem("b").name, "B"); assertEquals(exampleCopy.getItem("c").name, "C"); validate(example, persister); }
private static Serializer visitorStrategySerializer() { return new Persister(new VisitorStrategy(new RequestObjectInterceptor())); }