Skip to content

Commit

Permalink
Extracted and generalized the output of Java models in the XMI format (
Browse files Browse the repository at this point in the history
  • Loading branch information
HansMartinA committed Jul 30, 2023
1 parent 3c86d0d commit 2ed8d2b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tools.mdsd.jamopp.test;

import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMIResource;

import tools.mdsd.jamopp.model.java.containers.CompilationUnit;
import tools.mdsd.jamopp.model.java.containers.JavaRoot;
import tools.mdsd.jamopp.model.java.containers.Package;

public class OutputUtility {
public record TransferResult(ResourceSet targetSet, Map<Resource, Resource> sourceTargetMapping) {};

public static TransferResult transferToOutput(ResourceSet sourceSet, String outputFolder, String fileExtension, boolean includeAllResources) {
int emptyFileName = 0;

ResourceSet targetSet = new ResourceSetImpl();
HashMap<Resource, Resource> srcTrgMap = new HashMap<>();

for (Resource javaResource : new ArrayList<>(sourceSet.getResources())) {
if (javaResource.getContents().isEmpty()) {
System.out.println("WARNING: Emtpy Resource: " + javaResource.getURI());
continue;
}
if (!includeAllResources && !javaResource.getURI().isFile()) {
continue;
}

JavaRoot root = (JavaRoot) javaResource.getContents().get(0);
String outputFileName = "ERROR";
if (root instanceof CompilationUnit cu) {
outputFileName = cu.getNamespacesAsString().replace(".", File.separator) + File.separator;
if (cu.getClassifiers().size() > 0) {
outputFileName += cu.getClassifiers().get(0).getName();
} else {
outputFileName += emptyFileName++;
}
} else if (root instanceof Package) {
outputFileName = root.getNamespacesAsString()
.replace(".", File.separator) + File.separator + "package-info";
if (outputFileName.startsWith(File.separator)) {
outputFileName = outputFileName.substring(1);
}
} else if (root instanceof tools.mdsd.jamopp.model.java.containers.Module) {
outputFileName = root.getNamespacesAsString()
.replace(".", File.separator) + File.separator + "module-info";
} else {
fail();
}

File outputFile = new File("." + File.separator + outputFolder
+ File.separator + outputFileName);
URI fileURI = URI.createFileURI(outputFile.getAbsolutePath()).appendFileExtension(fileExtension);

Resource targetResource = targetSet.createResource(fileURI);
if (targetResource instanceof XMIResource xmiResource) {
xmiResource.setEncoding(StandardCharsets.UTF_8.toString());
}
targetResource.getContents().addAll(javaResource.getContents());
srcTrgMap.put(javaResource, targetResource);
}

for (Resource targetResource : targetSet.getResources()) {
try {
targetResource.save(targetSet.getLoadOptions());
} catch (Exception e) {
e.printStackTrace();
}
}

return new TransferResult(targetSet, srcTrgMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,24 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.EcoreUtil.EqualityHelper;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import tools.mdsd.jamopp.model.java.containers.CompilationUnit;
import tools.mdsd.jamopp.model.java.containers.JavaRoot;
import tools.mdsd.jamopp.model.java.containers.Package;
import tools.mdsd.jamopp.options.ParserOptions;
import tools.mdsd.jamopp.parser.jdt.singlefile.JaMoPPJDTSingleFileParser;
import tools.mdsd.jamopp.test.AbstractJaMoPPTests;
import tools.mdsd.jamopp.test.OutputUtility;

public class JavaXMISerializationTest extends AbstractJaMoPPTests {

Expand Down Expand Up @@ -88,56 +82,7 @@ public void testXMISerialization() throws Exception {
}

protected ResourceSet transferToXMI(ResourceSet sourceSet, boolean includeAllResources) throws Exception {
int emptyFileName = 0;

ResourceSet targetSet = new ResourceSetImpl();

for (Resource javaResource : new ArrayList<>(sourceSet.getResources())) {
if (javaResource.getContents().isEmpty()) {
System.out.println("WARNING: Emtpy Resource: " + javaResource.getURI());
continue;
}
if (!includeAllResources && !javaResource.getURI().isFile()) {
continue;
}
JavaRoot root = (JavaRoot) javaResource.getContents().get(0);
String outputFileName = "ERROR";
if (root instanceof CompilationUnit) {
outputFileName = root.getNamespacesAsString().replace(".", File.separator) + File.separator;
CompilationUnit cu = (CompilationUnit) root;
if (cu.getClassifiers().size() > 0) {
outputFileName += cu.getClassifiers().get(0).getName();
} else {
outputFileName += emptyFileName++;
}

} else if (root instanceof Package) {
outputFileName = root.getNamespacesAsString()
.replace(".", File.separator) + File.separator + "package-info";
if (outputFileName.startsWith(File.separator)) {
outputFileName = outputFileName.substring(1);
}
} else if (root instanceof tools.mdsd.jamopp.model.java.containers.Module) {
outputFileName = root.getNamespacesAsString()
.replace(".", File.separator) + File.separator + "module-info";
} else {
fail();
}
File outputFile = new File("." + File.separator + TEST_OUTPUT_FOLDER_NAME
+ File.separator + outputFileName);
URI xmiFileURI = URI.createFileURI(outputFile.getAbsolutePath()).appendFileExtension("xmi");
XMIResource xmiResource = (XMIResource) targetSet.createResource(xmiFileURI);
xmiResource.setEncoding(StandardCharsets.UTF_8.toString());
xmiResource.getContents().addAll(javaResource.getContents());
}
for (Resource xmiResource : targetSet.getResources()) {
try {
xmiResource.save(targetSet.getLoadOptions());
} catch (Exception e) {
e.printStackTrace();
}
}
return targetSet;
return OutputUtility.transferToOutput(sourceSet, TEST_OUTPUT_FOLDER_NAME, "xmi", includeAllResources).targetSet();
}

protected void compare(ResourceSet rs) throws Exception {
Expand Down

0 comments on commit 2ed8d2b

Please sign in to comment.