Java Code Examples for com.thoughtworks.xstream.mapper.MapperWrapper

The following examples show how to use com.thoughtworks.xstream.mapper.MapperWrapper. These examples are extracted from open source projects. 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: openmrs-module-initializer   Source File: InitializerSerializer.java    License: MIT License 6 votes vote down vote up
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
	return new MapperWrapper(next) {
		
		@Override
		public boolean shouldSerializeMember(Class definedIn, String fieldName) {
			if (definedIn == Object.class) {
				// This is not compatible with implicit collections where item name is not
				// defined
				return false;
			} else {
				return super.shouldSerializeMember(definedIn, fieldName);
			}
		}
	};
}
 
Example 2
Source Project: swingsane   Source File: XStreamUtility.java    License: Apache License 2.0 6 votes vote down vote up
private static XStream loadXStream() {
  XStream xstream = new XStream(new DomDriver("UTF-8")) {
    @Override
    protected MapperWrapper wrapMapper(MapperWrapper next) {
      return new MapperWrapper(next) {
        @Override
        public boolean shouldSerializeMember(@SuppressWarnings("rawtypes") Class definedIn,
            String fieldName) {
          if (definedIn == Object.class) {
            return false;
          }
          return super.shouldSerializeMember(definedIn, fieldName);
        }
      };
    }
  };
  xstream.setMode(XStream.NO_REFERENCES);
  return xstream;
}
 
Example 3
Source Project: brooklyn-server   Source File: XmlSerializer.java    License: Apache License 2.0 6 votes vote down vote up
/** Extension point where sub-classes can add mappers needed for handling class names.
 * This is used by {@link #wrapMapperForNormalUsage(Mapper)} and also to set up the {@link JavaClassConverter}
 * (see {@link #newCustomJavaClassConverter()} for what that does).
 * <p>
 * This should apply when nice names are used for inner classes, or classes are renamed;
 * however mappers which affect field aliases or intercept references to entities are not
 * wanted in the {@link JavaClassConverter} and so should be added by {@link #wrapMapperForNormalUsage(Mapper)}
 * instead of this.
 * <p>
 * Developers note this is called from the constructor; be careful when overriding and 
 * see comment on {@link #wrapMapperForNormalUsage(Mapper)} about field availability. */
protected MapperWrapper wrapMapperForHandlingClasses(Mapper next) {
    MapperWrapper result = new CompilerIndependentOuterClassFieldMapper(next);
    
    Supplier<ClassLoader> classLoaderSupplier = new Supplier<ClassLoader>() {
        @Override public ClassLoader get() {
            return xstream.getClassLoaderReference().getReference();
        }
    };
    result = new ClassRenamingMapper(result, deserializingClassRenames, classLoaderSupplier);
    result = new OsgiClassnameMapper(new Supplier<XStream>() {
        @Override public XStream get() { return xstream; } }, result);
    // TODO as noted in ClassRenamingMapper that class can be simplified if 
    // we swap the order of the above calls, because it _will_ be able to rely on
    // OsgiClassnameMapper to attempt to load with the xstream reference stack
    // (not doing it just now because close to a release)
    
    return result;
}
 
Example 4
Source Project: brooklyn-server   Source File: CompilerCompatibilityTest.java    License: Apache License 2.0 6 votes vote down vote up
private void deserialize(String inputUrl) throws Exception {
    XStream xstream = new XStream() {
        @Override
        protected MapperWrapper wrapMapper(MapperWrapper next) {
            return new CompilerIndependentOuterClassFieldMapper(super.wrapMapper(next));
        }
    };

    InputStream in = this.getClass().getResourceAsStream(inputUrl);
    try {
        Object obj = xstream.fromXML(in);
        assertNonNullOuterFields(obj);
    } catch (Exception e) {
    	System.out.println(e.getMessage());
    	throw e;
    } finally {
        in.close();
    }
}
 
Example 5
/**
 * @return
 */
private XStream initXStream(final Session session, final boolean nullifyPk)
{
  final XStream xstream = new XStream() {
    @Override
    protected MapperWrapper wrapMapper(final MapperWrapper next)
    {
      return new HibernateMapper(new HibernateCollectionsMapper(next));
    }
  };
  // Converter für die Hibernate-Collections
  xstream.registerConverter(new HibernateCollectionConverter(xstream.getConverterLookup()));
  xstream.registerConverter(
      new HibernateProxyConverter(xstream.getMapper(), new PureJavaReflectionProvider(), xstream.getConverterLookup()),
      XStream.PRIORITY_VERY_HIGH);
  xstream.setMarshallingStrategy(new XStreamMarshallingStrategy(XStreamMarshallingStrategy.RELATIVE));
  init(xstream);
  return xstream;
}
 
Example 6
Source Project: kogito-runtimes   Source File: XStreamUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static XStream createTrustingXStream(ReflectionProvider reflectionProvider, final Function<MapperWrapper, MapperWrapper> mapper) {
    return internalCreateTrustingXStream(new XStream(reflectionProvider) {
        protected MapperWrapper wrapMapper(MapperWrapper next) {
            return (MapperWrapper)mapper.apply(next);
        }
    });
}
 
Example 7
Source Project: kogito-runtimes   Source File: XStreamUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static XStream createNonTrustingXStream(ReflectionProvider reflectionProvider, final Function<MapperWrapper, MapperWrapper> mapper) {
    return internalCreateNonTrustingXStream(new XStream(reflectionProvider) {
        protected MapperWrapper wrapMapper(MapperWrapper next) {
            return (MapperWrapper)mapper.apply(next);
        }
    });
}
 
Example 8
Source Project: lams   Source File: XStream.java    License: 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 9
Source Project: cuba   Source File: EntitySnapshotManager.java    License: Apache License 2.0 5 votes vote down vote up
protected Object fromXML(String xml) {
    final List exclUpdateFields = Arrays.asList("updateTs", "updatedBy");
    XStream xStream = new CubaXStream() {
        @Override
        protected MapperWrapper wrapMapper(MapperWrapper next) {
            return new MapperWrapper(next) {
                @Override
                public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                    boolean result = super.shouldSerializeMember(definedIn, fieldName);
                    if (!result) {
                        return false;
                    }
                    if (fieldName != null) {
                        if (exclUpdateFields.contains(fieldName)
                                && Updatable.class.isAssignableFrom(definedIn)) {
                            return false;
                        }
                        if ("uuid".equals(fieldName)) {
                            if (!HasUuid.class.isAssignableFrom(definedIn)
                                    && BaseGenericIdEntity.class.isAssignableFrom(definedIn)) {
                                return false;
                            }
                        }
                    }
                    return true;
                }
            };
        }
    };
    XStream.setupDefaultSecurity(xStream);
    xStream.allowTypeHierarchy(Serializable.class);
    xStream.omitField(BaseGenericIdEntity.class, "createTs");
    xStream.omitField(BaseGenericIdEntity.class, "createdBy");

    return xStream.fromXML(xml);
}
 
Example 10
Source Project: brooklyn-server   Source File: OsgiClassnameMapper.java    License: Apache License 2.0 5 votes vote down vote up
public OsgiClassnameMapper(Supplier<XStream> xstream, MapperWrapper mapper) {
    super(mapper);
    // supplier, rather than instance, used for reasons noted at XmlSerializer.wrapMapperForNormalUsage 
    this.xstream = xstream;
    
    prefixer = new OsgiClassPrefixer();
}
 
Example 11
Source Project: brooklyn-server   Source File: XmlMementoSerializer.java    License: 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 12
public XmlObjectSerializerIgnoreMissingFieldsServiceImpl() {

        xstream = new XStream(new ProxyAwareJavaReflectionProvider()) {
            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    @Override
                    public boolean shouldSerializeMember(Class definedIn,
                            String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        }
                      return super.shouldSerializeMember(definedIn, fieldName);
                   }
               };
           }
       };

		xstream.registerConverter(new ProxyConverter(xstream.getMapper(), xstream.getReflectionProvider() ));
        try {
        	Class<?> objListProxyClass = Class.forName("org.apache.ojb.broker.core.proxy.ListProxyDefaultImpl");
            xstream.addDefaultImplementation(ArrayList.class, objListProxyClass);
        } catch ( Exception ex ) {
        	// Do nothing - this will blow if the OJB class does not exist, which it won't in some installs
        }
        xstream.registerConverter(new DateTimeConverter());
	}
 
Example 13
Source Project: engage-api-client   Source File: XStreamFactory.java    License: Apache License 2.0 5 votes vote down vote up
public XStream createXStream(ReflectionProvider reflectionProvider) {
       return new XStream(reflectionProvider, new DomDriver("UTF-8", new XmlFriendlyNameCoder("__","_"))) {
		@Override
		protected MapperWrapper wrapMapper(MapperWrapper next) {
               return new ApiMapperWrapper(next, apiResponseType);
		}
	};
}
 
Example 14
Source Project: vraptor4   Source File: VRaptorXStream.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
	/* this method is called in the super constructor, so we cannot use instance variables, so we're
	 * using this 'lazy' get */
	vraptorMapper = new VRaptorClassMapper(next, lazyTypeNameExtractor(), lazySerializee(), lazyReflectionProvider());
	return vraptorMapper;
}
 
