Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Amila Maha Arachchi
On Wed, May 11, 2011 at 10:14 PM, Afkham Azeez  wrote:

> no'
>
> try, *text = text.replaceAll("\\{" + key + "\\}", *
> Matcher.quoteReplacement(
> *entry.getValue()));*
>

It works. Sorry for not understanding it. Thanks.

>
> On Wed, May 11, 2011 at 10:09 PM, Amila Maha Arachchi wrote:
>
>>
>>
>> On Wed, May 11, 2011 at 9:51 PM, Afkham Azeez  wrote:
>>
>>> Cool. That should work with replaceAll.
>>
>>
>> I am a little confused. Does this mean ok to go with *String.replace* ?
>>
>>
>>>
>>> On Wed, May 11, 2011 at 9:44 PM, Sadeep Jayasumana wrote:
>>>
 Hi,

 Quoting from [1] - *Note that backslashes (\) and dollar signs ($) in
 the replacement string may cause the results to be different than if it 
 were
 being treated as a literal replacement string; see Matcher.replaceAll. Use
 Matcher.quoteReplacement(java.lang.String) to suppress the special meaning
 of these characters, if desired.*

 [1] 
 http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,
 java.lang.String)


 
 Thanks,
 Sadeep


 On Wed, May 11, 2011 at 9:40 PM, Amila Maha Arachchi 
 wrote:

> Found this in the internet.
>
>
> http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html
>
>
> On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi 
> wrote:
>
>>
>>
>> On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:
>>
>>> replaceAll takes a regex as the first parameter. The second parameter
>>> is just a normal String. So, unless the key contains special characters,
>>> this cannot fail. Also, the replace method takes two characters as
>>> parameters, or 2 CharSequences. So, replace & replaceAll have different
>>> behaviors.
>>
>>
>> This failed when the second string has special characters.
>>
>>>
>>>
>>> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi <
>>> ami...@wso2.com> wrote:
>>>
 Hi,

 Theres this code segment in org.wso2.carbon.email.sender component.

 public static String replacePlaceHolders(String text, Map>>> String> userParameters) {
 if (userParameters != null) {
 for (Map.Entry entry :
 userParameters.entrySet()) {
 String key = entry.getKey();
 *text = text.replaceAll("\\{" + key + "\\}",
 entry.getValue());*
 }
 }
 return text;
 }


 Above String.replaceAll() method throws exceptions when there are
 special characters in the string which is being replaced with. i.e. In 
 the
 above sample it is entry.getValue(). An example is the $ sign. This 
 can be
 avoided by using \\$. But it is a headache.

>>>
>>> Can you give us an example for key and entryValue, and if possible
>>> the exception stacktrace?
>>>
>>
>> key: subscription-charges
>> entryValues: Multitenancy Medium $50
>>
>> Here is the stacktrace.
>>
>> [2011-05-11 19:56:42,999] ERROR
>> {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
>> sending the bill for the customer. customer: d.com, invoice id:19
>> java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it
>> breaks at $50. Thats why the 5 is there.If the values was $10, it would 
>> say
>> No group 1.*
>> at java.util.regex.Matcher.group(Matcher.java:470)
>> at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
>> at java.util.regex.Matcher.replaceAll(Matcher.java:813)
>> at java.lang.String.replaceAll(String.java:2189)
>> at
>> org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
>> at
>> org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
>> at
>> org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
>> at
>> org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
>> at
>> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
>> at
>> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
>> at
>> org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
>> at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
>> at
>> org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
>>
>>
>>>
>>

Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Sadeep Jayasumana
Hi,

On Wed, May 11, 2011 at 10:09 PM, Amila Maha Arachchi wrote:

>
>
> On Wed, May 11, 2011 at 9:51 PM, Afkham Azeez  wrote:
>
>> Cool. That should work with replaceAll.
>
>
> I am a little confused. Does this mean ok to go with *String.replace* ?
>

I guess so. Java API documentation says that replaceAll() will not give what
we expect if the second argument contains backslashes or dollar signs.

Thanks,
Sadeep


>

>>
>> On Wed, May 11, 2011 at 9:44 PM, Sadeep Jayasumana wrote:
>>
>>> Hi,
>>>
>>> Quoting from [1] - *Note that backslashes (\) and dollar signs ($) in
>>> the replacement string may cause the results to be different than if it were
>>> being treated as a literal replacement string; see Matcher.replaceAll. Use
>>> Matcher.quoteReplacement(java.lang.String) to suppress the special meaning
>>> of these characters, if desired.*
>>>
>>> [1] 
>>> http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,
>>> java.lang.String)
>>>
>>>
>>> 
>>> Thanks,
>>> Sadeep
>>>
>>>
>>> On Wed, May 11, 2011 at 9:40 PM, Amila Maha Arachchi wrote:
>>>
 Found this in the internet.


 http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html


 On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi 
 wrote:

