org.eclipselabs.emfjson.gwt.map.EObjectMapper.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipselabs.emfjson.gwt.map.EObjectMapper.java

Source

/*******************************************************************************
 * Copyright (c) 2013 Guillaume Hillairet.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Guillaume Hillairet - initial API and implementation
 *******************************************************************************/
package org.eclipselabs.emfjson.gwt.map;

import static java.lang.Boolean.TRUE;
import static org.eclipselabs.emfjson.gwt.EMFJs.OPTION_PROXY_ATTRIBUTES;
import static org.eclipselabs.emfjson.gwt.EMFJs.OPTION_ROOT_ELEMENT;
import static org.eclipselabs.emfjson.gwt.EMFJs.OPTION_SERIALIZE_REF_TYPE;
import static org.eclipselabs.emfjson.gwt.EMFJs.OPTION_SERIALIZE_TYPE;
import static org.eclipselabs.emfjson.gwt.EMFJs.OPTION_SERIALIZE_NAMESPACES;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Map;

import org.eclipse.emf.common.util.Callback;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipselabs.emfjson.gwt.EMFJs;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

/**
 * 
 * @author ghillairet
 * @since 0.6.0
 */
public class EObjectMapper {

    private boolean serializeTypes = true;
    private boolean serializeRefTypes = true;
    private EClass rootClass = null;
    private boolean useProxyAttributes = false;
    private boolean serializeNamespaces = false;

    public EObjectMapper() {
    }

    public Object from(InputStream inputStream, Resource resource, Map<?, ?> options) {
        final JSONValue node = JSUtil.parse(inputStream);

        return from(node, resource, options);
    }

    public void from(InputStream inputStream, Resource resource, Map<?, ?> options, Callback<Resource> callback) {
        final JSONValue node = JSUtil.parse(inputStream);

        from(node, resource, options, callback);
    }

    public Object from(JSONValue node, Resource resource, Map<?, ?> options) {
        if (node == null)
            return null;

        configureDeserializer(options);

        if (node.isArray() != null) {
            return from(node.isArray(), resource);
        } else if (node.isObject() != null) {
            return from(node.isObject(), resource);
        } else {
            return null;
        }
    }

    public void from(JSONValue node, Resource resource, Map<?, ?> options, Callback<Resource> callback) {
        if (node == null)
            return;

        configureDeserializer(options);

        if (node.isArray() != null) {
            from(node.isArray(), resource, callback);
        } else if (node.isObject() != null) {
            from(node.isObject(), resource, callback);
        }
    }

    public EObject from(JSONObject node, Resource resource) {
        final EList<EObject> contents = resource.getContents();
        Deserializer from = new Deserializer(useProxyAttributes);
        EObject result = from.from(node, rootClass, resource);
        if (result != null) {
            contents.add(result);
            from.resolve(resource);
        }

        return result;
    }

    public void from(JSONObject node, final Resource resource, final Callback<Resource> callback) {
        final Deserializer from = new Deserializer(useProxyAttributes);
        from.from(node, rootClass, resource, true, new Callback<EObject>() {
            @Override
            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }

            @Override
            public void onSuccess(EObject result) {
                from.resolve(resource, callback);
            }
        });
    }

    public EList<EObject> from(JSONArray node, Resource resource) {
        final EList<EObject> contents = resource.getContents();

        Deserializer from = new Deserializer(useProxyAttributes);
        EList<EObject> result = from.from(node, rootClass, resource);
        contents.addAll(result);
        from.resolve(resource);

        return result;
    }

    public void from(JSONArray node, final Resource resource, final Callback<Resource> callback) {
        final Deserializer from = new Deserializer(useProxyAttributes);
        from.from(node, rootClass, resource, new Callback<EObject>() {
            @Override
            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }

            @Override
            public void onSuccess(EObject result) {
                from.resolve(resource, callback);
            }
        });
    }

    public JSONValue to(Resource resource, Map<?, ?> options) {
        configureSerializer(options);

        Serializer to = new Serializer();
        to.setSerializeNamespaces(serializeNamespaces);
        to.setSerializeRefTypes(serializeRefTypes);
        to.setSerializeTypes(serializeTypes);

        return to.to(resource);
    }

    public JSONObject to(EObject eObject, Resource resource) {
        Serializer to = new Serializer();
        to.setSerializeNamespaces(serializeNamespaces);
        to.setSerializeRefTypes(serializeRefTypes);
        to.setSerializeTypes(serializeTypes);

        return to.to(eObject, resource);
    }

    public JSONObject to(EObject eObject, Resource resource, Map<?, ?> options) {
        return to(eObject, resource);
    }

    public void write(OutputStream outStream, Resource resource, Map<?, ?> options) {
        write(outStream, to(resource, options));
    }

    public void write(OutputStream output, JSONValue value) {
        if (value != null) {
            String stringValue = value.toString();
            try {
                output.write(stringValue.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void configureDeserializer(Map<?, ?> options) {
        if (options == null) {
            options = Collections.emptyMap();
        }

        if (options.containsKey(OPTION_ROOT_ELEMENT)) {
            Object optionEClass = options.get(OPTION_ROOT_ELEMENT);
            if (optionEClass instanceof EClass) {
                configure(OPTION_ROOT_ELEMENT, (EClass) optionEClass);
            }
        }

        configure(OPTION_PROXY_ATTRIBUTES, TRUE.equals(options.get(OPTION_PROXY_ATTRIBUTES)));
    }

    private void configureSerializer(Map<?, ?> options) {
        if (options == null) {
            options = Collections.emptyMap();
        }

        boolean serializeTypes = true;
        boolean serializeRefTypes = true;

        if (options.containsKey(EMFJs.OPTION_SERIALIZE_TYPE)) {
            try {
                serializeTypes = (Boolean) options.get(OPTION_SERIALIZE_TYPE);
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        if (options.containsKey(OPTION_SERIALIZE_REF_TYPE)) {
            try {
                serializeRefTypes = (Boolean) options.get(OPTION_SERIALIZE_REF_TYPE);
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        if (options.containsKey(OPTION_SERIALIZE_NAMESPACES)) {
            try {
                serializeNamespaces = (Boolean) options.get(OPTION_SERIALIZE_NAMESPACES);
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }

        configure(OPTION_SERIALIZE_TYPE, serializeTypes);
        configure(OPTION_SERIALIZE_REF_TYPE, serializeRefTypes);
        configure(OPTION_SERIALIZE_NAMESPACES, serializeNamespaces);
    }

    public void configure(String key, Object value) {
        if (OPTION_SERIALIZE_TYPE.equals(key)) {
            serializeTypes = (Boolean) value;
        }
        if (OPTION_SERIALIZE_REF_TYPE.equals(key)) {
            serializeRefTypes = (Boolean) value;
        }
        if (OPTION_ROOT_ELEMENT.equals(key)) {
            rootClass = (EClass) value;
        }
        if (OPTION_PROXY_ATTRIBUTES.equals(key)) {
            useProxyAttributes = (Boolean) value;
        }
    }

}