Debian Codegraph @

net/sf/saxon/regex/ARegularExpression.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * Glue class to interface the Jakarta regex engine to Saxon
20 * (The prefix 'A' indicates an Apache regular expression, as distinct from
21 * a JDK regular expression).
22 */
23
24 public class ARegularExpression implements RegularExpression {
25
26 UnicodeString rawPattern;
27 String rawFlags;
28 REProgram regex;
29
30 /**
31 * Create and compile a regular expression
32 *
33 * @param pattern the regular expression
34 * @param flags the flags (ixsmq)
35 * @param hostLanguage one of "XP20", "XP30", "XSD10", "XSD11". Also allow combinations, e.g. "XP20/XSD11".
36 * @param warnings a list to be populated with any warnings arising during compilation of the regex
37 * @param config the Saxon Configuration: may be null
38 * @throws XPathException if the regular expression is invalid
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 * Determine whether the regular expression matches a given string in its entirety
69 *
70 * @param input the string to match
71 * @return true if the string matches, false otherwise
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 * Determine whether the regular expression contains a match of a given string
83 *
84 * @param input the string to match
85 * @return true if the string matches, false otherwise
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 * Use this regular expression to tokenize an input string.
94 *
95 * @param input the string to be tokenized
96 * @return a SequenceIterator containing the resulting tokens, as objects of type StringValue
97 */
98 public SequenceIterator<StringValue> tokenize(CharSequence input) {
99 return new ATokenIterator(UnicodeString.makeUnicodeString(input), new REMatcher(regex));
100 }
101
102 /**
103 * Use this regular expression to analyze an input string, in support of the XSLT
104 * analyze-string instruction. The resulting RegexIterator provides both the matching and
105 * non-matching substrings, and allows them to be distinguished. It also provides access
106 * to matched subgroups.
107 *
108 * @param input the character string to be analyzed using the regular expression
109 * @return an iterator over matched and unmatched substrings
110 */
111 public RegexIterator analyze(CharSequence input) {
112 return new ARegexIterator(UnicodeString.makeUnicodeString(input), rawPattern, new REMatcher(regex));
113 }
114
115 /**
116 * Replace all substrings of a supplied input string that match the regular expression
117 * with a replacement string.
118 *
119 * @param input the input string on which replacements are to be performed
120 * @param replacement the replacement string in the format of the XPath replace() function
121 * @return the result of performing the replacement
122 * @throws net.sf.saxon.trans.XPathException
123 * if the replacement string is invalid
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 * Get the flags used at the time the regular expression was compiled.
138 *
139 * @return a string containing the flags
140 */
141 @Override
142 public String getFlags() {
143 return rawFlags;
144 }
145 }
146