| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.regex; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.LastPositionFinder; |
| 11 |
import net.sf.saxon.trans.SaxonErrorCode; |
| 12 |
import net.sf.saxon.trans.XPathException; |
| 13 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 |
import net.sf.saxon.value.StringValue; |
| 15 |
import net.sf.saxon.z.IntHashMap; |
| 16 |
import net.sf.saxon.z.IntToIntHashMap; |
| 17 |
|
| 18 |
import java.util.ArrayList; |
| 19 |
import java.util.List; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class ARegexIterator implements RegexIterator, LastPositionFinder { |
| 27 |
|
| 28 |
private UnicodeString theString; |
| 29 |
private UnicodeString regex; |
| 30 |
private REMatcher matcher; |
| 31 |
private UnicodeString current; |
| 32 |
private UnicodeString next; |
| 33 |
|
| 34 |
private int prevEnd = 0; |
| 35 |
private IntToIntHashMap nestingTable = null; |
| 36 |
|
| 37 |
|
| 38 |
private boolean skip = false; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public ARegexIterator(UnicodeString string, UnicodeString regex, REMatcher matcher) { |
| 50 |
theString = string; |
| 51 |
this.regex = regex; |
| 52 |
this.matcher = matcher; |
| 53 |
next = null; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public int getLength() throws XPathException { |
| 58 |
ARegexIterator another = new ARegexIterator(theString, regex, new REMatcher(matcher.getProgram())); |
| 59 |
int n = 0; |
| 60 |
while (another.next() != null) { |
| 61 |
n++; |
| 62 |
} |
| 63 |
return n; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public StringValue next() throws XPathException { |
| 73 |
try { |
| 74 |
if (next == null && prevEnd >= 0) { |
| 75 |
|
| 76 |
int searchStart = prevEnd; |
| 77 |
if (skip) { |
| 78 |
|
| 79 |
searchStart++; |
| 80 |
if (searchStart >= theString.uLength()) { |
| 81 |
if (prevEnd < theString.uLength()) { |
| 82 |
current = theString.uSubstring(prevEnd, theString.uLength()); |
| 83 |
next = null; |
| 84 |
} else { |
| 85 |
current = null; |
| 86 |
prevEnd = -1; |
| 87 |
return null; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
if (matcher.match(theString, searchStart)) { |
| 92 |
int start = matcher.getParenStart(0); |
| 93 |
int end = matcher.getParenEnd(0); |
| 94 |
skip = start == end; |
| 95 |
if (prevEnd == start) { |
| 96 |
|
| 97 |
next = null; |
| 98 |
current = theString.uSubstring(start, end); |
| 99 |
prevEnd = end; |
| 100 |
} else { |
| 101 |
|
| 102 |
current = theString.uSubstring(prevEnd, start); |
| 103 |
next = theString.uSubstring(start, end); |
| 104 |
} |
| 105 |
} else { |
| 106 |
|
| 107 |
if (prevEnd < theString.uLength()) { |
| 108 |
current = theString.uSubstring(prevEnd, theString.uLength()); |
| 109 |
next = null; |
| 110 |
} else { |
| 111 |
|
| 112 |
current = null; |
| 113 |
prevEnd = -1; |
| 114 |
return null; |
| 115 |
} |
| 116 |
prevEnd = -1; |
| 117 |
} |
| 118 |
} else { |
| 119 |
|
| 120 |
if (prevEnd >= 0) { |
| 121 |
current = next; |
| 122 |
next = null; |
| 123 |
prevEnd = matcher.getParenEnd(0); |
| 124 |
} else { |
| 125 |
current = null; |
| 126 |
return null; |
| 127 |
} |
| 128 |
} |
| 129 |
return currentStringValue(); |
| 130 |
} catch (StackOverflowError e) { |
| 131 |
XPathException xe = new XPathException("Stack overflow (excessive recursion) during regular expression evaluation"); |
| 132 |
xe.setErrorCode(SaxonErrorCode.SXRE0001); |
| 133 |
throw xe; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private StringValue currentStringValue() { |
| 138 |
return StringValue.makeStringValue(current); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public int getProperties() { |
| 153 |
return LAST_POSITION_FINDER; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public boolean isMatching() { |
| 165 |
return next == null && prevEnd >= 0; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public String getRegexGroup(int number) { |
| 177 |
if (!isMatching()) { |
| 178 |
return null; |
| 179 |
} |
| 180 |
if (number >= matcher.getParenCount() || number < 0) return ""; |
| 181 |
UnicodeString us = matcher.getParen(number); |
| 182 |
return (us == null ? "" : us.toString()); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
@Override |
| 189 |
public int getNumberOfGroups() { |
| 190 |
return matcher.getParenCount(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public void processMatchingSubstring(MatchHandler action) throws XPathException { |
| 203 |
int c = matcher.getParenCount() - 1; |
| 204 |
if (c == 0) { |
| 205 |
action.characters(current.toString()); |
| 206 |
} else { |
| 207 |
|
| 208 |
|
| 209 |
IntHashMap<List<Integer>> actions = new IntHashMap<List<Integer>>(c); |
| 210 |
for (int i = 1; i <= c; i++) { |
| 211 |
int start = matcher.getParenStart(i) - matcher.getParenStart(0); |
| 212 |
if (start != -1) { |
| 213 |
int end = matcher.getParenEnd(i) - matcher.getParenStart(0); |
| 214 |
if (start < end) { |
| 215 |
|
| 216 |
List<Integer> s = actions.get(start); |
| 217 |
if (s == null) { |
| 218 |
s = new ArrayList<Integer>(4); |
| 219 |
actions.put(start, s); |
| 220 |
} |
| 221 |
s.add(i); |
| 222 |
|
| 223 |
List<Integer> e = actions.get(end); |
| 224 |
if (e == null) { |
| 225 |
e = new ArrayList<Integer>(4); |
| 226 |
actions.put(end, e); |
| 227 |
} |
| 228 |
e.add(0, -i); |
| 229 |
} else { |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (nestingTable == null) { |
| 235 |
nestingTable = computeNestingTable(regex); |
| 236 |
} |
| 237 |
int parentGroup = nestingTable.get(i); |
| 238 |
|
| 239 |
|
| 240 |
List<Integer> s = actions.get(start); |
| 241 |
if (s == null) { |
| 242 |
s = new ArrayList<Integer>(4); |
| 243 |
actions.put(start, s); |
| 244 |
s.add(i); |
| 245 |
s.add(-i); |
| 246 |
} else { |
| 247 |
int pos = s.size(); |
| 248 |
for (int e = 0; e < s.size(); e++) { |
| 249 |
if (s.get(e) == -parentGroup) { |
| 250 |
pos = e; |
| 251 |
break; |
| 252 |
} |
| 253 |
} |
| 254 |
s.add(pos, -i); |
| 255 |
s.add(pos, i); |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
} |
| 262 |
FastStringBuffer buff = new FastStringBuffer(current.uLength()); |
| 263 |
for (int i = 0; i < current.uLength() + 1; i++) { |
| 264 |
List<Integer> events = actions.get(i); |
| 265 |
if (events != null) { |
| 266 |
if (buff.length() > 0) { |
| 267 |
action.characters(buff); |
| 268 |
buff.setLength(0); |
| 269 |
} |
| 270 |
for (Integer group : events) { |
| 271 |
if (group > 0) { |
| 272 |
action.onGroupStart(group); |
| 273 |
} else { |
| 274 |
action.onGroupEnd(-group); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
if (i < current.uLength()) { |
| 279 |
buff.appendWideChar(current.uCharAt(i)); |
| 280 |
} |
| 281 |
} |
| 282 |
if (buff.length() > 0) { |
| 283 |
action.characters(buff); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public static IntToIntHashMap computeNestingTable(UnicodeString regex) { |
| 297 |
|
| 298 |
IntToIntHashMap nestingTable = new IntToIntHashMap(16); |
| 299 |
int[] stack = new int[regex.uLength()]; |
| 300 |
int tos = 0; |
| 301 |
boolean[] captureStack = new boolean[regex.uLength()]; |
| 302 |
int captureTos = 0; |
| 303 |
int group = 1; |
| 304 |
int inBrackets = 0; |
| 305 |
stack[tos++] = 0; |
| 306 |
for (int i = 0; i < regex.uLength(); i++) { |
| 307 |
int ch = regex.uCharAt(i); |
| 308 |
if (ch == '\\') { |
| 309 |
i++; |
| 310 |
} else if (ch == '[') { |
| 311 |
inBrackets++; |
| 312 |
} else if (ch == ']') { |
| 313 |
inBrackets--; |
| 314 |
} else if (ch == '(' && inBrackets == 0) { |
| 315 |
boolean capture = regex.uCharAt(i + 1) != '?'; |
| 316 |
captureStack[captureTos++] = capture; |
| 317 |
if (capture) { |
| 318 |
nestingTable.put(group, stack[tos - 1]); |
| 319 |
stack[tos++] = group++; |
| 320 |
} |
| 321 |
} else if (ch == ')' && inBrackets == 0) { |
| 322 |
boolean capture = captureStack[--captureTos]; |
| 323 |
if (capture) { |
| 324 |
tos--; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
return nestingTable; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
} |
| 333 |
|