@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 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 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 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); }
@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); }
/** * Method called when Google API Client connects to Wearable.API. */ @Override public void onConnected(final Bundle bundle) { if (!mPreferences.getBoolean(PREFS_WEAR_SYNCED, false)) { new Thread(new Runnable() { @Override public void run() { final Cursor cursor = mDatabaseHelper.getConfigurations(); try { while (cursor.moveToNext()) { final long id = cursor.getLong(0 /* _ID */); try { final String xml = cursor.getString(2 /* XML */); final Format format = new Format(new HyphenStyle()); final Serializer serializer = new Persister(format); final UartConfiguration configuration = serializer.read(UartConfiguration.class, xml); mWearableSynchronizer.onConfigurationAddedOrEdited(id, configuration).await(); } catch (final Exception e) { Log.w(TAG, "Deserializing configuration with id " + id + " failed", e); } } mPreferences.edit().putBoolean(PREFS_WEAR_SYNCED, true).apply(); } finally { cursor.close(); } } }).start(); } }
@Override public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) { if (position > 0) { // FIXME this is called twice after rotation. try { final String xml = mDatabaseHelper.getConfiguration(id); final Format format = new Format(new HyphenStyle()); final Serializer serializer = new Persister(format); mConfiguration = serializer.read(UartConfiguration.class, xml); mConfigurationListener.onConfigurationChanged(mConfiguration); } catch (final Exception e) { Log.e(TAG, "Selecting configuration failed", e); String message; if (e.getLocalizedMessage() != null) message = e.getLocalizedMessage(); else if (e.getCause() != null && e.getCause().getLocalizedMessage() != null) message = e.getCause().getLocalizedMessage(); else message = "Unknown error"; final String msg = message; Snackbar.make(mSlider, R.string.uart_configuration_loading_failed, Snackbar.LENGTH_INDEFINITE).setAction(R.string.uart_action_details, new View.OnClickListener() { @Override public void onClick(final View v) { new AlertDialog.Builder(UARTActivity.this).setMessage(msg).setTitle(R.string.uart_action_details).setPositiveButton(R.string.ok, null).show(); } }).show(); return; } mPreferences.edit().putLong(PREFS_CONFIGURATION, id).apply(); } }
@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 testCase() throws Exception { Style style = new HyphenStyle(); Format format = new Format(style); Persister writer = new Persister(format); Persister reader = new Persister(); CaseExample example = reader.read(CaseExample.class, SOURCE); assertEquals(example.version, 1.0f); assertEquals(example.name, "example"); assertEquals(example.URL, "http://domain.com/"); writer.write(example, System.err); validate(example, reader); validate(example, writer); }
/** * Method called when Google API Client connects to Wearable.API. */ @Override public void onConnected(final Bundle bundle) { // Ensure the Wearable API was connected if (!mWearableSynchronizer.hasConnectedApi()) return; if (!mPreferences.getBoolean(PREFS_WEAR_SYNCED, false)) { new Thread(() -> { final Cursor cursor = mDatabaseHelper.getConfigurations(); try { while (cursor.moveToNext()) { final long id = cursor.getLong(0 /* _ID */); try { final String xml = cursor.getString(2 /* XML */); final Format format = new Format(new HyphenStyle()); final Serializer serializer = new Persister(format); final UartConfiguration configuration = serializer.read(UartConfiguration.class, xml); mWearableSynchronizer.onConfigurationAddedOrEdited(id, configuration).await(); } catch (final Exception e) { Log.w(TAG, "Deserializing configuration with id " + id + " failed", e); } } mPreferences.edit().putBoolean(PREFS_WEAR_SYNCED, true).apply(); } finally { cursor.close(); } }).start(); } }
@Override public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) { if (position > 0) { // FIXME this is called twice after rotation. try { final String xml = mDatabaseHelper.getConfiguration(id); final Format format = new Format(new HyphenStyle()); final Serializer serializer = new Persister(format); mConfiguration = serializer.read(UartConfiguration.class, xml); mConfigurationListener.onConfigurationChanged(mConfiguration); } catch (final Exception e) { Log.e(TAG, "Selecting configuration failed", e); String message; if (e.getLocalizedMessage() != null) message = e.getLocalizedMessage(); else if (e.getCause() != null && e.getCause().getLocalizedMessage() != null) message = e.getCause().getLocalizedMessage(); else message = "Unknown error"; final String msg = message; Snackbar.make(mContainer, R.string.uart_configuration_loading_failed, Snackbar.LENGTH_INDEFINITE).setAction(R.string.uart_action_details, v -> new AlertDialog.Builder(UARTActivity.this).setMessage(msg).setTitle(R.string.uart_action_details).setPositiveButton(R.string.ok, null).show()).show(); return; } mPreferences.edit().putLong(PREFS_CONFIGURATION, id).apply(); } }
/** * Loads the configuration from the given input stream. * @param is the input stream */ private void loadConfiguration(final InputStream is) { try { final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); final StringBuilder builder = new StringBuilder(); for (String line = reader.readLine(); line != null; line = reader.readLine()) { builder.append(line).append("\n"); } final String xml = builder.toString(); final Format format = new Format(new HyphenStyle()); final Serializer serializer = new Persister(format); final UartConfiguration configuration = serializer.read(UartConfiguration.class, xml); final String name = configuration.getName(); if (!mDatabaseHelper.configurationExists(name)) { final long id = mDatabaseHelper.addConfiguration(name, xml); mWearableSynchronizer.onConfigurationAddedOrEdited(id, configuration); refreshConfigurations(); new Handler().post(() -> selectConfiguration(mConfigurationsAdapter.getItemPosition(id))); } else { Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show(); } } catch (final Exception e) { Log.e(TAG, "Loading configuration failed", e); String message; if (e.getLocalizedMessage() != null) message = e.getLocalizedMessage(); else if (e.getCause() != null && e.getCause().getLocalizedMessage() != null) message = e.getCause().getLocalizedMessage(); else message = "Unknown error"; final String msg = message; Snackbar.make(mContainer, R.string.uart_configuration_loading_failed, Snackbar.LENGTH_INDEFINITE).setAction(R.string.uart_action_details, v -> new AlertDialog.Builder(UARTActivity.this).setMessage(msg).setTitle(R.string.uart_action_details).setPositiveButton(R.string.ok, null).show()).show(); } }
@Before public void setUp() { queue = Jus.newRequestQueue(); Format format = new Format(0, null, new HyphenStyle(), Verbosity.HIGH); Persister persister = new Persister(format); RetroProxy retroProxy = new RetroProxy.Builder() .baseUrl(server.url("/").toString()) .addConverterFactory(SimpleXmlConverterFactory.create(persister)) .requestQueue(queue) .build(); service = retroProxy.create(Service.class); }
/** * Loads the configuration from the given input stream. * @param is the input stream */ private void loadConfiguration(final InputStream is) { try { final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); final StringBuilder builder = new StringBuilder(); for (String line = reader.readLine(); line != null; line = reader.readLine()) { builder.append(line).append("\n"); } final String xml = builder.toString(); final Format format = new Format(new HyphenStyle()); final Serializer serializer = new Persister(format); final UartConfiguration configuration = serializer.read(UartConfiguration.class, xml); final String name = configuration.getName(); if (!mDatabaseHelper.configurationExists(name)) { final long id = mDatabaseHelper.addConfiguration(name, xml); mWearableSynchronizer.onConfigurationAddedOrEdited(id, configuration); refreshConfigurations(); new Handler().post(new Runnable() { @Override public void run() { selectConfiguration(mConfigurationsAdapter.getItemPosition(id)); } }); } else { Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show(); } } catch (final Exception e) { Log.e(TAG, "Loading configuration failed", e); String message; if (e.getLocalizedMessage() != null) message = e.getLocalizedMessage(); else if (e.getCause() != null && e.getCause().getLocalizedMessage() != null) message = e.getCause().getLocalizedMessage(); else message = "Unknown error"; final String msg = message; Snackbar.make(mSlider, R.string.uart_configuration_loading_failed, Snackbar.LENGTH_INDEFINITE).setAction(R.string.uart_action_details, new View.OnClickListener() { @Override public void onClick(final View v) { new AlertDialog.Builder(UARTActivity.this).setMessage(msg).setTitle(R.string.uart_action_details).setPositiveButton(R.string.ok, null).show(); } }).show(); } }
public TaleParser(){ Style style = new HyphenStyle(); Format format = new Format(style); this.serializer = new Persister(new TaleMatcher(), format); }
private static Converter initConverter() { Format format = new Format(0, null, new HyphenStyle(), Verbosity.HIGH); Persister persister = new Persister(format); return new SimpleXMLConverter(persister); }