com.thoughtworks.xstream.mapper.Mapper Java Examples

The following examples show how to use com.thoughtworks.xstream.mapper.Mapper. 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: ToAttributedValueConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new ToAttributedValueConverter instance.
 * 
 * @param type the type that is handled by this converter instance
 * @param mapper the mapper in use
 * @param reflectionProvider the reflection provider in use
 * @param lookup the converter lookup in use
 * @param valueFieldName the field defining the tag's value (may be null)
 * @param valueDefinedIn the type defining the field
 */
public ToAttributedValueConverter(
    final Class type, final Mapper mapper, final ReflectionProvider reflectionProvider,
    final ConverterLookup lookup, final String valueFieldName, Class valueDefinedIn) {
    this.type = type;
    this.mapper = mapper;
    this.reflectionProvider = reflectionProvider;
    this.lookup = lookup;

    if (valueFieldName == null) {
        valueField = null;
    } else {
        Field field = null;
        try {
            field = (valueDefinedIn != null ? valueDefinedIn : type)
                .getDeclaredField(valueFieldName);
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
        } catch (NoSuchFieldException e) {
            throw new IllegalArgumentException(e.getMessage() + ": " + valueFieldName);
        }
        this.valueField = field;
    }
    enumMapper = JVM.isVersion(5) ? UseAttributeForEnumMapper.createEnumMapper(mapper) : null;
}
 
Example #2
Source File: IdentifiableToIdConverter.java    From cloudstreetmarket.com with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new ToAttributedValueConverter instance.
 * 
 * @param mapper the mapper in use
 * @param reflectionProvider the reflection provider in use
 * @param lookup the converter lookup in use
 * @param valueFieldName the field defining the tag's value (may be null)
 * @param valueDefinedIn the type defining the field
 */
public IdentifiableToIdConverter(
    final Class<Identifiable<?>> type, final Mapper mapper, final ReflectionProvider reflectionProvider,
    final ConverterLookup lookup, final String valueFieldName, Class<?> valueDefinedIn) {
    this.type = type;

       Field field = null;
       try {
           field = (valueDefinedIn != null ? valueDefinedIn : type.getSuperclass())
               .getDeclaredField("id");
          
           if (!field.isAccessible()) {
               field.setAccessible(true);
           }
       } catch (NoSuchFieldException e) {
           throw new IllegalArgumentException(e.getMessage() + ": " + valueFieldName);
       }
}
 
Example #3
Source File: NamedArrayConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
    final List list = new ArrayList();
    while (reader.hasMoreChildren()) {
        reader.moveDown();
        final Object item;
        final String className = HierarchicalStreams.readClassAttribute(reader, mapper);
        final Class itemType = className == null ? arrayType.getComponentType() : mapper.realClass(className);
        if (Mapper.Null.class.equals(itemType)) {
            item = null;
        } else {
            item = context.convertAnother(null, itemType);
        }
        list.add(item);
        reader.moveUp();
    }
    final Object array = Array.newInstance(arrayType.getComponentType(), list.size());
    for (int i = 0; i < list.size(); ++i) {
        Array.set(array, i, list.get(i));
    }
    return array;
}
 
Example #4
Source File: NamedArrayConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a NamedArrayConverter.
 * @param arrayType
 * @param mapper
 * @param itemName
 * @since 1.4.6
 */
public NamedArrayConverter(final Class arrayType, final Mapper mapper, final String itemName) {
    if (!arrayType.isArray()) {
        throw new IllegalArgumentException(arrayType.getName() + " is not an array");
    }
    this.arrayType = arrayType;
    this.mapper = mapper;
    this.itemName = itemName;
}
 
Example #5
Source File: TreeUnmarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public TreeUnmarshaller(
    Object root, HierarchicalStreamReader reader, ConverterLookup converterLookup,
    Mapper mapper) {
    this.root = root;
    this.reader = reader;
    this.converterLookup = converterLookup;
    this.mapper = mapper;
}
 
Example #6
Source File: StyleConverterUtils.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Read properties for the specified class into the provided properties map.
 *
 * @param reader     {@link HierarchicalStreamReader}
 * @param context    {@link UnmarshallingContext}
 * @param mapper     {@link Mapper}
 * @param properties map to read properties into
 * @param clazz      class to read properties for, it will be used to retrieve properties field types
 * @param styleId    component {@link com.alee.managers.style.StyleId}, might be used to report problems
 */
public static void readProperties ( @NotNull final HierarchicalStreamReader reader, @NotNull final UnmarshallingContext context,
                                    @NotNull final Mapper mapper, @NotNull final Map<String, Object> properties,
                                    @NotNull final Class clazz, final String styleId )
{
    while ( reader.hasMoreChildren () )
    {
        reader.moveDown ();
        readProperty ( reader, context, mapper, styleId, properties, clazz, reader.getNodeName () );
        reader.moveUp ();
    }
}
 
Example #7
Source File: CubaXStream.java    From cuba with Apache License 2.0 5 votes vote down vote up
protected CubaXStream(
        ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver, ClassLoaderReference classLoader,
        Mapper mapper, final CubaXStreamConverterLookup converterLookup) {
    super(reflectionProvider, driver, classLoader, mapper,
            converterLookup::lookupConverterForType,
            converterLookup::registerConverter);
}
 
Example #8
Source File: FontConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructs a FontConverter.
 * @param mapper
 * @since 1.4.5
 */
public FontConverter(Mapper mapper) {
    this.mapper = mapper;
    if (mapper == null) {
        textAttributeConverter = null;
    } else {
        textAttributeConverter = new TextAttributeConverter();
    }
}
 
Example #9
Source File: XStream.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Mapper buildMapper() {
    Mapper mapper = new DefaultMapper(classLoaderReference);
    if (useXStream11XmlFriendlyMapper()) {
        mapper = new XStream11XmlFriendlyMapper(mapper);
    }
    mapper = new DynamicProxyMapper(mapper);
    mapper = new PackageAliasingMapper(mapper);
    mapper = new ClassAliasingMapper(mapper);
    mapper = new ElementIgnoringMapper(mapper);
    mapper = new FieldAliasingMapper(mapper);
    mapper = new AttributeAliasingMapper(mapper);
    mapper = new SystemAttributeAliasingMapper(mapper);
    mapper = new ImplicitCollectionMapper(mapper, reflectionProvider);
    mapper = new OuterClassMapper(mapper);
    mapper = new ArrayMapper(mapper);
    mapper = new DefaultImplementationsMapper(mapper);
    mapper = new AttributeMapper(mapper, converterLookup, reflectionProvider);
    if (JVM.isVersion(5)) {
        mapper = buildMapperDynamically(
            "com.thoughtworks.xstream.mapper.EnumMapper", new Class[]{Mapper.class},
            new Object[]{mapper});
    }
    mapper = new LocalConversionMapper(mapper);
    mapper = new ImmutableTypesMapper(mapper);
    if (JVM.isVersion(8)) {
        mapper = buildMapperDynamically("com.thoughtworks.xstream.mapper.LambdaMapper", new Class[]{Mapper.class},
            new Object[]{mapper});
    }
    mapper = new SecurityMapper(mapper);
    if (JVM.isVersion(5)) {
        mapper = buildMapperDynamically(ANNOTATION_MAPPER_TYPE, new Class[]{
            Mapper.class, ConverterRegistry.class, ConverterLookup.class,
            ClassLoaderReference.class, ReflectionProvider.class}, new Object[]{
            mapper, converterRegistry, converterLookup, classLoaderReference,
            reflectionProvider});
    }
    mapper = wrapMapper((MapperWrapper)mapper);
    mapper = new CachingMapper(mapper);
    return mapper;
}
 
