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 File: XStreamSerializerTest.java    From vraptor4 with 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 #2
Source File: FilePersistenceStrategy.java    From lams with 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 #3
Source File: EnumMapper.java    From lams with 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 #4
Source File: MagicWandModel.java    From snap-desktop with 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 #5
Source File: Filter.java    From snap-desktop with 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 #6
Source File: XStreamExtensionProvider.java    From saros with 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 #7
Source File: AttributeMapper.java    From lams with 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 #8
Source File: LocalConversionMapper.java    From lams with 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 #9
Source File: LocalConversionMapper.java    From lams with 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 #10
Source File: LocalConversionMapper.java    From lams with 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 #11
Source File: EnumMapper.java    From lams with 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 #12
Source File: EnumMapper.java    From lams with 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 #13
Source File: XStreamBuilderImpl.java    From vraptor4 with 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 #14
Source File: XStreamExtensionProvider.java    From saros with 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 #15
Source File: NamedMapConverter.java    From lams with 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 #16
Source File: AttributeMapper.java    From lams with 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 #17
Source File: AttributeMapper.java    From lams with 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 #18
Source File: AttributeMapper.java    From lams with 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 #19
Source File: AttributeMapper.java    From lams with 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 #20
Source File: AttributeMapper.java    From lams with 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 #21
Source File: XStreamExtensionProvider.java    From saros with 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 #22
Source File: ReplaceableSingleValueConverterTest.java    From saros with 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 #23
Source File: MapperWrapper.java    From lams with 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 type, String attribute) {
    return getConverterFromAttributeMapper.getConverterFromAttribute(type, attribute);
}
 
Example #24
Source File: MapperWrapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public SingleValueConverter getConverterFromAttribute(Class definedIn, String attribute, Class type) {
    return getConverterFromAttributeMapper.getConverterFromAttribute(definedIn, attribute, type);
}
 
Example #25
Source File: EnumMapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private Object readResolve() {
    this.enumConverterMap = new HashMap<Class, SingleValueConverter>();
    this.attributeMapper = (AttributeMapper)lookupMapperOfType(AttributeMapper.class);
    return this;
}
 
Example #26
Source File: XStreamConverters.java    From vraptor4 with Apache License 2.0 4 votes vote down vote up
@Inject
public XStreamConverters(@Any Instance<Converter> converters, @Any Instance<SingleValueConverter> singleValueConverters) {
	this.converters = converters;
	this.singleValueConverters = singleValueConverters;
}
 
Example #27
Source File: MapperWrapper.java    From lams with 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 getConverterFromAttributeMapper.getConverterFromAttribute(name);
}
 
Example #28
Source File: UseAttributeForEnumMapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public SingleValueConverter getConverterFromItemType(String fieldName, Class type,
    Class definedIn) {
    return null;
}
 
Example #29
Source File: UseAttributeForEnumMapper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public SingleValueConverter getConverterFromAttribute(Class definedIn,
    String attribute, Class type) {
    return null;
}
 
Example #30
Source File: XStream.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void registerConverter(SingleValueConverter converter) {
    registerConverter(converter, PRIORITY_NORMAL);
}