| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.regex; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.lib.Feature; |
| 12 |
import net.sf.saxon.om.SequenceIterator; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.value.StringValue; |
| 15 |
|
| 16 |
import java.util.List; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class ARegularExpression implements RegularExpression { |
| 25 |
|
| 26 |
UnicodeString rawPattern; |
| 27 |
String rawFlags; |
| 28 |
REProgram regex; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public ARegularExpression(CharSequence pattern, String flags, String hostLanguage, List<String> warnings, Configuration config) throws XPathException { |
| 42 |
rawFlags = flags; |
| 43 |
REFlags reFlags; |
| 44 |
try { |
| 45 |
reFlags = new REFlags(flags, hostLanguage); |
| 46 |
} catch (RESyntaxException err) { |
| 47 |
throw new XPathException(err.getMessage(), "FORX0001"); |
| 48 |
} |
| 49 |
try { |
| 50 |
rawPattern = UnicodeString.makeUnicodeString(pattern); |
| 51 |
RECompiler comp2 = new RECompiler(); |
| 52 |
comp2.setFlags(reFlags); |
| 53 |
regex = comp2.compile(rawPattern); |
| 54 |
if (warnings != null) { |
| 55 |
for (String s : comp2.getWarnings()) { |
| 56 |
warnings.add(s); |
| 57 |
} |
| 58 |
} |
| 59 |
if (config != null) { |
| 60 |
regex.setBacktrackingLimit(config.getConfigurationProperty(Feature.REGEX_BACKTRACKING_LIMIT)); |
| 61 |
} |
| 62 |
} catch (RESyntaxException err) { |
| 63 |
throw new XPathException(err.getMessage(), "FORX0002"); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public boolean matches(CharSequence input) { |
| 74 |
if (StringValue.isEmpty(input) && regex.isNullable()) { |
| 75 |
return true; |
| 76 |
} |
| 77 |
REMatcher matcher = new REMatcher(regex); |
| 78 |
return matcher.anchoredMatch(UnicodeString.makeUnicodeString(input)); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public boolean containsMatch(CharSequence input) { |
| 88 |
REMatcher matcher = new REMatcher(regex); |
| 89 |
return matcher.match(UnicodeString.makeUnicodeString(input), 0); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public SequenceIterator<StringValue> tokenize(CharSequence input) { |
| 99 |
return new ATokenIterator(UnicodeString.makeUnicodeString(input), new REMatcher(regex)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public RegexIterator analyze(CharSequence input) { |
| 112 |
return new ARegexIterator(UnicodeString.makeUnicodeString(input), rawPattern, new REMatcher(regex)); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public CharSequence replace(CharSequence input, CharSequence replacement) throws XPathException { |
| 126 |
REMatcher matcher = new REMatcher(regex); |
| 127 |
UnicodeString in = UnicodeString.makeUnicodeString(input); |
| 128 |
UnicodeString rep = UnicodeString.makeUnicodeString(replacement); |
| 129 |
try { |
| 130 |
return matcher.replace(in, rep); |
| 131 |
} catch (RESyntaxException err) { |
| 132 |
throw new XPathException(err.getMessage(), "FORX0004"); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@Override |
| 142 |
public String getFlags() { |
| 143 |
return rawFlags; |
| 144 |
} |
| 145 |
} |
| 146 |
|