>
>
> On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:
>
>> replaceAll takes a regex as the first parameter. The second parameter
>> is just a normal String. So, unless the key contains special characters,
>> this cannot fail. Also, the replace method takes two characters as
>> parameters, or 2 CharSequences. So, replace & replaceAll have different
>> behaviors.
>
>
> This failed when the second string has special characters.
>
>>
>>
>> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi > > wrote:
>>
>>> Hi,
>>>
>>> Theres this code segment in org.wso2.carbon.email.sender component.
>>>
>>> public static String replacePlaceHolders(String text, Map>> String> userParameters) {
>>> if (userParameters != null) {
>>> for (Map.Entry entry :
>>> userParameters.entrySet()) {
>>> String key = entry.getKey();
>>> *text = text.replaceAll("\\{" + key + "\\}",
>>> entry.getValue());*
>>> }
>>> }
>>> return text;
>>> }
>>>
>>>
>>> Above String.replaceAll() method throws exceptions when there are
>>> special characters in the string which is being replaced with. i.e. In 
>>> the
>>> above sample it is entry.getValue(). An example is the $ sign. This can 
>>> be
>>> avoided by using \\$. But it is a headache.
>>>
>>
>> Can you give us an example for key and entryValue, and if possible the
>> exception stacktrace?
>>
>
> key: subscription-charges
> entryValues: Multitenancy Medium $50
>
> Here is the stacktrace.
>
> [2011-05-11 19:56:42,999] ERROR
> {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
> sending the bill for the customer. customer: d.com, invoice id:19
> java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it breaks
> at $50. Thats why the 5 is there.If the values was $10, it would say No
> group 1.*
> at java.util.regex.Matcher.group(Matcher.java:470)
> at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
> at java.util.regex.Matcher.replaceAll(Matcher.java:813)
> at java.lang.String.replaceAll(String.java:2189)
> at
> org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
> at
> org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
> at
> org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
> at
> org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
> at
> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
> at
> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
> at
> org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
> at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
> at
> org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
>
>
>>
>>
>>>
>>> Instead, String.replace() can be used and* it too replaces all the
>>> occurrences* of the string being replaced. i.e. We can use
>>>
>>> *text = text.replace("{" + key + "}", entry.getValue());
>>>
>>> *Therefore, is it ok that I change the above method with the latter
>>> mentioned one.
>>

Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Afkham Azeez
no'

try, *text = text.replaceAll("\\{" + key + "\\}", *
Matcher.quoteReplacement(
*entry.getValue()));*

On Wed, May 11, 2011 at 10:09 PM, Amila Maha Arachchi wrote:

>
>
> On Wed, May 11, 2011 at 9:51 PM, Afkham Azeez  wrote:
>
>> Cool. That should work with replaceAll.
>
>
> I am a little confused. Does this mean ok to go with *String.replace* ?
>
>
>>
>> On Wed, May 11, 2011 at 9:44 PM, Sadeep Jayasumana wrote:
>>
>>> Hi,
>>>
>>> Quoting from [1] - *Note that backslashes (\) and dollar signs ($) in
>>> the replacement string may cause the results to be different than if it were
>>> being treated as a literal replacement string; see Matcher.replaceAll. Use
>>> Matcher.quoteReplacement(java.lang.String) to suppress the special meaning
>>> of these characters, if desired.*
>>>
>>> [1] 
>>> http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,
>>> java.lang.String)
>>>
>>>
>>> 
>>> Thanks,
>>> Sadeep
>>>
>>>
>>> On Wed, May 11, 2011 at 9:40 PM, Amila Maha Arachchi wrote:
>>>
 Found this in the internet.


 http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html


 On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi 
 wrote:

