Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[34] Add the ability to add or retrieve custom data #35

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ pom.xml.next
pom.xml.releaseBackup
pom.xml.tag
release.properties

# IntelliJ IDEA
.idea
*.iws
*.iml
*.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.sirius.emfjson.ide;singleton:=true
Bundle-Version: 2.3.7.qualifier
Bundle-Version: 2.3.8.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Localization: bundle
Require-Bundle: org.eclipse.sirius.emfjson;bundle-version="2.1.0",
Expand Down
4 changes: 2 additions & 2 deletions bundles/org.eclipse.sirius.emfjson.ide/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
<parent>
<groupId>org.eclipse.sirius.emfjson</groupId>
<artifactId>org.eclipse.sirius.emfjson.releng</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<relativePath>../../releng/org.eclipse.sirius.emfjson.releng</relativePath>
</parent>


<artifactId>org.eclipse.sirius.emfjson.ide</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

<name>Sirius EMF JSON - IDE Integration</name>
Expand Down
2 changes: 1 addition & 1 deletion bundles/org.eclipse.sirius.emfjson/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.sirius.emfjson
Bundle-Version: 2.3.7.qualifier
Bundle-Version: 2.3.8.qualifier
Bundle-Vendor: %Bundle-Vendor
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-Localization: bundle
Expand Down
4 changes: 2 additions & 2 deletions bundles/org.eclipse.sirius.emfjson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
<parent>
<groupId>org.eclipse.sirius.emfjson</groupId>
<artifactId>org.eclipse.sirius.emfjson.releng</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<relativePath>../../releng/org.eclipse.sirius.emfjson.releng</relativePath>
</parent>


<artifactId>org.eclipse.sirius.emfjson</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>

<name>Sirius EMF JSON</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.sirius.emfjson.resource;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -495,6 +496,97 @@ public void onCrossReferenceURICreated(EObject eObject, EReference eReference, S
}
}

/**
* An option to provide an IJsonResourceProcessor.
*/
String OPTION_JSON_RESSOURCE_PROCESSOR = "OPTION_JSON_RESSOURCE_PROCESSOR"; //$NON-NLS-1$

/**
* Used to add data to be serialized in the document, or to retrieve data during deserialization.
*
* @author <a href="mailto:[email protected]">Michael Charfadi</a>
*/
interface IJsonResourceProcessor {

/**
* Called when a JsonResource is deserialized.
*
* @param resource
* The JsonResource that is deserialized
* @param jsonObject
* The root jsonObject
*/
void preDeserialization(JsonResource resource, JsonObject jsonObject);

/**
* Called when a JsonResource is serialized.
*
* @param resource
* The JsonResource that is serialized
* @param jsonObject
* The root jsonObject
*/
void postSerialization(JsonResource resource, JsonObject jsonObject);

/**
* Called during the parsing of JsonResources after loading an eObject. As such the eObject will have all his
* features set. The jsonObject is the one that was used to set the features and can be used to retrieve values
* of unknown features.
*
* @param eObject
* the eObject that have been loaded.
* @param jsonObject
* the jsonObject used to load the eObject.
* @param isTopObject
* if the given JsonObject is a top element.
*/
void postObjectLoading(EObject eObject, JsonObject jsonObject, boolean isTopObject);

/**
* Called during the parsing of JsonResource (at loading time). If a feature value has changed since a previous
* version, use this method to return the correct expected value. Return null if it did not change.
*
* @param eObject
* the object containing the feature.
* @param feature
* the feature to set value.
* @param value
* the initial serialized value.
* @return The new value.
*/
Object getValue(EObject eObject, EStructuralFeature feature, Object value);

/**
* Implementation of the interface which does nothing.
*
* @author <a href="mailto:[email protected]">Michael Charfadi</a>
*/
class NoOp implements IJsonResourceProcessor {

@Override
public void preDeserialization(JsonResource resource, JsonObject jsonObject) {
// Do nothing
}

@Override
public void postSerialization(JsonResource resource, JsonObject jsonObject) {
// Do nothing
}

@Override
public Object getValue(EObject eObject, EStructuralFeature feature, Object value) {
// Do nothing
return null;
}

@Override
public void postObjectLoading(EObject eObject, JsonObject jsonObject, boolean isTopObject) {
// Do nothing
}

}
}

/**
* Associate an ID to the {@link EObject}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.emfjson.resource.JsonResource.IEObjectHandler;
import org.eclipse.sirius.emfjson.resource.JsonResource.IJsonResourceProcessor;
import org.eclipse.sirius.emfjson.resource.JsonResource.URIHandler;
import org.eclipse.sirius.emfjson.resource.PackageNotFoundException;

Expand Down Expand Up @@ -134,6 +135,8 @@ public class GsonEObjectDeserializer implements JsonDeserializer<List<EObject>>
*/
private IEObjectHandler eObjectHandler;

private IJsonResourceProcessor jsonResourceProcessor;

/**
* The constructor.
*
Expand All @@ -150,9 +153,15 @@ public GsonEObjectDeserializer(Resource resource, Map<?, ?> options) {
this.options = options;
}

this.jsonResourceProcessor = (IJsonResourceProcessor) this.options.get(JsonResource.OPTION_JSON_RESSOURCE_PROCESSOR);
if (this.jsonResourceProcessor == null) {
this.jsonResourceProcessor = new IJsonResourceProcessor.NoOp();
}

this.helper = (JsonHelper) this.options.get(JsonResource.OPTION_CUSTOM_HELPER);
if (this.helper == null) {
this.helper = new JsonHelper(resource);
this.helper.setJsonResourceProcessor(this.jsonResourceProcessor);
}
if (resource instanceof JsonResource) {
this.resource = (JsonResource) resource;
Expand All @@ -175,6 +184,7 @@ public GsonEObjectDeserializer(Resource resource, Map<?, ?> options) {
} else {
this.extendedMetaData = (ExtendedMetaData) this.options.get(JsonResource.OPTION_EXTENDED_META_DATA);
}

if (this.extendedMetaData != null) {
this.helper.setExtendedMetaData(this.extendedMetaData);
}
Expand Down Expand Up @@ -245,6 +255,8 @@ public List<EObject> deserialize(JsonElement jsonElement, Type type, JsonDeseria
}
}

this.jsonResourceProcessor.preDeserialization(this.resource, jsonRoot);

// json content
this.deserializeContent(jsonRoot);

Expand Down Expand Up @@ -401,7 +413,7 @@ private EObject loadObject(JsonObject object, boolean isTopObject) {
this.resource.setID(eObject, idJsonElement.getAsString());
}
}

this.jsonResourceProcessor.postObjectLoading(eObject, object, isTopObject);
return eObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.emfjson.resource.JsonResource.EStructuralFeaturesFilter;
import org.eclipse.sirius.emfjson.resource.JsonResource.IEObjectHandler;
import org.eclipse.sirius.emfjson.resource.JsonResource.IJsonResourceProcessor;
import org.eclipse.sirius.emfjson.resource.JsonResource.ISerializationListener;
import org.eclipse.sirius.emfjson.resource.JsonResource.ResourceEntityHandler;
import org.eclipse.sirius.emfjson.resource.exception.DanglingHREFException;
Expand Down Expand Up @@ -130,6 +131,10 @@ public class GsonEObjectSerializer implements JsonSerializer<List<EObject>> {

private ISerializationListener serializationListener;

private IJsonResourceProcessor jsonResourceProcessor;

private Resource eResource;

/**
* The constructor.
*
Expand All @@ -141,7 +146,7 @@ public class GsonEObjectSerializer implements JsonSerializer<List<EObject>> {
public GsonEObjectSerializer(Resource resource, Map<?, ?> options) {
super();
Map<?, ?> serializedOptions = options;

this.eResource = resource;
this.resourceEntityHandler = (ResourceEntityHandler) serializedOptions.get(JsonResource.OPTION_RESOURCE_ENTITY_HANDLER);
if (this.resourceEntityHandler instanceof JsonResource.URIHandler && !serializedOptions.containsKey(JsonResource.OPTION_URI_HANDLER)) {
Map<Object, Object> newOptions = new LinkedHashMap<Object, Object>(serializedOptions);
Expand Down Expand Up @@ -174,6 +179,10 @@ public GsonEObjectSerializer(Resource resource, Map<?, ?> options) {
if (this.serializationListener == null) {
this.serializationListener = new ISerializationListener.NoOp();
}
this.jsonResourceProcessor = (IJsonResourceProcessor) serializedOptions.get(JsonResource.OPTION_JSON_RESSOURCE_PROCESSOR);
if (this.jsonResourceProcessor == null) {
this.jsonResourceProcessor = new IJsonResourceProcessor.NoOp();
}

this.declareSchemaLocation = Boolean.TRUE.equals(options.get(JsonResource.OPTION_SCHEMA_LOCATION));

Expand Down Expand Up @@ -202,6 +211,9 @@ public JsonElement serialize(List<EObject> eObjects, Type type, JsonSerializatio
JsonObject jsonObject = new JsonObject();
jsonObject.add(IGsonConstants.JSON, jsonHeader);
jsonObject.add(IGsonConstants.NS, nsHeader);

this.jsonResourceProcessor.postSerialization((JsonResource) this.eResource, jsonObject);

if (schemaLocationHeader != null) {
jsonObject.add(IGsonConstants.SCHEMA_LOCATION, schemaLocationHeader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.emfjson.resource.JsonResource.IJsonResourceProcessor;
import org.eclipse.sirius.emfjson.resource.JsonResource.URIHandler;
import org.eclipse.sirius.emfjson.resource.exception.DanglingHREFException;

Expand All @@ -58,7 +59,7 @@ public class JsonHelper {
/**
* This class is used to register uri and all prefixes that refers to this uri.
*/
private class PrefixToURI extends BasicEMap<String, String> {
private final class PrefixToURI extends BasicEMap<String, String> {

/**
* .
Expand Down Expand Up @@ -179,6 +180,11 @@ protected void didRemove(Entry<String, String> entry) {
*/
private DanglingHREFException danglingHREFException;

/**
* IJsonResourceProcessor.
*/
private IJsonResourceProcessor jsonResourceProcessor;

/**
* How manage dangling references.
*
Expand Down Expand Up @@ -218,6 +224,15 @@ public Resource getResource() {
return this.resource;
}

/**
* Set the jsonResourceProcessor.
*
* @param jsonResourceProcessor
*/
public void setJsonResourceProcessor(IJsonResourceProcessor jsonResourceProcessor) {
this.jsonResourceProcessor = jsonResourceProcessor;
}

/**
* Set the resource and also its package registry and if its URI should be deresolved.
*
Expand Down Expand Up @@ -593,12 +608,16 @@ public EClassifier getType(EFactory eFactory, String typeName) {
* the value to set
*/
public void setValue(EObject object, EStructuralFeature feature, Object value) {
Object newValue = this.jsonResourceProcessor.getValue(object, feature, value);
if (newValue == null) {
newValue = value;
}
if (feature.isMany()) {
@SuppressWarnings("unchecked")
Collection<Object> collection = (Collection<Object>) object.eGet(feature);
collection.add(value);
collection.add(newValue);
} else {
object.eSet(feature, value);
object.eSet(feature, newValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="org.eclipse.sirius.emfjson.feature.ide"
label="%Feature-name"
version="2.3.7.qualifier"
version="2.3.8.qualifier"
provider-name="%Feature-vendor">

<description url="">
Expand Down
4 changes: 2 additions & 2 deletions features/org.eclipse.sirius.emfjson.feature.ide/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
<parent>
<groupId>org.eclipse.sirius.emfjson</groupId>
<artifactId>org.eclipse.sirius.emfjson.releng</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<relativePath>../../releng/org.eclipse.sirius.emfjson.releng</relativePath>
</parent>


<artifactId>org.eclipse.sirius.emfjson.feature.ide</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<packaging>eclipse-feature</packaging>

<name>Sirius EMF JSON - IDE Integration Feature</name>
Expand Down
2 changes: 1 addition & 1 deletion features/org.eclipse.sirius.emfjson.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="org.eclipse.sirius.emfjson.feature"
label="%Feature-name"
version="2.3.7.qualifier"
version="2.3.8.qualifier"
provider-name="%Feature-vendor">

<description url="">
Expand Down
4 changes: 2 additions & 2 deletions features/org.eclipse.sirius.emfjson.feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
<parent>
<groupId>org.eclipse.sirius.emfjson</groupId>
<artifactId>org.eclipse.sirius.emfjson.releng</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<relativePath>../../releng/org.eclipse.sirius.emfjson.releng</relativePath>
</parent>


<artifactId>org.eclipse.sirius.emfjson.feature</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<packaging>eclipse-feature</packaging>

<name>Sirius EMF JSON Feature</name>
Expand Down
2 changes: 1 addition & 1 deletion releng/org.eclipse.sirius.emfjson.releng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<groupId>org.eclipse.sirius.emfjson</groupId>
<artifactId>org.eclipse.sirius.emfjson.releng</artifactId>
<version>2.3.7-SNAPSHOT</version>
<version>2.3.8-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Sirius EMF JSON</name>
Expand Down
Loading
Loading