Example #10
Source File: NamedCollectionConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @deprecated As of 1.4.11 use {@link #writeCompleteItem(Object, MarshallingContext, HierarchicalStreamWriter)}
 *             instead.
 */
protected void writeItem(Object item, MarshallingContext context, HierarchicalStreamWriter writer) {
    final Class itemType = item == null ? Mapper.Null.class : item.getClass();
    ExtendedHierarchicalStreamWriterHelper.startNode(writer, name, itemType);
    if (!itemType.equals(type)) {
        String attributeName = mapper().aliasForSystemAttribute("class");
        if (attributeName != null) {
            writer.addAttribute(attributeName, mapper().serializedClass(itemType));
        }
    }
    if (item != null) {
        context.convertAnother(item);
    }
    writer.endNode();
}
 
Example #11
Source File: HierarchicalStreams.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static String readClassAttribute(HierarchicalStreamReader reader, Mapper mapper) {
    String attributeName = mapper.aliasForSystemAttribute("resolves-to");
    String classAttribute = attributeName == null ? null : reader.getAttribute(attributeName);
    if (classAttribute == null) {
        attributeName = mapper.aliasForSystemAttribute("class");
        if (attributeName != null) {
            classAttribute = reader.getAttribute(attributeName);
        }
    }
    return classAttribute;
}
 
Example #12
Source File: NamedMapConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void writeItem(String name, Class type, Object item, MarshallingContext context,
    HierarchicalStreamWriter writer) {
    Class itemType = item == null ? Mapper.Null.class : item.getClass();
    ExtendedHierarchicalStreamWriterHelper.startNode(writer, name, itemType);
    if (!itemType.equals(type)) {
        String attributeName = mapper().aliasForSystemAttribute("class");
        if (attributeName != null) {
            writer.addAttribute(attributeName, mapper().serializedClass(itemType));
        }
    }
    if (item != null) {
        context.convertAnother(item);
    }
    writer.endNode();
}
 
Example #13
Source File: CollectionConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a CollectionConverter for a special Collection type.
 * @param mapper the mapper
 * @param type the Collection type to handle
 * @since 1.4.5
 */
public CollectionConverter(Mapper mapper, Class type) {
    super(mapper);
    this.type = type;
    if (type != null && !Collection.class.isAssignableFrom(type)) {
        throw new IllegalArgumentException(type + " not of type " + Collection.class);
    }
}
 
Example #14
Source File: XmlMementoSerializer.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static Class<?> readClassType(HierarchicalStreamReader reader, Mapper mapper) {
    String classAttribute = readClassAttribute(reader, mapper);
    Class<?> type;
    if (classAttribute == null) {
        type = mapper.realClass(reader.getNodeName());
    } else {
        type = mapper.realClass(classAttribute);
    }
    return type;
}
 
Example #15
Source File: MapConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a MapConverter for a special Map type.
 * @param mapper the mapper
 * @param type the type to handle
 * @since 1.4.5
 */
public MapConverter(Mapper mapper, Class type) {
    super(mapper);
    this.type = type;
    if (type != null && !Map.class.isAssignableFrom(type)) {
        throw new IllegalArgumentException(type + " not of type " + Map.class);
    }
}
 
Example #16
Source File: XmlMementoSerializer.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Override
protected MapperWrapper wrapMapperForNormalUsage(Mapper next) {
    MapperWrapper mapper = super.wrapMapperForNormalUsage(next);
    mapper = new CustomMapper(mapper, Entity.class, "entityProxy");
    mapper = new CustomMapper(mapper, Location.class, "locationProxy");
    mapper = new UnwantedStateLoggingMapper(mapper);
    return mapper;
}
 
Example #17
Source File: UseAttributeForEnumMapper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
static Mapper createEnumMapper(final Mapper mapper) {
    try {
        Class enumMapperClass = Class.forName(
            "com.thoughtworks.xstream.mapper.EnumMapper", true,
            Mapper.class.getClassLoader());
        return (Mapper)DependencyInjectionFactory.newInstance(
            enumMapperClass,
            new Object[]{new UseAttributeForEnumMapper(mapper
                .lookupMapperOfType(DefaultMapper.class))});
    } catch (Exception e) {
        return null;
    }
}
 
Example #18
Source File: AbstractJsonWriter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method to return the appropriate JSON type for a Java type.
 * 
 * @param clazz the type
 * @return One of the {@link Type} instances
 * @since 1.4.4
 */
protected Type getType(Class clazz) {
    return clazz == Mapper.Null.class
        ? Type.NULL
        : (clazz == Boolean.class || clazz == Boolean.TYPE) 
            ? Type.BOOLEAN 
            : NUMBER_TYPES.contains(clazz) 
                ? Type.NUMBER 
                : Type.STRING;
}
 
Example #19
Source File: XStream.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Constructs an XStream with a special {@link HierarchicalStreamDriver},
 * {@link ReflectionProvider}, a prepared {@link Mapper} chain, the
 * {@link ClassLoaderReference} and an own {@link ConverterLookup} and
 * {@link ConverterRegistry}.
 * <p>
 * The ClassLoaderReference should also be used for the Mapper chain. The ConverterLookup
 * should access the ConverterRegistry if you intent to register {@link Converter} instances
 * with XStream facade or you are using annotations.
 * </p>
 * 
 * @param reflectionProvider the reflection provider to use or <em>null</em> for best
 *            matching Provider
 * @param driver the driver instance
 * @param classLoaderReference the reference to the {@link ClassLoader} to use
 * @param mapper the instance with the {@link Mapper} chain or <em>null</em> for the default
 *            chain
 * @param converterLookup the instance that is used to lookup the converters
 * @param converterRegistry an instance to manage the converter instances or <em>null</em>
 *            to prevent any further registry (including annotations)
 * @throws InitializationException in case of an initialization problem
 * @since 1.4.5
 */
public XStream(
    ReflectionProvider reflectionProvider, HierarchicalStreamDriver driver,
    ClassLoaderReference classLoaderReference, Mapper mapper, ConverterLookup converterLookup,
    ConverterRegistry converterRegistry) {
    if (reflectionProvider == null) {
        reflectionProvider = JVM.newReflectionProvider();
    }
    this.reflectionProvider = reflectionProvider;
    this.hierarchicalStreamDriver = driver;
    this.classLoaderReference = classLoaderReference;
    this.converterLookup = converterLookup;
    this.converterRegistry = converterRegistry;
    this.mapper = mapper == null ? buildMapper() : mapper;

    setupMappers();
    setupSecurity();
    setupAliases();
    setupDefaultImplementations();
    setupConverters();
    setupImmutableTypes();
    setMode(XPATH_RELATIVE_REFERENCES);
}
 
Example #20
Source File: MapConverter.java    From onedev with MIT License 4 votes vote down vote up
public MapConverter(Mapper mapper) {
	super(mapper);
}
 
Example #21
Source File: CollectionConverter.java    From onedev with MIT License 4 votes vote down vote up
public CollectionConverter(Mapper mapper) {
	super(mapper);
}
 
Example #22
Source File: NullConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
    ExtendedHierarchicalStreamWriterHelper.startNode(writer, "null", Mapper.Null.class);
    writer.endNode();
}
 
