| 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.functions.Count; |
| 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.Iterator; |
| 20 |
import java.util.List; |
| 21 |
import java.util.regex.Matcher; |
| 22 |
import java.util.regex.Pattern; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class JRegexIterator implements RegexIterator, LastPositionFinder { |
| 32 |
|
| 33 |
private String theString; |
| 34 |
private Pattern pattern; |
| 35 |
private Matcher matcher; |
| 36 |
private String current; |
| 37 |
private String next; |
| 38 |
|
| 39 |
private int prevEnd = 0; |
| 40 |
private IntToIntHashMap nestingTable = null; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public JRegexIterator(String string, Pattern pattern) { |
| 54 |
theString = string; |
| 55 |
this.pattern = pattern; |
| 56 |
matcher = pattern.matcher(string); |
| 57 |
next = null; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public int getLength() throws XPathException { |
| 62 |
JRegexIterator another = new JRegexIterator(theString, pattern); |
| 63 |
return Count.steppingCount(another); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public StringValue next() { |
| 74 |
if (next == null && prevEnd >= 0) { |
| 75 |
|
| 76 |
if (matcher.find()) { |
| 77 |
int start = matcher.start(); |
| 78 |
int end = matcher.end(); |
| 79 |
if (prevEnd == start) { |
| 80 |
|
| 81 |
next = null; |
| 82 |
current = theString.substring(start, end); |
| 83 |
prevEnd = end; |
| 84 |
} else { |
| 85 |
|
| 86 |
current = theString.substring(prevEnd, start); |
| 87 |
next = theString.substring(start, end); |
| 88 |
} |
| 89 |
} else { |
| 90 |
|
| 91 |
if (prevEnd < theString.length()) { |
| 92 |
current = theString.substring(prevEnd); |
| 93 |
next = null; |
| 94 |
} else { |
| 95 |
|
| 96 |
current = null; |
| 97 |
prevEnd = -1; |
| 98 |
return null; |
| 99 |
} |
| 100 |
prevEnd = -1; |
| 101 |
} |
| 102 |
} else { |
| 103 |
|
| 104 |
if (prevEnd >= 0) { |
| 105 |
current = next; |
| 106 |
next = null; |
| 107 |
prevEnd = matcher.end(); |
| 108 |
} else { |
| 109 |
current = null; |
| 110 |
return null; |
| 111 |
} |
| 112 |
} |
| 113 |
return StringValue.makeStringValue(current); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public int getProperties() { |
| 127 |
return LAST_POSITION_FINDER; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public boolean isMatching() { |
| 139 |
return next == null && prevEnd >= 0; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public String getRegexGroup(int number) { |
| 151 |
if (!isMatching()) return null; |
| 152 |
if (number > matcher.groupCount() || number < 0) return ""; |
| 153 |
String s = matcher.group(number); |
| 154 |
if (s == null) return ""; |
| 155 |
return s; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public int getNumberOfGroups() { |
| 162 |
return matcher.groupCount(); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public void processMatchingSubstring(MatchHandler action) throws XPathException { |
| 175 |
int c = matcher.groupCount(); |
| 176 |
if (c == 0) { |
| 177 |
action.characters(current); |
| 178 |
} else { |
| 179 |
|
| 180 |
|
| 181 |
IntHashMap<List<Integer>> actions = new IntHashMap<List<Integer>>(c); |
| 182 |
for (int i = 1; i <= c; i++) { |
| 183 |
int start = matcher.start(i) - matcher.start(); |
| 184 |
if (start != -1) { |
| 185 |
int end = matcher.end(i) - matcher.start(); |
| 186 |
if (start < end) { |
| 187 |
|
| 188 |
List<Integer> s = actions.get(start); |
| 189 |
if (s == null) { |
| 190 |
s = new ArrayList<Integer>(4); |
| 191 |
actions.put(start, s); |
| 192 |
} |
| 193 |
s.add(i); |
| 194 |
|
| 195 |
List<Integer> e = actions.get(end); |
| 196 |
if (e == null) { |
| 197 |
e = new ArrayList<Integer>(4); |
| 198 |
actions.put(end, e); |
| 199 |
} |
| 200 |
e.add(0, -i); |
| 201 |
} else { |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
if (nestingTable == null) { |
| 207 |
computeNestingTable(); |
| 208 |
} |
| 209 |
int parentGroup = nestingTable.get(i); |
| 210 |
|
| 211 |
|
| 212 |
List<Integer> s = actions.get(start); |
| 213 |
if (s == null) { |
| 214 |
s = new ArrayList<Integer>(4); |
| 215 |
actions.put(start, s); |
| 216 |
s.add(i); |
| 217 |
s.add(-i); |
| 218 |
} else { |
| 219 |
int pos = s.size(); |
| 220 |
for (int e = 0; e < s.size(); e++) { |
| 221 |
if (s.get(e) == -parentGroup) { |
| 222 |
pos = e; |
| 223 |
break; |
| 224 |
} |
| 225 |
} |
| 226 |
s.add(pos, -i); |
| 227 |
s.add(pos, i); |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
FastStringBuffer buff = new FastStringBuffer(current.length()); |
| 235 |
for (int i = 0; i < current.length() + 1; i++) { |
| 236 |
List<Integer> events = actions.get(i); |
| 237 |
if (events != null) { |
| 238 |
if (buff.length() > 0) { |
| 239 |
action.characters(buff); |
| 240 |
buff.setLength(0); |
| 241 |
} |
| 242 |
for (Iterator<Integer> ii = events.iterator(); ii.hasNext(); ) { |
| 243 |
int group = ii.next(); |
| 244 |
if (group > 0) { |
| 245 |
action.onGroupStart(group); |
| 246 |
} else { |
| 247 |
action.onGroupEnd(-group); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
if (i < current.length()) { |
| 252 |
buff.append(current.charAt(i)); |
| 253 |
} |
| 254 |
} |
| 255 |
if (buff.length() > 0) { |
| 256 |
action.characters(buff); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private void computeNestingTable() { |
| 270 |
nestingTable = new IntToIntHashMap(16); |
| 271 |
String s = pattern.pattern(); |
| 272 |
int[] stack = new int[s.length()]; |
| 273 |
int tos = 0; |
| 274 |
int group = 1; |
| 275 |
int inBrackets = 0; |
| 276 |
stack[tos++] = 0; |
| 277 |
for (int i = 0; i < s.length(); i++) { |
| 278 |
char ch = s.charAt(i); |
| 279 |
if (ch == '\'') { |
| 280 |
i++; |
| 281 |
} else if (ch == '[') { |
| 282 |
inBrackets++; |
| 283 |
} else if (ch == ']') { |
| 284 |
inBrackets--; |
| 285 |
} else if (ch == '(' && s.charAt(i + 1) != '?' && inBrackets == 0) { |
| 286 |
nestingTable.put(group, stack[tos - 1]); |
| 287 |
stack[tos++] = group++; |
| 288 |
} else if (ch == ')' && inBrackets == 0) { |
| 289 |
tos--; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
} |
| 296 |
|