com.thoughtworks.xstream.converters.SingleValueConverter Java Examples

The following examples show how to use com.thoughtworks.xstream.converters.SingleValueConverter. 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 Project: lams   Author: lamsfoundation   File: FilePersistenceStrategy.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Given a key, the escape method returns the filename which shall be used.
 *
 * @param key the key
 * @return the desired and escaped filename
 */
protected String getName(final Object key) {
    if (key == null) {
        return "[email protected]";
    }
    final Class type = key.getClass();
    final Converter converter = getConverterLookup().lookupConverterForType(type);
    if (converter instanceof SingleValueConverter) {
        final SingleValueConverter svConverter = (SingleValueConverter)converter;
        return getMapper().serializedClass(type) + '@' + escape(svConverter.toString(key)) + ".xml";
    } else {
        final ConversionException exception = new ConversionException(
            "No SingleValueConverter available for key type");
        exception.add("key-type", type.getName());
        throw exception;
    }
}
 
Example #2
Source Project: lams   Author: lamsfoundation   File: EnumMapper.java    License: GNU General Public License v2.0 6 votes vote down vote up
private SingleValueConverter getLocalConverter(String fieldName, Class type, Class definedIn) {
    if (attributeMapper != null
            && Enum.class.isAssignableFrom(type)
            && attributeMapper.shouldLookForSingleValueConverter(fieldName, type, definedIn)) {
        synchronized (enumConverterMap) {
            SingleValueConverter singleValueConverter = enumConverterMap.get(type);
            if (singleValueConverter == null) {
                singleValueConverter = super.getConverterFromItemType(fieldName, type, definedIn);
                if (singleValueConverter == null) {
                    @SuppressWarnings("unchecked")
                    Class<? extends Enum> enumType = type;
                    singleValueConverter = new EnumSingleValueConverter(enumType);
                }
                enumConverterMap.put(type, singleValueConverter);
            }
            return singleValueConverter;
        }
    }
    return null;
}
 
Example #3
Source Project: snap-desktop   Author: senbox-org   File: MagicWandModel.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static XStream createXStream() {
    final XStream xStream = new XStream();
    xStream.alias("magicWandSettings", MagicWandModel.class);
    xStream.registerConverter(new SingleValueConverter() {
        @Override
        public boolean canConvert(Class type) {
            return type.equals(double[].class);
        }

        @Override
        public String toString(Object obj) {
            return StringUtils.arrayToString(obj, ",");
        }

        @Override
        public Object fromString(String str) {
            return StringUtils.toDoubleArray(str, ",");
        }
    });
    return xStream;
}
 
Example #4
Source Project: snap-desktop   Author: senbox-org   File: Filter.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static XStream createXStream() {
    final XStream xStream = new XStream();
    xStream.setClassLoader(Filter.class.getClassLoader());
    xStream.alias("filter", Filter.class);
    xStream.registerLocalConverter(Filter.class, "kernelElements", new SingleValueConverter() {
        @Override
        public String toString(Object o) {
            double[] o1 = (double[]) o;
            // todo - find out how to obtain width, height
            return Filter.formatKernelElements(o1, new Dimension(o1.length, 1), ",");
        }

        @Override
        public Object fromString(String s) {
            return Filter.parseKernelElementsFromText(s, null);
        }

        @Override
        public boolean canConvert(Class aClass) {
            return aClass.equals(double[].class);
        }
    });
    return xStream;
}
 
Example #5
Source Project: saros   Author: saros-project   File: XStreamExtensionProvider.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Register additional {@link SingleValueConverter}s at runtime. This is useful if a converter
 * cannot be used isolatedly, e.g. because it requires a running Saros session.
 *
 * @param converter The {@link SingleValueConverter} to be registered to XStream. There can only
 *     be one instance per converter class. If a new instance of an already registered class is
 *     registered, the old instance will be replaced.
 */
public void registerConverter(SingleValueConverter converter) {
  Class<? extends SingleValueConverter> clazz = converter.getClass();

  if (replaceableSingles.containsKey(clazz)) {
    log.debug("Renewing existing converter of " + clazz);
    replaceableSingles.get(clazz).replace(converter);
    return;
  }

  log.debug("Registering new converter of " + clazz);

  ReplaceableSingleValueConverter replaceable = new ReplaceableSingleValueConverter(converter);
  xstream.registerConverter(replaceable);
  replaceableSingles.put(clazz, replaceable);
}
 