>
>
> On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:
>
>> replaceAll takes a regex as the first parameter. The second parameter
>> is just a normal String. So, unless the key contains special characters,
>> this cannot fail. Also, the replace method takes two characters as
>> parameters, or 2 CharSequences. So, replace & replaceAll have different
>> behaviors.
>
>
> This failed when the second string has special characters.
>
>>
>>
>> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi > > wrote:
>>
>>> Hi,
>>>
>>> Theres this code segment in org.wso2.carbon.email.sender component.
>>>
>>> public static String replacePlaceHolders(String text, Map>> String> userParameters) {
>>> if (userParameters != null) {
>>> for (Map.Entry entry :
>>> userParameters.entrySet()) {
>>> String key = entry.getKey();
>>> *text = text.replaceAll("\\{" + key + "\\}",
>>> entry.getValue());*
>>> }
>>> }
>>> return text;
>>> }
>>>
>>>
>>> Above String.replaceAll() method throws exceptions when there are
>>> special characters in the string which is being replaced with. i.e. In 
>>> the
>>> above sample it is entry.getValue(). An example is the $ sign. This can 
>>> be
>>> avoided by using \\$. But it is a headache.
>>>
>>
>> Can you give us an example for key and entryValue, and if possible the
>> exception stacktrace?
>>
>
> key: subscription-charges
> entryValues: Multitenancy Medium $50
>
> Here is the stacktrace.
>
> [2011-05-11 19:56:42,999] ERROR
> {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
> sending the bill for the customer. customer: d.com, invoice id:19
> java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it breaks
> at $50. Thats why the 5 is there.If the values was $10, it would say No
> group 1.*
> at java.util.regex.Matcher.group(Matcher.java:470)
> at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
> at java.util.regex.Matcher.replaceAll(Matcher.java:813)
> at java.lang.String.replaceAll(String.java:2189)
> at
> org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
> at
> org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
> at
> org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
> at
> org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
> at
> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
> at
> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
> at
> org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
> at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
> at
> org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
>
>
>>
>>
>>>
>>> Instead, String.replace() can be used and* it too replaces all the
>>> occurrences* of the string being replaced. i.e. We can use
>>>
>>> *text = text.replace("{" + key + "}", entry.getValue());
>>>
>>> *Therefore, is it ok that I change the above metho

Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Amila Maha Arachchi
On Wed, May 11, 2011 at 9:51 PM, Afkham Azeez  wrote:

> Cool. That should work with replaceAll.


I am a little confused. Does this mean ok to go with *String.replace* ?


>
> On Wed, May 11, 2011 at 9:44 PM, Sadeep Jayasumana wrote:
>
>> Hi,
>>
>> Quoting from [1] - *Note that backslashes (\) and dollar signs ($) in the
>> replacement string may cause the results to be different than if it were
>> being treated as a literal replacement string; see Matcher.replaceAll. Use
>> Matcher.quoteReplacement(java.lang.String) to suppress the special meaning
>> of these characters, if desired.*
>>
>> [1] 
>> http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,
>> java.lang.String)
>>
>>
>> 
>> Thanks,
>> Sadeep
>>
>>
>> On Wed, May 11, 2011 at 9:40 PM, Amila Maha Arachchi wrote:
>>
>>> Found this in the internet.
>>>
>>>
>>> http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html
>>>
>>>
>>> On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi wrote:
>>>


 On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:

> replaceAll takes a regex as the first parameter. The second parameter
> is just a normal String. So, unless the key contains special characters,
> this cannot fail. Also, the replace method takes two characters as
> parameters, or 2 CharSequences. So, replace & replaceAll have different
> behaviors.


 This failed when the second string has special characters.

>
>
> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi 
> wrote:
>
>> Hi,
>>
>> Theres this code segment in org.wso2.carbon.email.sender component.
>>
>> public static String replacePlaceHolders(String text, Map> String> userParameters) {
>> if (userParameters != null) {
>> for (Map.Entry entry :
>> userParameters.entrySet()) {
>> String key = entry.getKey();
>> *text = text.replaceAll("\\{" + key + "\\}",
>> entry.getValue());*
>> }
>> }
>> return text;
>> }
>>
>>
>> Above String.replaceAll() method throws exceptions when there are
>> special characters in the string which is being replaced with. i.e. In 
>> the
>> above sample it is entry.getValue(). An example is the $ sign. This can 
>> be
>> avoided by using \\$. But it is a headache.
>>
>
> Can you give us an example for key and entryValue, and if possible the
> exception stacktrace?
>

 key: subscription-charges
 entryValues: Multitenancy Medium $50

 Here is the stacktrace.

 [2011-05-11 19:56:42,999] ERROR
 {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
 sending the bill for the customer. customer: d.com, invoice id:19
 java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it breaks
 at $50. Thats why the 5 is there.If the values was $10, it would say No
 group 1.*
 at java.util.regex.Matcher.group(Matcher.java:470)
 at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
 at java.util.regex.Matcher.replaceAll(Matcher.java:813)
 at java.lang.String.replaceAll(String.java:2189)
 at
 org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
 at
 org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
 at
 org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
 at
 org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
 at
 org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
 at
 org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
 at
 org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
 at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
 at
 org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)


>
>
>>
>> Instead, String.replace() can be used and* it too replaces all the
>> occurrences* of the string being replaced. i.e. We can use
>>
>> *text = text.replace("{" + key + "}", entry.getValue());
>>
>> *Therefore, is it ok that I change the above method with the latter
>> mentioned one.
>>
>> Thanks,
>> AmilaM.
>> *
>> *
>> ___
>> Carbon-dev mailing list
>> Carbon-dev@wso2.org
>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>
>>
>
>
> --
> *Afkham Azeez*
> Director of Architecture; WSO2, Inc.; http://wso2.com
> Member; Apache Software Fo

Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Afkham Azeez
Cool. That should work with replaceAll.

On Wed, May 11, 2011 at 9:44 PM, Sadeep Jayasumana  wrote:

> Hi,
>
> Quoting from [1] - *Note that backslashes (\) and dollar signs ($) in the
> replacement string may cause the results to be different than if it were
> being treated as a literal replacement string; see Matcher.replaceAll. Use
> Matcher.quoteReplacement(java.lang.String) to suppress the special meaning
> of these characters, if desired.*
>
> [1] 
> http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,
> java.lang.String)
>
>
> 
> Thanks,
> Sadeep
>
>
> On Wed, May 11, 2011 at 9:40 PM, Amila Maha Arachchi wrote:
>
>> Found this in the internet.
>>
>>
>> http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html
>>
>>
>> On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi wrote:
>>
>>>
>>>
>>> On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:
>>>
 replaceAll takes a regex as the first parameter. The second parameter is
 just a normal String. So, unless the key contains special characters, this
 cannot fail. Also, the replace method takes two characters as parameters, 
 or
 2 CharSequences. So, replace & replaceAll have different behaviors.
>>>
>>>
>>> This failed when the second string has special characters.
>>>


 On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi 
 wrote:

> Hi,
>
> Theres this code segment in org.wso2.carbon.email.sender component.
>
> public static String replacePlaceHolders(String text, Map String> userParameters) {
> if (userParameters != null) {
> for (Map.Entry entry :
> userParameters.entrySet()) {
> String key = entry.getKey();
> *text = text.replaceAll("\\{" + key + "\\}",
> entry.getValue());*
> }
> }
> return text;
> }
>
>
> Above String.replaceAll() method throws exceptions when there are
> special characters in the string which is being replaced with. i.e. In the
> above sample it is entry.getValue(). An example is the $ sign. This can be
> avoided by using \\$. But it is a headache.
>

 Can you give us an example for key and entryValue, and if possible the
 exception stacktrace?

>>>
>>> key: subscription-charges
>>> entryValues: Multitenancy Medium $50
>>>
>>> Here is the stacktrace.
>>>
>>> [2011-05-11 19:56:42,999] ERROR
>>> {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
>>> sending the bill for the customer. customer: d.com, invoice id:19
>>> java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it breaks
>>> at $50. Thats why the 5 is there.If the values was $10, it would say No
>>> group 1.*
>>> at java.util.regex.Matcher.group(Matcher.java:470)
>>> at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
>>> at java.util.regex.Matcher.replaceAll(Matcher.java:813)
>>> at java.lang.String.replaceAll(String.java:2189)
>>> at
>>> org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
>>> at
>>> org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
>>> at
>>> org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
>>> at
>>> org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
>>> at
>>> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
>>> at
>>> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
>>> at
>>> org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
>>> at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
>>> at
>>> org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
>>>
>>>


>
> Instead, String.replace() can be used and* it too replaces all the
> occurrences* of the string being replaced. i.e. We can use
>
> *text = text.replace("{" + key + "}", entry.getValue());
>
> *Therefore, is it ok that I change the above method with the latter
> mentioned one.
>
> Thanks,
> AmilaM.
> *
> *
> ___
> Carbon-dev mailing list
> Carbon-dev@wso2.org
> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>
>


 --
 *Afkham Azeez*
 Director of Architecture; WSO2, Inc.; http://wso2.com
 Member; Apache Software Foundation; http://www.apache.org/
 * **
 email: **az...@wso2.com* * cell: +94 77 3320919
 blog: **http://blog.afkham.org* *
 twitter: 
 **http://twitter.com/afkham_azeez*
 *
 linked-

Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Sadeep Jayasumana
Hi,

Quoting from [1] - *Note that backslashes (\) and dollar signs ($) in the
replacement string may cause the results to be different than if it were
being treated as a literal replacement string; see Matcher.replaceAll. Use
Matcher.quoteReplacement(java.lang.String) to suppress the special meaning
of these characters, if desired.*

[1] 
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String,
java.lang.String)

Thanks,
Sadeep

On Wed, May 11, 2011 at 9:40 PM, Amila Maha Arachchi wrote:

> Found this in the internet.
>
>
> http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html
>
>
> On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi wrote:
>
>>
>>
>> On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:
>>
>>> replaceAll takes a regex as the first parameter. The second parameter is
>>> just a normal String. So, unless the key contains special characters, this
>>> cannot fail. Also, the replace method takes two characters as parameters, or
>>> 2 CharSequences. So, replace & replaceAll have different behaviors.
>>
>>
>> This failed when the second string has special characters.
>>
>>>
>>>
>>> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi wrote:
>>>
 Hi,

 Theres this code segment in org.wso2.carbon.email.sender component.

 public static String replacePlaceHolders(String text, Map>>> String> userParameters) {
 if (userParameters != null) {
 for (Map.Entry entry :
 userParameters.entrySet()) {
 String key = entry.getKey();
 *text = text.replaceAll("\\{" + key + "\\}",
 entry.getValue());*
 }
 }
 return text;
 }


 Above String.replaceAll() method throws exceptions when there are
 special characters in the string which is being replaced with. i.e. In the
 above sample it is entry.getValue(). An example is the $ sign. This can be
 avoided by using \\$. But it is a headache.

>>>
>>> Can you give us an example for key and entryValue, and if possible the
>>> exception stacktrace?
>>>
>>
>> key: subscription-charges
>> entryValues: Multitenancy Medium $50
>>
>> Here is the stacktrace.
>>
>> [2011-05-11 19:56:42,999] ERROR
>> {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
>> sending the bill for the customer. customer: d.com, invoice id:19
>> java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it breaks at
>> $50. Thats why the 5 is there.If the values was $10, it would say No group
>> 1.*
>> at java.util.regex.Matcher.group(Matcher.java:470)
>> at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
>> at java.util.regex.Matcher.replaceAll(Matcher.java:813)
>> at java.lang.String.replaceAll(String.java:2189)
>> at
>> org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
>> at
>> org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
>> at
>> org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
>> at
>> org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
>> at
>> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
>> at
>> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
>> at
>> org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
>> at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
>> at
>> org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
>>
>>
>>>
>>>

 Instead, String.replace() can be used and* it too replaces all the
 occurrences* of the string being replaced. i.e. We can use

 *text = text.replace("{" + key + "}", entry.getValue());

 *Therefore, is it ok that I change the above method with the latter
 mentioned one.

 Thanks,
 AmilaM.
 *
 *
 ___
 Carbon-dev mailing list
 Carbon-dev@wso2.org
 http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev


>>>
>>>
>>> --
>>> *Afkham Azeez*
>>> Director of Architecture; WSO2, Inc.; http://wso2.com
>>> Member; Apache Software Foundation; http://www.apache.org/
>>> * **
>>> email: **az...@wso2.com* * cell: +94 77 3320919
>>> blog: **http://blog.afkham.org* *
>>> twitter: **http://twitter.com/afkham_azeez*
>>> *
>>> linked-in: **http://lk.linkedin.com/in/afkhamazeez*
>>> *
>>> *
>>> *Lean . Enterprise . Middleware*
>>>
>>>
>>> ___
>>> Stratos-dev mailing list
>>> stratos-...@wso2.org
>>> https://wso2.org/cgi-bin/mailman/listinfo/stratos-dev
>>>
>>>

Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Amila Maha Arachchi
Found this in the internet.

http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html

On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi wrote:

>
>
> On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:
>
>> replaceAll takes a regex as the first parameter. The second parameter is
>> just a normal String. So, unless the key contains special characters, this
>> cannot fail. Also, the replace method takes two characters as parameters, or
>> 2 CharSequences. So, replace & replaceAll have different behaviors.
>
>
> This failed when the second string has special characters.
>
>>
>>
>> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi wrote:
>>
>>> Hi,
>>>
>>> Theres this code segment in org.wso2.carbon.email.sender component.
>>>
>>> public static String replacePlaceHolders(String text, Map
>>> userParameters) {
>>> if (userParameters != null) {
>>> for (Map.Entry entry :
>>> userParameters.entrySet()) {
>>> String key = entry.getKey();
>>> *text = text.replaceAll("\\{" + key + "\\}",
>>> entry.getValue());*
>>> }
>>> }
>>> return text;
>>> }
>>>
>>>
>>> Above String.replaceAll() method throws exceptions when there are special
>>> characters in the string which is being replaced with. i.e. In the above
>>> sample it is entry.getValue(). An example is the $ sign. This can be avoided
>>> by using \\$. But it is a headache.
>>>
>>
>> Can you give us an example for key and entryValue, and if possible the
>> exception stacktrace?
>>
>
> key: subscription-charges
> entryValues: Multitenancy Medium $50
>
> Here is the stacktrace.
>
> [2011-05-11 19:56:42,999] ERROR
> {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
> sending the bill for the customer. customer: d.com, invoice id:19
> java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it breaks at
> $50. Thats why the 5 is there.If the values was $10, it would say No group
> 1.*
> at java.util.regex.Matcher.group(Matcher.java:470)
> at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
> at java.util.regex.Matcher.replaceAll(Matcher.java:813)
> at java.lang.String.replaceAll(String.java:2189)
> at
> org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
> at
> org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
> at
> org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
> at
> org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
> at
> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
> at
> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
> at
> org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
> at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
> at
> org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
>
>
>>
>>
>>>
>>> Instead, String.replace() can be used and* it too replaces all the
>>> occurrences* of the string being replaced. i.e. We can use
>>>
>>> *text = text.replace("{" + key + "}", entry.getValue());
>>>
>>> *Therefore, is it ok that I change the above method with the latter
>>> mentioned one.
>>>
>>> Thanks,
>>> AmilaM.
>>> *
>>> *
>>> ___
>>> Carbon-dev mailing list
>>> Carbon-dev@wso2.org
>>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>>
>>>
>>
>>
>> --
>> *Afkham Azeez*
>> Director of Architecture; WSO2, Inc.; http://wso2.com
>> Member; Apache Software Foundation; http://www.apache.org/
>> * **
>> email: **az...@wso2.com* * cell: +94 77 3320919
>> blog: **http://blog.afkham.org* *
>> twitter: **http://twitter.com/afkham_azeez*
>> *
>> linked-in: **http://lk.linkedin.com/in/afkhamazeez*
>> *
>> *
>> *Lean . Enterprise . Middleware*
>>
>>
>> ___
>> Stratos-dev mailing list
>> stratos-...@wso2.org
>> https://wso2.org/cgi-bin/mailman/listinfo/stratos-dev
>>
>>
>
___
Carbon-dev mailing list
Carbon-dev@wso2.org
http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev


Re: [Carbon-dev] [Stratos-dev] String.replace() vs. String.replaceAll()

2011-05-11 Thread Amila Maha Arachchi
On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez  wrote:

> replaceAll takes a regex as the first parameter. The second parameter is
> just a normal String. So, unless the key contains special characters, this
> cannot fail. Also, the replace method takes two characters as parameters, or
> 2 CharSequences. So, replace & replaceAll have different behaviors.


This failed when the second string has special characters.

>
>
> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi wrote:
>
>> Hi,
>>
>> Theres this code segment in org.wso2.carbon.email.sender component.
>>
>> public static String replacePlaceHolders(String text, Map
>> userParameters) {
>> if (userParameters != null) {
>> for (Map.Entry entry :
>> userParameters.entrySet()) {
>> String key = entry.getKey();
>> *text = text.replaceAll("\\{" + key + "\\}",
>> entry.getValue());*
>> }
>> }
>> return text;
>> }
>>
>>
>> Above String.replaceAll() method throws exceptions when there are special
>> characters in the string which is being replaced with. i.e. In the above
>> sample it is entry.getValue(). An example is the $ sign. This can be avoided
>> by using \\$. But it is a headache.
>>
>
> Can you give us an example for key and entryValue, and if possible the
> exception stacktrace?
>

key: subscription-charges
entryValues: Multitenancy Medium $50

Here is the stacktrace.

[2011-05-11 19:56:42,999] ERROR
{org.wso2.carbon.billing.core.handlers.EmailSendingHandler} -  Error in
sending the bill for the customer. customer: d.com, invoice id:19
java.lang.IndexOutOfBoundsException: No group 5   *<== Here, it breaks at
$50. Thats why the 5 is there.If the values was $10, it would say No group
1.*
at java.util.regex.Matcher.group(Matcher.java:470)
at java.util.regex.Matcher.appendReplacement(Matcher.java:737)
at java.util.regex.Matcher.replaceAll(Matcher.java:813)
at java.lang.String.replaceAll(String.java:2189)
at
org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97)
at
org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106)
at
org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55)
at
org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82)
at
org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77)
at
org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59)
at
org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)


>
>
>>
>> Instead, String.replace() can be used and* it too replaces all the
>> occurrences* of the string being replaced. i.e. We can use
>>
>> *text = text.replace("{" + key + "}", entry.getValue());
>>
>> *Therefore, is it ok that I change the above method with the latter
>> mentioned one.
>>
>> Thanks,
>> AmilaM.
>> *
>> *
>> ___
>> Carbon-dev mailing list
>> Carbon-dev@wso2.org
>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
>>
>>
>
>
> --
> *Afkham Azeez*
> Director of Architecture; WSO2, Inc.; http://wso2.com
> Member; Apache Software Foundation; http://www.apache.org/
> * **
> email: **az...@wso2.com* * cell: +94 77 3320919
> blog: **http://blog.afkham.org* *
> twitter: **http://twitter.com/afkham_azeez*
> *
> linked-in: **http://lk.linkedin.com/in/afkhamazeez*
> *
> *
> *Lean . Enterprise . Middleware*
>
>
> ___
> Stratos-dev mailing list
> stratos-...@wso2.org
> https://wso2.org/cgi-bin/mailman/listinfo/stratos-dev
>
>
___
Carbon-dev mailing list
Carbon-dev@wso2.org
http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev