Skip to content

Commit

Permalink
Tidy up deleteOnExit cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbattle committed Jan 27, 2024
1 parent 68e404d commit 092022e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
21 changes: 12 additions & 9 deletions fmi2/vdmcheck/src/main/java/maestro/MaestroCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ public List<OnFailError> check(InputStream xmlStream) throws Exception {
public List<OnFailError> check(File xmlFile) throws Exception {
List<OnFailError> errors = new Vector<>();

File fmi2 = Files.createTempDirectory("fmi2", new FileAttribute[0]).toFile();
fmi2.deleteOnExit();
File maestro = Files.createTempDirectory("fmi2", new FileAttribute[0]).toFile();
maestro.deleteOnExit();

File fmi2schema = new File(fmi2, "fmi2schema");
File fmi2schema = new File(maestro, "fmi2schema");
fmi2schema.mkdir();
fmi2schema.deleteOnExit();

copyResources(fmi2,
copyResources(maestro,
"/fmi2schema/fmi2Annotation.xsd",
"/fmi2schema/fmi2AttributeGroups.xsd",
"/fmi2schema/fmi2ModelDescription.xsd",
Expand All @@ -130,7 +130,7 @@ public List<OnFailError> check(File xmlFile) throws Exception {
if (validation != null) {
errors.add(validation);
} else {
File vdmsl = new File(fmi2, "vdmsl");
File vdmsl = new File(maestro, "vdmsl");
vdmsl.mkdir();
vdmsl.deleteOnExit();

Expand All @@ -139,11 +139,15 @@ public List<OnFailError> check(File xmlFile) throws Exception {

Xsd2VDM converter = new Xsd2VDM();
Xsd2VDM.loadProperties(xsdFile);
Map<String, Type> vdmSchema = converter.createVDMSchema(xsdFile, xmlFile, false, true);
Map<String, Type> vdmSchema = converter.createVDMSchema(xsdFile, null, false, true);
converter.createVDMValue(vdmSchema, vdmFile, xmlFile, "model");

if (vdmFile.exists()) // Means successful?
{
File fsm = new File(vdmsl, "fmi2-static-model");
fsm.deleteOnExit();
fsm.mkdir();

copyResources(vdmsl,
"/fmi2-static-model/CoSimulation_4.3.1.vdmsl",
"/fmi2-static-model/DefaultExperiment_2.2.5.vdmsl",
Expand Down Expand Up @@ -267,14 +271,13 @@ private void copyResources(File target, String... resources) throws Exception {

private void copyStream(InputStream data, File target, String file) throws IOException {
File targetFile = new File(target.getAbsolutePath() + file);
targetFile.getParentFile().mkdirs();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
targetFile.deleteOnExit(); // Note! All files temporary
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));

while (data.available() > 0) {
bos.write(data.read());
}

bos.close();
targetFile.deleteOnExit(); // Note! All files temporary
}
}
20 changes: 9 additions & 11 deletions fmi2/vdmcheck/src/main/java/maestro/MaestroCheckFMI2.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ public List<OnFailError> check(File modelFile, File buildFile, File termsFile) t

if (vdmFile.exists()) // Means successful?
{
File frm = new File(vdmsl, "fmi2-rule-model");
frm.deleteOnExit();
frm.mkdir();

File rules = new File(frm, "Rules");
rules.deleteOnExit();
rules.mkdir();

copyResources(vdmsl,
"/fmi2-rule-model/Rules/BuildConfiguration.adoc",
"/fmi2-rule-model/Rules/CoSimulation.adoc",
Expand Down Expand Up @@ -356,16 +364,7 @@ private void copyResources(File target, String... resources) throws Exception
private String copyStream(InputStream data, File target, String file) throws IOException
{
File targetFile = new File(target.getAbsolutePath() + file);

// Mark folders deleteOnExit up to, but excluding, the target root
File parent = targetFile.getParentFile();

while (!parent.equals(target))
{
parent.mkdirs();
parent.deleteOnExit();
parent = parent.getParentFile();
}
targetFile.deleteOnExit(); // Note! All files temporary

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
Expand Down Expand Up @@ -397,7 +396,6 @@ else if (xmlContent.contains("<fmiTerminalsAndIcons"))
fos.write(xmlContent.getBytes("UTF-8"));
fos.close();

targetFile.deleteOnExit(); // Note! All files temporary
return varName;
}

Expand Down
20 changes: 9 additions & 11 deletions fmi3/vdmcheck/src/main/java/maestro/MaestroCheckFMI3.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ public List<OnFailError> check(File modelFile, File buildFile, File termsFile) t

if (vdmFile.exists()) // Means successful?
{
File frm = new File(vdmsl, "fmi3-rule-model");
frm.deleteOnExit();
frm.mkdir();

File rules = new File(frm, "Rules");
rules.deleteOnExit();
rules.mkdir();

copyResources(vdmsl,
"/fmi3-rule-model/Rules/BuildConfiguration.adoc",
"/fmi3-rule-model/Rules/CoSimulation.adoc",
Expand Down Expand Up @@ -357,16 +365,7 @@ private void copyResources(File target, String... resources) throws Exception
private String copyStream(InputStream data, File target, String file) throws IOException
{
File targetFile = new File(target.getAbsolutePath() + file);

// Mark folders deleteOnExit up to, but excluding, the target root
File parent = targetFile.getParentFile();

while (!parent.equals(target))
{
parent.mkdirs();
parent.deleteOnExit();
parent = parent.getParentFile();
}
targetFile.deleteOnExit(); // Note! All files temporary

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
Expand Down Expand Up @@ -398,7 +397,6 @@ else if (xmlContent.contains("<fmiTerminalsAndIcons"))
fos.write(xmlContent.getBytes("UTF-8"));
fos.close();

targetFile.deleteOnExit(); // Note! All files temporary
return varName;
}

Expand Down

0 comments on commit 092022e

Please sign in to comment.