Skip to content

Commit

Permalink
optimize pre-scan : more todo
Browse files Browse the repository at this point in the history
  • Loading branch information
sboeckelmann committed Oct 31, 2023
1 parent 5985e23 commit 161538b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
<artifactId>openepcis-model-epcis</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.openepcis</groupId>
<artifactId>openepcis-document-validation-service</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>mutiny</artifactId>
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/io/openepcis/convert/VersionTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.openepcis.convert.xml.XmlToJsonConverter;
import io.openepcis.convert.xml.XmlVersionTransformer;
import io.openepcis.model.rest.ProblemResponseBody;
import io.openepcis.validation.xml.PreScanUtil;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -179,12 +180,20 @@ public final EPCISVersion versionDetector(final BufferedInputStream epcisDocumen
if (conversion.fromVersion() != null) {
return conversion.fromVersion();
}
epcisDocument.mark(1024);
// pre scan 1024 bytes to detect version
final byte[] preScan = new byte[1024];
epcisDocument.read(preScan, 0, preScan.length);
epcisDocument.reset();
final String preScanVersion = new String(preScan, StandardCharsets.UTF_8);

// TODO: optimize prescan with regex matcher
String preScanVersion = null;
if (EPCISFormat.XML.equals(conversion.fromMediaType())) {
preScanVersion = PreScanUtil.scanFirstTag(epcisDocument);
} else {
epcisDocument.mark(1024);
// pre scan 1024 bytes to detect version
final byte[] preScan = new byte[1024];
epcisDocument.read(preScan, 0, preScan.length);
epcisDocument.reset();
preScanVersion = new String(preScan, StandardCharsets.UTF_8);
}
System.out.println(preScanVersion);

if (!preScanVersion.contains(EPCIS.SCHEMA_VERSION)) {
throw new FormatConverterException(
Expand Down

0 comments on commit 161538b

Please sign in to comment.