Java Code Examples for com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider

The following examples show how to use com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider. 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
/**
 * @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 2
Source Project: weixin-java-tools   Source File: XStreamInitializer.java    License: Apache License 2.0 5 votes vote down vote up
public static XStream getInstance() {
  XStream xstream = new XStream(new PureJavaReflectionProvider(), new XppDriver() {

    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
      return new PrettyPrintWriter(out, getNameCoder()) {
        protected String PREFIX_CDATA = "<![CDATA[";
        protected String SUFFIX_CDATA = "]]>";
        protected String PREFIX_MEDIA_ID = "<MediaId>";
        protected String SUFFIX_MEDIA_ID = "</MediaId>";

        @Override
        protected void writeText(QuickWriter writer, String text) {
          if (text.startsWith(this.PREFIX_CDATA) && text.endsWith(this.SUFFIX_CDATA)) {
            writer.write(text);
          } else if (text.startsWith(this.PREFIX_MEDIA_ID) && text.endsWith(this.SUFFIX_MEDIA_ID)) {
            writer.write(text);
          } else {
            super.writeText(writer, text);
          }

        }

        @Override
        public String encodeNode(String name) {
          //防止将_转换成__
          return name;
        }
      };
    }
  });

  xstream.ignoreUnknownElements();
  xstream.setMode(XStream.NO_REFERENCES);
  xstream.addPermission(NullPermission.NULL);
  xstream.addPermission(PrimitiveTypePermission.PRIMITIVES);
  xstream.setClassLoader(Thread.currentThread().getContextClassLoader());
  return xstream;
}
 
Example 3
Source Project: vraptor4   Source File: VRaptorXStream.java    License: Apache License 2.0 5 votes vote down vote up
public VRaptorXStream(TypeNameExtractor extractor, HierarchicalStreamDriver hierarchicalStreamDriver, 
		Serializee serializee, ReflectionProvider reflectionProvider) {
	super(new PureJavaReflectionProvider(),hierarchicalStreamDriver);
	this.extractor = extractor;
	this.serializee = serializee;
	this.reflectionProvider = reflectionProvider;
}
 
Example 4
Source Project: weblaf   Source File: XmlUtils.java    License: GNU General Public License v3.0 4 votes vote down vote up
/**
 * Initializes global XStream instance.
 */
private static void initializeXStream ()
{
    try
    {
        // Custom ReflectionProvider to provide pure-java objects instantiation
        // Unlike default SunUnsafeReflectionProvider it has limited objects instantiation options
        // Also it properly initializes object field values specified directly near the fields
        final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider ();

        // Custom HierarchicalStreamDriver implementation based on StaxDriver
        // It provides us HierarchicalStreamReader to allow XStreamContext usage
        hierarchicalStreamDriver = new XmlDriver ();

        // XStream instance initialization
        xStream = new XStream ( reflectionProvider, hierarchicalStreamDriver );

        // Make sure that XStream ClassLoader finds WebLaF classes in cases where multiple ClassLoaders are used
        // E.g. IntelliJ IDEA uses different ClassLoaders for plugins (e.g. JFormDesigner) and its core (which includes XStream)
        if ( XmlUtils.class.getClassLoader () != xStream.getClass ().getClassLoader () )
        {
            final ClassLoader classLoader = xStream.getClassLoader ();
            if ( classLoader instanceof CompositeClassLoader )
            {
                ( ( CompositeClassLoader ) classLoader ).add ( XmlUtils.class.getClassLoader () );
            }
            else
            {
                final CompositeClassLoader compositeClassLoader = new CompositeClassLoader ();
                compositeClassLoader.add ( XmlUtils.class.getClassLoader () );
                xStream.setClassLoader ( compositeClassLoader );
            }
        }

        // Standard Java-classes aliases
        if ( aliasJdkClasses )
        {
            // Custom {@link java.awt.Point} mapping
            xStream.alias ( "Point", Point.class );
            xStream.useAttributeFor ( Point.class, "x" );
            xStream.useAttributeFor ( Point.class, "y" );

            // Custom {@link java.awt.geom.Point2D} mapping
            xStream.alias ( "Point2D", Point2D.class );
            xStream.registerConverter ( new Point2DConverter () );

            // Custom {@link java.awt.Dimension} mapping
            xStream.alias ( "Dimension", Dimension.class );
            xStream.registerConverter ( new DimensionConverter () );

            // Custom {@link java.awt.Rectangle} mapping
            xStream.alias ( "Rectangle", Rectangle.class );
            xStream.useAttributeFor ( Rectangle.class, "x" );
            xStream.useAttributeFor ( Rectangle.class, "y" );
            xStream.useAttributeFor ( Rectangle.class, "width" );
            xStream.useAttributeFor ( Rectangle.class, "height" );

            // Custom {@link java.awt.Font} mapping
            xStream.alias ( "Font", Font.class );

            // Custom {@link java.awt.Color} mapping
            xStream.alias ( "Color", Color.class );
            xStream.registerConverter ( new ColorConverter () );

            // Custom {@link java.awt.Insets} mapping
            xStream.alias ( "Insets", Insets.class );
            xStream.registerConverter ( new InsetsConverter () );

            // Custom {@link java.awt.Stroke} mapping
            xStream.alias ( "Stroke", Stroke.class );
            xStream.registerConverter ( new StrokeConverter () );
        }

        // Additional WebLaF data classes aliases
        xStream.processAnnotations ( Pair.class );
        xStream.processAnnotations ( Scale.class );
    }
    catch ( final Exception e )
    {
        // It is a pretty fatal for library if something goes wrong here
        throw new UtilityException ( "Unable to initialize XStream instance", e );
    }
}
 
Example 5
Source Project: vraptor4   Source File: VRaptorXStream.java    License: Apache License 2.0 4 votes vote down vote up
public VRaptorXStream(TypeNameExtractor extractor, Serializee serializee, ReflectionProvider reflectionProvider) {
	super(new PureJavaReflectionProvider());
	this.extractor = extractor;
	this.serializee = serializee;
	this.reflectionProvider = reflectionProvider;
}