Skip to content

Commit

Permalink
refactor packages
Browse files Browse the repository at this point in the history
  • Loading branch information
sboeckelmann committed Jun 12, 2024
1 parent ba7b4b0 commit 9c1c589
Show file tree
Hide file tree
Showing 15 changed files with 347 additions and 220 deletions.
9 changes: 9 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@
<groupId>io.smallrye.reactive</groupId>
<artifactId>mutiny</artifactId>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>serializer</artifactId>
</dependency>

<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import io.openepcis.converter.Conversion;
import io.openepcis.converter.VersionTransformerFeature;
import io.openepcis.converter.exception.FormatConverterException;

import javax.xml.XMLConstants;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
Expand All @@ -40,155 +39,158 @@
*/
public class DefaultXmlVersionTransformer implements XmlVersionTransformer {

private final Transformer from12To20;
private final Transformer from20T012;
private final ExecutorService executorService;

public DefaultXmlVersionTransformer(final ExecutorService executorService) {
this.executorService = executorService;
private static final TransformerFactory TRANSFORMER_FACTORY = createTransformerFactory();

private static Templates FROM_12_TO_20;

private static Templates FROM_20_TO_12;

private static TransformerFactory createTransformerFactory() {

final TransformerFactory transformerFactory = TransformerFactory.newInstance();
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");

from12To20 =
transformerFactory.newTransformer(
new StreamSource(
DefaultXmlVersionTransformer.class
FROM_12_TO_20 = transformerFactory.newTemplates(
new StreamSource(DefaultXmlVersionTransformer.class
.getClassLoader()
.getResourceAsStream("xalan-conversion/convert-1.2-to-2.0.xsl")));

from20T012 =
transformerFactory.newTransformer(
new StreamSource(
DefaultXmlVersionTransformer.class
.getClassLoader()
FROM_20_TO_12 = transformerFactory.newTemplates(
new StreamSource(DefaultXmlVersionTransformer.class.getClassLoader()
.getResourceAsStream("xalan-conversion/convert-2.0-to-1.2.xsl")));
} catch (TransformerConfigurationException e) {
throw new FormatConverterException(
"Creation of Transformer instance failed : " + e.getMessage(), e);
} catch (Exception e) {
throw new RuntimeException(e);
}

return transformerFactory;
}

@Override
public InputStream xmlConverter(InputStream inputStream, Function<Conversion.StartStage, Conversion.BuildStage> fn) throws UnsupportedOperationException, IOException {
return xmlConverter(inputStream, fn.apply(Conversion.builder()).build());
public DefaultXmlVersionTransformer(final ExecutorService executorService) {
this.executorService = executorService;
}

/**
* Public method invoked by the calling application by indicating the type of conversion i.e. from
* XML 1.2 -> XML 2.0 or vice versa.
*
* @param inputStream Stream of EPCIS 1.2/2.0 XML document.
* @param conversion Conversion setting
* @return returns the InputStream of EPCIS 1.2/2.0 XML document.
*
* @throws UnsupportedOperationException if user is trying to convert different version other than
* specified then throw the error
* @throws IOException If any exception occur during the conversion then throw the error
*/
@Override
public final InputStream xmlConverter(
final InputStream inputStream, final Conversion conversion)
throws UnsupportedOperationException, IOException {
if (conversion.fromVersion().equals(conversion.toVersion())) {
// if input document version and conversion version are equal then return same document.
return inputStream;
} else if (EPCISVersion.VERSION_1_2_0.equals(conversion.fromVersion())
&& EPCISVersion.VERSION_2_0_0.equals(conversion.toVersion())) {
// If input document version is 1.2 and conversion version is 2.0, convert from XML 1.2 -> 2.0
return convert12To20(inputStream);
} else if (EPCISVersion.VERSION_2_0_0.equals(conversion.fromVersion())
&& EPCISVersion.VERSION_1_2_0.equals(conversion.toVersion())) {
// If input document version is 2.0 and conversion version is 1.2, convert from XML 2.0 -> 1.2
return convert20To12(inputStream, VersionTransformerFeature.enabledFeatures(conversion));
} else {
throw new UnsupportedOperationException(
"Requested conversion is not supported, Please check provided MediaType/Version and try again");

@Override
public InputStream xmlConverter(InputStream inputStream, Function<Conversion.StartStage, Conversion.BuildStage> fn) throws UnsupportedOperationException, IOException {
return xmlConverter(inputStream, fn.apply(Conversion.builder()).build());
}
}

/**
* Convert EPCIS 1.2 XML document to EPCIS 2.0 XML document
*
* @param inputDocument EPCIS 1.2 XML document as a InputStream
* @return converted EPCIS 2.0 XML document as a InputStream
* @throws IOException If any exception occur during the conversion then throw the error
*/
private InputStream convert12To20(final InputStream inputDocument) {
final PipedInputStream convertedDocument = new PipedInputStream();
final AtomicBoolean pipeConnected = new AtomicBoolean(false);

executorService.execute(
() -> {
final PipedOutputStream outTransform = new PipedOutputStream();
try (outTransform) {
outTransform.connect(convertedDocument);
pipeConnected.set(true);
from12To20.transform(
new StreamSource(inputDocument),
new StreamResult(new BufferedOutputStream(outTransform)));
} catch (Exception e) {
try {
outTransform.write(e.getMessage().getBytes());
} catch (IOException ioException) {
// ignore
}
throw new FormatConverterException(
"Exception occurred during conversion of EPCIS XML document from 1.2 to 2.0 : "
+ e.getMessage(),
e);

}
});
while (!pipeConnected.get()) {
Thread.yield();
/**
* Public method invoked by the calling application by indicating the type of conversion i.e. from
* XML 1.2 -> XML 2.0 or vice versa.
*
* @param inputStream Stream of EPCIS 1.2/2.0 XML document.
* @param conversion Conversion setting
* @return returns the InputStream of EPCIS 1.2/2.0 XML document.
* @throws UnsupportedOperationException if user is trying to convert different version other than
* specified then throw the error
* @throws IOException If any exception occur during the conversion then throw the error
*/
@Override
public final InputStream xmlConverter(
final InputStream inputStream, final Conversion conversion)
throws UnsupportedOperationException, IOException {
if (conversion.fromVersion().equals(conversion.toVersion())) {
// if input document version and conversion version are equal then return same document.
return inputStream;
} else if (EPCISVersion.VERSION_1_2_0.equals(conversion.fromVersion())
&& EPCISVersion.VERSION_2_0_0.equals(conversion.toVersion())) {
// If input document version is 1.2 and conversion version is 2.0, convert from XML 1.2 -> 2.0
return convert12To20(inputStream);
} else if (EPCISVersion.VERSION_2_0_0.equals(conversion.fromVersion())
&& EPCISVersion.VERSION_1_2_0.equals(conversion.toVersion())) {
// If input document version is 2.0 and conversion version is 1.2, convert from XML 2.0 -> 1.2
try {
return convert20To12(inputStream, VersionTransformerFeature.enabledFeatures(conversion));
} catch (TransformerConfigurationException e) {
throw new UnsupportedOperationException(e.getMessage(), e);
}
} else {
throw new UnsupportedOperationException(
"Requested conversion is not supported, Please check provided MediaType/Version and try again");
}
}
return convertedDocument;
}

/**
* Convert EPCIS 2.0 XML document to EPCIS 1.2 XML document
*
* @param inputDocument EPCIS 2.0 document as a InputStream
* @param enabledFeatures list of enabled VersionTransformer features
*
* @return converted EPCIS 1.2 XML document as a InputStream
*
* @throws IOException If any exception occur during the conversion then throw the error
*/
private InputStream convert20To12(final InputStream inputDocument, final List<VersionTransformerFeature> enabledFeatures) {
final PipedInputStream convertedDocument = new PipedInputStream();
final AtomicBoolean pipeConnected = new AtomicBoolean(false);

from20T012.setParameter("includeAssociationEvent", enabledFeatures.contains(VersionTransformerFeature.EPCIS_1_2_0_INCLUDE_ASSOCIATION_EVENT) ? "yes" : "no");
from20T012.setParameter("includePersistentDisposition", enabledFeatures.contains(VersionTransformerFeature.EPCIS_1_2_0_INCLUDE_PERSISTENT_DISPOSITION) ? "yes" : "no");
from20T012.setParameter("includeSensorElementList", enabledFeatures.contains(VersionTransformerFeature.EPCIS_1_2_0_INCLUDE_SENSOR_ELEMENT_LIST) ? "yes" : "no");

executorService.execute(
() -> {
final PipedOutputStream outTransform = new PipedOutputStream();
try (outTransform) {
outTransform.connect(convertedDocument);
pipeConnected.set(true);
from20T012.transform(
new StreamSource(inputDocument),
new StreamResult(new BufferedOutputStream(outTransform)));
} catch (Exception e) {
try {
outTransform.write(e.getMessage().getBytes());
} catch (IOException ioException) {
// ignore
}
throw new FormatConverterException(
"Exception occurred during conversion of EPCIS XML document from 2.0 to 1.2, Failed to convert : "
+ e.getMessage(),
e);
}
});
while (!pipeConnected.get()) {
Thread.yield();
/**
* Convert EPCIS 1.2 XML document to EPCIS 2.0 XML document
*
* @param inputDocument EPCIS 1.2 XML document as a InputStream
* @return converted EPCIS 2.0 XML document as a InputStream
* @throws IOException If any exception occur during the conversion then throw the error
*/
private InputStream convert12To20(final InputStream inputDocument) {
final PipedInputStream convertedDocument = new PipedInputStream();
final AtomicBoolean pipeConnected = new AtomicBoolean(false);

executorService.execute(
() -> {
final PipedOutputStream outTransform = new PipedOutputStream();
try (outTransform) {
outTransform.connect(convertedDocument);
pipeConnected.set(true);
FROM_12_TO_20.newTransformer().transform(
new StreamSource(inputDocument),
new StreamResult(new BufferedOutputStream(outTransform)));
} catch (Exception e) {
try {
outTransform.write(e.getMessage().getBytes());
} catch (IOException ioException) {
// ignore
}
throw new FormatConverterException(
"Exception occurred during conversion of EPCIS XML document from 1.2 to 2.0 : "
+ e.getMessage(),
e);

}
});
while (!pipeConnected.get()) {
Thread.yield();
}
return convertedDocument;
}
return convertedDocument;
}

/**
* Convert EPCIS 2.0 XML document to EPCIS 1.2 XML document
*
* @param inputDocument EPCIS 2.0 document as a InputStream
* @param enabledFeatures list of enabled VersionTransformer features
* @return converted EPCIS 1.2 XML document as a InputStream
* @throws IOException If any exception occur during the conversion then throw the error
*/
private InputStream convert20To12(final InputStream inputDocument, final List<VersionTransformerFeature> enabledFeatures) throws TransformerConfigurationException {
final PipedInputStream convertedDocument = new PipedInputStream();
final AtomicBoolean pipeConnected = new AtomicBoolean(false);
final Transformer from20T012 = FROM_20_TO_12.newTransformer();
from20T012.setParameter("includeAssociationEvent", enabledFeatures.contains(VersionTransformerFeature.EPCIS_1_2_0_INCLUDE_ASSOCIATION_EVENT) ? "yes" : "no");
from20T012.setParameter("includePersistentDisposition", enabledFeatures.contains(VersionTransformerFeature.EPCIS_1_2_0_INCLUDE_PERSISTENT_DISPOSITION) ? "yes" : "no");
from20T012.setParameter("includeSensorElementList", enabledFeatures.contains(VersionTransformerFeature.EPCIS_1_2_0_INCLUDE_SENSOR_ELEMENT_LIST) ? "yes" : "no");

executorService.execute(
() -> {
final PipedOutputStream outTransform = new PipedOutputStream();
try (outTransform) {
outTransform.connect(convertedDocument);
pipeConnected.set(true);
from20T012.transform(
new StreamSource(inputDocument),
new StreamResult(new BufferedOutputStream(outTransform)));
} catch (Exception e) {
try {
outTransform.write(e.getMessage().getBytes());
} catch (IOException ioException) {
// ignore
}
throw new FormatConverterException(
"Exception occurred during conversion of EPCIS XML document from 2.0 to 1.2, Failed to convert : "
+ e.getMessage(),
e);
}
});
while (!pipeConnected.get()) {
Thread.yield();
}
return convertedDocument;
}

}
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@
<artifactId>openepcis-test-resources</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>serializer</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
7 changes: 3 additions & 4 deletions quarkus/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.openepcis</groupId>
<artifactId>openepcis-document-converter-parent</artifactId>
<groupId>io.openepcis.quarkus</groupId>
<artifactId>quarkus-document-converter-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>io.openepcis.quarkus</groupId>
<artifactId>quarkus-document-converter-deployment</artifactId>
<name>quarkus-document-converter-deployment</name>
<description>Quarkus OpenEPCIS Document Converter Deployment</description>
Expand Down
Loading

0 comments on commit 9c1c589

Please sign in to comment.