update transform

This commit is contained in:
Tanapattara Wongkhamchan 2024-08-16 15:35:21 +07:00
parent bdbedfa799
commit d6b8f9581e
3 changed files with 64 additions and 13 deletions

View file

@ -229,8 +229,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version> <version>3.13.0</version>
<configuration> <configuration>
<source>1.8</source> <source>9</source>
<target>1.8</target> <target>9</target>
<encoding>UTF-8</encoding> <encoding>UTF-8</encoding>
</configuration> </configuration>
</plugin> </plugin>

View file

@ -27,8 +27,10 @@ import de.kosit.validationtool.impl.input.StreamHelper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.s9api.*; import net.sf.saxon.s9api.*;
import org.w3c.dom.Document;
import javax.xml.transform.Source; import javax.xml.transform.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import java.io.*; import java.io.*;
import java.net.URI; import java.net.URI;
@ -79,29 +81,71 @@ public class TransformHandler extends BaseHandler {
} }
private byte[] serializeXR2HTML(final Source source) { private byte[] serializeXR2HTML(final Source source) {
try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) { try ( final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ByteArrayOutputStream sourceCopy = new ByteArrayOutputStream() ) {
Processor processor = new Processor(false); Processor processor = new Processor(false);
XsltCompiler xsltCompiler = processor.newXsltCompiler(); XsltCompiler xsltCompiler = processor.newXsltCompiler();
// Copy source content if it's a StreamSource
if (source instanceof StreamSource) {
try ( InputStream sourceInputStream = ((StreamSource) source).getInputStream() ) {
sourceInputStream.transferTo(sourceCopy);
}
}
// 0. Check Namespace of the XML Source
Source namespaceCheckSource = (source instanceof StreamSource)
? new StreamSource(new ByteArrayInputStream(sourceCopy.toByteArray()))
: source;
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMResult domResult = new DOMResult();
transformer.transform(namespaceCheckSource, domResult);
Document doc = (Document) domResult.getNode();
String namespace = doc.getDocumentElement().getNamespaceURI();
String xsltStyle = "visualization/xsl/";
switch (namespace) {
case "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2":
xsltStyle += "ubl-invoice-xr.xsl";
break;
case "urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2":
xsltStyle += "ubl-creditnote-xr.xsl";
break;
case "urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100":
xsltStyle += "cii-xr.xsl";
break;
default:
throw new TransformerException("Unknown namespace:" + namespace);
}
// 1. XSLT -> XR // 1. XSLT -> XR
XsltExecutable stylesheet = xsltCompiler.compile(new StreamSource(new File("visualization/xsl/ubl-invoice-xr.xsl"))); Source firstTransformSource = (source instanceof StreamSource)
Xslt30Transformer transformer = stylesheet.load30(); ? new StreamSource(new ByteArrayInputStream(sourceCopy.toByteArray()))
transformer.transform(source, processor.newSerializer(out)); : source;
XsltExecutable stylesheet = xsltCompiler.compile(new StreamSource(new File(xsltStyle)));
Xslt30Transformer xslt30Transformer = stylesheet.load30();
xslt30Transformer.transform(firstTransformSource, processor.newSerializer(out));
// 2. XR -> HTML // 2. XR -> HTML
XsltExecutable htmlStylesheet = xsltCompiler.compile(new StreamSource(new File("visualization/xsl/xrechnung-html.xsl"))); String htmlStylePath = "visualization/xsl/xrechnung-html.xsl";
XsltExecutable htmlStylesheet = xsltCompiler.compile(new StreamSource(new File(htmlStylePath)));
Xslt30Transformer htmlTransformer = htmlStylesheet.load30(); Xslt30Transformer htmlTransformer = htmlStylesheet.load30();
try ( final ByteArrayOutputStream htmlOut = new ByteArrayOutputStream() ) { try ( final ByteArrayOutputStream htmlOut = new ByteArrayOutputStream() ) {
// Use the XR output as the input for the HTML transformation // Create a new ByteArrayInputStream from out's content without closing out
htmlTransformer.transform(new StreamSource(new ByteArrayInputStream(out.toByteArray())), processor.newSerializer(htmlOut)); ByteArrayInputStream xrInputStream = new ByteArrayInputStream(out.toByteArray());
htmlTransformer.transform(new StreamSource(xrInputStream), processor.newSerializer(htmlOut));
// Return the HTML output
return htmlOut.toByteArray(); return htmlOut.toByteArray();
} }
} catch (final IOException | SaxonApiException e) { } catch (final IOException e) {
Printer.writeOut("Error serializeXR2HTML IOException result", e); Printer.writeOut("Error serializeXR2HTML IOException result", e);
throw new IllegalStateException("Can not serialize result", e); throw new IllegalStateException("Can not serialize result", e);
} catch (final SaxonApiException e) {
Printer.writeOut("Error serializeXR2HTML SaxonApiException result", e);
throw new IllegalStateException("Can not serialize result", e);
} catch (TransformerException e) {
Printer.writeOut("Error serializeXR2HTML TransformerException result", e);
throw new IllegalStateException("Can not serialize result", e);
} }
} }
} }

View file

@ -22,16 +22,23 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import javax.xml.bind.util.JAXBSource; import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Result;
import javax.xml.transform.Source; import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import de.kosit.validationtool.impl.Printer;
import org.apache.commons.io.input.ReaderInputStream; import org.apache.commons.io.input.ReaderInputStream;
import lombok.Getter; import lombok.Getter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.sf.saxon.om.TreeInfo; import net.sf.saxon.om.TreeInfo;
import org.w3c.dom.Document;
/** /**
* A validator {@link de.kosit.validationtool.api.Input} based on a {@link Source}. * A validator {@link de.kosit.validationtool.api.Input} based on a {@link Source}.