Hi all,
I am struggling trying to convert an .xml file (containing .xsl style information!) to another format like .docx or .pdf. The xml/xsl file combination should use XSL-T instead of converting the raw .xml file.
The code I use is pretty simple, there is an XmlLoadOptions
with a Supplier<Stream>
providing the xsl-t information (from my understanding). It seems like the xsl loadOptions is being ignored, because the raw .xml content is being converted instead of the result of an xsl-transformation.
Is there any code in the example project in GitLab that I missed or any other JavaDoc that explains this procedure a bit more in detail? Or is using the XmlLoadOptions
with its method #setXslFoFactory
the wrong approach? Since the internal code seems to be obfuscated, I also am not sure if I am using the correct classes for Stream
/ MemoryStream
.
Heres my code:
import com.groupdocs.conversion.contracts.LoadOptionsProvider;
import com.groupdocs.conversion.options.convert.WordProcessingConvertOptions;
import com.groupdocs.conversion.options.load.XmlLoadOptions;
...
public void convertXml(String xmlSourcePath, String fileSourceName) {
WordProcessingConvertOptions convertOptions = new WordProcessingConvertOptions();
String targetPath = xmlSourcePath + FilenameUtils.getBaseName(fileSourceName) + ".docx";
String xslSourcePath = xmlSourcePath + FilenameUtils.getBaseName(fileSourceName) + ".xsl";
LoadOptionsProvider loadOptionsProvider = () -> {
XmlLoadOptions xmlLoadOptions = new XmlLoadOptions();
xmlLoadOptions.setXslFoFactory(new XmlXslSupplier(xslSourcePath));
return xmlLoadOptions;
};
try (Converter converter = new Converter(xmlSourcePath + fileSourceName, loadOptionsProvider)) {
converter.convert(targetPath, convertOptions);
} catch (Exception e) {
}
My XmlXslSupplier
provides a MemoryStream
:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.function.Supplier;
import com.groupdocs.conversion.internal.c.a.ms.System.IO.MemoryStream;
import com.groupdocs.conversion.internal.c.a.ms.System.IO.Stream;
public class XmlXslSupplier implements Supplier<Stream> {
private final String xslSourcePath;
public XmlXslSupplier(String xslSourcePath) {
this.xslSourcePath = xslSourcePath;
}
@Override
public Stream get() {
MemoryStream memoryStream = new MemoryStream();
byte[] buffer = new byte[1024];
int bytesRead;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xslSourcePath.getBytes());
while (true) {
try {
if ((bytesRead = byteArrayInputStream.read(buffer)) == -1) break;
} catch (IOException e) {
throw new RuntimeException(e);
}
memoryStream.write(buffer, 0, bytesRead);
}
return memoryStream;
}
}
GroupDocs conversion version is 22.11.
Any help is very appreciated.