web stats
Puzzling Javascript Error in Regexp on Mirth 1.8.2.4472 - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 10-13-2010, 01:44 PM
bdilworth bdilworth is offline
OBX.2 Kenobi
 
Join Date: Aug 2006
Location: Toledo, Ohio USA
Posts: 79
bdilworth
Default Puzzling Javascript Error in Regexp on Mirth 1.8.2.4472

Hi All!

I have a file called testfile.dat in our appliance's /folders/outbox folder that contains one line of data: "none\n". The file size is reported as 5 bytes and includes the 0x0A (newline) character.

I have a simple javascript script that reads the file and uses a regular expression in an attempt to remove the 0x0a newline character after which it compares that value to one extracted from the mirthdb database in the channel's source area. Here is the script:

Code:
var thevalue = msg['max'].toString();
var cont = FileUtil.read('/folders/outbox/testfile.dat');
var trimmed = cont.replace(/\s+$/g, "");
if (trimmed == "none")
{
    connectorMap.put('ourvalue',thevalue);
}
else if (trimmed == thevalue)
{
    connectorMap.put('ourvalue',trimmed + "Equal" + thevalue);
}
else
{
    connectorMap.put('ourvalue',trimmed + "NotEqual" + thevalue);
}
I receive the following error:

Code:
FAILURE: Error evaluating transformer
MirthJavascriptTransformerException

CHANNEL:
test-sql
CONNECTOR:
Destination 1
SCRIPT SOURCE:

SOURCE CODE:

7: function doFilter() { phase = 'filter'; return true; }function doTransform() { phase = 'transformer'; logger = Packages.org.apache.log4j.Logger.getLogger(phase);
8: 
9: 
10: var thevalue = msg['max'].toString();
11: var cont = FileUtil.read('/folders/outbox/testfile.dat');
12: var trimmed = cont.replace(/\s+$/g, "");
13: if (trimmed == "none")
14: {
15:     connectorMap.put('ourvalue',thevalue);
16: }
LINE NUMBER:
12
DETAILS:
The choice of Java constructor replace matching JavaScript argument types (function,string) is ambiguous; candidate constructors are: 
    class java.lang.String replace(char,char)
    class java.lang.String replace(java.lang.CharSequence,java.lang.CharSequence)
I'm assuming something is incorrect with my regular expression. If the regexp is OK does Mirth 1.8.2.4472 have any issues with regular expressions in a string.replace?

Bob D.
Reply With Quote
  #2  
Old 10-13-2010, 02:51 PM
apatwa apatwa is offline
OBX.2 Kenobi
 
Join Date: May 2010
Location: Nashville, TN
Posts: 94
apatwa is on a distinguished road
Default

Try:

var cont = new String(FileUtil.read('/folders/outbox/testfile.dat'));

or

cont.toString().replace(/\s+$/g, "");
Reply With Quote
  #3  
Old 10-14-2010, 07:18 AM
bdilworth bdilworth is offline
OBX.2 Kenobi
 
Join Date: Aug 2006
Location: Toledo, Ohio USA
Posts: 79
bdilworth
Default Re: Puzzling Javascript Error in Regexp on Mirth 1.8.2.4472

Quote:
Originally Posted by apatwa View Post
Try:

var cont = new String(FileUtil.read('/folders/outbox/testfile.dat'));

or

cont.toString().replace(/\s+$/g, "");
Thanks for the suggestions! I tried them both together as well as separately with the same results: I receive the same runtime channel error I provided in my original posting.

To make absolutely sure that my regex is correct I wrote a little stand-alone Perl script on my Linux box to do the same thing and this regex works fine: s/\s*$//g; However, when I used the same syntax in the javascript step like this: cont.replace(/\s*$/g, ""); I still receive the same error. For grins I also tried: cont.replace(/\s*$//g, ""); and cont.replace(/\s*$/, ""); which also didn't work.

I even tried separating out the regex itself into a variable and using that variable in the string.replace like this:

Code:
var cont = new String(FileUtil.read('/folders/outbox/testfile.dat'));
var patt = "/\s*$/g";
var trimmed = cont.toString().replace(patt, "");
The channel error went away but the the newline remained at the end of the string.

Does anyone out there have a working string.replace that uses a regex they'd be willing to share?

Bob D.

Last edited by bdilworth; 10-14-2010 at 08:10 AM. Reason: corrected fat finger error in code
Reply With Quote
  #4  
Old 10-14-2010, 08:17 AM
Paul Drover Paul Drover is offline
Mirth Newb
 
Join Date: May 2010
Posts: 21
Paul Drover is on a distinguished road
Default

Try

Code:
var trimmed = cont.replace("/\s+$/g", "");
I think the regex needs to be a string. You may also need to escape the '\' with a '\\', i.e.

Code:
var trimmed = cont.replace("/\\s+$/g", "");
Reply With Quote
  #5  
Old 10-14-2010, 08:47 AM
apatwa apatwa is offline
OBX.2 Kenobi
 
Join Date: May 2010
Location: Nashville, TN
Posts: 94
apatwa is on a distinguished road
Default

To handle new lines, you want to do .replace(/\n|\r/g, '');
Reply With Quote
  #6  
Old 10-14-2010, 10:37 AM
bdilworth bdilworth is offline
OBX.2 Kenobi
 
Join Date: Aug 2006
Location: Toledo, Ohio USA
Posts: 79
bdilworth
Default Re: Puzzling Javascript Error in Regexp on Mirth 1.8.2.4472

Thanks again for the new suggestions! I tried them all a variety of ways still to no avail. Here's the variations I tried based on the most recent suggestions. Please note that I did not run all these variations at the same time! :-) Each of the vairations did not error but they also did not remove the newline at the end of the string:

Code:
var cont = FileUtil.read('/folders/outbox/testfile.dat');

var trimmed = cont.replace("/\n$/", '');
var trimmed = cont.replace("/\\n$/", '');
var trimmed = cont.replace("/\\s+$/", '');
var trimmed = cont.replace("/\s+$/", '');
I also discovered that if one does not surround the regex with double quotes it errors off when it runs. Looks like the double quotes around the regex are indeed needed?

For grins I also tried them all in Mirth 1.8.1 with exactly the same results.

Any other thoughts or suggestions or is there some sort of bug in the javascript implementation in Mirth 1.8.1 and 1.8.2?

Bob D.
Reply With Quote
  #7  
Old 10-14-2010, 10:41 AM
dans dans is offline
Mirth Employee
 
Join Date: Apr 2007
Location: Irvine, CA
Posts: 590
dans is an unknown quantity at this point
Default

FileUtil.read returns a java string. The replace function you are trying to use is for a javascript string. You could change your code to this to ensure you have a javascript string:
Code:
var cont = FileUtil.read('/folders/outbox/testfile.dat') + "";
Or you can use the java replace function instead.
__________________
Daniel Svanstedt
Software Engineer
Mirth Corporation

Want professional services, support, and enterprise or virtual appliances? It's all available from the Mirth Corporation:
Mirth Support | Mirth Training | Mirth Appliances | Online Training | Developer Q&A

Don't forget, Mirth Support gives you access to all of our online training videos, and silver support gives you access to developer Q&As!
Reply With Quote
  #8  
Old 10-14-2010, 12:16 PM
bdilworth bdilworth is offline
OBX.2 Kenobi
 
Join Date: Aug 2006
Location: Toledo, Ohio USA
Posts: 79
bdilworth
Default

Quote:
Originally Posted by dans View Post
FileUtil.read returns a java string. The replace function you are trying to use is for a javascript string. You could change your code to this to ensure you have a javascript string:
Code:
var cont = FileUtil.read('/folders/outbox/testfile.dat') + "";
Hi Daniel!

Thank you SO much for the tip!!!! Adding the + "" to the end of the FileUtil.read did the trick. The regular expression worked great once I did that:

Code:
var cont = FileUtil.read('/folders/outbox/testfile.dat') + "";
var trimmed = cont.replace(/\s+$/g, "");
Thanks again!!!!!!!!!!!!!!!!!!

Bob D.
Reply With Quote
  #9  
Old 08-16-2019, 07:35 AM
bassicallychris bassicallychris is offline
Mirth Newb
 
Join Date: Sep 2018
Posts: 20
bassicallychris is on a distinguished road
Default

I don't intend to necro an old thread but I'm curious as to why appending "" to the string is necessary. I'm especially curious because I had a working regex in this function.

Code:
function replaceSingleQuote(object){
	for (property in object) {
		var propertyString = object[property];
		var regex = /\'/g;
		// var subString = "''";
		if (typeof object[property] === "string") {
			object[property] = propertyString.replace(regex,"''");
		}
	}
}
Through doing some refactoring of the code the function changed to this and the regex no longer works without throwing that error:

Code:
function replaceSingleQuote(string){
	/* appending string with "" is necessary to avoid this error: The choice of Java method java.lang.String.replace matching JavaScript argument types (function,string) is ambiguous; candidate methods are: */
	string = string + "";
	var regex = /\'/g;
	return string.replace(regex,"''");
The only thing that I can tell that changed in a meaningful way is that I changed object to string. The function is now used more appropriately as a utility function called by others to reformat a string while they iterate through an object instead of iterating through the object itself.

Thanks in advance for helping me understand this.

Last edited by bassicallychris; 08-16-2019 at 07:46 AM.
Reply With Quote
  #10  
Old 08-16-2019, 12:20 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,107
agermano is on a distinguished road
Default

As the error implies, the java.lang.String.replace and String.prototype.replace methods don't behave in the same way.

In many cases, java and javascript strings can be used interchangeably, with Rhino converting them automatically based on context. Invoking the replace method is one of the cases where it can't do that.

Any time you do string concatenation in Rhino, it will result in a javascript string, even if you are concatenating two Java Strings.

A more explicit way to convert them to the desired type is to do the following:

Code:
function replaceSingleQuote(string){
	// force to javascript string
	string = String(string);
	var regex = /'/g;
	return string.replace(regex,"''"); // returns a javascript string
}
or

Code:
function replaceSingleQuote(string){
	// force to java String
	string = new java.lang.String(string);
	var regex = "'";
	return string.replaceAll(regex,"''"); // returns a java String
}
Reply With Quote
Reply

Tags
javascript, regexp

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 05:10 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation