return logicalStatement;
}
private String shellToMVEL(final String subStmt, final boolean noShellCall)
{
StringAppender buf = new StringAppender();
boolean stmtStart = true;
boolean openShellCall = false;
boolean scriptOnly = false;
Nest nest = new Nest();
for (int i = 0; i < subStmt.length(); i++)
{
if (stmtStart)
{
while ((i < subStmt.length()) && isWhitespace(subStmt.charAt(i)))
i++;
if (i >= subStmt.length())
{
break;
}
int firstToken = getEndOfToken(subStmt, i);
String tk = subStmt.substring(i, firstToken).trim();
if (!noShellCall && (tk.charAt(0) != '@') && ((firstToken == -1) || !isReservedWord(tk)))
{
buf.append("shell(\"");
openShellCall = true;
}
else
{
scriptOnly = true;
stmtStart = false;
if (tk.charAt(0) == '@')
{
continue;
}
}
stmtStart = false;
}
switch (subStmt.charAt(i))
{
case '\\':
buf.append("\\");
if (nest.isLiteral())
{
buf.append("\\");
}
buf.append(subStmt.charAt(++i));
if (subStmt.charAt(i) == '\\')
{
buf.append("\\");
}
break;
case '\'':
nest.nestSingleQuote();
buf.append(subStmt.charAt(i));
break;
case '"':
nest.nestDoubleQuote();
if (openShellCall)
{
buf.append("\\\"");
}
else
{
buf.append(subStmt.charAt(i));
}
break;
case '(':
if (!nest.isLiteral())
{
nest.bracket++;
}
buf.append(subStmt.charAt(i));
break;
case '{':
buf.append(subStmt.charAt(i));
if (!nest.isLiteral())
{
int start = ++i;
buf.append(shellToMVEL(subStmt.substring(start,
i = balancedCapture(subStmt.toCharArray(), i, '{')), false)).append('}');
}
break;
case '[':
if (!nest.isLiteral())
{
nest.square++;
}
buf.append(subStmt.charAt(i));
break;
case ')':
if (!nest.isLiteral())
{
nest.bracket--;
}
buf.append(subStmt.charAt(i));
break;
case '}':
if (!nest.isLiteral())
{
nest.curly--;
}
buf.append(subStmt.charAt(i));
break;
case ']':
if (!nest.isLiteral())
{
nest.square--;
}
buf.append(subStmt.charAt(i));
break;
case ';':
if (!nest.isLiteral())
{
if (openShellCall)
{
buf.append("\")");
openShellCall = false;
}
stmtStart = true;
scriptOnly = false;
}
buf.append(subStmt.charAt(i));
break;
case '$':
if (!scriptOnly)
{
buf.append("\"+");
int start = ++i;
i = captureToken(i, subStmt.length(), subStmt.toCharArray());
buf.append(subStmt.substring(start, i));
if (i < subStmt.length())
{
buf.append("+\"");
i--;
}
else
{
buf.append(")");
openShellCall = false;
}
}
else
{
if (++i < subStmt.length())
{
buf.append(subStmt.charAt(i));
}
}
break;
default:
buf.append(subStmt.charAt(i));
}
}
if (nest.isLiteral())
{
throw new RuntimeException("unterminated string literal while parsing script");
}
if (nest.isNested())
{
throw new RuntimeException("unterminated nest while parsing script");
}
if (openShellCall)
{
buf.append("\")");
}
return buf.toString();
}