Birt Report Indian Rupee formatting - java

I want to format numbers based on Indian Rupee/Number format (basically commas) in Birt through scripting (for some conditional reasons).
if I use:
this.getStyle().numberFormat="#,##,##,##0.000";
It still adds commas after every 3 characters .. as in 12,345,678.000 but I want the number to be 1,23,45,678.000 in this format
Can you please advise
EDIT: Bug with BIRT raised as : https://bugs.eclipse.org/bugs/show_bug.cgi?id=432211

EDIT: set a custom format number
Here is a possible workaround, forcing BIRT to make use of com.ibm.icu.text.DecimalFormat class. I don't know why indian format is not natively supported, you could report this in bugzilla of eclipse.org site.
Edit your dataset
Create a new computed column, select "String" datatype
Enter as expression: (in the first line, replace "value" with the actual name of the numeric column containing values)
var columnvalue=row["value"], customformat="#,##,##,##0.000"; //we can add here a test for conditional formatting
if (columnvalue!=null){
var symbols=Packages.com.ibm.icu.text.DecimalFormatSymbols(new Packages.java.util.Locale("en","IN"));
var formatter=Packages.com.ibm.icu.text.DecimalFormat(customformat,symbols);
var value=new Packages.java.math.BigDecimal(columnvalue.toString());
formatter.format(value);
}else{
"-"
}
Click "Preview results" in the dataset editor, a new column should be added at the end with the expected format.

You can use NumberFormat by setting the locale to Indian setting.
Locale locale = new Locale("en","IN");
String str = NumberFormat.getNumberInstance(locale).format(<your number>);
That's if you are looking for JAVA code to resolve your problem.

**you can use following javascript currency format and call it from BIRT.
function getSouthAsianCurrencyFormat(amount)
{
var l,ftemp,temp,camount,k,adecimal;
var decimals=2;
var ptrn="##,##,###,##,##,###.##";
var ptrnLength=0;
var adecimal=0;
var counts = {};
var ch, index, len, count;
amount= Number(Math.round(amount+'e'+decimals)+'e-'+decimals);
amount=amount.toFixed( decimals );
for (index = 0, len = ptrn.length; index < len; ++index) {
ch = ptrn.charAt(index);
count = counts[ch];
counts[ch] = count ? count + 1 : 1;
}
for (ch in counts) {
if(ch=="#"){
ptrnLength=counts[ch];
console.log(ch + " count: " + ptrnLength+"("+ptrn.length+")");
console.log( "amount length: " + amount.toString().length);
//console.log("decimalLength: "+decimalLength.toString().length);
}
}
if(counts['.']=0){
amount=amount+".00";
}
k=ptrn.toString().length;
l=amount.toString().length;
ftemp=amount.toString();
temp="";
camount="";
if(ptrnLength<(amount.toString().length-1)) return 0;
else {
k=k-1;
l=l-1;
for(i=l;i>-1;i--){
if(ptrn.charAt(k)=="#" || ptrn.charAt(k)=="." ){
camount=ftemp.charAt(i)+camount;
}
else{
camount=ptrn.charAt(k)+camount;
k=k-1;
if(ptrn.charAt(k)=="#"){
camount=ftemp.charAt(i)+camount;
}
}
k=k-1;
}
return (camount);
}
}

Related

Email Reading in Java by keeping the text positions

I am trying to read emails using Java. I got the in-box mails correctly.
But the problem is text body is shown in line by line. I need the body text as it is shown in mail,ie output text should be in same order (rephrasing proposal: "tabular alignment") as shown in mail.
This is the code I used to get body text from Message object,
private static String getTextFromMessage(Message message) throws MessagingException, IOException
{
String result = "";
if (message.isMimeType("text/plain"))
{
result = message.getContent().toString();
}
else if (message.isMimeType("multipart/*"))
{
MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
result = getTextFromMimeMultipart(mimeMultipart);
}
return result;
}
private static String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException
{
String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++)
{
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain"))
{
result = result + "\n" + bodyPart.getContent();
break;
}
else if (bodyPart.isMimeType("text/html"))
{
String html = (String) bodyPart.getContent();
result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
}
else if (bodyPart.getContent() instanceof MimeMultipart)
{
result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
}
}
return result;
}
For example, this is the mail content:
.
I need output as,
Beschreibung Stückpreis Anzahl Betrag
22545047 106,56 EUR 1 €106,56 EUR
as it as shown in mail.
But I got the output,
Beschreibung
Stückpreis
Anzahl
Betrag
22545047
106,56 EUR
1
€106,56 EUR
Can anyone please help me to solve this issue.
Thanks in advance
By the way, the strange words are German for "description", "price per piece", "number of pieces", "total price for this kind". I.e. they form a bill and are irrelevant for the problem.
It seems that you do not like the newlines which you explicitly insert in some of your "rendering" methods.
In order to get rid of them, delete all occurrences of
+ "\n"
in your code.
Then consider adding a single + "\n" at the end of the output.
In case the text your are outputting is the result of a html->plain text conversion, you lose the tabular alignment created by the html rendering. There are no "ten spaces". In order to get the alignment information translated into ascii-art (spaces to align columns) you'd have to do some intense analysis of the html markup and derive an appropriate number of spaces to insert.

