Tuesday 3 June 2014

Generating plain xml from Jasper Irrespective of the Locale

Generating plain xml from the jasper report has another problem for me.

If the locale is changed then the column names will get changed.

As I want the xml for the Data Feed, it would not work as the xml parsing
mechanism will change as the locale will change.


So to achieve the same i.e. to get the xml attribute in English all the time
irrespective of the locale, need to do some changes in the code.

Here is the modified exportText method.


public void exportText(JRPrintText text) throws IOException
{

JRStyle style = text.getStyle();
if (style != null){
String styleName = style.getName();
if(!isColumn && "TableColumnHeaderTextStyle".equals(styleName)){
columnMap.put(columnCount, text.getOriginalText());
columnCount++;
}else if(!isRow && "TableDetailTextStyle".equals(styleName)){
isColumn = true;
isRow = true;
}
}

if(isRow){
if (!isNewColumn && text.hasProperties())
{
JRPropertiesMap propertiesMap = text.getPropertiesMap();
String[] propertyNames = propertiesMap.getPropertyNames();
if (propertyNames != null && propertyNames.length > 0)
{
for(int i = 0; i < propertyNames.length; i++)
{
String value = propertiesMap.getProperty(propertyNames[i]);
if (value != null)
{
int p = value.indexOf('.');
String regex = "[_][0-9][0-9][0-9]";
if (p >= 0) {
   String right = value.substring(p + 1);
   String []val = right.split(regex);
   String modValue = val[0];
   if(newColumnMap.containsValue(modValue)){
    isNewColumn = true;
    }else{
    newColumnMap.put(newCoulmnCount, modValue);
    newCoulmnCount++ ;
    }
   }
}
}
}
}

if(style != null && "TableDetailTextStyle".equals(style.getName())){
isRowClosed = false;
if(rowCount == 0){
xmlWriter.startElement(ELEMENT_row);
}

if(null == text.getOriginalText()){
System.out.println("text value is null");
}

String attributName = newColumnMap.get(rowCount);
xmlWriter.addEncodedAttribute(attributName, text.getOriginalText());
rowCount++;
}

if(rowCount == columnCount){
isRowClosed = true;
xmlWriter.closeElement();
rowCount = 0;
}
}

}

Sunday 1 June 2014

XML parsing Issues

While working on Generating XML output/export in jasper, I came across some xml issues.

Xml generation was successful, but parsing the same in browsers had some issues.

Listing out those issues here and the solution for the same.

Every Browser show the error or explain it differently.

Here I am showing the errors by crome and Firefox.


Issue 1:

On Crome :
This page contains the following errors:
error on line 4 at column 60: Specification mandate value for attribute Size
Below is a rendering of the page up to the first error.

Here Size is the attribute name, being used by me in the xml.
The original column name in jasper was Size(Kb)

On FireFox :
XML Parsing Error: not well-formed
Location: http://10.55.36.34:8080/xyz?id=dT01ODg5NyZwPTEzODI4JnI9VGVzdCBSZXBvcnQmaj0vOTYzNDctdXNyLWZvbC11cmkvMTM4MjgtdXNyLXdzLXJlcG9ydHMtZm9sLXVyaS9UZXN0X1JlcG9ydCZkPTE0MDA3MzkxODYwODgmYz0xNjgyMjA4NTg2
Line Number 4, Column 171:
<row Reference="abc-16316AD-2411101F000" Action_Date="18/03/2014 11:45" Title="audit.png" User_Name="SEND NO MESSAGES KEINE NACHRICHT SENDEN" Content_Id="21565941" Size(Kb)="72"

FireFox highlights the error by pointing to the attribute "Size(Kb)". The attribute contains the special character.

By removing/replacing the special character "(", the above issue gets resolved.


Issue 2:

On crome :
This page contains the following errors:
error on line 6843 at column 19: Extra content at the end of the document
Below is a rendering of the page up to the first error.

On FireFox :
XML Parsing Error: no element found
Location: http://10.55.36.34:8080/xyz?id=dT01ODg5NyZwPTEzODI4JnI9RG9jdW1lbnQgRGlzdHJpYnV0aW9uIFYxIFJlcG9ydCZqPS85NjM0Ny11c3ItZm9sLXVyaS8xMzgyOC11c3Itd3MtcmVwb3J0cy1mb2wtdXJpL0RvY3VtZW50X0Rpc3RyaWJ1dGlvbl9WMV9SZXBvcnQmZD0xNDAwNzUxODUzNDUzJmM9MzI3NjMxMDE0Mg
Line Number 6844, Column 1:

The error was because of xml root node is missing.


Issue 3:

Mozilla....

XML Parsing Error: duplicate attribute
Location: http://10.55.36.34:8080/BIReportDataFeed?id=dT01ODg5NyZwPTEzODI4Jmo9Lzk2MzQ3LXVzci1mb2wtdXJpLzEzODI4LXVzci13cy1yZXBvcnRzLWZvbC11cmkvRG9jdW1lbnRfTWV0YWRhdGEmZD0xNDAwODUyNzU5Mjg2JmM9MjUzMTIwMTg0NQ
Line Number 4, Column 42: <row Reference="GER-BRU-7" Property="" Property="" Property="" Property="" Property="" Property="" Property="" Property="" Property=""/>


This is very straight forward. Xml does not allow duplicate attributes.