Skip to content

Commit

Permalink
[#697] Encodings: Simplify API of DMN and TCK serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
opatrascoiu committed Aug 4, 2024
1 parent 0cf8a2d commit eba5c56
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,20 @@

import com.gs.dmn.ast.TDefinitions;

import java.io.*;
import java.net.URL;
import java.io.File;
import java.io.Reader;
import java.io.Writer;

public interface DMNMarshaller {
TDefinitions unmarshal(String input, boolean validateSchema);

TDefinitions unmarshal(File input, boolean validateSchema);

TDefinitions unmarshal(URL input, boolean validateSchema);

TDefinitions unmarshal(InputStream input, boolean validateSchema);

TDefinitions unmarshal(Reader input, boolean validateSchema);

String marshal(TDefinitions o);

void marshal(TDefinitions o, File output);

void marshal(TDefinitions o, OutputStream output);

void marshal(TDefinitions o, Writer output);
}
59 changes: 8 additions & 51 deletions dmn-core/src/main/java/com/gs/dmn/serialization/DMNSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import com.gs.dmn.transformation.InputParameters;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -49,12 +46,12 @@ public List<TDefinitions> readModels(List<File> files) {
if (files == null) {
throw new DMNRuntimeException("Missing DMN files");
} else {
for (File file: files) {
for (File file : files) {
if (isDMNFile(file)) {
TDefinitions definitions = readModel(file);
definitionsList.add(definitions);
} else {
this.logger.warn(String.format("Skipping file '%s", file == null ? null: file.getAbsoluteFile()));
this.logger.warn(String.format("Skipping file '%s", file == null ? null : file.getAbsoluteFile()));
}
}
return definitionsList;
Expand All @@ -70,7 +67,7 @@ public List<TDefinitions> readModels(File file) {
definitionsList.add(definitions);
return definitionsList;
} else if (file.isDirectory()) {
for (File child: file.listFiles()) {
for (File child : file.listFiles()) {
if (isDMNFile(child)) {
TDefinitions definitions = readModel(child);
definitionsList.add(definitions);
Expand All @@ -86,45 +83,20 @@ public TDefinitions readModel(File input) {
try {
this.logger.info(String.format("Reading DMN '%s' ...", input.getAbsolutePath()));

TDefinitions definitions = transform(unmarshall(input));
this.logger.info("DMN read.");
return definitions;
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot read DMN from '%s'", input.getAbsolutePath()), e);
}
}

public TDefinitions readModel(InputStream input) {
try {
this.logger.info(String.format("Reading DMN '%s' ...", input.toString()));

TDefinitions definitions = transform(unmarshall(input));
TDefinitions definitions = transform(this.dmnMarshaller.unmarshal(input, this.inputParameters.isXsdValidation()));

this.logger.info("DMN read.");
return definitions;
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot read DMN from '%s'", input.toString()), e);
}
}

public TDefinitions readModel(URL input) {
try {
this.logger.info(String.format("Reading DMN '%s' ...", input.toString()));

TDefinitions definitions = transform(unmarshall(input));

this.logger.info("DMN read.");
return definitions;
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot read DMN from '%s'", input.toString()), e);
throw new DMNRuntimeException(String.format("Cannot read DMN from '%s'", input.getAbsolutePath()), e);
}
}

public TDefinitions readModel(Reader input) {
try {
this.logger.info(String.format("Reading DMN '%s' ...", input.toString()));

TDefinitions result = transform(unmarshall(input));
TDefinitions result = transform(this.dmnMarshaller.unmarshal(input, this.inputParameters.isXsdValidation()));

this.logger.info("DMN read.");
return result;
Expand All @@ -134,8 +106,8 @@ public TDefinitions readModel(Reader input) {
}

public void writeModel(TDefinitions definitions, File output) {
try (FileOutputStream fos = new FileOutputStream(output)) {
this.dmnMarshaller.marshal(definitions, fos);
try {
this.dmnMarshaller.marshal(definitions, output);
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot write DMN to '%s'", output.getPath()), e);
}
Expand All @@ -160,19 +132,4 @@ private TDefinitions transform(TDefinitions definitions) {
}
}

protected TDefinitions unmarshall(File input) {
return this.dmnMarshaller.unmarshal(input, this.inputParameters.isXsdValidation());
}

protected TDefinitions unmarshall(URL input) {
return this.dmnMarshaller.unmarshal(input, this.inputParameters.isXsdValidation());
}

protected TDefinitions unmarshall(InputStream input) {
return this.dmnMarshaller.unmarshal(input, this.inputParameters.isXsdValidation());
}

protected TDefinitions unmarshall(Reader input) {
return this.dmnMarshaller.unmarshal(input, this.inputParameters.isXsdValidation());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import com.gs.dmn.runtime.DMNRuntimeException;
import com.gs.dmn.serialization.DMNMarshaller;

import java.io.*;
import java.net.URL;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class JsonDMNMarshaller implements DMNMarshaller {
public static final ObjectMapper JSON_MAPPER = makeJsonMapper();
Expand Down Expand Up @@ -72,26 +74,6 @@ public TDefinitions unmarshal(File input, boolean validateSchema) {
}
}

@Override
public TDefinitions unmarshal(URL input, boolean validateSchema) {
try {
checkSchemaValidationFlag(validateSchema);
return JSON_MAPPER.readValue(input, TDefinitions.class);
} catch (IOException e) {
throw new DMNRuntimeException(String.format("Cannot read DMN from '%s'", input), e);
}
}

@Override
public TDefinitions unmarshal(InputStream input, boolean validateSchema) {
try {
checkSchemaValidationFlag(validateSchema);
return JSON_MAPPER.readValue(input, TDefinitions.class);
} catch (IOException e) {
throw new DMNRuntimeException(String.format("Cannot read DMN from '%s'", input), e);
}
}

@Override
public TDefinitions unmarshal(Reader input, boolean validateSchema) {
try {
Expand Down Expand Up @@ -120,15 +102,6 @@ public void marshal(TDefinitions o, File output) {
}
}

@Override
public void marshal(TDefinitions o, OutputStream output) {
try {
JSON_MAPPER.writeValue(output, o);
} catch (IOException e) {
throw new DMNRuntimeException(String.format("Cannot write DMN to '%s'", output), e);
}
}

@Override
public void marshal(TDefinitions o, Writer output) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,38 +151,6 @@ public TDefinitions unmarshal(File input, boolean validateSchema) {
return null;
}

@Override
public TDefinitions unmarshal(URL input, boolean validateSchema) {
try (Reader firstStringReader = new InputStreamReader(input.openStream()); Reader secondStringReader = new InputStreamReader(input.openStream())) {
DMNVersion dmnVersion = inferDMNVersion(firstStringReader);
if (validateSchema && dmnVersion != null) {
try (InputStreamReader reader = new InputStreamReader(input.openStream())) {
validateXMLSchema(new StreamSource(reader), dmnVersion.getSchemaLocation());
}
}
return unmarshal(dmnVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling DMN model from reader.", e);
}
return null;
}

@Override
public TDefinitions unmarshal(InputStream input, boolean validateSchema) {
try (Reader firstStringReader = new InputStreamReader(input); Reader secondStringReader = new InputStreamReader(input)) {
DMNVersion dmnVersion = inferDMNVersion(firstStringReader);
if (validateSchema && dmnVersion != null) {
try (InputStreamReader reader = new InputStreamReader(input)) {
validateXMLSchema(new StreamSource(reader), dmnVersion.getSchemaLocation());
}
}
return unmarshal(dmnVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling DMN model from reader.", e);
}
return null;
}

@Override
public TDefinitions unmarshal(Reader input, boolean validateSchema) {
try (BufferedReader buffer = new BufferedReader(input)) {
Expand Down Expand Up @@ -228,15 +196,6 @@ public void marshal(TDefinitions o, File output) {
}
}

@Override
public void marshal(TDefinitions o, OutputStream output) {
try (Writer fileWriter = new OutputStreamWriter(output)) {
marshal(o, fileWriter);
} catch (Exception e) {
LOGGER.error("Error marshalling object {}", o);
}
}

@Override
public void marshal(TDefinitions o, Writer output) {
if (o != null) {
Expand Down Expand Up @@ -284,7 +243,7 @@ private boolean validateXMLSchema(Source source, String schemaPath) {
Validator validator = schema.newValidator();
validator.validate(source);
return true;
} catch (Exception e){
} catch (Exception e) {
LOGGER.error(String.format("Invalid DMN file: %s", e.getMessage()));
throw new DMNRuntimeException(e);
}
Expand Down
17 changes: 4 additions & 13 deletions dmn-core/src/main/java/com/gs/dmn/tck/TCKSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.gs.dmn.transformation.InputParameters;

import java.io.File;
import java.net.URL;

public abstract class TCKSerializer {
public static final String DEFAULT_TEST_CASE_FILE_EXTENSION = ".xml";
Expand All @@ -44,22 +43,14 @@ public TCKSerializer(BuildLogger logger, TCKMarshaller marshaller, InputParamete

public TestCases read(File input) {
try {
return read(input.toURI().toURL());
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot read TCK from '%s'", input.getPath()), e);
}
}
this.logger.info(String.format("Reading TCK '%s' ...", input.getPath()));

public TestCases read(URL input) {
try {
logger.info(String.format("Reading TCK '%s' ...", input.toString()));

TestCases testCases = this.marshaller.unmarshal(input, this.inputParameters.isXsdValidation());
TestCases testCases = this.marshaller.unmarshal(input, inputParameters.isXsdValidation());

logger.info("TCK read.");
this.logger.info("TCK read.");
return testCases;
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot read TCK from '%s'", input.toString()), e);
throw new DMNRuntimeException(String.format("Cannot read TCK from '%s'", input.getPath()), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,20 @@

import com.gs.dmn.tck.ast.TestCases;

import java.io.*;
import java.net.URL;
import java.io.File;
import java.io.Reader;
import java.io.Writer;

public interface TCKMarshaller {
TestCases unmarshal(String input, boolean validateSchema);

TestCases unmarshal(File input, boolean validateSchema);

TestCases unmarshal(URL input, boolean validateSchema);

TestCases unmarshal(InputStream input, boolean validateSchema);

TestCases unmarshal(Reader input, boolean validateSchema);

String marshal(TestCases o);

void marshal(TestCases o, File output);

void marshal(TestCases o, OutputStream output);

void marshal(TestCases o, Writer output);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.gs.dmn.serialization.xstream.DMNExtensionRegister;
import com.gs.dmn.tck.ast.TestCases;
import com.gs.dmn.tck.serialization.TCKMarshaller;
import com.thoughtworks.xstream.io.xml.StaxDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -92,38 +91,6 @@ public TestCases unmarshal(File input, boolean validateSchema) {
return null;
}

@Override
public TestCases unmarshal(URL input, boolean validateSchema) {
try (Reader firstStringReader = new InputStreamReader(input.openStream()); Reader secondStringReader = new InputStreamReader(input.openStream())) {
TCKVersion tckVersion = inferTCKVersion(firstStringReader);
if (validateSchema) {
try (InputStreamReader reader = new InputStreamReader(input.openStream())) {
validateXMLSchema(new StreamSource(reader), tckVersion.getSchemaLocation());
}
}
return unmarshal(tckVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling TCK content from URL.", e);
}
return null;
}

@Override
public TestCases unmarshal(InputStream input, boolean validateSchema) {
try (Reader firstStringReader = new InputStreamReader(input); Reader secondStringReader = new InputStreamReader(input)) {
TCKVersion tckVersion = inferTCKVersion(firstStringReader);
if (validateSchema) {
try (InputStreamReader reader = new InputStreamReader(input)) {
validateXMLSchema(new StreamSource(reader), tckVersion.getSchemaLocation());
}
}
return unmarshal(tckVersion, secondStringReader);
} catch (Exception e) {
LOGGER.error("Error unmarshalling TCK content from InputStream.", e);
}
return null;
}

@Override
public TestCases unmarshal(Reader input, boolean validateSchema) {
try (BufferedReader buffer = new BufferedReader(input)) {
Expand Down Expand Up @@ -163,15 +130,6 @@ public void marshal(TestCases o, File output) {
}
}

@Override
public void marshal(TestCases o, OutputStream output) {
try (Writer fileWriter = new OutputStreamWriter(output)) {
marshal(o, fileWriter);
} catch (Exception e) {
LOGGER.error("Error marshalling object {}", o);
}
}

@Override
public void marshal(TestCases o, Writer output) {
if (o != null) {
Expand Down
Loading

0 comments on commit eba5c56

Please sign in to comment.