Java : how to get the expression through the jcombo list

How do i create the one line expression using Java swing, link image picture. the every minute, every day,every month, every weekday and every hour need to convert it to "*" and also all the combo box contain the list of number list number link and weekday contain the click the picture
what i want is, if the user select "Every Minute" , "Every day","month = 2", "Weekday = monday", "hour= 3"
note of weekday JCombo : sunday = 0 , monday = 1, tuesday = 2 .....
the output will print as : * * 2 1 3
thanks alot.
i already tried this , my beginning code but cant do much :
String sjcb_EM = jcb_EM.getSelectedItem().toString();
String sjcb_EH = jcb_EH.getSelectedItem().toString();
String sjcb_ED = jcb_ED.getSelectedItem().toString();
String sjcb_EEM = jcb_EEM.getSelectedItem().toString();
String sjcb_EW = jcb_EW.getSelectedItem().toString();
String vb_1 = sjcb_EM + " " + sjcb_EH + " " + sjcb_ED + " " + sjcb_EEM + " " + sjcb_EW;
System.out.println(vb_1);
now i stuck, how to make the expression that i wanted.
Start by building a class which can hold both the display value and the query value...
public class WorkoutUnit {
private String displayValue;
private String queryValue;
public WorkoutUnit(String displayValue, String queryValue) {
this.displayValue = displayValue;
this.queryValue = queryValue;
}
public String getDisplayValue() {
return displayValue;
}
public String getQueryValue() {
return queryValue;
}
#Override
public String toString() {
return displayValue;
}
}
Build a ComboBoxModel using these values...
DefaultComboBoxModel<WorkoutUnit> model = new DefaultComboBoxModel<>();
model.addElement(new WorkoutUnit("Every Minute", "*"));
for (int index = 10; index < 61; index += 10) {
model.addElement(new WorkoutUnit(Integer.toString(index), Integer.toString(index)));
}
JComboBox<WorkoutUnit> cb = new JComboBox(model);
When needed, get the selected item from the combo box and get its query value...
WorkoutUnit unit = (WorkoutUnit)cb.getSelectedItem();
System.out.println("Query = " + unit.getQueryValue());
In this example, I've used toString to provide the display value to the JComboBox, this is not my preferred solution, I'd prefer to use a ListCellRenderer as demonstrated here
Oh, and because it looks like you're heading down a database query route, you should also have a look at Using Prepared Statements

Display Stanford NER confidence score

I'm extracting named-entities from news articles with the use of Stanford NER CRFClassifier and in order to implement active learning, I would like to know what are the confidence scores of the classes for each labelled entity.
Exemple of display :
LOCATION(0.20) PERSON(0.10) ORGANIZATION(0.60) MISC(0.10)
Here is my code for extracting named-entities from a text :
AbstractSequenceClassifier<CoreLabel> classifier = CRFClassifier.getClassifierNoExceptions(classifier_path);
String annnotatedText = classifier.classifyWithInlineXML(text);
Is there a workaround to get thoses values along with the annotations ?
I've found it out by myself, in CRFClassifier's doc it is written :
Probabilities assigned by the CRF can be interrogated using either the
printProbsDocument() or getCliqueTrees() methods.
The first method is not useful since it only prints what I want on the console, but I want to be able to access this data, so I have read how this method is coded and copied a bit its behaviour like this :
List<CoreLabel> classifiedLabels = classifier.classify(sentences);
CRFCliqueTree<String> cliqueTree = classifier.getCliqueTree(classifiedLabels);
for (int i = 0; i < cliqueTree.length(); i++) {
CoreLabel wi = classifiedLabels.get(i);
for (Iterator<String> iter = classifier.classIndex.iterator(); iter.hasNext();) {
String label = iter.next();
int index = classifier.classIndex.indexOf(label);
double prob = cliqueTree.prob(i, index);
System.out.println("\t" + label + "(" + prob + ")");
}
String tag = StringUtils.getNotNullString(wi.get(CoreAnnotations.AnswerAnnotation.class));
System.out.println("Class : " + tag);
}

Convert International String to \u Codes in java

