@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 Serializer getReadSerializer() throws Exception { if (readSerializer == null) { Log.d("hv", "Begin Creating Serializer"); RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(Date.class, new DateFormatTransformer()); Registry registry = new Registry(); registry.bind(String.class, SimpleXMLStringConverter.class); Strategy strategy = new RegistryStrategy(registry); Serializer s = new Persister(strategy, matcher); Log.d("hv", "Done Creating Serializer"); readSerializer = s; } return readSerializer; }
public static DefaultingModel load(String filename) { DefaultingModel read = null; Strategy strategy = new CycleStrategy("id", "ref"); Serializer serializer = new Persister(strategy); File source = new File(filename); try { read = serializer.read(DefaultingModel.class, source); if (read != null ) { read.finish(); } } catch (Exception e) { e.printStackTrace(); } return read; }
public static Model load(String filename) { Model read = null; Strategy strategy = new CycleStrategy("id", "ref"); Serializer serializer = new Persister(strategy); File source = new File(filename); try { read = serializer.read(Model.class, source); if (read != null ) { read.finish(); } } catch (Exception e) { e.printStackTrace(); } return read; }
public static HeuristicsModel load(String filename) { HeuristicsModel read = null; Strategy strategy = new CycleStrategy("id", "ref"); Serializer serializer = new Persister(strategy); File source = new File(filename); try { if (source.exists()) { read = serializer.read(HeuristicsModel.class, source); } } catch (Exception e) { e.printStackTrace(); } return read; }
public static BaseTraceability load(String filename) { BaseTraceability read = null; Strategy strategy = new CycleStrategy("id", "ref"); Serializer serializer = new Persister(strategy); File source = new File(filename); try { read = serializer.read(BaseTraceability.class, source); } catch (Exception e) { System.err.println("Exception when loading file:"+ filename); e.printStackTrace(); } return read; }
private void importRoomGroups(File f) throws Exception { log.info("Room import complete, starting room groups import"); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); registry.bind(Group.class, new GroupConverter(groupDao, groupMap)); registry.bind(Room.class, new RoomConverter(roomDao, roomMap)); List<RoomGroup> list = readList(serializer, f, "rooms_organisation.xml", "room_organisations", RoomGroup.class); for (RoomGroup ro : list) { Room r = roomDao.get(ro.getRoom().getId()); if (r == null || ro.getGroup() == null || ro.getGroup().getId() == null) { continue; } if (r.getGroups() == null) { r.setGroups(new ArrayList<>()); } ro.setId(null); ro.setRoom(r); r.getGroups().add(ro); roomDao.update(r, null); } }
private void importChat(File f) throws Exception { log.info("Room groups import complete, starting chat messages import"); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); registry.bind(User.class, new UserConverter(userDao, userMap)); registry.bind(Room.class, new RoomConverter(roomDao, roomMap)); registry.bind(Date.class, DateConverter.class); List<ChatMessage> list = readList(serializer, f, "chat_messages.xml", "chat_messages", ChatMessage.class); for (ChatMessage m : list) { m.setId(null); if (m.getFromUser() == null || m.getFromUser().getId() == null) { continue; } chatDao.update(m, m.getSent()); } }
private void importContacts(File f) throws Exception { log.info("Private message folder import complete, starting user contacts import"); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); registry.bind(User.class, new UserConverter(userDao, userMap)); List<UserContact> list = readList(serializer, f, "userContacts.xml", "usercontacts", UserContact.class); for (UserContact uc : list) { Long ucId = uc.getId(); UserContact storedUC = userContactDao.get(ucId); if (storedUC == null && uc.getContact() != null && uc.getContact().getId() != null) { uc.setId(null); if (uc.getOwner() != null && uc.getOwner().getId() == null) { uc.setOwner(null); } uc = userContactDao.update(uc); userContactMap.put(ucId, uc.getId()); } } }
private void importRoomFiles(File f) throws Exception { log.info("Poll import complete, starting room files import"); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); Serializer serializer = new Persister(strategy); registry.bind(BaseFileItem.class, new BaseFileItemConverter(fileItemDao, fileItemMap)); List<RoomFile> list = readList(serializer, f, "roomFiles.xml", "RoomFiles", RoomFile.class, true); for (RoomFile rf : list) { Room r = roomDao.get(roomMap.get(rf.getRoomId())); if (r == null || rf.getFile() == null || rf.getFile().getId() == null) { continue; } if (r.getFiles() == null) { r.setFiles(new ArrayList<>()); } rf.setId(null); rf.setRoomId(r.getId()); r.getFiles().add(rf); roomDao.update(r, null); } }
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"); }
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"); }
public void testEntryMap() throws Exception { Strategy strategy = new CycleStrategy(); Serializer serializer = new Persister(strategy); EntryMap example = serializer.read(EntryMap.class, ENTRY_MAP); assertEquals("example 1", example.getValue("a")); assertEquals("example 2", example.getValue("b")); assertEquals("example 1", example.getValue("c")); assertEquals("example 1", example.getValue("d")); MapEntry a = example.getEntry("a"); MapEntry b = example.getEntry("b"); MapEntry c = example.getEntry("c"); MapEntry d = example.getEntry("d"); assertTrue(a == c); assertTrue(c == d); assertFalse(a == b); validate(example, serializer); }
/** * @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); }
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 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 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 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 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); } } }
/** * Retrieve a {@link Serializer} instance suitable for deserialising a * {@link ConfigurationReport}. * * @return a new {@link Serializer} instance */ private Serializer getSerializer() { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(Timestamp.class, new DateFormatConverter(format)); Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy, matcher); return serializer; }