Example #6
Source Project: vraptor4   Author: caelum   File: XStreamSerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@Before
public void setup() throws Exception {
	stream = new ByteArrayOutputStream();
	environment = mock(Environment.class);

	HttpServletResponse response = mock(HttpServletResponse.class);
	when(response.getWriter()).thenReturn(new PrintWriter(stream));

	List<Converter> converters = new ArrayList<>();
	converters.add(new CalendarConverter());

	final DefaultTypeNameExtractor extractor = new DefaultTypeNameExtractor();

	Instance<Converter> convertersInst = new MockInstanceImpl<>(converters);
	Instance<SingleValueConverter> singleValueConverters = new MockInstanceImpl<>();
	XStreamConverters xStreamConverters = new XStreamConverters(convertersInst, singleValueConverters);
	serialization = new XStreamXMLSerialization(response, new XStreamBuilderImpl(xStreamConverters, extractor, 
			new Serializee(new DefaultReflectionProvider()), new DefaultReflectionProvider()), environment);
}
 
Example #7
Source Project: lams   Author: lamsfoundation   File: NamedMapConverter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void marshal(Object source, HierarchicalStreamWriter writer,
    MarshallingContext context) {
    Map map = (Map)source;
    SingleValueConverter keyConverter = null;
    SingleValueConverter valueConverter = null;
    if (keyAsAttribute) {
        keyConverter = getSingleValueConverter(keyType, "key");
    }
    if (valueAsAttribute || valueName == null) {
        valueConverter = getSingleValueConverter(valueType, "value");
    }
    for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
        Map.Entry entry = (Map.Entry)iterator.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
        if (entryName != null) {
            ExtendedHierarchicalStreamWriterHelper.startNode(
                writer, entryName, entry.getClass());
            if (keyConverter != null && key != null) {
                writer.addAttribute(keyName, keyConverter.toString(key));
            }
            if (valueName != null && valueConverter != null && value != null) {
                writer.addAttribute(valueName, valueConverter.toString(value));
            }
        }

        if (keyConverter == null) {
            writeItem(keyName, keyType, key, context, writer);
        }
        if (valueConverter == null) {
            writeItem(valueName, valueType, value, context, writer);
        } else if (valueName == null) {
            writer.setValue(valueConverter.toString(value));
        }

        if (entryName != null) {
            writer.endNode();
        }
    }
}
 
Example #8
Source Project: lams   Author: lamsfoundation   File: AttributeMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
private SingleValueConverter getLocalConverterFromItemType(Class type) {
    Converter converter = converterLookup.lookupConverterForType(type);
    if (converter != null && converter instanceof SingleValueConverter) {
        return (SingleValueConverter)converter;
    } else {
        return null;
    }
}
 
Example #9
Source Project: lams   Author: lamsfoundation   File: AttributeMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @deprecated As of 1.3, use {@link #getConverterFromItemType(String, Class, Class)}
 */
public SingleValueConverter getConverterFromItemType(String fieldName, Class type) {
    if (fieldNameToTypeMap.get(fieldName) == type) {
        return getLocalConverterFromItemType(type);
    } else {
        return null;
    }
}
 
Example #10
Source Project: lams   Author: lamsfoundation   File: AttributeMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
public SingleValueConverter getConverterFromItemType(String fieldName, Class type,
    Class definedIn) {
    if (shouldLookForSingleValueConverter(fieldName, type, definedIn)) {
        SingleValueConverter converter = getLocalConverterFromItemType(type);
        if (converter != null) {
            return converter;
        }
    }
    return super.getConverterFromItemType(fieldName, type, definedIn);
}
 
Example #11
Source Project: lams   Author: lamsfoundation   File: AttributeMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @deprecated As of 1.3, use {@link #getConverterFromItemType(String, Class, Class)}
 */
public SingleValueConverter getConverterFromItemType(Class type) {
    if (typeSet.contains(type)) {
        return getLocalConverterFromItemType(type);
    } else {
        return null;
    }
}
 
Example #12
Source Project: lams   Author: lamsfoundation   File: AttributeMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @deprecated As of 1.3, use {@link #getConverterFromAttribute(Class, String, Class)}
 */
public SingleValueConverter getConverterFromAttribute(String attributeName) {
    SingleValueConverter converter = null;
    Class type = (Class)fieldNameToTypeMap.get(attributeName);
    if (type != null) {
        converter = getLocalConverterFromItemType(type);
    }
    return converter;
}
 
Example #13
Source Project: lams   Author: lamsfoundation   File: AttributeMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
public SingleValueConverter getConverterFromAttribute(Class definedIn, String attribute, Class type) {
    if (shouldLookForSingleValueConverter(attribute, type, definedIn)) {
        SingleValueConverter converter = getLocalConverterFromItemType(type);
        if (converter != null) {
            return converter;
        }
    }
    return super.getConverterFromAttribute(definedIn, attribute, type);
}
 
Example #14
Source Project: lams   Author: lamsfoundation   File: LocalConversionMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
public SingleValueConverter getConverterFromAttribute(Class definedIn, String attribute,
    Class type) {
    SingleValueConverter converter = getLocalSingleValueConverter(
        definedIn, attribute, type);
    return converter == null
        ? super.getConverterFromAttribute(definedIn, attribute, type)
        : converter;
}
 
Example #15
Source Project: lams   Author: lamsfoundation   File: LocalConversionMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
public SingleValueConverter getConverterFromItemType(String fieldName, Class type,
    Class definedIn) {
    SingleValueConverter converter = getLocalSingleValueConverter(
        definedIn, fieldName, type);
    return converter == null
        ? super.getConverterFromItemType(fieldName, type, definedIn)
        : converter;
}
 
Example #16
Source Project: lams   Author: lamsfoundation   File: LocalConversionMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
private SingleValueConverter getLocalSingleValueConverter(Class definedIn,
    String fieldName, Class type) {
    if (attributeMapper != null
        && attributeMapper.shouldLookForSingleValueConverter(fieldName, type, definedIn)) {
        Converter converter = getLocalConverter(definedIn, fieldName);
        if (converter != null && converter instanceof SingleValueConverter) {
            return (SingleValueConverter)converter;
        }
    }
    return null;
}
 
Example #17
Source Project: lams   Author: lamsfoundation   File: EnumMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SingleValueConverter getConverterFromItemType(String fieldName, Class type,
        Class definedIn) {
        SingleValueConverter converter = getLocalConverter(fieldName, type, definedIn);
        return converter == null
            ? super.getConverterFromItemType(fieldName, type, definedIn)
            : converter;
}
 
Example #18
Source Project: lams   Author: lamsfoundation   File: EnumMapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public SingleValueConverter getConverterFromAttribute(Class definedIn, String attribute,
    Class type) {
    SingleValueConverter converter = getLocalConverter(attribute, type, definedIn);
    return converter == null
        ? super.getConverterFromAttribute(definedIn, attribute, type)
        : converter;
}
 
Example #19
Source Project: saros   Author: saros-project   File: XStreamExtensionProvider.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new XStreamExtensionProvider using the given element name as the XML root element with
 * the given namespace. The Provider is able to understand the given classes, which should be
 * annotated using XStream annotations.
 *
 * <p><b>Important</b>: use valid XML element names and namespaces or the receiving side will be
 * unable to decode the extension !
 */
public XStreamExtensionProvider(String namespace, String elementName, Class<?>... classes) {

  if (namespace == null) throw new NullPointerException("namespace is null");

  ClassLoader classLoader = currentClassloader;

  this.elementName = elementName;
  this.namespace = namespace;

  xstream = XStreamFactory.getSecureXStream();

  if (classLoader != null) xstream.setClassLoader(classLoader);
  else xstream.setClassLoader(getClass().getClassLoader());

  xstream.registerConverter(BooleanConverter.BINARY);
  xstream.registerConverter(new UrlEncodingStringConverter());
  xstream.processAnnotations(XStreamPacketExtension.class);
  xstream.processAnnotations(classes);
  xstream.alias(elementName, XStreamPacketExtension.class);

  ProviderManager providerManager = ProviderManager.getInstance();
  providerManager.addExtensionProvider(getElementName(), getNamespace(), this);
  providerManager.addIQProvider(getElementName(), getNamespace(), this);

  // TODO Validate that elementName is a valid XML identifier

  replaceables = new HashMap<Class<? extends Converter>, ReplaceableConverter>();
  replaceableSingles =
      new HashMap<Class<? extends SingleValueConverter>, ReplaceableSingleValueConverter>();
}
 
Example #20
Source Project: saros   Author: saros-project   File: XStreamExtensionProvider.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unregisters a previously registered {@link SingleValueConverter} from XStream.
 *
 * @param converter If this converter (more precisely: one of the same class) was registered
 *     through {@link #registerConverter(SingleValueConverter)}, it will no longer be called by
 *     XStream. Otherwise, nothing happens.
 */
public void unregisterConverter(SingleValueConverter converter) {
  Class<? extends SingleValueConverter> clazz = converter.getClass();

  if (replaceableSingles.containsKey(clazz)) {
    log.debug("Unregistering (resetting) converter of " + clazz);
    replaceableSingles.get(clazz).reset();
  }
}
 
Example #21
Source Project: saros   Author: saros-project   File: ReplaceableSingleValueConverterTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
public void reset() {
  /* Mocks */
  SingleValueConverter c1 = EasyMock.createMock(SingleValueConverter.class);
  expect(c1.canConvert(Dummy.class)).andStubReturn(true);

  EasyMock.expect(c1.toString(isA(Object.class))).andReturn("dummy").once();

  EasyMock.replay(c1);

  /* XStream config */
  XStream xstream = XStreamFactory.getSecureXStream(new DomDriver());
  ReplaceableSingleValueConverter resetable = new ReplaceableSingleValueConverter(c1);
  xstream.registerConverter(resetable);

  /* Test it */
  assertFalse("ReplaceableSingleValueConverter was not properly set up", resetable.isReset());

  assertNotNull("Converter cannot convert", xstream.toXML(new Dummy()));

  resetable.reset();
  assertTrue("ReplaceableSingleValueConverter was not properly reset", resetable.isReset());

  /*
   * This call should not reach the actual converter.
   */
  xstream.toXML(new Dummy());

  /*
   * Verify that the converter was used exactly once, i.e. it was not
   * called while it was inactive.
   */
  EasyMock.verify(c1);
}
 
Example #22
Source Project: vraptor4   Author: caelum   File: XStreamBuilderImpl.java    License: Apache License 2.0 5 votes vote down vote up
public static XStreamBuilder cleanInstance(Converter...converters) {
	Instance<Converter> convertersInst = new MockInstanceImpl<>(converters);
	Instance<SingleValueConverter> singleValueConverters = new MockInstanceImpl<>();
	XStreamConverters xStreamConverters = new XStreamConverters(convertersInst, singleValueConverters);
	return new XStreamBuilderImpl(xStreamConverters, new DefaultTypeNameExtractor(), 
			new Serializee(new DefaultReflectionProvider()), new DefaultReflectionProvider());
}
 
Example #23
Source Project: lams   Author: lamsfoundation   File: UseAttributeForEnumMapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
public SingleValueConverter getConverterFromItemType(String fieldName, Class type,
    Class definedIn) {
    return null;
}
 
Example #24
Source Project: lams   Author: lamsfoundation   File: UseAttributeForEnumMapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
public SingleValueConverter getConverterFromAttribute(Class definedIn,
    String attribute, Class type) {
    return null;
}
 
Example #25
Source Project: lams   Author: lamsfoundation   File: XStream.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void registerConverter(SingleValueConverter converter) {
    registerConverter(converter, PRIORITY_NORMAL);
}
 
Example #26
Source Project: lams   Author: lamsfoundation   File: XStream.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void registerConverter(SingleValueConverter converter, int priority) {
    if (converterRegistry != null) {
        converterRegistry.registerConverter(
            new SingleValueConverterWrapper(converter), priority);
    }
}
 
Example #27
Source Project: lams   Author: lamsfoundation   File: AttributeMapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @deprecated As of 1.3.1, use {@link #getConverterFromAttribute(Class, String, Class)}
 */
public SingleValueConverter getConverterFromAttribute(Class definedIn, String attribute) {
    final Field field = reflectionProvider.getFieldOrNull(definedIn, attribute);
    return field != null ? getConverterFromAttribute(definedIn, attribute, field.getType()) : null;
}
 
Example #28
Source Project: lams   Author: lamsfoundation   File: DefaultMapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @deprecated As of 1.3, use {@link #getConverterFromAttribute(Class, String, Class)}
 */
public SingleValueConverter getConverterFromAttribute(String name) {
    return null;
}
 
Example #29
Source Project: lams   Author: lamsfoundation   File: DefaultMapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @deprecated As of 1.3, use {@link #getConverterFromItemType(String, Class, Class)}
 */
public SingleValueConverter getConverterFromItemType(String fieldName, Class type) {
    return null;
}
 
Example #30
Source Project: lams   Author: lamsfoundation   File: DefaultMapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * @deprecated As of 1.3, use {@link #getConverterFromItemType(String, Class, Class)}
 */
public SingleValueConverter getConverterFromItemType(Class type) {
    return null;
}