How can I convert an international (e.g. Russian) String to \u numbers (unicode numbers)
e.g. \u041e\u041a for OK ?
there is a JDK tools executed via command line as following :
native2ascii -encoding utf8 src.txt output.txt
Example :
src.txt
بسم الله الرحمن الرحيم
output.txt
\u0628\u0633\u0645 \u0627\u0644\u0644\u0647 \u0627\u0644\u0631\u062d\u0645\u0646 \u0627\u0644\u0631\u062d\u064a\u0645
If you want to use it in your Java application, you can wrap this command line by :
String pathSrc = "./tmp/src.txt";
String pathOut = "./tmp/output.txt";
String cmdLine = "native2ascii -encoding utf8 " + new File(pathSrc).getAbsolutePath() + " " + new File(pathOut).getAbsolutePath();
Runtime.getRuntime().exec(cmdLine);
System.out.println("THE END");
Then read content of the new file.
You could use escapeJavaStyleString from org.apache.commons.lang.StringEscapeUtils.
I also had this problem. I had some Portuguese text with some special characters, but these characters where already in unicode format (ex.: \u00e3).
So I want to convert S\u00e3o to São.
I did it using the apache commons StringEscapeUtils. As #sorin-sbarnea said. Can be downloaded here.
Use the method unescapeJava, like this:
String text = "S\u00e3o"
text = StringEscapeUtils.unescapeJava(text);
System.out.println("text " + text);
(There is also the method escapeJava, but this one puts the unicode characters in the string.)
If any one knows a solution on pure Java, please tell us.
Here's an improved version of ArtB's answer:
StringBuilder b = new StringBuilder();
for (char c : input.toCharArray()) {
if (c >= 128)
b.append("\\u").append(String.format("%04X", (int) c));
else
b.append(c);
}
return b.toString();
This version escapes all non-ASCII chars and works correctly for low Unicode code points like Ä.
There are three parts to the answer
Get the Unicode for each character
Determine if it is in the Cyrillic Page
Convert to Hexadecimal.
To get each character you can iterate through the String using the charAt() or toCharArray() methods.
for( char c : s.toCharArray() )
The value of the char is the Unicode value.
The Cyrillic Unicode characters are any character in the following ranges:
Cyrillic: U+0400–U+04FF ( 1024 - 1279)
Cyrillic Supplement: U+0500–U+052F ( 1280 - 1327)
Cyrillic Extended-A: U+2DE0–U+2DFF (11744 - 11775)
Cyrillic Extended-B: U+A640–U+A69F (42560 - 42655)
If it is in this range it is Cyrillic. Just perform an if check. If it is in the range use Integer.toHexString() and prepend the "\\u". Put together it should look something like this:
final int[][] ranges = new int[][]{
{ 1024, 1279 },
{ 1280, 1327 },
{ 11744, 11775 },
{ 42560, 42655 },
};
StringBuilder b = new StringBuilder();
for( char c : s.toCharArray() ){
int[] insideRange = null;
for( int[] range : ranges ){
if( range[0] <= c && c <= range[1] ){
insideRange = range;
break;
}
}
if( insideRange != null ){
b.append( "\\u" ).append( Integer.toHexString(c) );
}else{
b.append( c );
}
}
return b.toString();
Edit: probably should make the check c < 128 and reverse the if and the else bodies; you probably should escape everything that isn't ASCII. I was probably too literal in my reading of your question.
There's a command-line tool that ships with java called native2ascii. This converts unicode files to ASCII-escaped files. I've found that this is a necessary step for generating .properties files for localization.
In case you need this to write a .properties file you can just add the Strings into a Properties object and then save it to a file. It will take care for the conversion.
Apache commons StringEscapeUtils.escapeEcmaScript(String) returns a string with unicode characters escaped using the \u notation.
"Art of Beer 🎨 🍺" -> "Art of Beer \u1F3A8 \u1F37A"
Just some basic Methods for that (inspired from native2ascii tool):
/**
* Encode a String like äöü to \u00e4\u00f6\u00fc
*
* #param text
* #return
*/
public String native2ascii(String text) {
if (text == null)
return text;
StringBuilder sb = new StringBuilder();
for (char ch : text.toCharArray()) {
sb.append(native2ascii(ch));
}
return sb.toString();
}
/**
* Encode a Character like ä to \u00e4
*
* #param ch
* #return
*/
public String native2ascii(char ch) {
if (ch > '\u007f') {
StringBuilder sb = new StringBuilder();
// write \udddd
sb.append("\\u");
StringBuffer hex = new StringBuffer(Integer.toHexString(ch));
hex.reverse();
int length = 4 - hex.length();
for (int j = 0; j < length; j++) {
hex.append('0');
}
for (int j = 0; j < 4; j++) {
sb.append(hex.charAt(3 - j));
}
return sb.toString();
} else {
return Character.toString(ch);
}
}
There is an Open Source java library MgntUtils that has a Utility that converts Strings to unicode sequence and vise versa:
result = "Hello World";
result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
System.out.println(result);
result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
System.out.println(result);
The output of this code is:
\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
Hello World
The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc
Here is javadoc for the class StringUnicodeEncoderDecoder
You could probably hack if from this JavaScript code:
/* convert 🙌 to \uD83D\uDE4C */
function text_to_unicode(string) {
'use strict';
function is_whitespace(c) { return 9 === c || 10 === c || 13 === c || 32 === c; }
function left_pad(string) { return Array(4).concat(string).join('0').slice(-1 * Math.max(4, string.length)); }
string = string.split('').map(function(c){ return "\\u" + left_pad(c.charCodeAt(0).toString(16).toUpperCase()); }).join('');
return string;
}
/* convert \uD83D\uDE4C to 🙌 */
function unicode_to_text(string) {
var prefix = "\\\\u"
, regex = new RegExp(prefix + "([\da-f]{4})","ig")
;
string = string.replace(regex, function(match, backtrace1){
return String.fromCharCode( parseInt(backtrace1, 16) )
});
return string;
}
source: iCompile - Yet Another JavaScript Unicode Encode/Decode
this type name is Decode/Unescape Unicode.
this site link online convertor.

