zugferdimporter to accept xml files

This commit is contained in:
jstaerk
2024-03-31 20:21:39 +02:00
parent 15122e809d
commit df0eef061f
2 changed files with 52 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
- EN16931 validation 1.3.11 codelists v11 #357 - EN16931 validation 1.3.11 codelists v11 #357
- Fonts removed #358 - Fonts removed #358
- xrechnungimporter to read from filename, inputstream
- zugferdimporter to accept xml files
2.10.0 2.10.0
======= =======

View File

@@ -1,6 +1,9 @@
package org.mustangproject; package org.mustangproject;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.AbstractList; import java.util.AbstractList;
@@ -14,33 +17,38 @@ import org.w3c.dom.NodeList;
public class XMLTools extends XMLWriter { public class XMLTools extends XMLWriter {
@Override @Override
public String escapeAttributeEntities(String s) { public String escapeAttributeEntities(String s) {
return super.escapeAttributeEntities(s); return super.escapeAttributeEntities(s);
} }
@Override @Override
public String escapeElementEntities(String s) { public String escapeElementEntities(String s) {
return super.escapeElementEntities(s); return super.escapeElementEntities(s);
} }
public static List<Node> asList(NodeList n) { public static List<Node> asList(NodeList n) {
return n.getLength()==0? return n.getLength() == 0 ?
Collections.<Node>emptyList(): new NodeListWrapper(n); Collections.<Node>emptyList() : new NodeListWrapper(n);
} }
static final class NodeListWrapper extends AbstractList<Node> static final class NodeListWrapper extends AbstractList<Node>
implements RandomAccess { implements RandomAccess {
private final NodeList list; private final NodeList list;
NodeListWrapper(NodeList l) { NodeListWrapper(NodeList l) {
list=l; list = l;
} }
public Node get(int index) { public Node get(int index) {
return list.item(index); return list.item(index);
} }
public int size() { public int size() {
return list.getLength(); return list.getLength();
} }
} }
public static String nDigitFormat(BigDecimal value, int scale) { public static String nDigitFormat(BigDecimal value, int scale) {
/* /*
* I needed 123,45, locale independent.I tried * I needed 123,45, locale independent.I tried
@@ -63,9 +71,9 @@ public class XMLTools extends XMLWriter {
public static String encodeXML(CharSequence s) { public static String encodeXML(CharSequence s) {
if (s == null) { if (s == null) {
return ""; return "";
} }
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
final int len = s.length(); final int len = s.length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
@@ -122,24 +130,24 @@ public class XMLTools extends XMLWriter {
* @see <a href="https://www.w3.org/TR/xml/#sec-guessing">Autodetection of Character Encodings</a> * @see <a href="https://www.w3.org/TR/xml/#sec-guessing">Autodetection of Character Encodings</a>
* *
public static int guessBOMSize(ByteArrayInputStream is) throws IOException { public static int guessBOMSize(ByteArrayInputStream is) throws IOException {
byte[] pad = new byte[4]; byte[] pad = new byte[4];
is.read(pad); is.read(pad);
is.reset(); is.reset();
int test2 = ((pad[0] & 0xFF) << 8) | (pad[1] & 0xFF); int test2 = ((pad[0] & 0xFF) << 8) | (pad[1] & 0xFF);
int test3 = ((test2 & 0xFFFF) << 8) | (pad[2] & 0xFF); int test3 = ((test2 & 0xFFFF) << 8) | (pad[2] & 0xFF);
int test4 = ((test3 & 0xFFFFFF) << 8) | (pad[3] & 0xFF); int test4 = ((test3 & 0xFFFFFF) << 8) | (pad[3] & 0xFF);
// //
if (test4 == 0x0000FEFF || test4 == 0xFFFE0000 || test4 == 0x0000FFFE || test4 == 0xFEFF0000) { if (test4 == 0x0000FEFF || test4 == 0xFFFE0000 || test4 == 0x0000FFFE || test4 == 0xFEFF0000) {
// UCS-4: BOM takes 4 bytes // UCS-4: BOM takes 4 bytes
return 4; return 4;
} else if (test3 == 0xEFBBFF) { } else if (test3 == 0xEFBBFF) {
// UTF-8: BOM takes 3 bytes // UTF-8: BOM takes 3 bytes
return 3; return 3;
} else if (test2 == 0xFEFF || test2 == 0xFFFE) { } else if (test2 == 0xFEFF || test2 == 0xFFFE) {
// UTF-16: BOM takes 2 bytes // UTF-16: BOM takes 2 bytes
return 2; return 2;
} }
return 0; return 0;
}*/ }*/
/*** /***
@@ -159,5 +167,21 @@ public class XMLTools extends XMLWriter {
return zugferdData; return zugferdData;
} }
public static byte[] getBytesFromStream(InputStream fileinput) throws IOException {
// we're on java 8 so we cant use inputstream.readallbytes
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
BufferedInputStream bufferedInput=new BufferedInputStream(fileinput);
while ((nRead = bufferedInput.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
// end of polyfill
}
} }