Example #23
Source File: NullConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public boolean canConvert(Class type) {
    return type == null || Mapper.Null.class.isAssignableFrom(type);
}
 
Example #24
Source File: VersionedExternalizable.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public Converter(final Mapper mapper, final ReflectionProvider reflectionProvider) {
	super(mapper, reflectionProvider);
}
 
Example #25
Source File: ReferenceByXPathMarshallingStrategy.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected TreeUnmarshaller createUnmarshallingContext(Object root,
    HierarchicalStreamReader reader, ConverterLookup converterLookup, Mapper mapper) {
    return new ReferenceByXPathUnmarshaller(root, reader, converterLookup, mapper);
}
 
Example #26
Source File: XmlMementoSerializer.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public CustomMapper(Mapper wrapped, Class<?> clazz, String alias) {
    super(wrapped);
    this.clazz = checkNotNull(clazz, "clazz");
    this.alias = checkNotNull(alias, "alias");
}
 
Example #27
Source File: ReflectionConverter.java    From onedev with MIT License 4 votes vote down vote up
@SuppressWarnings("rawtypes")
public ReflectionConverter(Mapper mapper, ReflectionProvider reflectionProvider, Class type) {
   	super(mapper, reflectionProvider, type);
   }
 
Example #28
Source File: CompilerIndependentOuterClassFieldMapper.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public CompilerIndependentOuterClassFieldMapper(Mapper wrapped) {
    super(wrapped);
    classOuterFields.put(Object.class.getName(), Collections.<String>emptyList());
}
 
Example #29
Source File: HibernateProxyConverter.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public HibernateProxyConverter(Mapper arg0, ReflectionProvider arg1, ConverterLookup converterLookup)
{
  super(arg0, arg1);
  this.converterLookup = converterLookup;
}
 
Example #30
Source File: XmlMementoSerializer.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
TaskConverter(Mapper mapper) {
    this.mapper = mapper;
}