Skip to content

Commit

Permalink
[24] Schemalocation can use fragments to locate an EPackage
Browse files Browse the repository at this point in the history
Bug: #24
Signed-off-by: Arthur Daussy <[email protected]>
  • Loading branch information
adaussy authored and sbegaudeau committed Dec 5, 2023
1 parent 0f420f3 commit 4b2a9f8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,10 @@ public List<EObject> deserialize(JsonElement jsonElement, Type type, JsonDeseria
// TODO: in condition, add a test for extendedMetaData (see line 2898 of
// XMLHandler.class)
uri = this.helper.resolve(uri, this.resourceURI);
Resource ePackageResource = this.resourceSet.getResource(uri, true);
TreeIterator<EObject> iterator = ePackageResource.getAllContents();
while (iterator.hasNext()) {
EObject eObject = iterator.next();
if (eObject instanceof EPackage) {
EPackage ePackage = (EPackage) eObject;
this.resourceSet.getPackageRegistry().put(ePackage.getNsURI(), ePackage);
}

EPackage ePackage = this.getEPackage(uri);
if (ePackage != null) {
this.resourceSet.getPackageRegistry().put(ePackage.getNsURI(), ePackage);
}
}
}
Expand All @@ -255,6 +251,34 @@ public List<EObject> deserialize(JsonElement jsonElement, Type type, JsonDeseria
return this.rootElements;
}

/**
* Gets the EPackage matching the given URI
*
* @param uri
* an URI
* @return a {@link EPackage} or <code>null</code> if not found
*/
private EPackage getEPackage(URI uri) {
EPackage ePackage = null;
if (uri.hasFragment()) {
EObject target = this.resourceSet.getEObject(uri, true);
if (target instanceof EPackage) {
ePackage = (EPackage) target;
}
} else {
Resource ePackageResource = this.resourceSet.getResource(uri, true);
TreeIterator<EObject> iterator = ePackageResource.getAllContents();
while (iterator.hasNext()) {
EObject eObject = iterator.next();
if (eObject instanceof EPackage) {
ePackage = (EPackage) eObject;
break;
}
}
}
return ePackage;
}

/**
* Deserialize the data field. Add the created EObject to the list of EObject that contain root elements.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ public void testLoadSchemaLocation() {
this.testLoad("SchemaLocation.xmi"); //$NON-NLS-1$
}

@Test
public void testLoadSchemaLocationWithFragment() {
this.testLoad("SchemaLocationWithFragment.xmi"); //$NON-NLS-1$
}

@Test
public void testSaveSchemaLocationWithFragment() {
this.testSave("SchemaLocationWithFragment.xmi"); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" xmi:id="CustomEPackageID" name="DynamicEcore" nsURI="http://DynamicEcore"
nsPrefix="dynLib">
<eClassifiers xsi:type="ecore:EClass" xmi:id="CustomEClassID" name="DynamicEClass">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="dynName" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
</ecore:EPackage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"json": {
"version": "1.0",
"encoding": "utf-8"
},
"ns": {
"dynLib": "http://DynamicEcore",
"nodes": "http://www.obeo.fr/EMFJson"
},
"schemaLocation": {
"http://DynamicEcore": "Dynamic.ecore#CustomEPackageID",
"http://www.obeo.fr/EMFJson": "../../nodes.ecore"
},
"content": [
{
"eClass": "nodes:Node"
},
{
"eClass": "dynLib:DynamicEClass"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="ASCII"?>
<xmi:XMI
xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dynLib="http://DynamicEcore"
xmlns:nodes="http://www.obeo.fr/EMFJson"
xsi:schemaLocation="http://DynamicEcore Dynamic.ecore#CustomEPackageID http://www.obeo.fr/EMFJson ../../nodes.ecore">
<nodes:Node/>
<dynLib:DynamicEClass/>
</xmi:XMI>

0 comments on commit 4b2a9f8

Please sign in to comment.