All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restlet.ext.emf.EmfConverter Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2005-2020 Talend
 * 
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
 * select the license that you prefer but you may not use this file except in
 * compliance with one of these Licenses.
 * 
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 * 
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 * 
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 * 
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * https://restlet.talend.com/
 * 
 * Restlet is a registered trademark of Talend S.A.
 */

package org.restlet.ext.emf;

import java.io.IOException;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.restlet.data.MediaType;
import org.restlet.data.Preference;
import org.restlet.engine.converter.ConverterHelper;
import org.restlet.engine.resource.VariantInfo;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.Resource;

/**
 * Converter between the XML/XMI/ECore and representation classes based on EMF.
 * 
 * @author Jerome Louvel
 * @deprecated Will be removed in next minor release.
 */
@Deprecated
public class EmfConverter extends ConverterHelper {

    private static final VariantInfo VARIANT_APPLICATION_ALL_XML = new VariantInfo(
            MediaType.APPLICATION_ALL_XML);

    private static final VariantInfo VARIANT_APPLICATION_ECORE = new VariantInfo(
            MediaType.APPLICATION_ECORE);

    private static final VariantInfo VARIANT_APPLICATION_XMI = new VariantInfo(
            MediaType.APPLICATION_XMI);

    private static final VariantInfo VARIANT_APPLICATION_XML = new VariantInfo(
            MediaType.APPLICATION_XML);

    private static final VariantInfo VARIANT_TEXT_HTML = new VariantInfo(
            MediaType.TEXT_HTML);

    private static final VariantInfo VARIANT_TEXT_XML = new VariantInfo(
            MediaType.TEXT_XML);

    /**
     * Creates the marshaling {@link EmfRepresentation}.
     * 
     * @param 
     * @param mediaType
     *            The target media type.
     * @param source
     *            The source object to marshal.
     * @return The marshaling {@link EmfRepresentation}.
     */
    protected  EmfRepresentation create(
            MediaType mediaType, T source) {
        return new EmfRepresentation(mediaType, source);
    }

    /**
     * Creates the unmarshaling {@link EmfRepresentation}.
     * 
     * @param 
     * @param source
     *            The source representation to unmarshal.
     * @return The unmarshaling {@link EmfRepresentation}.
     */
    protected  EmfRepresentation create(
            Representation source) {
        return new EmfRepresentation(source);
    }

    @Override
    public List> getObjectClasses(Variant source) {
        List> result = null;

        if (isCompatible(source)) {
            result = addObjectClass(result, EObject.class);
            result = addObjectClass(result, EmfRepresentation.class);
        }

        return result;
    }

    @Override
    public List getVariants(Class source) {
        List result = null;

        if ((source != null) && EObject.class.isAssignableFrom(source)) {
            result = addVariant(result, VARIANT_APPLICATION_ALL_XML);
            result = addVariant(result, VARIANT_APPLICATION_XML);
            result = addVariant(result, VARIANT_APPLICATION_XMI);
            result = addVariant(result, VARIANT_APPLICATION_ECORE);
            result = addVariant(result, VARIANT_TEXT_XML);
            result = addVariant(result, VARIANT_TEXT_HTML);
        }

        return result;
    }

    /**
     * Indicates if the given variant is compatible with the media types
     * supported by this converter.
     * 
     * @param variant
     *            The variant.
     * @return True if the given variant is compatible with the media types
     *         supported by this converter.
     */
    protected boolean isCompatible(Variant variant) {
        return (variant != null)
                && (VARIANT_APPLICATION_ALL_XML.isCompatible(variant)
                        || VARIANT_APPLICATION_XML.isCompatible(variant)
                        || VARIANT_APPLICATION_XMI.isCompatible(variant)
                        || VARIANT_APPLICATION_ECORE.isCompatible(variant)
                        || VARIANT_TEXT_HTML.isCompatible(variant) || VARIANT_TEXT_XML
                            .isCompatible(variant));
    }

    @Override
    public float score(Object source, Variant target, Resource resource) {
        float result = -1.0F;

        if (source instanceof EmfRepresentation) {
            result = 1.0F;
        } else {
            if (target == null) {
                result = 0.5F;
            } else if (isCompatible(target)) {
                result = 0.8F;
            } else {
                result = 0.5F;
            }
        }

        return result;
    }

    @Override
    public  float score(Representation source, Class target,
            Resource resource) {
        float result = -1.0F;

        if (target != null) {
            if (isCompatible(source)) {
                result = 0.8F;
            }
        } else {
            result = 0.5F;
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    public  T toObject(Representation source, Class target,
            Resource resource) throws IOException {
        EmfRepresentation emfSource = null;
        if (source instanceof EmfRepresentation) {
            emfSource = (EmfRepresentation) source;
        } else if (isCompatible(source)) {
            emfSource = create(source);
        }

        Object result = null;
        if (emfSource != null) {
            if (target != null
                    && EmfRepresentation.class.isAssignableFrom(target)) {
                result = emfSource;
            } else {
                result = emfSource.getObject();
            }
        }

        return (T) result;
    }

    @Override
    public Representation toRepresentation(Object source, Variant target,
            Resource resource) {
        Representation result = null;

        if (source instanceof EmfRepresentation) {
            result = (EmfRepresentation) source;
        } else {
            if (target.getMediaType() == null) {
                target.setMediaType(MediaType.TEXT_XML);
            }

            if (isCompatible(target)) {
                result = create(target.getMediaType(), (EObject) source);
            }
        }

        return result;
    }

    @Override
    public  void updatePreferences(List> preferences,
            Class entity) {
        if (EObject.class.isAssignableFrom(entity)
                || EmfRepresentation.class.isAssignableFrom(entity)) {
            updatePreferences(preferences, MediaType.APPLICATION_ALL_XML, 1.0F);
            updatePreferences(preferences, MediaType.APPLICATION_XML, 1.0F);
            updatePreferences(preferences, MediaType.APPLICATION_XMI, 1.0F);
            updatePreferences(preferences, MediaType.APPLICATION_ECORE, 1.0F);
            updatePreferences(preferences, MediaType.TEXT_HTML, 1.0F);
            updatePreferences(preferences, MediaType.TEXT_XML, 1.0F);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy