Package com.dooapp.gaedo.blueprints.transformers

Source Code of com.dooapp.gaedo.blueprints.transformers.SerializableLiteralTransformer

package com.dooapp.gaedo.blueprints.transformers;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

import com.dooapp.gaedo.blueprints.Properties;
import com.tinkerpop.blueprints.pgm.Vertex;

/**
* Cass performing (de)serialization from/to XML before to put it in vertex property
* @author ndx
*
*/
public class SerializableLiteralTransformer extends AbstractLiteralTransformer<Serializable> implements LiteralTransformer<Serializable>{
  @Override
  public Serializable loadObject(Class valueClass, Vertex key) {
    Object value = key.getProperty(Properties.value.name());
    ByteArrayInputStream stream = new ByteArrayInputStream(value.toString().getBytes());
    XMLDecoder decoder = new XMLDecoder(stream);
    return (Serializable) decoder.readObject();
  }


  @Override
  protected Object getVertexValue(Serializable value) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    XMLEncoder encoder = new XMLEncoder(stream);
    encoder.writeObject(value);
    encoder.close();
    try {
      stream.close();
      return new String(stream.toByteArray());
    } catch (IOException e) {
      throw new UnableToStoreSerializableException("impossible to store serializable value "+value, e);
    }
   
  }

}
TOP

Related Classes of com.dooapp.gaedo.blueprints.transformers.SerializableLiteralTransformer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.