Jump to content
Changes to the Jaspersoft community edition download ×

style tag and special characters


mtassinari

Recommended Posts

Hi everybody!

I've noticed a problem while using JasperReports to make printed reports for our application. The problem is, if you have a styled text field or label, and the text between the style tags contains special characters like for example the character '&' , the style tag is not interpreted correctly but rather printed "as is".

Is there a way to solve this problem? 'Cause it's quite an issue for us.

Link to comment
Share on other sites

  • Replies 13
  • Created
  • Last Reply

Top Posters In This Topic

  • 2 weeks later...
  • 3 weeks later...

mtassinari
Wrote:

$F{param}.replaceAll("&","&")

That should work (see the attached test report).  Can you post a sample illustrating your problem?

Regards,

Lucian

Code:
<?xml version="1.0" encoding="UTF-8"?><jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"	name="test" whenNoDataType="AllSectionsNoDetail">	<detail>		<band height="30">			<textField>				<reportElement x="0" y="0" width="100" height="20"/>				<textElement isStyledText="true">				</textElement>				<textFieldExpression><![CDATA["a&<style isBold="true">b</style>".replaceAll("&","&")]]></textFieldExpression>			</textField>		</band>	</detail></jasperReport>
Link to comment
Share on other sites

I think that this works because it is directly in the XML template, while my data with the '&' character is stored in the database and retrieved at "compile time" when the template is filled.

in one of my subreports I have a text field defined as (1) and "ragione_sociale_azienda" sometime may contain an '&' character. The problem here is that the character would appear within the style tag, and this completely screws it, so what I obtain is somethink like (2)
with the "&" explicitly written and the style not parsed correctly.

I hope this helps in showing what my problem is.

Code:
(1) "<style isBold="true">"+$F{ragione_sociale_azienda}.replaceAll("&","&")+"</style>n<style size="8">"+$F{ragione_sociale_sede}+"</style>"(2) <style isBold="true">Centro Trasformazione Rottami Sud di Maione Rosa & C. s.n.c.</style>

Post Edited by mtassinari at 12/03/2009 08:49
Link to comment
Share on other sites

I tried your expression in a report using an map collection data source (see below).  It worked fine.

So I still don't know what happens in your case.  Do you have a self-contained test case to reproduce the problem?

Regards,

Lucian

Code:
Map data = new HashMap();data.put("ragione_sociale_azienda", "Centro Trasformazione Rottami Sud di Maione Rosa & C. s.n.c.");data.put("ragione_sociale_sede", "sede");List dataList = new ArrayList();dataList.add(data);
Link to comment
Share on other sites

I'm sorry, but we have no test case to try to reproduce the issue in a controlled environment, nor I have the time to prepare one.

One thing I can say, is that we do not fill that parameter using the hashmap, in fact in the report that field is not a "parameter" (those which you call with $P{param_name}) but a database field (which is accessed with $F{field_name}) and it seems that the moment in which the data is loaded in the report and thereby accessible using $F{field_name} is too late to make the character substitution. That is, it is still performed, but the desired effect is not achieved.

What you could do to try and reproduce it as close as our case as possible, is to put the string "Centro Trasformazione Rottami Sud di Maione Rosa & C. s.n.c." into a database table field and prepare a small report to fetch that string while applying the substitution.



Post Edited by mtassinari at 12/10/2009 15:44

 

EDIT: I also tried to anticipate the substitution in the database query itself by using REPLACE(ragione_sociale_azienda,'&','&') but it would still print the style tag as is, without correctly parsing it



Post Edited by mtassinari at 12/10/2009 15:55
Link to comment
Share on other sites

I don't understand how fetching the text from a DB would change the way JR works.  A Java String is a Java String no matter if it comes from a DB, from a literal in the code, from an XML file or so on.  I tried what you suggested anyway and it worked fine.

I ran out of clues.

Regards,

Lucian

Link to comment
Share on other sites

  • 3 years later...

I had issues in a text  field where I was doing a .replaceAll("&", "&") at the end and it wasn't replacing all the & signs.

I ended up needing to put the ..replaceAll("&", "&") on every field that I referenced rather than trying to do it to the entire text block.

I realize this post is old, but hopefully this helps someone :). I am on iReport 4.0.1.

Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...