}
private void buildDebugRegExp(String input, String match) {
String convInput;
String result = "";
RE re = null;
boolean inBracket = false;
boolean escape = false;
String character;
int start = 0, end = 0;
int bracketStart = 0;
debugInfo = new DebugInfo();
debugInfo.setInput(input);
debugInfo.setMatchString(match);
try {
// Replace escaped characters
re = new RE("\\[^.]");
convInput = re.substitute(input, "..");
if (convInput.indexOf('(') == -1) {
for (int i = 0; i < input.length(); i++) {
character = input.substring(i, i + 1);
if (!inBracket) {
re = new RE("[\\[\\{]");
if (re.isMatch(character)) {
bracketStart = result.length();
result += "(" + character;
inBracket = true;
start = i;
} else {
if (!escape) {
bracketStart = result.length();
result += "(";
start = i;
} else {
escape = false;
}
result += character;
if (character.equals("\\")) {
escape = true;
continue;
}
if ((i + 1) < input.length()) {
String nextChar = input.substring(i + 1, i + 2);
re = new RE("[\\*\\+]");
if (re.isMatch(nextChar)) {
result += nextChar;
i++;
}
}
if ((i + 1) < input.length()) {
String nextChar = input.substring(i + 1, i + 2);
re = new RE("[\\?]");
if (re.isMatch(nextChar)) {
result += nextChar;
i++;
}
}
result += ")";
debugInfo.addSubexpressionPosition(start, i + 1);
debugInfo.addBracketPosition(bracketStart, result
.length() - 1);
}
} else {
re = new RE("[\\]\\}]");
if (re.isMatch(character)) {
if ((i + 1) < input.length()) {
String nextChar = input.substring(i + 1, i + 2);
if (nextChar.equals("{")) {
result += character + nextChar;
i++;
continue;
}
re = new RE("[\\+\\*]");
if (re.isMatch(nextChar)) {
result += character + nextChar;
i++;
if ((i + 1) < input.length()) {
nextChar = input
.substring(i + 1, i + 2);
re = new RE("[\\?]");
if (re.isMatch(nextChar)) {
result += nextChar;
i++;
}
}
result += ")";
debugInfo.addSubexpressionPosition(start,
i + 1);
debugInfo.addBracketPosition(bracketStart,
result.length() - 1);
inBracket = false;
} else {
result += character + ")";
debugInfo.addSubexpressionPosition(start,
i + 1);
debugInfo.addBracketPosition(bracketStart,
result.length() - 1);
inBracket = false;
}
}
// If it's the last character
else {
result += character + ")";
debugInfo
.addSubexpressionPosition(start, i + 1);
debugInfo.addBracketPosition(bracketStart,
result.length() - 1);
inBracket = false;
}
} else {
result += character;
}
}
}
} else {
// Handle pre-formatted regexp
//..................................
result = input;
re = new RE("\\((.*?)\\)");
REMatch[] matches = re.getAllMatches(convInput);
for (int i = 0; i < matches.length; i++) {
for (int j = 1; j <= re.getNumSubs(); j++) {
start = matches[i].getStartIndex(j);
end = matches[i].getEndIndex(j);
debugInfo.addSubexpressionPosition(start, end);
debugInfo.addBracketPosition(start, end);
}