org.simpleframework.xml.strategy.Strategy Java Examples

The following examples show how to use org.simpleframework.xml.strategy.Strategy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HideEnclosingConverterTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #2
Source File: ErasureHandlingTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #3
Source File: ErasureHandlingTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #4
Source File: PathWithConverterTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #5
Source File: PathWithConverterTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #6
Source File: ErasureHandlingTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #7
Source File: DynamicMapOfAttributesTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #8
Source File: ConverterDecorationTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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);     
}
 
Example #9
Source File: ElementsStrategyCallbackTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #10
Source File: SuperTypeTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example #11
Source File: UARTActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onRenameConfiguration(final String newName) {
	final boolean exists = databaseHelper.configurationExists(newName);
	if (exists) {
		Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show();
		return;
	}

	final String oldName = configuration.getName();
	configuration.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(configuration, writer);
		final String xml = writer.toString();

		databaseHelper.renameConfiguration(oldName, newName, xml);
		wearableSynchronizer.onConfigurationAddedOrEdited(preferences.getLong(PREFS_CONFIGURATION, 0), configuration);
		refreshConfigurations();
	} catch (final Exception e) {
		Log.e(TAG, "Error while renaming configuration", e);
	}
}
 
Example #12
Source File: UARTActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Saves the given configuration in the database.
 */
private void saveConfiguration() {
	final UartConfiguration configuration = this.configuration;
	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();

		databaseHelper.updateConfiguration(configuration.getName(), xml);
		wearableSynchronizer.onConfigurationAddedOrEdited(preferences.getLong(PREFS_CONFIGURATION, 0), configuration);
	} catch (final Exception e) {
		Log.e(TAG, "Error while creating a new configuration", e);
	}
}
 
Example #13
Source File: MapCycleTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #14
Source File: SimpleXmlParser.java    From openkeepass with Apache License 2.0 6 votes vote down vote up
private Serializer createSerializer(ProtectionStrategy protectionStrategy) {
    RegistryMatcher matcher = new RegistryMatcher();
    matcher.bind(Boolean.class, BooleanSimpleXmlAdapter.class);
    matcher.bind(GregorianCalendar.class, CalendarSimpleXmlAdapter.class);
    matcher.bind(UUID.class, UUIDSimpleXmlAdapter.class);
    matcher.bind(byte[].class, ByteSimpleXmlAdapter.class);
    
    Registry registry = new Registry();
    PropertyValueXmlAdapter converter = new PropertyValueXmlAdapter(protectionStrategy);
    try {
        registry.bind(PropertyValue.class, converter);
        Strategy strategy = new RegistryStrategy(registry);
        return new Persister(strategy, matcher);
    }
    catch (Exception e) {
        throw new KeePassDatabaseUnreadableException("Could not register xml converter", e);
    }
}
 
