Debian Codegraph @

net/sf/saxon/regex/JavaRegularExpression.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.expr.regexj.Matcher;
11 //import net.sf.saxon.expr.regexj.Pattern;
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 * An implementation of RegularExpression that calls the JDK regular expression library directly.
24 * This can be invoked by appending ";j" to the flags attribute/argument
25 */
26 public class JavaRegularExpression implements RegularExpression {
27
28 Pattern pattern;
29 String javaRegex;
30 int flagBits;
31
32 /**
33 * Create a regular expression, starting with an already-translated Java regex.
34 * NOTE: this constructor is called from compiled XQuery code
35 *
36 * @param javaRegex the regular expression after translation to Java notation
37 * @param flags the user-specified flags (prior to any semicolon)
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 * Get the Java regular expression (after translation from an XPath regex, but before compilation)
52 *
53 * @return the regular expression in Java notation
54 */
55
56 public String getJavaRegularExpression() {
57 return javaRegex;
58 }
59
60 /**
61 * Get the flag bits as used by the Java regular expression engine
62 *
63 * @return the flag bits
64 */
65
66 public int getFlagBits() {
67 return flagBits;
68 }
69
70 /**
71 * Use this regular expression to analyze an input string, in support of the XSLT
72 * analyze-string instruction. The resulting RegexIterator provides both the matching and
73 * non-matching substrings, and allows them to be distinguished. It also provides access
74 * to matched subgroups.
75 */
76
77 public RegexIterator analyze(CharSequence input) {
78 return new JRegexIterator(input.toString(), pattern);
79 }
80
81 /**
82 * Determine whether the regular expression contains a match for a given string
83 *
84 * @param input the string to match
85 * @return true if the string matches, false otherwise
86 */
87
88 public boolean containsMatch(CharSequence input) {
89 return pattern.matcher(input).find();
90 }
91
92 /**
93 * Determine whether the regular expression matches a given string in its entirety
94 *
95 * @param input the string to match
96 * @return true if the string matches, false otherwise
97 */
98
99 public boolean matches(CharSequence input) {
100 return pattern.matcher(input).matches();
101 }
102
103 /**
104 * Replace all substrings of a supplied input string that match the regular expression
105 * with a replacement string.
106 *
107 * @param input the input string on which replacements are to be performed
108 * @param replacement the replacement string in the format of the XPath replace() function
109 * @return the result of performing the replacement
110 * @throws net.sf.saxon.trans.XPathException
111 * if the replacement string is invalid
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 * Use this regular expression to tokenize an input string.
126 *
127 * @param input the string to be tokenized
128 * @return a SequenceIterator containing the resulting tokens, as objects of type StringValue
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 * Set the Java flags from the supplied XPath flags. The flags recognized have their
140 * Java-defined meanings rather than their XPath-defined meanings. The available flags are:
141 * <p>d - UNIX_LINES</p>
142 * <p>m - MULTILINE</p>
143 * <p>i - CASE_INSENSITIVE</p>
144 * <p>s - DOTALL</p>
145 * <p>x - COMMENTS</p>
146 * <p>u - UNICODE_CASE</p>
147 * <p>q - LITERAL</p>
148 * <p>c - CANON_EQ</p>
149 *
150 * @param inFlags the flags as a string, e.g. "im"
151 * @return the flags as a bit-significant integer
152 * @throws XPathException if the supplied value contains an unrecognized flag character
153 * @see java.util.regex.Pattern
154 */
155
156 public static int setFlags(/*@NotNull*/ 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; // note, this enables comments as well as whitespace
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 * Get the flags used at the time the regular expression was compiled.
196 *
197 * @return a string containing the flags
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