I'm trying to use a XML String as a source of a report which is published in a JasperServer. First of all I'm using Jasper 6.5.1, and my application is JSF.
I have an report which receives an xPath as source, but the only way that I'd work was:
- I create a webservice where I select the entity in database (the xml field is in this entity), so I return the Response.ok and the method produces a XML.
- Then in the print calling I create the URL pointing to my own WS and I pass the URL as parameter net.sf.jasperreports.xml.source, but I think is a code smell because in the print calling method I already have the entity and the XML String, but I need to build an URL send to the report and the report call the webservice to get the same XML as a source.
It works, but I think is a code smell.
Reference Remote XML DataSource
So now I'm have a similar situation but I don't have the entity yet so I couldn't use that workaround, so I need to pass the XML to the report as String, as Document, as InputStream or whatever.
But it didn't work. Is there any example or way that I could do this?
I'm following this parameters possibilities: Jasper XML Constants
And I'm trying to use XML_DATA_DOCUMENT parameter:
InputSource source = new InputSource(new StringReader(nfe.getSisXml()));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(source);
parameters.put("XML_DATA_DOCUMENT", document);
But I'm getting the exception:
Infinite recursion (StackOverflowError) (through reference chain: org.apache.xerces.dom.DeferredDocumentImpl["firstChild"]->org.apache.xerces.dom.DeferredElementImpl["ownerDocument"]->org.apache.xerces.dom.DeferredDocumentImpl["firstChild"]->
Thanks in advance
EDIT: After some comments I'm trying to use XML_INPUT_STREAM parameter here is a piece of my JRXML:
<queryString language="xPath">
<![CDATA[//infNFe/det]]>
</queryString>
<field name="NfeId" class="java.lang.String">
<fieldDescription><![CDATA[/nfeProc/NFe/infNFe/@Id]]></fieldDescription>
</field>
<field name="NfeVersao" class="java.lang.String">
<fieldDescription><![CDATA[/nfeProc/NFe/infNFe/@versao]]></fieldDescription>
</field>
The XML_INPUT_STREAM parameter doesn't appear in JRXML and if I edit the JRXML and add it and save jasper exclude the tags.
Here is a Screenshot of my parameters
I'm trying to sending my XML string as InputStream like this:
InputStream inputStream = new ByteArrayInputStream(nfe.getSisXml().getBytes(Charset.forName("UTF-8")));
parameters.put("XML_INPUT_STREAM", inputStream);
And I'm using my own client where I convert parameters using Jackson in JSON before sending to report in server:
final ObjectNode requestNode = JsonNodeFactory.instance.objectNode()
.put("reportUnitUri", path)
.put("async", false)
.put("freshData", false)
.put("saveDataSnapshot", false)
.put("outputFormat", "pdf")
.put("ignorePagination", false);
//.put("pages", "0");
if (parameters != null && !parameters.isEmpty()) {
final ArrayNode parametersNode = requestNode.putObject("parameters").putArray("reportParameter");
for (Map.Entry<String, Object> e : parameters.entrySet()) {
final ObjectNode parameterNode = parametersNode.addObject()
.put("name", e.getKey());
if (e.getValue() instanceof String) {
parameterNode.putArray("value").add((String) e.getValue());
} else {
parameterNode.putArray("value").add("" + new ObjectMapper().convertValue(e.getValue(), JsonNode.class));
}
}
}