Example #15
Source File: EmptyMapEntryTest.java    From simplexml with Apache License 2.0 6 votes vote down vote up
/**
 * @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);
}
 
Example #16
Source File: UnionInlineListWitinPathTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testListWithPath() throws Exception {
   Strategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   Department department = new Department("Human Resources");
   Manager manager = new Manager(department, "Tom");
   Graduate graduate = new Graduate(department, "Dick");
   Assistant assistant = new Assistant(department, "Harry");
   department.getEmployees().add(manager);
   department.getEmployees().add(graduate);
   department.getEmployees().add(assistant);
   manager.getEmployees().add(graduate);
   manager.getEmployees().add(assistant);
   persister.write(department, System.out);
}
 
Example #17
Source File: MapWithValueAttributeTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testPrimitiveTextMap() throws Exception {
   Strategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   MapWithValueTextExample example = persister.read(MapWithValueTextExample.class, PRIMITIVE_TEXT_VALUE);
   assertEquals(example.map.get("a"), 0.0);
   assertEquals(example.map.get("b"), 1.0);
   assertEquals(example.map.get("c"), 2.0);
   assertEquals(example.map.get("d"), 3.0);
   persister.write(example, System.err);
   validate(example, persister);
}
 
Example #18
Source File: ErasureHandlingTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #19
Source File: SardineUtil.java    From simpletask-android with GNU General Public License v3.0 5 votes vote down vote up
private static Serializer getSerializer() throws Exception {
    Format format = new Format("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    Registry registry = new Registry();
    Strategy strategy = new RegistryStrategy(registry);
    Serializer serializer = new Persister(strategy, format);

    registry.bind(Prop.class, new EntityWithAnyElementConverter<>(serializer, Prop.class));
    registry.bind(Resourcetype.class, new EntityWithAnyElementConverter<>(serializer, Resourcetype.class));
    registry.bind(Property.class, Property.PropertyConverter.class);

    return serializer;
}
 
Example #20
Source File: MapWithValueAttributeTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testPrimitiveElementMap() throws Exception {
   Strategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   MapWithValueElementExample example = persister.read(MapWithValueElementExample.class, PRIMITIVE_ELEMENT_VALUE);
   assertEquals(example.map.get("a"), 0.0);
   assertEquals(example.map.get("b"), 1.0);
   assertEquals(example.map.get("c"), 2.0);
   assertEquals(example.map.get("d"), 3.0);      
   persister.write(example, System.err);
   validate(example, persister);
}
 
Example #21
Source File: MapWithValueAttributeTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testPrimitiveAttributeMap() throws Exception {
   Strategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   MapWithValueAttributeExample example = persister.read(MapWithValueAttributeExample.class, PRIMITIVE_ATTRIBUTE_VALUE);
   assertEquals(example.map.get("a"), 0.0);
   assertEquals(example.map.get("b"), 1.0);
   assertEquals(example.map.get("c"), 2.0);
   assertEquals(example.map.get("d"), 3.0);      
   persister.write(example, System.err);
   validate(example, persister);
}
 
Example #22
Source File: MapWithValueAttributeTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testCompositeValueMap() throws Exception {
   Strategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   MapWithCompositeValueExample example = persister.read(MapWithCompositeValueExample.class, COMPOSITE_VALUE);
   assertEquals(example.map.get("a").name, "B");
   assertEquals(example.map.get("a").value, "C");
   assertEquals(example.map.get("x").name, "Y");
   assertEquals(example.map.get("x").value, "Z");
   persister.write(example, System.err);
   validate(example, persister);
}
 
Example #23
Source File: MapWithValueAttributeTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testCompositeValueAndElementKeyMap() throws Exception {
   Strategy strategy = new CycleStrategy();
   Persister persister = new Persister(strategy);
   MapWithCompositeValueAndElementKeyExample example = persister.read(MapWithCompositeValueAndElementKeyExample.class, COMPOSITE_VALUE_AND_ELEMENT_KEY);
   assertEquals(example.map.get("a").name, "B");
   assertEquals(example.map.get("a").value, "C");
   assertEquals(example.map.get("x").name, "Y");
   assertEquals(example.map.get("x").value, "Z");
   persister.write(example, System.err);
   validate(example, persister);
}
 
Example #24
Source File: ConversionTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testConverter() throws Exception {
   Registry registry = new Registry();
   Strategy strategy = new TreeStrategy();
   Strategy interceptor = new Interceptor(strategy, registry);
   Persister persister = new Persister(interceptor);
   StringWriter writer = new StringWriter();
   PetShop shop = new PetShop();
   
   registry.register(Cat.class, CatConverter.class);
   registry.register(Dog.class, DogConverter.class);

   shop.addPet(new Cat("Kitty", 2));
   shop.addPet(new Dog("Lassie", 10));
   
   persister.write(shop, writer);
   persister.write(shop, System.out);
   
   String text = writer.toString();
   PetShop newShop = persister.read(PetShop.class, text);
   
   assertEquals("Lassie", newShop.getPet("Lassie").getName());
   assertEquals(10, newShop.getPet("Lassie").getAge());
   assertEquals("Kitty", newShop.getPet("Kitty").getName());
   assertEquals(2, newShop.getPet("Kitty").getAge());
   
   assertElementHasAttribute(text, "/petShop/pets/pet[2]", "name", "Lassie");
   assertElementHasAttribute(text, "/petShop/pets/pet[2]", "age", "10");
   assertElementExists(text, "/petShop/pets/pet[1]/name");
   assertElementExists(text, "/petShop/pets/pet[1]/age");
}
 
Example #25
Source File: UARTActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onNewConfiguration(final String name, final boolean duplicate) {
	final boolean exists = databaseHelper.configurationExists(name);
	if (exists) {
		Toast.makeText(this, R.string.uart_configuration_name_already_taken, Toast.LENGTH_LONG).show();
		return;
	}

	UartConfiguration configuration = this.configuration;
	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 = databaseHelper.addConfiguration(name, xml);
		wearableSynchronizer.onConfigurationAddedOrEdited(id, configuration);
		refreshConfigurations();
		selectConfiguration(configurationsAdapter.getItemPosition(id));
	} catch (final Exception e) {
		Log.e(TAG, "Error while creating a new configuration", e);
	}
}
 
Example #26
Source File: UARTActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 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(@NonNull final DatabaseHelper databaseHelper) {
	// This method ensures that the "old", single configuration has been saved to the database.
	if (databaseHelper.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 = preferences.getString(PREFS_BUTTON_COMMAND + i, null);
			if (cmd != null) {
				final Command command = new Command();
				command.setCommand(cmd);
				command.setActive(preferences.getBoolean(PREFS_BUTTON_ENABLED + i, false));
				command.setEol(0); // default one
				command.setIconIndex(preferences.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();

			databaseHelper.addConfiguration(configuration.getName(), xml);
		} catch (final Exception e) {
			Log.e(TAG, "Error while creating default configuration", e);
		}
	}
}
 
Example #27
Source File: SardineUtil.java    From sardine-android with Apache License 2.0 5 votes vote down vote up
private static Serializer getSerializer() throws Exception {
    Format format = new Format("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    Registry registry = new Registry();
    Strategy strategy = new RegistryStrategy(registry);
    Serializer serializer = new Persister(strategy, format);

    registry.bind(Prop.class, new EntityWithAnyElementConverter<>(serializer, Prop.class));
    registry.bind(Resourcetype.class, new EntityWithAnyElementConverter<>(serializer, Resourcetype.class));
    registry.bind(Property.class, Property.PropertyConverter.class);

    return serializer;
}
 
Example #28
Source File: MapCycleTest.java    From simplexml with Apache License 2.0 5 votes vote down vote up
public void testComplexMap() throws Exception {
   Strategy strategy = new CycleStrategy();
   Serializer serializer = new Persister(strategy);
   ComplexMap example = serializer.read(ComplexMap.class, COMPLEX_MAP);
   
   assertEquals("example 2", example.getValue(new CompositeKey("name 1", "address 1")));
   assertEquals("example 2", example.getValue(new CompositeKey("name 3", "address 3")));
   assertEquals("example 4", example.getValue(new CompositeKey("name 4", "address 4")));
   
   validate(example, serializer);
}
 
Example #29
Source File: ConfigurationLoaderImpl.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #30
Source File: ApplicationModule.java    From RetrofitSoapSample with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
public UsStatesApi providesApi(){

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

    Strategy strategy = new AnnotationStrategy();

    Serializer serializer = new Persister(strategy);

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .connectTimeout(2, TimeUnit.MINUTES)
            .writeTimeout(2, TimeUnit.MINUTES)
            .readTimeout(2, TimeUnit.MINUTES)
            .build();

    Retrofit retrofit =  new Retrofit.Builder()
            .addConverterFactory(SimpleXmlConverterFactory.create(serializer))
            .baseUrl("http://www.webservicex.net/")
            .client(okHttpClient)
            .build();

    return retrofit.create( UsStatesApi.class);

}