| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.regex; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import net.sf.saxon.om.SequenceIterator; |
| 14 |
import net.sf.saxon.trans.XPathException; |
| 15 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 16 |
import net.sf.saxon.value.StringValue; |
| 17 |
|
| 18 |
import java.util.regex.Matcher; |
| 19 |
import java.util.regex.Pattern; |
| 20 |
import java.util.regex.PatternSyntaxException; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class JavaRegularExpression implements RegularExpression { |
| 27 |
|
| 28 |
Pattern pattern; |
| 29 |
String javaRegex; |
| 30 |
int flagBits; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public JavaRegularExpression(CharSequence javaRegex, String flags) throws XPathException { |
| 41 |
this.flagBits = setFlags(flags); |
| 42 |
this.javaRegex = javaRegex.toString(); |
| 43 |
try { |
| 44 |
pattern = Pattern.compile(this.javaRegex, flagBits & (~(Pattern.COMMENTS))); |
| 45 |
} catch (PatternSyntaxException e) { |
| 46 |
throw new XPathException("Incorrect syntax for Java regular expression", e); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public String getJavaRegularExpression() { |
| 57 |
return javaRegex; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public int getFlagBits() { |
| 67 |
return flagBits; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public RegexIterator analyze(CharSequence input) { |
| 78 |
return new JRegexIterator(input.toString(), pattern); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public boolean containsMatch(CharSequence input) { |
| 89 |
return pattern.matcher(input).find(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public boolean matches(CharSequence input) { |
| 100 |
return pattern.matcher(input).matches(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public CharSequence replace(CharSequence input, CharSequence replacement) throws XPathException { |
| 115 |
Matcher matcher = pattern.matcher(input); |
| 116 |
try { |
| 117 |
return matcher.replaceAll(replacement.toString()); |
| 118 |
} catch (IndexOutOfBoundsException e) { |
| 119 |
throw new XPathException(e.getMessage(), "FORX0004"); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public SequenceIterator<StringValue> tokenize(CharSequence input) { |
| 132 |
if (input.length() == 0) { |
| 133 |
return EmptyIterator.getInstance(); |
| 134 |
} |
| 135 |
return new JTokenIterator(input, pattern); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public static int setFlags( CharSequence inFlags) throws XPathException { |
| 157 |
int flags = Pattern.UNIX_LINES; |
| 158 |
for (int i = 0; i < inFlags.length(); i++) { |
| 159 |
char c = inFlags.charAt(i); |
| 160 |
switch (c) { |
| 161 |
case 'd': |
| 162 |
flags |= Pattern.UNIX_LINES; |
| 163 |
break; |
| 164 |
case 'm': |
| 165 |
flags |= Pattern.MULTILINE; |
| 166 |
break; |
| 167 |
case 'i': |
| 168 |
flags |= Pattern.CASE_INSENSITIVE; |
| 169 |
break; |
| 170 |
case 's': |
| 171 |
flags |= Pattern.DOTALL; |
| 172 |
break; |
| 173 |
case 'x': |
| 174 |
flags |= Pattern.COMMENTS; |
| 175 |
break; |
| 176 |
case 'u': |
| 177 |
flags |= Pattern.UNICODE_CASE; |
| 178 |
break; |
| 179 |
case 'q': |
| 180 |
flags |= Pattern.LITERAL; |
| 181 |
break; |
| 182 |
case 'c': |
| 183 |
flags |= Pattern.CANON_EQ; |
| 184 |
break; |
| 185 |
default: |
| 186 |
XPathException err = new XPathException("Invalid character '" + c + "' in regular expression flags"); |
| 187 |
err.setErrorCode("FORX0001"); |
| 188 |
throw err; |
| 189 |
} |
| 190 |
} |
| 191 |
return flags; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
@Override |
| 200 |
public String getFlags() { |
| 201 |
String flags = ""; |
| 202 |
if ((flagBits & Pattern.UNIX_LINES) != 0) { |
| 203 |
flags += 'd'; |
| 204 |
} |
| 205 |
if ((flagBits & Pattern.MULTILINE) != 0) { |
| 206 |
flags += 'm'; |
| 207 |
} |
| 208 |
if ((flagBits & Pattern.CASE_INSENSITIVE) != 0) { |
| 209 |
flags += 'i'; |
| 210 |
} |
| 211 |
if ((flagBits & Pattern.DOTALL) != 0) { |
| 212 |
flags += 's'; |
| 213 |
} |
| 214 |
if ((flagBits & Pattern.COMMENTS) != 0) { |
| 215 |
flags += 'x'; |
| 216 |
} |
| 217 |
if ((flagBits & Pattern.UNICODE_CASE) != 0) { |
| 218 |
flags += 'u'; |
| 219 |
} |
| 220 |
if ((flagBits & Pattern.LITERAL) != 0) { |
| 221 |
flags += 'q'; |
| 222 |
} |
| 223 |
if ((flagBits & Pattern.CANON_EQ) != 0) { |
| 224 |
flags += 'c'; |
| 225 |
} |
| 226 |
return flags; |
| 227 |
} |
| 228 |
} |
| 229 |
|