Example 15
Source Project: lams   Source File: XStream.java    License: GNU General Public License v2.0 4 votes vote down vote up
protected MapperWrapper wrapMapper(MapperWrapper next) {
    return next;
}
 
Example 16
Source Project: brooklyn-server   Source File: XmlSerializer.java    License: Apache License 2.0 4 votes vote down vote up
public XmlSerializer(ClassLoader loader, Map<String, String> deserializingClassRenames) {
    this.deserializingClassRenames = deserializingClassRenames;
    xstream = new XStream() {
        @Override
        protected MapperWrapper wrapMapper(MapperWrapper next) {
            return XmlSerializer.this.wrapMapperForNormalUsage( super.wrapMapper(next) );
        }
    };

    XStream.setupDefaultSecurity(xstream);
    xstream.allowTypesByWildcard(new String[] {
           "**"
    });

    if (loader!=null) {
        xstream.setClassLoader(loader);
    }
    
    xstream.registerConverter(newCustomJavaClassConverter(), XStream.PRIORITY_NORMAL);
    
    // list as array list is default
    xstream.alias("map", Map.class, LinkedHashMap.class);
    xstream.alias("set", Set.class, LinkedHashSet.class);
    
    xstream.registerConverter(new StringKeyMapConverter(xstream.getMapper()), /* priority */ 10);
    xstream.alias("MutableMap", MutableMap.class);
    xstream.alias("MutableSet", MutableSet.class);
    xstream.alias("MutableList", MutableList.class);
    
    // Needs an explicit MutableSet converter!
    // Without it, the alias for "set" seems to interfere with the MutableSet.map field, so it gets
    // a null field on deserialization.
    xstream.registerConverter(new MutableSetConverter(xstream.getMapper()));
    
    xstream.aliasType("ImmutableList", ImmutableList.class);
    xstream.registerConverter(new ImmutableListConverter(xstream.getMapper()));
    xstream.registerConverter(new ImmutableSetConverter(xstream.getMapper()));
    xstream.registerConverter(new ImmutableMapConverter(xstream.getMapper()));

    xstream.registerConverter(new EnumCaseForgivingConverter());
    xstream.registerConverter(new Inet4AddressConverter());
    
    // See ObjectWithDefaultStringImplConverter (and its usage) for why we want to auto-detect 
    // annotations (usages of this is in the camp project, so we can't just list it statically
    // here unfortunately).
    xstream.autodetectAnnotations(true);
}
 
Example 17
public HibernateMapper(MapperWrapper arg0)
{
  super(arg0);
  init();
}
 
Example 18
Source Project: spring-analysis-note   Source File: XStreamMarshaller.java    License: MIT License 2 votes vote down vote up
/**
 * Set one or more custom XStream {@link MapperWrapper} classes.
 * Each of those classes needs to have a constructor with a single argument
 * of type {@link Mapper} or {@link MapperWrapper}.
 * @since 4.0
 */
@SuppressWarnings("unchecked")
public void setMapperWrappers(Class<? extends MapperWrapper>... mapperWrappers) {
	this.mapperWrappers = mapperWrappers;
}
 
Example 19
Source Project: java-technology-stack   Source File: XStreamMarshaller.java    License: MIT License 2 votes vote down vote up
/**
 * Set one or more custom XStream {@link MapperWrapper} classes.
 * Each of those classes needs to have a constructor with a single argument
 * of type {@link Mapper} or {@link MapperWrapper}.
 * @since 4.0
 */
@SuppressWarnings("unchecked")
public void setMapperWrappers(Class<? extends MapperWrapper>... mapperWrappers) {
	this.mapperWrappers = mapperWrappers;
}
 
Example 20
Source Project: brooklyn-server   Source File: XmlSerializer.java    License: Apache License 2.0 2 votes vote down vote up
/** Extension point where sub-classes can add mappers to set up the main {@link Mapper} given to XStream.
 * This includes all of {@link #wrapMapperForHandlingClasses(Mapper)} plus anything wanted for normal usage.
 * <p>
 * Typically any non-class-name mappers wanted should be added in a subclass by overriding this field,
 * calling this superclass method, then wrapping the result.
 * <p>
 * Developers note this is called from the constructor; be careful when overriding 
 * because most fields won't be available.  In particular in a subclass, 
 * this method in the subclass will be invoked very early in its constructor.
 * Fields like {@link #xstream} (and <i>anything</i> set in the subclass) won't
 * yet be available. For this reason some mappers will need to be given a {@link Supplier} for late resolution. */
protected MapperWrapper wrapMapperForNormalUsage(Mapper next) {
    return wrapMapperForHandlingClasses(next);
}