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;
}
}

}

No comments:

Post a Comment