Inserting multiple values in one row

I need to insert data from my parsed XML file to mySQL table. Problem is that I have few attributes and don't know how to insert them in one row. I tried with updateString but it writes only last attribute.
Here is example from XML file:
<Tr rn=\"999999999999999\" vr=\"T\" sSpre=\"S\" reg=\"P\" dSpre=\"2010-09-30\" dOdprt=\"2000-01-01\" iban=\"SI56\" eno=\"R\" vir=\"R\" maticnaPps=\"00000000\"><Imetnik davcna=\"0000000\" matSub=\"0000000\" drz=\"705\"><PopolnoIme>xxx</PopolnoIme><KratkoIme>xxx</KratkoIme><Naslov sifTipNaslova=\"01\" sifObcina=\"039\" sifPosta=\"1303\" sifUlica=\"0000\" sifNaselje=\"059\" stHisna=\"027\" sifHsmid=\"11694551\"><Obcina>xxx</Obcina><Posta>xxx</Posta><Ulica>xxx</Ulica><Naselje>xxx</Naselje></Naslov></Imetnik></Tr>
This is scratch from my java program that I used for writing in mySQL table.
if (myWorkLine.substring(0,4).equals(Tr)) {
uprs.afterLast();
uprs.moveToInsertRow();
if (myWorkLine.contains(Tr)) {
myWorkLine = myWorkLine.substring(myWorkLine.indexOf(Tr)+4);
while (!myWorkLine.substring(0,1).equals("<")) {
myTag = myWorkLine.substring(0,myWorkLine.indexOf("="));
myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2);
myValue = myWorkLine.substring(0,myWorkLine.indexOf("\""));
myWorkLine = myWorkLine.substring(myWorkLine.indexOf("\"")+2);
uprs.updateString("Tr",myTag + " " + myValue);
if (myWorkLine.substring(0,myWorkLine.indexOf("\">")).indexOf(">") > 0)
break;
}
}
So once again, I need that in MySQL table column Tr contains attributes rn value, vr value, sSpre value,...
Thanks in advance.
P.S.: Please don't ask why I'm parsing XML file by this method, I had to do it that way. :)
Your code will repeatedly replace the "Tr" column with your concatenation of tag + " " + value so it'll only be the last one that goes in. Don't you perhaps want the different tags to go in different columns? Or maybe you need to continue concatenating and only call updateString at the end.
Could you post the desired table row for the given XML? That should help in determining what you are trying to achieve.
For example, if you just want to append them:
StringBuffer tr = new StringBuffer();
while (!myWorkLine.substring(0,1).equals("<")) {
myTag = myWorkLine.substring(0,myWorkLine.indexOf("="));
myWorkLine = myWorkLine.substring(myWorkLine.indexOf("=")+2);
myValue = myWorkLine.substring(0,myWorkLine.indexOf("\""));
myWorkLine = myWorkLine.substring(myWorkLine.indexOf("\"")+2);
tr.append(myTag + " " + myValue).append(",");
if (myWorkLine.substring(0,myWorkLine.indexOf("\">")).indexOf(">") > 0)
break;
}
if (tr.length() > 0) {
tr.deleteCharAt(tr.length()-1); // get rid of last comma
}
uprs.updateString("Tr",tr.toString());

Categories