String to InputStream help needed

I

iksrazal

Hi all,

I have a String represented as an xml file which I get from a mysql db
column, which is of type 'text' . I need to pass the String to a jasper
reports method which has the following signature:

JasperReport compileReport(java.io.InputStream inputStream)
Compiles the serialized report design object read from the
supplied input stream and returns the generated compiled report design
object.

I tried the following, but I get:

01:48:48,200 ERROR [Digester] Parse Fatal Error at line -1 column -1:
Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.

Logging the the string I'm passing in, it appears to be ok.

I'm using this code:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer wr = new OutputStreamWriter( baos );
wr.write( jasperTemplateAsStr );
byte[] byteArray = baos.toByteArray();
InputStream is = new ByteArrayInputStream( byteArray );

I also tried this, which did not work either:

InputStream is = new
ByteArrayInputStream(jasperTemplateAsStr.getBytes());

Any ideas?
iksrazal
 
C

cbongior

Your exception has nothing to do with your use of a
ByteArrayInputStream as the Input source (I routinely use this trick).
I have seen this error and generally it means that the stream hit a -1
before the parse was complete. It can also mean a character not
supported by the XML encoding format is present in the document.

So, if you say <?xml version="1.0" encoding="ISO-8859-1"?>
it had better not contain anything characters greater than 127 or some
of the esoteric whitespaces in the very low ascii range.

Christian
http://christian.bongiorno.org/resume.pdf
 
I

iksrazal

Indeed, this turned out to be pretty sucky problem. First, mysql
doesn't do UTF8 by default, which my doc used. You can get around that
by specifying utf8 on the drivers connection string and a couple of
other ways.

However, what really was the problem was jasper itself, which made the
problem elusive.

Using ByteArrayOutputStream as shown creates the sax error. However,
ByteArrayInputStream(str.getBytes() can work fine - if jasper is
configured correctly. If not, it throws:

JRException: Errors were encountered when compiling report expressions
class file:

Since I in jboss, and jasper uses its own classpath, I needed this from
CompileServlet.java in jaspers docs:
ServletContext context =
this.getServletConfig().getServletContext();

JRProperties.setProperty(
JRProperties.COMPILER_CLASSPATH,

context.getRealPath("/WEB-INF/lib/jasperreports-1.0.0.jar") +
System.getProperty("path.separator") +
context.getRealPath("/WEB-INF/classes/")
);

JRProperties.setProperty(
JRProperties.COMPILER_TEMP_DIR,
context.getRealPath("/reports/")
);

or you could do hack like:

String wtf = System.getProperty("jboss.server.home.dir");
StringBuffer sb = new StringBuffer();
sb.append(wtf);
sb.append("/lib/jasperreports-1.0.0.jar");

iksrazal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top