-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve prescan for schemaVersion attribute
- Loading branch information
1 parent
0942263
commit 1fed64c
Showing
2 changed files
with
40 additions
and
31 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/io/openepcis/convert/AttributePreScanUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.openepcis.convert; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class AttributePreScanUtil { | ||
|
||
private static final String SCHEMA_VERSION_REGEX ="schemaVersion\"?'?\\s*[=:]\\s*([\"'])?([^\"']*)"; | ||
private static final Pattern SCHEMA_VERSION_PATTERN = Pattern.compile(SCHEMA_VERSION_REGEX); | ||
private static final int READ_LIMIT = 4096; | ||
public static final String scanSchemaVersion(final BufferedInputStream input) throws IOException { | ||
input.mark(READ_LIMIT); | ||
try { | ||
final StringBuilder sb = new StringBuilder(); | ||
final byte[] buffer = new byte[64]; | ||
int len = -1; | ||
int bytesReceived = 0; | ||
Matcher matcher = SCHEMA_VERSION_PATTERN.matcher(sb.toString()); | ||
while (!matcher.find(0) && bytesReceived < READ_LIMIT && (len = input.read(buffer)) != -1) { | ||
sb.append(new String(buffer, 0, len, StandardCharsets.UTF_8)); | ||
bytesReceived += len; | ||
matcher = SCHEMA_VERSION_PATTERN.matcher(sb.toString()); | ||
} | ||
if (matcher.find(0)) { | ||
return matcher.group(2); | ||
} | ||
return ""; | ||
} finally { | ||
input.reset(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters