我正在使用名为Mirth(版本3.6)的产品来执行转换代码。它似乎没有交互式调试器,因此谁能解释为什么目标转换中的以下代码仅从IF语句而不是SWITCH语句记录?可以在Eclipse中运行的常规Java上完美运行,但是此代码是在Mirth使用的JavaScript引擎(如果我没记错的话,是Rhino)内执行的。
for each (node in msg['PID'].children())
{
if(node.name() == "PID.3")
{
logger.info("IF Succeeded");
}
switch(node.name())
{
case "PID.3":
logger.info("SWITCH Succeeded"); // This line never logs
break;
}
}
答案 0 :(得分:1)
这是Mirth Connect的常见问题,因为它使用Rhino作为引擎(使用Java和JavaScript)。 Switch语句不适用于Java String对象 简单的解决方案是通过将空字符串连接到可疑字符串(即str +'')来将任何未知的字符串类型转换为本地JS字符串
// See in action
for each (node in msg['PID'].children())
{
var nodeName = node.name();
if (nodeName != "PID.3")
{
continue;
}
var type = typeof(nodeName);
logger.debug('NodeName: ' + nodeName + '; Type: ' + type);
if (type == 'object') {
logger.debug('Type is object');
if (nodeName instanceof String) {
logger.debug('Object is Java String instance');
} else {
logger.debug('Object is not Java String instance');
}
} else if (type === 'string') {
logger.debug('Type is JS string.');
} else {
logger.debug('Type is not object or string. Type: ' + type);
}
// Works with String object or JS string
if (nodeName == "PID.3")
{
logger.info("IF Succeeded");
}
// Only works with JS string
switch (nodeName)
{
case "PID.3":
logger.info("SWITCH Succeeded"); // This line never logs
break;
default:
logger.info("SWITCH not matched: " + nodeName);
break;
}
logger.debug('Convert to JS string');
nodeName = nodeName + '';
type = typeof(nodeName);
logger.debug('Converted Type: ' + type);
if (type == 'object') {
logger.debug('Converted Type is object');
if (nodeName instanceof String) {
logger.debug('Converted Object is String instance');
} else if (nodeName instanceof string) {
logger.debug('Converted Object is string instance');
} else {
logger.debug('Converted Object is not Java String instance');
}
} else if (type === 'string') {
logger.debug('Converted Type is JS string.');
} else {
logger.debug('Converted Type is not object or string. Type: ' + type);
}
switch(nodeName)
{
case "PID.3":
logger.info("SWITCH with js string Succeeded");
break;
default:
logger.info("SWITCH with js string not matched: " + nodeName);
break;
}
break;
}