Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

- name: Build
run: ./gradlew build

- name: Upload test reports
if: failure()
uses: actions/upload-artifact@v4
with:
name: test-reports
path: build/reports/tests/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Package Files #
*.jar
!gradle/wrapper/gradle-wrapper.jar
*.war
*.ear

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ For large files, use callbacks to process records without storing them all in me

```java
SieDocumentReader reader = new SieDocumentReader();
reader.streamValues = true;
reader.callbacks.setVER(voucher -> {
reader.setStreamValues(true);
reader.getCallbacks().setVER(voucher -> {
// Process each voucher as it is parsed
});
reader.callbacks.setIB(periodValue -> {
reader.getCallbacks().setIB(periodValue -> {
// Process each opening balance entry
});
reader.readDocument("large-file.SE");
Expand Down
2 changes: 1 addition & 1 deletion docs/DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ RuntimeException
├── SieDateException
├── SieMissingMandatoryDateException
├── SieMissingObjectException
├── SieVoucherMissmatchException
├── SieVoucherMismatchException
└── MissingFieldException
```

Expand Down
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
27 changes: 4 additions & 23 deletions src/main/java/alipsa/sieparser/Encoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ of this software and associated documentation files (the "Software"), to deal

package alipsa.sieparser;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Handles character encoding for SIE files.
Expand All @@ -41,9 +35,7 @@ public class Encoding {

private Encoding() {}

Logger logger = LoggerFactory.getLogger(Encoding.class);

private static Charset defaultCharset=Charset.forName("IBM437");
private static final Charset defaultCharset = Charset.forName("IBM437");

/**
* Returns the charset used for SIE file encoding (IBM437).
Expand All @@ -55,23 +47,12 @@ public static Charset getCharset() {
}

/**
* Converts a string to a collection of bytes using the SIE charset.
* Converts a string to a byte array using the SIE charset.
*
* @param value the string to convert
* @return a collection of bytes representing the string in IBM437 encoding
* @return a byte array representing the string in IBM437 encoding
*/
public static Collection<Byte> getBytes(String value) {
byte[] byteArray = value.getBytes(getCharset());
List<Byte> byteList = new ArrayList<>();
for (byte b : byteArray) {
byteList.add(Byte.valueOf(b));
}
return byteList;
}

/*
public byte[] getByteArray(String value) {
public static byte[] getBytes(String value) {
return value.getBytes(getCharset());
}
*/
}
27 changes: 2 additions & 25 deletions src/main/java/alipsa/sieparser/IoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ of this software and associated documentation files (the "Software"), to deal
package alipsa.sieparser;

import java.io.*;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

Expand All @@ -47,14 +43,7 @@ private IoUtil() {}
* @throws IOException if the file cannot be opened
*/
public static BufferedReader getReader(String fileName) throws IOException {
/*
CharsetDecoder decoder = Encoding.getCharset().newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPORT);
InputStreamReader isReader = new InputStreamReader(new FileInputStream(fileName), decoder);
return new BufferedReader( isReader );
*/
return Files.newBufferedReader(Paths.get(fileName), Encoding.getCharset());

}

/**
Expand All @@ -66,19 +55,7 @@ public static BufferedReader getReader(String fileName) throws IOException {
* @throws IOException if the file cannot be opened or created
*/
public static BufferedWriter getWriter(String fileName) throws IOException {
/*
CharsetEncoder encoder = Encoding.getCharset().newEncoder();
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
OutputStreamWriter osWriter = new OutputStreamWriter(new FileOutputStream(fileName), encoder);
return new BufferedWriter( osWriter );
*/
Path filePath = Paths.get(fileName);
StandardOpenOption option;
if (Files.exists(filePath)) {
option = StandardOpenOption.WRITE;
} else {
option = StandardOpenOption.CREATE_NEW;
}
return Files.newBufferedWriter(Paths.get(fileName),Encoding.getCharset(), option);
return Files.newBufferedWriter(Paths.get(fileName), Encoding.getCharset(),
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
}
10 changes: 10 additions & 0 deletions src/main/java/alipsa/sieparser/MissingFieldException.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ public class MissingFieldException extends SieException {
public MissingFieldException(String s) {
super(s);
}

/**
* Creates a new MissingFieldException with the given message and cause.
*
* @param s the detail message describing which field is missing
* @param cause the underlying cause
*/
public MissingFieldException(String s, Throwable cause) {
super(s, cause);
}
}
15 changes: 5 additions & 10 deletions src/main/java/alipsa/sieparser/SieCRC32.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ of this software and associated documentation files (the "Software"), to deal

package alipsa.sieparser;

import java.util.ArrayList;
import java.util.List;

/**
* Implementation of the CRC32 checksum algorithm as specified in the SIE file format (#KSUMMA).
* Used to verify the integrity of SIE file data.
Expand Down Expand Up @@ -79,13 +76,11 @@ public void start() {
* @param item the data item to include in the checksum
*/
public void addData(SieDataItem item) {
List<Byte> buffer = new ArrayList<>();
buffer.addAll(Encoding.getBytes(item.getItemType()));
crcAccumulate(Encoding.getBytes(item.getItemType()));
for (String d : item.getData()) {
String foo = d.replace("{", "").replace("}", "");
buffer.addAll(Encoding.getBytes(foo));
crcAccumulate(Encoding.getBytes(foo));
}
cRC_accumulate(buffer);
}

/**
Expand All @@ -96,12 +91,12 @@ public long checksum() {
return (~crc);
}

private void cRC_accumulate(List<Byte> buffer) {
private void crcAccumulate(byte[] buffer) {
long temp1;
long temp2;
for (Byte p : buffer) {
for (byte p : buffer) {
temp1 = (crc >> 8) & 0x00FFFFFF;
temp2 = CRCTable[((int) crc ^ p.byteValue()) & 0xff];
temp2 = CRCTable[((int) crc ^ p) & 0xff];
crc = temp1 ^ temp2;
}
}
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/alipsa/sieparser/SieDataItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,18 @@ public LocalDate getDate(int field) {
if (foo.isEmpty()) return null;

if (foo.length() != 8) {
getDocumentReader().callbacks.callbackException(new SieDateException(foo + " is not a valid date"));
getDocumentReader().getCallbacks().callbackException(new SieDateException(foo + " is not a valid date"));
return null;
}
try {
int y = Integer.parseInt(foo.substring(0, 4));
int m = Integer.parseInt(foo.substring(4, 6));
int d = Integer.parseInt(foo.substring(6, 8));
return LocalDate.of(y, m, d);
} catch (NumberFormatException | java.time.DateTimeException e) {
getDocumentReader().getCallbacks().callbackException(new SieDateException(foo + " is not a valid date"));
return null;
}
int y = Integer.parseInt(foo.substring(0, 4));
int m = Integer.parseInt(foo.substring(4, 6));
int d = Integer.parseInt(foo.substring(6, 8));
return LocalDate.of(y, m, d);
}

/**
Expand All @@ -292,7 +297,7 @@ public List<SieObject> getObjects() {
}
}
if (data == null) {
getDocumentReader().callbacks.callbackException(new SieMissingObjectException(getRawData()));
getDocumentReader().getCallbacks().callbackException(new SieMissingObjectException(getRawData()));
return null;
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/alipsa/sieparser/SieDateException.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ public SieDateException(String description) {
super(description);
}

/**
* Creates a new SieDateException with the given description and cause.
*
* @param description a message describing the invalid date
* @param cause the underlying cause
*/
public SieDateException(String description, Throwable cause) {
super(description, cause);
}
}
8 changes: 3 additions & 5 deletions src/main/java/alipsa/sieparser/SieDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,11 @@ public void setUB(List<SiePeriodValue> value) {
}

/**
* Returns the currency code (#VALUTA), or an empty string if not set.
* @return the currency code
* Returns the currency code (#VALUTA), or {@code null} if not set.
* Per the SIE specification, if this post is absent the reader should assume SEK.
* @return the currency code, or {@code null} if not set
*/
public String getVALUTA() {
if (valuta == null) {
return "";
}
return valuta;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/alipsa/sieparser/SieDocumentComparer.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void compareNonListItems() {
if (docA.getTAXAR() != docB.getTAXAR())
errors.add("TAXAR differs First, Second: '" + docA.getTAXAR() + "' , '" + docB.getTAXAR() + "'");

if (!docA.getVALUTA().equals(docB.getVALUTA()))
if (!StringUtil.equals(docA.getVALUTA(), docB.getVALUTA()))
errors.add("VALUTA differs First, Second: '" + docA.getVALUTA() + "' , '" + docB.getVALUTA() + "'");
}

Expand Down
Loading