286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.xpath.regex;
286N/A
286N/Aimport java.text.CharacterIterator;
286N/Aimport java.util.Locale;
286N/Aimport java.util.Stack;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.util.IntStack;
286N/A
286N/A/**
286N/A * A regular expression matching engine using Non-deterministic Finite Automaton (NFA).
286N/A * This engine does not conform to the POSIX regular expression.
286N/A *
286N/A * <hr width="50%">
286N/A * <h3>How to use</h3>
286N/A *
286N/A * <dl>
286N/A * <dt>A. Standard way
286N/A * <dd>
286N/A * <pre>
286N/A * RegularExpression re = new RegularExpression(<var>regex</var>);
286N/A * if (re.matches(text)) { ... }
286N/A * </pre>
286N/A *
286N/A * <dt>B. Capturing groups
286N/A * <dd>
286N/A * <pre>
286N/A * RegularExpression re = new RegularExpression(<var>regex</var>);
286N/A * Match match = new Match();
286N/A * if (re.matches(text, match)) {
286N/A * ... // You can refer captured texts with methods of the <code>Match</code> class.
286N/A * }
286N/A * </pre>
286N/A *
286N/A * </dl>
286N/A *
286N/A * <h4>Case-insensitive matching</h4>
286N/A * <pre>
286N/A * RegularExpression re = new RegularExpression(<var>regex</var>, "i");
286N/A * if (re.matches(text) >= 0) { ...}
286N/A * </pre>
286N/A *
286N/A * <h4>Options</h4>
286N/A * <p>You can specify options to <a href="#RegularExpression(java.lang.String, java.lang.String)"><code>RegularExpression(</code><var>regex</var><code>, </code><var>options</var><code>)</code></a>
286N/A * or <a href="#setPattern(java.lang.String, java.lang.String)"><code>setPattern(</code><var>regex</var><code>, </code><var>options</var><code>)</code></a>.
286N/A * This <var>options</var> parameter consists of the following characters.
286N/A * </p>
286N/A * <dl>
286N/A * <dt><a name="I_OPTION"><code>"i"</code></a>
286N/A * <dd>This option indicates case-insensitive matching.
286N/A * <dt><a name="M_OPTION"><code>"m"</code></a>
286N/A * <dd class="REGEX"><kbd>^</kbd> and <kbd>$</kbd> consider the EOL characters within the text.
286N/A * <dt><a name="S_OPTION"><code>"s"</code></a>
286N/A * <dd class="REGEX"><kbd>.</kbd> matches any one character.
286N/A * <dt><a name="U_OPTION"><code>"u"</code></a>
286N/A * <dd class="REGEX">Redefines <Kbd>\d \D \w \W \s \S \b \B \&lt; \></kbd> as becoming to Unicode.
286N/A * <dt><a name="W_OPTION"><code>"w"</code></a>
286N/A * <dd class="REGEX">By this option, <kbd>\b \B \&lt; \></kbd> are processed with the method of
286N/A * 'Unicode Regular Expression Guidelines' Revision 4.
286N/A * When "w" and "u" are specified at the same time,
286N/A * <kbd>\b \B \&lt; \></kbd> are processed for the "w" option.
286N/A * <dt><a name="COMMA_OPTION"><code>","</code></a>
286N/A * <dd>The parser treats a comma in a character class as a range separator.
286N/A * <kbd class="REGEX">[a,b]</kbd> matches <kbd>a</kbd> or <kbd>,</kbd> or <kbd>b</kbd> without this option.
286N/A * <kbd class="REGEX">[a,b]</kbd> matches <kbd>a</kbd> or <kbd>b</kbd> with this option.
286N/A *
286N/A * <dt><a name="X_OPTION"><code>"X"</code></a>
286N/A * <dd class="REGEX">
286N/A * By this option, the engine confoms to <a href="http://www.w3.org/TR/2000/WD-xmlschema-2-20000407/#regexs">XML Schema: Regular Expression</a>.
286N/A * The <code>match()</code> method does not do subsring matching
286N/A * but entire string matching.
286N/A *
286N/A * </dl>
286N/A *
286N/A * <hr width="50%">
286N/A * <h3>Syntax</h3>
286N/A * <table border="1" bgcolor="#ddeeff">
286N/A * <tr>
286N/A * <td>
286N/A * <h4>Differences from the Perl 5 regular expression</h4>
286N/A * <ul>
286N/A * <li>There is 6-digit hexadecimal character representation (<kbd>\u005cv</kbd><var>HHHHHH</var>.)
286N/A * <li>Supports subtraction, union, and intersection operations for character classes.
286N/A * <li>Not supported: <kbd>\</kbd><var>ooo</var> (Octal character representations),
286N/A * <Kbd>\G</kbd>, <kbd>\C</kbd>, <kbd>\l</kbd><var>c</var>,
286N/A * <kbd>\u005c u</kbd><var>c</var>, <kbd>\L</kbd>, <kbd>\U</kbd>,
286N/A * <kbd>\E</kbd>, <kbd>\Q</kbd>, <kbd>\N{</kbd><var>name</var><kbd>}</kbd>,
286N/A * <Kbd>(?{<kbd><var>code</var><kbd>})</kbd>, <Kbd>(??{<kbd><var>code</var><kbd>})</kbd>
286N/A * </ul>
286N/A * </td>
286N/A * </tr>
286N/A * </table>
286N/A *
286N/A * <P>Meta characters are `<KBD>. * + ? { [ ( ) | \ ^ $</KBD>'.</P>
286N/A * <ul>
286N/A * <li>Character
286N/A * <dl>
286N/A * <dt class="REGEX"><kbd>.</kbd> (A period)
286N/A * <dd>Matches any one character except the following characters.
286N/A * <dd>LINE FEED (U+000A), CARRIAGE RETURN (U+000D),
286N/A * PARAGRAPH SEPARATOR (U+2029), LINE SEPARATOR (U+2028)
286N/A * <dd>This expression matches one code point in Unicode. It can match a pair of surrogates.
286N/A * <dd>When <a href="#S_OPTION">the "s" option</a> is specified,
286N/A * it matches any character including the above four characters.
286N/A *
286N/A * <dt class="REGEX"><Kbd>\e \f \n \r \t</kbd>
286N/A * <dd>Matches ESCAPE (U+001B), FORM FEED (U+000C), LINE FEED (U+000A),
286N/A * CARRIAGE RETURN (U+000D), HORIZONTAL TABULATION (U+0009)
286N/A *
286N/A * <dt class="REGEX"><kbd>\c</kbd><var>C</var>
286N/A * <dd>Matches a control character.
286N/A * The <var>C</var> must be one of '<kbd>@</kbd>', '<kbd>A</kbd>'-'<kbd>Z</kbd>',
286N/A * '<kbd>[</kbd>', '<kbd>\u005c</kbd>', '<kbd>]</kbd>', '<kbd>^</kbd>', '<kbd>_</kbd>'.
286N/A * It matches a control character of which the character code is less than
286N/A * the character code of the <var>C</var> by 0x0040.
286N/A * <dd class="REGEX">For example, a <kbd>\cJ</kbd> matches a LINE FEED (U+000A),
286N/A * and a <kbd>\c[</kbd> matches an ESCAPE (U+001B).
286N/A *
286N/A * <dt class="REGEX">a non-meta character
286N/A * <dd>Matches the character.
286N/A *
286N/A * <dt class="REGEX"><KBD>\</KBD> + a meta character
286N/A * <dd>Matches the meta character.
286N/A *
286N/A * <dt class="REGEX"><kbd>\u005cx</kbd><var>HH</var> <kbd>\u005cx{</kbd><var>HHHH</var><kbd>}</kbd>
286N/A * <dd>Matches a character of which code point is <var>HH</var> (Hexadecimal) in Unicode.
286N/A * You can write just 2 digits for <kbd>\u005cx</kbd><var>HH</var>, and
286N/A * variable length digits for <kbd>\u005cx{</kbd><var>HHHH</var><kbd>}</kbd>.
286N/A *
286N/A * <!--
286N/A * <dt class="REGEX"><kbd>\u005c u</kbd><var>HHHH</var>
286N/A * <dd>Matches a character of which code point is <var>HHHH</var> (Hexadecimal) in Unicode.
286N/A * -->
286N/A *
286N/A * <dt class="REGEX"><kbd>\u005cv</kbd><var>HHHHHH</var>
286N/A * <dd>Matches a character of which code point is <var>HHHHHH</var> (Hexadecimal) in Unicode.
286N/A *
286N/A * <dt class="REGEX"><kbd>\g</kbd>
286N/A * <dd>Matches a grapheme.
286N/A * <dd class="REGEX">It is equivalent to <kbd>(?[\p{ASSIGNED}]-[\p{M}\p{C}])?(?:\p{M}|[\x{094D}\x{09CD}\x{0A4D}\x{0ACD}\x{0B3D}\x{0BCD}\x{0C4D}\x{0CCD}\x{0D4D}\x{0E3A}\x{0F84}]\p{L}|[\x{1160}-\x{11A7}]|[\x{11A8}-\x{11FF}]|[\x{FF9E}\x{FF9F}])*</kbd>
286N/A *
286N/A * <dt class="REGEX"><kbd>\X</kbd>
286N/A * <dd class="REGEX">Matches a combining character sequence.
286N/A * It is equivalent to <kbd>(?:\PM\pM*)</kbd>
286N/A * </dl>
286N/A * </li>
286N/A *
286N/A * <li>Character class
286N/A * <dl>
286N/A+ * <dt class="REGEX"><kbd>[</kbd><var>R<sub>1</sub></var><var>R<sub>2</sub></var><var>...</var><var>R<sub>n</sub></var><kbd>]</kbd> (without <a href="#COMMA_OPTION">"," option</a>)
286N/A+ * <dt class="REGEX"><kbd>[</kbd><var>R<sub>1</sub></var><kbd>,</kbd><var>R<sub>2</sub></var><kbd>,</kbd><var>...</var><kbd>,</kbd><var>R<sub>n</sub></var><kbd>]</kbd> (with <a href="#COMMA_OPTION">"," option</a>)
286N/A * <dd>Positive character class. It matches a character in ranges.
286N/A * <dd><var>R<sub>n</sub></var>:
286N/A * <ul>
286N/A * <li class="REGEX">A character (including <Kbd>\e \f \n \r \t</kbd> <kbd>\u005cx</kbd><var>HH</var> <kbd>\u005cx{</kbd><var>HHHH</var><kbd>}</kbd> <!--kbd>\u005c u</kbd><var>HHHH</var--> <kbd>\u005cv</kbd><var>HHHHHH</var>)
286N/A * <p>This range matches the character.
286N/A * <li class="REGEX"><var>C<sub>1</sub></var><kbd>-</kbd><var>C<sub>2</sub></var>
286N/A * <p>This range matches a character which has a code point that is >= <var>C<sub>1</sub></var>'s code point and &lt;= <var>C<sub>2</sub></var>'s code point.
286N/A+ * <li class="REGEX">A POSIX character class: <Kbd>[:alpha:] [:alnum:] [:ascii:] [:cntrl:] [:digit:] [:graph:] [:lower:] [:print:] [:punct:] [:space:] [:upper:] [:xdigit:]</kbd>,
286N/A+ * and negative POSIX character classes in Perl like <kbd>[:^alpha:]</kbd>
286N/A * <p>...
286N/A * <li class="REGEX"><kbd>\d \D \s \S \w \W \p{</kbd><var>name</var><kbd>} \P{</kbd><var>name</var><kbd>}</kbd>
286N/A * <p>These expressions specifies the same ranges as the following expressions.
286N/A * </ul>
286N/A * <p class="REGEX">Enumerated ranges are merged (union operation).
286N/A * <kbd>[a-ec-z]</kbd> is equivalent to <kbd>[a-z]</kbd>
286N/A *
286N/A * <dt class="REGEX"><kbd>[^</kbd><var>R<sub>1</sub></var><var>R<sub>2</sub></var><var>...</var><var>R<sub>n</sub></var><kbd>]</kbd> (without a <a href="#COMMA_OPTION">"," option</a>)
286N/A * <dt class="REGEX"><kbd>[^</kbd><var>R<sub>1</sub></var><kbd>,</kbd><var>R<sub>2</sub></var><kbd>,</kbd><var>...</var><kbd>,</kbd><var>R<sub>n</sub></var><kbd>]</kbd> (with a <a href="#COMMA_OPTION">"," option</a>)
286N/A * <dd>Negative character class. It matches a character not in ranges.
286N/A *
286N/A * <dt class="REGEX"><kbd>(?[</kbd><var>ranges</var><kbd>]</kbd><var>op</var><kbd>[</kbd><var>ranges</var><kbd>]</kbd><var>op</var><kbd>[</kbd><var>ranges</var><kbd>]</kbd> ... <Kbd>)</kbd>
286N/A * (<var>op</var> is <kbd>-</kbd> or <kbd>+</kbd> or <kbd>&</kbd>.)
286N/A * <dd>Subtraction or union or intersection for character classes.
286N/A * <dd class="REGEX">For exmaple, <kbd>(?[A-Z]-[CF])</kbd> is equivalent to <kbd>[A-BD-EG-Z]</kbd>, and <kbd>(?[0x00-0x7f]-[K]&[\p{Lu}])</kbd> is equivalent to <kbd>[A-JL-Z]</kbd>.
286N/A * <dd>The result of this operations is a <u>positive character class</u>
286N/A * even if an expression includes any negative character classes.
286N/A * You have to take care on this in case-insensitive matching.
286N/A * For instance, <kbd>(?[^b])</kbd> is equivalent to <kbd>[\x00-ac-\x{10ffff}]</kbd>,
286N/A * which is equivalent to <kbd>[^b]</kbd> in case-sensitive matching.
286N/A * But, in case-insensitive matching, <kbd>(?[^b])</kbd> matches any character because
286N/A * it includes '<kbd>B</kbd>' and '<kbd>B</kbd>' matches '<kbd>b</kbd>'
286N/A * though <kbd>[^b]</kbd> is processed as <kbd>[^Bb]</kbd>.
286N/A *
286N/A * <dt class="REGEX"><kbd>[</kbd><var>R<sub>1</sub>R<sub>2</sub>...</var><kbd>-[</kbd><var>R<sub>n</sub>R<sub>n+1</sub>...</var><kbd>]]</kbd> (with an <a href="#X_OPTION">"X" option</a>)</dt>
286N/A * <dd>Character class subtraction for the XML Schema.
286N/A * You can use this syntax when you specify an <a href="#X_OPTION">"X" option</a>.
286N/A *
286N/A * <dt class="REGEX"><kbd>\d</kbd>
286N/A * <dd class="REGEX">Equivalent to <kbd>[0-9]</kbd>.
286N/A * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
286N/A * <span class="REGEX"><kbd>\p{Nd}</kbd></span>.
286N/A *
286N/A * <dt class="REGEX"><kbd>\D</kbd>
286N/A * <dd class="REGEX">Equivalent to <kbd>[^0-9]</kbd>
286N/A * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
286N/A * <span class="REGEX"><kbd>\P{Nd}</kbd></span>.
286N/A *
286N/A * <dt class="REGEX"><kbd>\s</kbd>
286N/A * <dd class="REGEX">Equivalent to <kbd>[ \f\n\r\t]</kbd>
286N/A * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
286N/A * <span class="REGEX"><kbd>[ \f\n\r\t\p{Z}]</kbd></span>.
286N/A *
286N/A * <dt class="REGEX"><kbd>\S</kbd>
286N/A * <dd class="REGEX">Equivalent to <kbd>[^ \f\n\r\t]</kbd>
286N/A * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
286N/A * <span class="REGEX"><kbd>[^ \f\n\r\t\p{Z}]</kbd></span>.
286N/A *
286N/A * <dt class="REGEX"><kbd>\w</kbd>
286N/A * <dd class="REGEX">Equivalent to <kbd>[a-zA-Z0-9_]</kbd>
286N/A * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
286N/A * <span class="REGEX"><kbd>[\p{Lu}\p{Ll}\p{Lo}\p{Nd}_]</kbd></span>.
286N/A *
286N/A * <dt class="REGEX"><kbd>\W</kbd>
286N/A * <dd class="REGEX">Equivalent to <kbd>[^a-zA-Z0-9_]</kbd>
286N/A * <dd>When <a href="#U_OPTION">a "u" option</a> is set, it is equivalent to
286N/A * <span class="REGEX"><kbd>[^\p{Lu}\p{Ll}\p{Lo}\p{Nd}_]</kbd></span>.
286N/A *
286N/A * <dt class="REGEX"><kbd>\p{</kbd><var>name</var><kbd>}</kbd>
286N/A * <dd>Matches one character in the specified General Category (the second field in <a href="ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt"><kbd>UnicodeData.txt</kbd></a>) or the specified <a href="ftp://ftp.unicode.org/Public/UNIDATA/Blocks.txt">Block</a>.
286N/A * The following names are available:
286N/A * <dl>
286N/A * <dt>Unicode General Categories:
286N/A * <dd><kbd>
286N/A * L, M, N, Z, C, P, S, Lu, Ll, Lt, Lm, Lo, Mn, Me, Mc, Nd, Nl, No, Zs, Zl, Zp,
286N/A * Cc, Cf, Cn, Co, Cs, Pd, Ps, Pe, Pc, Po, Sm, Sc, Sk, So,
286N/A * </kbd>
286N/A * <dd>(Currently the Cn category includes U+10000-U+10FFFF characters)
286N/A * <dt>Unicode Blocks:
286N/A * <dd><kbd>
286N/A * Basic Latin, Latin-1 Supplement, Latin Extended-A, Latin Extended-B,
286N/A * IPA Extensions, Spacing Modifier Letters, Combining Diacritical Marks, Greek,
286N/A * Cyrillic, Armenian, Hebrew, Arabic, Devanagari, Bengali, Gurmukhi, Gujarati,
286N/A * Oriya, Tamil, Telugu, Kannada, Malayalam, Thai, Lao, Tibetan, Georgian,
286N/A * Hangul Jamo, Latin Extended Additional, Greek Extended, General Punctuation,
286N/A * Superscripts and Subscripts, Currency Symbols, Combining Marks for Symbols,
286N/A * Letterlike Symbols, Number Forms, Arrows, Mathematical Operators,
286N/A * Miscellaneous Technical, Control Pictures, Optical Character Recognition,
286N/A * Enclosed Alphanumerics, Box Drawing, Block Elements, Geometric Shapes,
286N/A * Miscellaneous Symbols, Dingbats, CJK Symbols and Punctuation, Hiragana,
286N/A * Katakana, Bopomofo, Hangul Compatibility Jamo, Kanbun,
286N/A * Enclosed CJK Letters and Months, CJK Compatibility, CJK Unified Ideographs,
286N/A * Hangul Syllables, High Surrogates, High Private Use Surrogates, Low Surrogates,
286N/A * Private Use, CJK Compatibility Ideographs, Alphabetic Presentation Forms,
286N/A * Arabic Presentation Forms-A, Combining Half Marks, CJK Compatibility Forms,
286N/A * Small Form Variants, Arabic Presentation Forms-B, Specials,
286N/A * Halfwidth and Fullwidth Forms
286N/A * </kbd>
286N/A * <dt>Others:
286N/A * <dd><kbd>ALL</kbd> (Equivalent to <kbd>[\u005cu0000-\u005cv10FFFF]</kbd>)
286N/A * <dd><kbd>ASSGINED</kbd> (<kbd>\p{ASSIGNED}</kbd> is equivalent to <kbd>\P{Cn}</kbd>)
286N/A * <dd><kbd>UNASSGINED</kbd>
286N/A * (<kbd>\p{UNASSIGNED}</kbd> is equivalent to <kbd>\p{Cn}</kbd>)
286N/A * </dl>
286N/A *
286N/A * <dt class="REGEX"><kbd>\P{</kbd><var>name</var><kbd>}</kbd>
286N/A * <dd>Matches one character not in the specified General Category or the specified Block.
286N/A * </dl>
286N/A * </li>
286N/A *
286N/A * <li>Selection and Quantifier
286N/A * <dl>
286N/A * <dt class="REGEX"><VAR>X</VAR><kbd>|</kbd><VAR>Y</VAR>
286N/A * <dd>...
286N/A *
286N/A * <dt class="REGEX"><VAR>X</VAR><kbd>*</KBD>
286N/A * <dd>Matches 0 or more <var>X</var>.
286N/A *
286N/A * <dt class="REGEX"><VAR>X</VAR><kbd>+</KBD>
286N/A * <dd>Matches 1 or more <var>X</var>.
286N/A *
286N/A * <dt class="REGEX"><VAR>X</VAR><kbd>?</KBD>
286N/A * <dd>Matches 0 or 1 <var>X</var>.
286N/A *
286N/A * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>number</var><kbd>}</kbd>
286N/A * <dd>Matches <var>number</var> times.
286N/A *
286N/A * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,}</kbd>
286N/A * <dd>...
286N/A *
286N/A * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,</kbd><var>max</var><kbd>}</kbd>
286N/A * <dd>...
286N/A *
286N/A * <dt class="REGEX"><VAR>X</VAR><kbd>*?</kbd>
286N/A * <dt class="REGEX"><VAR>X</VAR><kbd>+?</kbd>
286N/A * <dt class="REGEX"><VAR>X</VAR><kbd>??</kbd>
286N/A * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,}?</kbd>
286N/A * <dt class="REGEX"><var>X</var><kbd>{</kbd><var>min</var><kbd>,</kbd><var>max</var><kbd>}?</kbd>
286N/A * <dd>Non-greedy matching.
286N/A * </dl>
286N/A * </li>
286N/A *
286N/A * <li>Grouping, Capturing, and Back-reference
286N/A * <dl>
286N/A * <dt class="REGEX"><KBD>(?:</kbd><VAR>X</VAR><kbd>)</KBD>
286N/A * <dd>Grouping. "<KBD>foo+</KBD>" matches "<KBD>foo</KBD>" or "<KBD>foooo</KBD>".
286N/A * If you want it matches "<KBD>foofoo</KBD>" or "<KBD>foofoofoo</KBD>",
286N/A * you have to write "<KBD>(?:foo)+</KBD>".
286N/A *
286N/A * <dt class="REGEX"><KBD>(</kbd><VAR>X</VAR><kbd>)</KBD>
286N/A * <dd>Grouping with capturing.
286N/A * It make a group and applications can know
286N/A * where in target text a group matched with methods of a <code>Match</code> instance
286N/A * after <code><a href="#matches(java.lang.String, com.sun.org.apache.xerces.internal.utils.regex.Match)">matches(String,Match)</a></code>.
286N/A * The 0th group means whole of this regular expression.
286N/A * The <VAR>N</VAR>th gorup is the inside of the <VAR>N</VAR>th left parenthesis.
286N/A *
286N/A * <p>For instance, a regular expression is
286N/A * "<FONT color=blue><KBD> *([^&lt;:]*) +&lt;([^&gt;]*)&gt; *</KBD></FONT>"
286N/A * and target text is
286N/A * "<FONT color=red><KBD>From: TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;</KBD></FONT>":
286N/A * <ul>
286N/A * <li><code>Match.getCapturedText(0)</code>:
286N/A * "<FONT color=red><KBD> TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;</KBD></FONT>"
286N/A * <li><code>Match.getCapturedText(1)</code>: "<FONT color=red><KBD>TAMURA Kent</KBD></FONT>"
286N/A * <li><code>Match.getCapturedText(2)</code>: "<FONT color=red><KBD>kent@trl.ibm.co.jp</KBD></FONT>"
286N/A * </ul>
286N/A *
286N/A * <dt class="REGEX"><kbd>\1 \2 \3 \4 \5 \6 \7 \8 \9</kbd>
286N/A * <dd>
286N/A *
286N/A * <dt class="REGEX"><kbd>(?></kbd><var>X</var><kbd>)</kbd>
286N/A * <dd>Independent expression group. ................
286N/A *
286N/A * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>:</kbd><var>X</var><kbd>)</kbd>
286N/A * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>-</kbd><var>options2</var><kbd>:</kbd><var>X</var><kbd>)</kbd>
286N/A * <dd>............................
286N/A * <dd>The <var>options</var> or the <var>options2</var> consists of 'i' 'm' 's' 'w'.
286N/A * Note that it can not contain 'u'.
286N/A *
286N/A * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>)</kbd>
286N/A * <dt class="REGEX"><kbd>(?</kbd><var>options</var><kbd>-</kbd><var>options2</var><kbd>)</kbd>
286N/A * <dd>......
286N/A * <dd>These expressions must be at the beginning of a group.
286N/A * </dl>
286N/A * </li>
286N/A *
286N/A * <li>Anchor
286N/A * <dl>
286N/A * <dt class="REGEX"><kbd>\A</kbd>
286N/A * <dd>Matches the beginnig of the text.
286N/A *
286N/A * <dt class="REGEX"><kbd>\Z</kbd>
286N/A * <dd>Matches the end of the text, or before an EOL character at the end of the text,
286N/A * or CARRIAGE RETURN + LINE FEED at the end of the text.
286N/A *
286N/A * <dt class="REGEX"><kbd>\z</kbd>
286N/A * <dd>Matches the end of the text.
286N/A *
286N/A * <dt class="REGEX"><kbd>^</kbd>
286N/A * <dd>Matches the beginning of the text. It is equivalent to <span class="REGEX"><Kbd>\A</kbd></span>.
286N/A * <dd>When <a href="#M_OPTION">a "m" option</a> is set,
286N/A * it matches the beginning of the text, or after one of EOL characters (
286N/A * LINE FEED (U+000A), CARRIAGE RETURN (U+000D), LINE SEPARATOR (U+2028),
286N/A * PARAGRAPH SEPARATOR (U+2029).)
286N/A *
286N/A * <dt class="REGEX"><kbd>$</kbd>
286N/A * <dd>Matches the end of the text, or before an EOL character at the end of the text,
286N/A * or CARRIAGE RETURN + LINE FEED at the end of the text.
286N/A * <dd>When <a href="#M_OPTION">a "m" option</a> is set,
286N/A * it matches the end of the text, or before an EOL character.
286N/A *
286N/A * <dt class="REGEX"><kbd>\b</kbd>
286N/A * <dd>Matches word boundary.
286N/A * (See <a href="#W_OPTION">a "w" option</a>)
286N/A *
286N/A * <dt class="REGEX"><kbd>\B</kbd>
286N/A * <dd>Matches non word boundary.
286N/A * (See <a href="#W_OPTION">a "w" option</a>)
286N/A *
286N/A * <dt class="REGEX"><kbd>\&lt;</kbd>
286N/A * <dd>Matches the beginning of a word.
286N/A * (See <a href="#W_OPTION">a "w" option</a>)
286N/A *
286N/A * <dt class="REGEX"><kbd>\&gt;</kbd>
286N/A * <dd>Matches the end of a word.
286N/A * (See <a href="#W_OPTION">a "w" option</a>)
286N/A * </dl>
286N/A * </li>
286N/A * <li>Lookahead and lookbehind
286N/A * <dl>
286N/A * <dt class="REGEX"><kbd>(?=</kbd><var>X</var><kbd>)</kbd>
286N/A * <dd>Lookahead.
286N/A *
286N/A * <dt class="REGEX"><kbd>(?!</kbd><var>X</var><kbd>)</kbd>
286N/A * <dd>Negative lookahead.
286N/A *
286N/A * <dt class="REGEX"><kbd>(?&lt;=</kbd><var>X</var><kbd>)</kbd>
286N/A * <dd>Lookbehind.
286N/A * <dd>(Note for text capturing......)
286N/A *
286N/A * <dt class="REGEX"><kbd>(?&lt;!</kbd><var>X</var><kbd>)</kbd>
286N/A * <dd>Negative lookbehind.
286N/A * </dl>
286N/A * </li>
286N/A *
286N/A * <li>Misc.
286N/A * <dl>
286N/A * <dt class="REGEX"><kbd>(?(</Kbd><var>condition</var><Kbd>)</kbd><var>yes-pattern</var><kbd>|</kbd><var>no-pattern</var><kbd>)</kbd>,
286N/A * <dt class="REGEX"><kbd>(?(</kbd><var>condition</var><kbd>)</kbd><var>yes-pattern</var><kbd>)</kbd>
286N/A * <dd>......
286N/A * <dt class="REGEX"><kbd>(?#</kbd><var>comment</var><kbd>)</kbd>
286N/A * <dd>Comment. A comment string consists of characters except '<kbd>)</kbd>'.
286N/A * You can not write comments in character classes and before quantifiers.
286N/A * </dl>
286N/A * </li>
286N/A * </ul>
286N/A *
286N/A *
286N/A * <hr width="50%">
286N/A * <h3>BNF for the regular expression</h3>
286N/A * <pre>
286N/A * regex ::= ('(?' options ')')? term ('|' term)*
286N/A * term ::= factor+
286N/A * factor ::= anchors | atom (('*' | '+' | '?' | minmax ) '?'? )?
286N/A * | '(?#' [^)]* ')'
286N/A * minmax ::= '{' ([0-9]+ | [0-9]+ ',' | ',' [0-9]+ | [0-9]+ ',' [0-9]+) '}'
286N/A * atom ::= char | '.' | char-class | '(' regex ')' | '(?:' regex ')' | '\' [0-9]
286N/A * | '\w' | '\W' | '\d' | '\D' | '\s' | '\S' | category-block | '\X'
286N/A * | '(?>' regex ')' | '(?' options ':' regex ')'
286N/A * | '(?' ('(' [0-9] ')' | '(' anchors ')' | looks) term ('|' term)? ')'
286N/A * options ::= [imsw]* ('-' [imsw]+)?
286N/A * anchors ::= '^' | '$' | '\A' | '\Z' | '\z' | '\b' | '\B' | '\&lt;' | '\>'
286N/A * looks ::= '(?=' regex ')' | '(?!' regex ')'
286N/A * | '(?&lt;=' regex ')' | '(?&lt;!' regex ')'
286N/A * char ::= '\\' | '\' [efnrtv] | '\c' [@-_] | code-point | character-1
286N/A * category-block ::= '\' [pP] category-symbol-1
286N/A * | ('\p{' | '\P{') (category-symbol | block-name
286N/A * | other-properties) '}'
286N/A * category-symbol-1 ::= 'L' | 'M' | 'N' | 'Z' | 'C' | 'P' | 'S'
286N/A * category-symbol ::= category-symbol-1 | 'Lu' | 'Ll' | 'Lt' | 'Lm' | Lo'
286N/A * | 'Mn' | 'Me' | 'Mc' | 'Nd' | 'Nl' | 'No'
286N/A * | 'Zs' | 'Zl' | 'Zp' | 'Cc' | 'Cf' | 'Cn' | 'Co' | 'Cs'
286N/A * | 'Pd' | 'Ps' | 'Pe' | 'Pc' | 'Po'
286N/A * | 'Sm' | 'Sc' | 'Sk' | 'So'
286N/A * block-name ::= (See above)
286N/A * other-properties ::= 'ALL' | 'ASSIGNED' | 'UNASSIGNED'
286N/A * character-1 ::= (any character except meta-characters)
286N/A *
286N/A * char-class ::= '[' ranges ']'
286N/A * | '(?[' ranges ']' ([-+&] '[' ranges ']')? ')'
286N/A * ranges ::= '^'? (range <a href="#COMMA_OPTION">','?</a>)+
286N/A * range ::= '\d' | '\w' | '\s' | '\D' | '\W' | '\S' | category-block
286N/A * | range-char | range-char '-' range-char
286N/A * range-char ::= '\[' | '\]' | '\\' | '\' [,-efnrtv] | code-point | character-2
286N/A * code-point ::= '\x' hex-char hex-char
286N/A * | '\x{' hex-char+ '}'
286N/A * <!-- | '\u005c u' hex-char hex-char hex-char hex-char
286N/A * --> | '\v' hex-char hex-char hex-char hex-char hex-char hex-char
286N/A * hex-char ::= [0-9a-fA-F]
286N/A * character-2 ::= (any character except \[]-,)
286N/A * </pre>
286N/A *
286N/A * <hr width="50%">
286N/A * <h3>TODO</h3>
286N/A * <ul>
286N/A * <li><a href="http://www.unicode.org/unicode/reports/tr18/">Unicode Regular Expression Guidelines</a>
286N/A * <ul>
286N/A * <li>2.4 Canonical Equivalents
286N/A * <li>Level 3
286N/A * </ul>
286N/A * <li>Parsing performance
286N/A * </ul>
286N/A *
286N/A * <hr width="50%">
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
286N/A * @version $Id: RegularExpression.java,v 1.9 2010/07/27 05:02:34 joehw Exp $
286N/A */
286N/Apublic class RegularExpression implements java.io.Serializable {
286N/A
286N/A private static final long serialVersionUID = 6242499334195006401L;
286N/A
286N/A static final boolean DEBUG = false;
286N/A
286N/A /**
286N/A * Compiles a token tree into an operation flow.
286N/A */
286N/A private synchronized void compile(Token tok) {
286N/A if (this.operations != null)
286N/A return;
286N/A this.numberOfClosures = 0;
286N/A this.operations = this.compile(tok, null, false);
286N/A }
286N/A
286N/A /**
286N/A * Converts a token to an operation.
286N/A */
286N/A private Op compile(Token tok, Op next, boolean reverse) {
286N/A Op ret;
286N/A switch (tok.type) {
286N/A case Token.DOT:
286N/A ret = Op.createDot();
286N/A ret.next = next;
286N/A break;
286N/A
286N/A case Token.CHAR:
286N/A ret = Op.createChar(tok.getChar());
286N/A ret.next = next;
286N/A break;
286N/A
286N/A case Token.ANCHOR:
286N/A ret = Op.createAnchor(tok.getChar());
286N/A ret.next = next;
286N/A break;
286N/A
286N/A case Token.RANGE:
286N/A case Token.NRANGE:
286N/A ret = Op.createRange(tok);
286N/A ret.next = next;
286N/A break;
286N/A
286N/A case Token.CONCAT:
286N/A ret = next;
286N/A if (!reverse) {
286N/A for (int i = tok.size()-1; i >= 0; i --) {
286N/A ret = compile(tok.getChild(i), ret, false);
286N/A }
286N/A } else {
286N/A for (int i = 0; i < tok.size(); i ++) {
286N/A ret = compile(tok.getChild(i), ret, true);
286N/A }
286N/A }
286N/A break;
286N/A
286N/A case Token.UNION:
286N/A Op.UnionOp uni = Op.createUnion(tok.size());
286N/A for (int i = 0; i < tok.size(); i ++) {
286N/A uni.addElement(compile(tok.getChild(i), next, reverse));
286N/A }
286N/A ret = uni; // ret.next is null.
286N/A break;
286N/A
286N/A case Token.CLOSURE:
286N/A case Token.NONGREEDYCLOSURE:
286N/A Token child = tok.getChild(0);
286N/A int min = tok.getMin();
286N/A int max = tok.getMax();
286N/A if (min >= 0 && min == max) { // {n}
286N/A ret = next;
286N/A for (int i = 0; i < min; i ++) {
286N/A ret = compile(child, ret, reverse);
286N/A }
286N/A break;
286N/A }
286N/A if (min > 0 && max > 0)
286N/A max -= min;
286N/A if (max > 0) {
286N/A // X{2,6} -> XX(X(X(XX?)?)?)?
286N/A ret = next;
286N/A for (int i = 0; i < max; i ++) {
286N/A Op.ChildOp q = Op.createQuestion(tok.type == Token.NONGREEDYCLOSURE);
286N/A q.next = next;
286N/A q.setChild(compile(child, ret, reverse));
286N/A ret = q;
286N/A }
286N/A } else {
286N/A Op.ChildOp op;
286N/A if (tok.type == Token.NONGREEDYCLOSURE) {
286N/A op = Op.createNonGreedyClosure();
286N/A } else { // Token.CLOSURE
286N/A op = Op.createClosure(this.numberOfClosures++);
286N/A }
286N/A op.next = next;
286N/A op.setChild(compile(child, op, reverse));
286N/A ret = op;
286N/A }
286N/A if (min > 0) {
286N/A for (int i = 0; i < min; i ++) {
286N/A ret = compile(child, ret, reverse);
286N/A }
286N/A }
286N/A break;
286N/A
286N/A case Token.EMPTY:
286N/A ret = next;
286N/A break;
286N/A
286N/A case Token.STRING:
286N/A ret = Op.createString(tok.getString());
286N/A ret.next = next;
286N/A break;
286N/A
286N/A case Token.BACKREFERENCE:
286N/A ret = Op.createBackReference(tok.getReferenceNumber());
286N/A ret.next = next;
286N/A break;
286N/A
286N/A case Token.PAREN:
286N/A if (tok.getParenNumber() == 0) {
286N/A ret = compile(tok.getChild(0), next, reverse);
286N/A } else if (reverse) {
286N/A next = Op.createCapture(tok.getParenNumber(), next);
286N/A next = compile(tok.getChild(0), next, reverse);
286N/A ret = Op.createCapture(-tok.getParenNumber(), next);
286N/A } else {
286N/A next = Op.createCapture(-tok.getParenNumber(), next);
286N/A next = compile(tok.getChild(0), next, reverse);
286N/A ret = Op.createCapture(tok.getParenNumber(), next);
286N/A }
286N/A break;
286N/A
286N/A case Token.LOOKAHEAD:
286N/A ret = Op.createLook(Op.LOOKAHEAD, next, compile(tok.getChild(0), null, false));
286N/A break;
286N/A case Token.NEGATIVELOOKAHEAD:
286N/A ret = Op.createLook(Op.NEGATIVELOOKAHEAD, next, compile(tok.getChild(0), null, false));
286N/A break;
286N/A case Token.LOOKBEHIND:
286N/A ret = Op.createLook(Op.LOOKBEHIND, next, compile(tok.getChild(0), null, true));
286N/A break;
286N/A case Token.NEGATIVELOOKBEHIND:
286N/A ret = Op.createLook(Op.NEGATIVELOOKBEHIND, next, compile(tok.getChild(0), null, true));
286N/A break;
286N/A
286N/A case Token.INDEPENDENT:
286N/A ret = Op.createIndependent(next, compile(tok.getChild(0), null, reverse));
286N/A break;
286N/A
286N/A case Token.MODIFIERGROUP:
286N/A ret = Op.createModifier(next, compile(tok.getChild(0), null, reverse),
286N/A ((Token.ModifierToken)tok).getOptions(),
286N/A ((Token.ModifierToken)tok).getOptionsMask());
286N/A break;
286N/A
286N/A case Token.CONDITION:
286N/A Token.ConditionToken ctok = (Token.ConditionToken)tok;
286N/A int ref = ctok.refNumber;
286N/A Op condition = ctok.condition == null ? null : compile(ctok.condition, null, reverse);
286N/A Op yes = compile(ctok.yes, next, reverse);
286N/A Op no = ctok.no == null ? null : compile(ctok.no, next, reverse);
286N/A ret = Op.createCondition(next, ref, condition, yes, no);
286N/A break;
286N/A
286N/A default:
286N/A throw new RuntimeException("Unknown token type: "+tok.type);
286N/A } // switch (tok.type)
286N/A return ret;
286N/A }
286N/A
286N/A
286N/A//Public
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
286N/A *
286N/A * @return true if the target is matched to this regular expression.
286N/A */
286N/A public boolean matches(char[] target) {
286N/A return this.matches(target, 0, target .length , (Match)null);
286N/A }
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
286N/A * in specified range or not.
286N/A *
286N/A * @param start Start offset of the range.
286N/A * @param end End offset +1 of the range.
286N/A * @return true if the target is matched to this regular expression.
286N/A */
286N/A public boolean matches(char[] target, int start, int end) {
286N/A return this.matches(target, start, end, (Match)null);
286N/A }
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
286N/A *
286N/A * @param match A Match instance for storing matching result.
286N/A * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
286N/A */
286N/A public boolean matches(char[] target, Match match) {
286N/A return this.matches(target, 0, target .length , match);
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
286N/A * in specified range or not.
286N/A *
286N/A * @param start Start offset of the range.
286N/A * @param end End offset +1 of the range.
286N/A * @param match A Match instance for storing matching result.
286N/A * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
286N/A */
286N/A public boolean matches(char[] target, int start, int end, Match match) {
286N/A
286N/A synchronized (this) {
286N/A if (this.operations == null)
286N/A this.prepare();
286N/A if (this.context == null)
286N/A this.context = new Context();
286N/A }
286N/A Context con = null;
286N/A synchronized (this.context) {
286N/A con = this.context.inuse ? new Context() : this.context;
286N/A con.reset(target, start, end, this.numberOfClosures);
286N/A }
286N/A if (match != null) {
286N/A match.setNumberOfGroups(this.nofparen);
286N/A match.setSource(target);
286N/A } else if (this.hasBackReferences) {
286N/A match = new Match();
286N/A match.setNumberOfGroups(this.nofparen);
286N/A // Need not to call setSource() because
286N/A // a caller can not access this match instance.
286N/A }
286N/A con.match = match;
286N/A
286N/A if (RegularExpression.isSet(this.options, XMLSCHEMA_MODE)) {
286N/A int matchEnd = this. match(con, this.operations, con.start, 1, this.options);
286N/A //System.err.println("DEBUG: matchEnd="+matchEnd);
286N/A if (matchEnd == con.limit) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, con.start);
286N/A con.match.setEnd(0, matchEnd);
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A }
286N/A return false;
286N/A }
286N/A
286N/A /*
286N/A * The pattern has only fixed string.
286N/A * The engine uses Boyer-Moore.
286N/A */
286N/A if (this.fixedStringOnly) {
286N/A //System.err.println("DEBUG: fixed-only: "+this.fixedString);
286N/A int o = this.fixedStringTable.matches(target, con.start, con.limit);
286N/A if (o >= 0) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, o);
286N/A con.match.setEnd(0, o+this.fixedString.length());
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A }
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A
286N/A /*
286N/A * The pattern contains a fixed string.
286N/A * The engine checks with Boyer-Moore whether the text contains the fixed string or not.
286N/A * If not, it return with false.
286N/A */
286N/A if (this.fixedString != null) {
286N/A int o = this.fixedStringTable.matches(target, con.start, con.limit);
286N/A if (o < 0) {
286N/A //System.err.println("Non-match in fixed-string search.");
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A }
286N/A
286N/A int limit = con.limit-this.minlength;
286N/A int matchStart;
286N/A int matchEnd = -1;
286N/A
286N/A /*
286N/A * Checks whether the expression starts with ".*".
286N/A */
286N/A if (this.operations != null
286N/A && this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) {
286N/A if (isSet(this.options, SINGLE_LINE)) {
286N/A matchStart = con.start;
286N/A matchEnd = this. match(con, this.operations, con.start, 1, this.options);
286N/A } else {
286N/A boolean previousIsEOL = true;
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A int ch = target [ matchStart ] ;
286N/A if (isEOLChar(ch)) {
286N/A previousIsEOL = true;
286N/A } else {
286N/A if (previousIsEOL) {
286N/A if (0 <= (matchEnd = this. match(con, this.operations,
286N/A matchStart, 1, this.options)))
286N/A break;
286N/A }
286N/A previousIsEOL = false;
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A /*
286N/A * Optimization against the first character.
286N/A */
286N/A else if (this.firstChar != null) {
286N/A //System.err.println("DEBUG: with firstchar-matching: "+this.firstChar);
286N/A RangeToken range = this.firstChar;
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A int ch = target [matchStart] ;
286N/A if (REUtil.isHighSurrogate(ch) && matchStart+1 < con.limit) {
286N/A ch = REUtil.composeFromSurrogates(ch, target[matchStart+1]);
286N/A }
286N/A if (!range.match(ch)) {
286N/A continue;
286N/A }
286N/A if (0 <= (matchEnd = this. match(con, this.operations,
286N/A matchStart, 1, this.options))) {
286N/A break;
286N/A }
286N/A }
286N/A }
286N/A
286N/A /*
286N/A * Straightforward matching.
286N/A */
286N/A else {
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A if (0 <= (matchEnd = this. match(con, this.operations, matchStart, 1, this.options)))
286N/A break;
286N/A }
286N/A }
286N/A
286N/A if (matchEnd >= 0) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, matchStart);
286N/A con.match.setEnd(0, matchEnd);
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A } else {
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
286N/A *
286N/A * @return true if the target is matched to this regular expression.
286N/A */
286N/A public boolean matches(String target) {
286N/A return this.matches(target, 0, target .length() , (Match)null);
286N/A }
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
286N/A * in specified range or not.
286N/A *
286N/A * @param start Start offset of the range.
286N/A * @param end End offset +1 of the range.
286N/A * @return true if the target is matched to this regular expression.
286N/A */
286N/A public boolean matches(String target, int start, int end) {
286N/A return this.matches(target, start, end, (Match)null);
286N/A }
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
286N/A *
286N/A * @param match A Match instance for storing matching result.
286N/A * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
286N/A */
286N/A public boolean matches(String target, Match match) {
286N/A return this.matches(target, 0, target .length() , match);
286N/A }
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern
286N/A * in specified range or not.
286N/A *
286N/A * @param start Start offset of the range.
286N/A * @param end End offset +1 of the range.
286N/A * @param match A Match instance for storing matching result.
286N/A * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
286N/A */
286N/A public boolean matches(String target, int start, int end, Match match) {
286N/A
286N/A synchronized (this) {
286N/A if (this.operations == null)
286N/A this.prepare();
286N/A if (this.context == null)
286N/A this.context = new Context();
286N/A }
286N/A Context con = null;
286N/A synchronized (this.context) {
286N/A con = this.context.inuse ? new Context() : this.context;
286N/A con.reset(target, start, end, this.numberOfClosures);
286N/A }
286N/A if (match != null) {
286N/A match.setNumberOfGroups(this.nofparen);
286N/A match.setSource(target);
286N/A } else if (this.hasBackReferences) {
286N/A match = new Match();
286N/A match.setNumberOfGroups(this.nofparen);
286N/A // Need not to call setSource() because
286N/A // a caller can not access this match instance.
286N/A }
286N/A con.match = match;
286N/A
286N/A if (RegularExpression.isSet(this.options, XMLSCHEMA_MODE)) {
286N/A if (DEBUG) {
286N/A System.err.println("target string="+target);
286N/A }
286N/A int matchEnd = this. match(con, this.operations, con.start, 1, this.options);
286N/A if (DEBUG) {
286N/A System.err.println("matchEnd="+matchEnd);
286N/A System.err.println("con.limit="+con.limit);
286N/A }
286N/A if (matchEnd == con.limit) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, con.start);
286N/A con.match.setEnd(0, matchEnd);
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A }
286N/A return false;
286N/A }
286N/A
286N/A /*
286N/A * The pattern has only fixed string.
286N/A * The engine uses Boyer-Moore.
286N/A */
286N/A if (this.fixedStringOnly) {
286N/A //System.err.println("DEBUG: fixed-only: "+this.fixedString);
286N/A int o = this.fixedStringTable.matches(target, con.start, con.limit);
286N/A if (o >= 0) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, o);
286N/A con.match.setEnd(0, o+this.fixedString.length());
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A }
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A
286N/A /*
286N/A * The pattern contains a fixed string.
286N/A * The engine checks with Boyer-Moore whether the text contains the fixed string or not.
286N/A * If not, it return with false.
286N/A */
286N/A if (this.fixedString != null) {
286N/A int o = this.fixedStringTable.matches(target, con.start, con.limit);
286N/A if (o < 0) {
286N/A //System.err.println("Non-match in fixed-string search.");
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A }
286N/A
286N/A int limit = con.limit-this.minlength;
286N/A int matchStart;
286N/A int matchEnd = -1;
286N/A
286N/A /*
286N/A * Checks whether the expression starts with ".*".
286N/A */
286N/A if (this.operations != null
286N/A && this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) {
286N/A if (isSet(this.options, SINGLE_LINE)) {
286N/A matchStart = con.start;
286N/A matchEnd = this.match(con, this.operations, con.start, 1, this.options);
286N/A } else {
286N/A boolean previousIsEOL = true;
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A int ch = target .charAt( matchStart ) ;
286N/A if (isEOLChar(ch)) {
286N/A previousIsEOL = true;
286N/A } else {
286N/A if (previousIsEOL) {
286N/A if (0 <= (matchEnd = this.match(con, this.operations,
286N/A matchStart, 1, this.options)))
286N/A break;
286N/A }
286N/A previousIsEOL = false;
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A /*
286N/A * Optimization against the first character.
286N/A */
286N/A else if (this.firstChar != null) {
286N/A //System.err.println("DEBUG: with firstchar-matching: "+this.firstChar);
286N/A RangeToken range = this.firstChar;
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A int ch = target .charAt( matchStart ) ;
286N/A if (REUtil.isHighSurrogate(ch) && matchStart+1 < con.limit) {
286N/A ch = REUtil.composeFromSurrogates(ch, target.charAt(matchStart+1));
286N/A }
286N/A if (!range.match(ch)) {
286N/A continue;
286N/A }
286N/A if (0 <= (matchEnd = this.match(con, this.operations,
286N/A matchStart, 1, this.options))) {
286N/A break;
286N/A }
286N/A }
286N/A }
286N/A
286N/A /*
286N/A * Straightforward matching.
286N/A */
286N/A else {
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A if (0 <= (matchEnd = this.match(con, this.operations, matchStart, 1, this.options)))
286N/A break;
286N/A }
286N/A }
286N/A
286N/A if (matchEnd >= 0) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, matchStart);
286N/A con.match.setEnd(0, matchEnd);
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A } else {
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * @return -1 when not match; offset of the end of matched string when match.
286N/A */
286N/A private int match(Context con, Op op, int offset, int dx, int opts) {
286N/A final ExpressionTarget target = con.target;
286N/A final Stack opStack = new Stack();
286N/A final IntStack dataStack = new IntStack();
286N/A final boolean isSetIgnoreCase = isSet(opts, IGNORE_CASE);
286N/A int retValue = -1;
286N/A boolean returned = false;
286N/A
286N/A for (;;) {
286N/A if (op == null || offset > con.limit || offset < con.start) {
286N/A if (op == null) {
286N/A retValue = isSet(opts, XMLSCHEMA_MODE) && offset != con.limit ? -1 : offset;
286N/A }
286N/A else {
286N/A retValue = -1;
286N/A }
286N/A returned = true;
286N/A }
286N/A else {
286N/A retValue = -1;
286N/A // dx value is either 1 or -1
286N/A switch (op.type) {
286N/A case Op.CHAR:
286N/A {
286N/A final int o1 = (dx > 0) ? offset : offset -1;
286N/A if (o1 >= con.limit || o1 < 0 || !matchChar(op.getData(), target.charAt(o1), isSetIgnoreCase)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A offset += dx;
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.DOT:
286N/A {
286N/A int o1 = (dx > 0) ? offset : offset - 1;
286N/A if (o1 >= con.limit || o1 < 0) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A if (isSet(opts, SINGLE_LINE)) {
286N/A if (REUtil.isHighSurrogate(target.charAt(o1)) && o1+dx >= 0 && o1+dx < con.limit) {
286N/A o1 += dx;
286N/A }
286N/A }
286N/A else {
286N/A int ch = target.charAt(o1);
286N/A if (REUtil.isHighSurrogate(ch) && o1+dx >= 0 && o1+dx < con.limit) {
286N/A o1 += dx;
286N/A ch = REUtil.composeFromSurrogates(ch, target.charAt(o1));
286N/A }
286N/A if (isEOLChar(ch)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A }
286N/A offset = (dx > 0) ? o1 + 1 : o1;
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.RANGE:
286N/A case Op.NRANGE:
286N/A {
286N/A int o1 = (dx > 0) ? offset : offset -1;
286N/A if (o1 >= con.limit || o1 < 0) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A int ch = target.charAt(offset);
286N/A if (REUtil.isHighSurrogate(ch) && o1+dx < con.limit && o1+dx >=0) {
286N/A o1 += dx;
286N/A ch = REUtil.composeFromSurrogates(ch, target.charAt(o1));
286N/A }
286N/A final RangeToken tok = op.getToken();
286N/A if (!tok.match(ch)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A offset = (dx > 0) ? o1+1 : o1;
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.ANCHOR:
286N/A {
286N/A if (!matchAnchor(target, op, con, offset, opts)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.BACKREFERENCE:
286N/A {
286N/A int refno = op.getData();
286N/A if (refno <= 0 || refno >= this.nofparen) {
286N/A throw new RuntimeException("Internal Error: Reference number must be more than zero: "+refno);
286N/A }
286N/A if (con.match.getBeginning(refno) < 0 || con.match.getEnd(refno) < 0) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A int o2 = con.match.getBeginning(refno);
286N/A int literallen = con.match.getEnd(refno)-o2;
286N/A if (dx > 0) {
286N/A if (!target.regionMatches(isSetIgnoreCase, offset, con.limit, o2, literallen)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A offset += literallen;
286N/A }
286N/A else {
286N/A if (!target.regionMatches(isSetIgnoreCase, offset-literallen, con.limit, o2, literallen)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A offset -= literallen;
286N/A }
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.STRING:
286N/A {
286N/A String literal = op.getString();
286N/A int literallen = literal.length();
286N/A if (dx > 0) {
286N/A if (!target.regionMatches(isSetIgnoreCase, offset, con.limit, literal, literallen)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A offset += literallen;
286N/A }
286N/A else {
286N/A if (!target.regionMatches(isSetIgnoreCase, offset-literallen, con.limit, literal, literallen)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A offset -= literallen;
286N/A }
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.CLOSURE:
286N/A {
286N/A // Saves current position to avoid zero-width repeats.
286N/A final int id = op.getData();
286N/A if (con.closureContexts[id].contains(offset)) {
286N/A returned = true;
286N/A break;
286N/A }
286N/A
286N/A con.closureContexts[id].addOffset(offset);
286N/A }
286N/A // fall through
286N/A
286N/A case Op.QUESTION:
286N/A {
286N/A opStack.push(op);
286N/A dataStack.push(offset);
286N/A op = op.getChild();
286N/A }
286N/A break;
286N/A
286N/A case Op.NONGREEDYCLOSURE:
286N/A case Op.NONGREEDYQUESTION:
286N/A {
286N/A opStack.push(op);
286N/A dataStack.push(offset);
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.UNION:
286N/A if (op.size() == 0) {
286N/A returned = true;
286N/A }
286N/A else {
286N/A opStack.push(op);
286N/A dataStack.push(0);
286N/A dataStack.push(offset);
286N/A op = op.elementAt(0);
286N/A }
286N/A break;
286N/A
286N/A case Op.CAPTURE:
286N/A {
286N/A final int refno = op.getData();
286N/A if (con.match != null) {
286N/A if (refno > 0) {
286N/A dataStack.push(con.match.getBeginning(refno));
286N/A con.match.setBeginning(refno, offset);
286N/A }
286N/A else {
286N/A final int index = -refno;
286N/A dataStack.push(con.match.getEnd(index));
286N/A con.match.setEnd(index, offset);
286N/A }
286N/A opStack.push(op);
286N/A dataStack.push(offset);
286N/A }
286N/A op = op.next;
286N/A }
286N/A break;
286N/A
286N/A case Op.LOOKAHEAD:
286N/A case Op.NEGATIVELOOKAHEAD:
286N/A case Op.LOOKBEHIND:
286N/A case Op.NEGATIVELOOKBEHIND:
286N/A {
286N/A opStack.push(op);
286N/A dataStack.push(dx);
286N/A dataStack.push(offset);
286N/A dx = (op.type == Op.LOOKAHEAD || op.type == Op.NEGATIVELOOKAHEAD) ? 1 : -1;
286N/A op = op.getChild();
286N/A }
286N/A break;
286N/A
286N/A case Op.INDEPENDENT:
286N/A {
286N/A opStack.push(op);
286N/A dataStack.push(offset);
286N/A op = op.getChild();
286N/A }
286N/A break;
286N/A
286N/A case Op.MODIFIER:
286N/A {
286N/A int localopts = opts;
286N/A localopts |= op.getData();
286N/A localopts &= ~op.getData2();
286N/A opStack.push(op);
286N/A dataStack.push(opts);
286N/A dataStack.push(offset);
286N/A opts = localopts;
286N/A op = op.getChild();
286N/A }
286N/A break;
286N/A
286N/A case Op.CONDITION:
286N/A {
286N/A Op.ConditionOp cop = (Op.ConditionOp)op;
286N/A if (cop.refNumber > 0) {
286N/A if (cop.refNumber >= this.nofparen) {
286N/A throw new RuntimeException("Internal Error: Reference number must be more than zero: "+cop.refNumber);
286N/A }
286N/A if (con.match.getBeginning(cop.refNumber) >= 0
286N/A && con.match.getEnd(cop.refNumber) >= 0) {
286N/A op = cop.yes;
286N/A }
286N/A else if (cop.no != null) {
286N/A op = cop.no;
286N/A }
286N/A else {
286N/A op = cop.next;
286N/A }
286N/A }
286N/A else {
286N/A opStack.push(op);
286N/A dataStack.push(offset);
286N/A op = cop.condition;
286N/A }
286N/A }
286N/A break;
286N/A
286N/A default:
286N/A throw new RuntimeException("Unknown operation type: " + op.type);
286N/A }
286N/A }
286N/A
286N/A // handle recursive operations
286N/A while (returned) {
286N/A // exhausted all the operations
286N/A if (opStack.isEmpty()) {
286N/A return retValue;
286N/A }
286N/A
286N/A op = (Op) opStack.pop();
286N/A offset = dataStack.pop();
286N/A
286N/A switch (op.type) {
286N/A case Op.CLOSURE:
286N/A case Op.QUESTION:
286N/A if (retValue < 0) {
286N/A op = op.next;
286N/A returned = false;
286N/A }
286N/A break;
286N/A
286N/A case Op.NONGREEDYCLOSURE:
286N/A case Op.NONGREEDYQUESTION:
286N/A if (retValue < 0) {
286N/A op = op.getChild();
286N/A returned = false;
286N/A }
286N/A break;
286N/A
286N/A case Op.UNION:
286N/A {
286N/A int unionIndex = dataStack.pop();
286N/A if (DEBUG) {
286N/A System.err.println("UNION: "+unionIndex+", ret="+retValue);
286N/A }
286N/A
286N/A if (retValue < 0) {
286N/A if (++unionIndex < op.size()) {
286N/A opStack.push(op);
286N/A dataStack.push(unionIndex);
286N/A dataStack.push(offset);
286N/A op = op.elementAt(unionIndex);
286N/A returned = false;
286N/A }
286N/A else {
286N/A retValue = -1;
286N/A }
286N/A }
286N/A }
286N/A break;
286N/A
286N/A case Op.CAPTURE:
286N/A final int refno = op.getData();
286N/A final int saved = dataStack.pop();
286N/A if (retValue < 0) {
286N/A if (refno > 0) {
286N/A con.match.setBeginning(refno, saved);
286N/A }
286N/A else {
286N/A con.match.setEnd(-refno, saved);
286N/A }
286N/A }
286N/A break;
286N/A
286N/A case Op.LOOKAHEAD:
286N/A case Op.LOOKBEHIND:
286N/A {
286N/A dx = dataStack.pop();
286N/A if (0 <= retValue) {
286N/A op = op.next;
286N/A returned = false;
286N/A }
286N/A retValue = -1;
286N/A }
286N/A break;
286N/A
286N/A case Op.NEGATIVELOOKAHEAD:
286N/A case Op.NEGATIVELOOKBEHIND:
286N/A {
286N/A dx = dataStack.pop();
286N/A if (0 > retValue) {
286N/A op = op.next;
286N/A returned = false;
286N/A }
286N/A retValue = -1;
286N/A }
286N/A break;
286N/A
286N/A case Op.MODIFIER:
286N/A opts = dataStack.pop();
286N/A // fall through
286N/A
286N/A case Op.INDEPENDENT:
286N/A if (retValue >= 0) {
286N/A offset = retValue;
286N/A op = op.next;
286N/A returned = false;
286N/A }
286N/A break;
286N/A
286N/A case Op.CONDITION:
286N/A {
286N/A final Op.ConditionOp cop = (Op.ConditionOp)op;
286N/A if (0 <= retValue) {
286N/A op = cop.yes;
286N/A }
286N/A else if (cop.no != null) {
286N/A op = cop.no;
286N/A }
286N/A else {
286N/A op = cop.next;
286N/A }
286N/A }
286N/A returned = false;
286N/A break;
286N/A
286N/A default:
286N/A break;
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A private boolean matchChar(int ch, int other, boolean ignoreCase) {
286N/A return (ignoreCase) ? matchIgnoreCase(ch, other) : ch == other;
286N/A }
286N/A
286N/A boolean matchAnchor(ExpressionTarget target, Op op, Context con, int offset, int opts) {
286N/A boolean go = false;
286N/A switch (op.getData()) {
286N/A case '^':
286N/A if (isSet(opts, MULTIPLE_LINES)) {
286N/A if (!(offset == con.start
286N/A || offset > con.start && offset < con.limit && isEOLChar(target.charAt(offset-1))))
286N/A return false;
286N/A } else {
286N/A if (offset != con.start)
286N/A return false;
286N/A }
286N/A break;
286N/A
286N/A case '@': // Internal use only.
286N/A // The @ always matches line beginnings.
286N/A if (!(offset == con.start
286N/A || offset > con.start && isEOLChar(target.charAt(offset-1))))
286N/A return false;
286N/A break;
286N/A
286N/A case '$':
286N/A if (isSet(opts, MULTIPLE_LINES)) {
286N/A if (!(offset == con.limit
286N/A || offset < con.limit && isEOLChar(target.charAt(offset))))
286N/A return false;
286N/A } else {
286N/A if (!(offset == con.limit
286N/A || offset+1 == con.limit && isEOLChar(target.charAt(offset))
286N/A || offset+2 == con.limit && target.charAt(offset) == CARRIAGE_RETURN
286N/A && target.charAt(offset+1) == LINE_FEED))
286N/A return false;
286N/A }
286N/A break;
286N/A
286N/A case 'A':
286N/A if (offset != con.start) return false;
286N/A break;
286N/A
286N/A case 'Z':
286N/A if (!(offset == con.limit
286N/A || offset+1 == con.limit && isEOLChar(target.charAt(offset))
286N/A || offset+2 == con.limit && target.charAt(offset) == CARRIAGE_RETURN
286N/A && target.charAt(offset+1) == LINE_FEED))
286N/A return false;
286N/A break;
286N/A
286N/A case 'z':
286N/A if (offset != con.limit) return false;
286N/A break;
286N/A
286N/A case 'b':
286N/A if (con.length == 0)
286N/A return false;
286N/A {
286N/A int after = getWordType(target, con.start, con.limit, offset, opts);
286N/A if (after == WT_IGNORE) return false;
286N/A int before = getPreviousWordType(target, con.start, con.limit, offset, opts);
286N/A if (after == before) return false;
286N/A }
286N/A break;
286N/A
286N/A case 'B':
286N/A if (con.length == 0)
286N/A go = true;
286N/A else {
286N/A int after = getWordType(target, con.start, con.limit, offset, opts);
286N/A go = after == WT_IGNORE
286N/A || after == getPreviousWordType(target, con.start, con.limit, offset, opts);
286N/A }
286N/A if (!go) return false;
286N/A break;
286N/A
286N/A case '<':
286N/A if (con.length == 0 || offset == con.limit) return false;
286N/A if (getWordType(target, con.start, con.limit, offset, opts) != WT_LETTER
286N/A || getPreviousWordType(target, con.start, con.limit, offset, opts) != WT_OTHER)
286N/A return false;
286N/A break;
286N/A
286N/A case '>':
286N/A if (con.length == 0 || offset == con.start) return false;
286N/A if (getWordType(target, con.start, con.limit, offset, opts) != WT_OTHER
286N/A || getPreviousWordType(target, con.start, con.limit, offset, opts) != WT_LETTER)
286N/A return false;
286N/A break;
286N/A } // switch anchor type
286N/A
286N/A return true;
286N/A }
286N/A
286N/A private static final int getPreviousWordType(ExpressionTarget target, int begin, int end,
286N/A int offset, int opts) {
286N/A int ret = getWordType(target, begin, end, --offset, opts);
286N/A while (ret == WT_IGNORE)
286N/A ret = getWordType(target, begin, end, --offset, opts);
286N/A return ret;
286N/A }
286N/A
286N/A private static final int getWordType(ExpressionTarget target, int begin, int end,
286N/A int offset, int opts) {
286N/A if (offset < begin || offset >= end) return WT_OTHER;
286N/A return getWordType0(target.charAt(offset) , opts);
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
286N/A *
286N/A * @return true if the target is matched to this regular expression.
286N/A */
286N/A public boolean matches(CharacterIterator target) {
286N/A return this.matches(target, (Match)null);
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.
286N/A *
286N/A * @param match A Match instance for storing matching result.
286N/A * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.
286N/A */
286N/A public boolean matches(CharacterIterator target, Match match) {
286N/A int start = target.getBeginIndex();
286N/A int end = target.getEndIndex();
286N/A
286N/A
286N/A
286N/A synchronized (this) {
286N/A if (this.operations == null)
286N/A this.prepare();
286N/A if (this.context == null)
286N/A this.context = new Context();
286N/A }
286N/A Context con = null;
286N/A synchronized (this.context) {
286N/A con = this.context.inuse ? new Context() : this.context;
286N/A con.reset(target, start, end, this.numberOfClosures);
286N/A }
286N/A if (match != null) {
286N/A match.setNumberOfGroups(this.nofparen);
286N/A match.setSource(target);
286N/A } else if (this.hasBackReferences) {
286N/A match = new Match();
286N/A match.setNumberOfGroups(this.nofparen);
286N/A // Need not to call setSource() because
286N/A // a caller can not access this match instance.
286N/A }
286N/A con.match = match;
286N/A
286N/A if (RegularExpression.isSet(this.options, XMLSCHEMA_MODE)) {
286N/A int matchEnd = this.match(con, this.operations, con.start, 1, this.options);
286N/A //System.err.println("DEBUG: matchEnd="+matchEnd);
286N/A if (matchEnd == con.limit) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, con.start);
286N/A con.match.setEnd(0, matchEnd);
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A }
286N/A return false;
286N/A }
286N/A
286N/A /*
286N/A * The pattern has only fixed string.
286N/A * The engine uses Boyer-Moore.
286N/A */
286N/A if (this.fixedStringOnly) {
286N/A //System.err.println("DEBUG: fixed-only: "+this.fixedString);
286N/A int o = this.fixedStringTable.matches(target, con.start, con.limit);
286N/A if (o >= 0) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, o);
286N/A con.match.setEnd(0, o+this.fixedString.length());
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A }
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A
286N/A /*
286N/A * The pattern contains a fixed string.
286N/A * The engine checks with Boyer-Moore whether the text contains the fixed string or not.
286N/A * If not, it return with false.
286N/A */
286N/A if (this.fixedString != null) {
286N/A int o = this.fixedStringTable.matches(target, con.start, con.limit);
286N/A if (o < 0) {
286N/A //System.err.println("Non-match in fixed-string search.");
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A }
286N/A
286N/A int limit = con.limit-this.minlength;
286N/A int matchStart;
286N/A int matchEnd = -1;
286N/A
286N/A /*
286N/A * Checks whether the expression starts with ".*".
286N/A */
286N/A if (this.operations != null
286N/A && this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) {
286N/A if (isSet(this.options, SINGLE_LINE)) {
286N/A matchStart = con.start;
286N/A matchEnd = this.match(con, this.operations, con.start, 1, this.options);
286N/A } else {
286N/A boolean previousIsEOL = true;
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A int ch = target .setIndex( matchStart ) ;
286N/A if (isEOLChar(ch)) {
286N/A previousIsEOL = true;
286N/A } else {
286N/A if (previousIsEOL) {
286N/A if (0 <= (matchEnd = this.match(con, this.operations,
286N/A matchStart, 1, this.options)))
286N/A break;
286N/A }
286N/A previousIsEOL = false;
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A /*
286N/A * Optimization against the first character.
286N/A */
286N/A else if (this.firstChar != null) {
286N/A //System.err.println("DEBUG: with firstchar-matching: "+this.firstChar);
286N/A RangeToken range = this.firstChar;
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A int ch = target .setIndex( matchStart ) ;
286N/A if (REUtil.isHighSurrogate(ch) && matchStart+1 < con.limit) {
286N/A ch = REUtil.composeFromSurrogates(ch, target.setIndex(matchStart+1));
286N/A }
286N/A if (!range.match(ch)) {
286N/A continue;
286N/A }
286N/A if (0 <= (matchEnd = this.match(con, this.operations,
286N/A matchStart, 1, this.options))) {
286N/A break;
286N/A }
286N/A }
286N/A }
286N/A
286N/A /*
286N/A * Straightforward matching.
286N/A */
286N/A else {
286N/A for (matchStart = con.start; matchStart <= limit; matchStart ++) {
286N/A if (0 <= (matchEnd = this. match(con, this.operations, matchStart, 1, this.options)))
286N/A break;
286N/A }
286N/A }
286N/A
286N/A if (matchEnd >= 0) {
286N/A if (con.match != null) {
286N/A con.match.setBeginning(0, matchStart);
286N/A con.match.setEnd(0, matchEnd);
286N/A }
286N/A con.setInUse(false);
286N/A return true;
286N/A } else {
286N/A con.setInUse(false);
286N/A return false;
286N/A }
286N/A }
286N/A
286N/A // ================================================================
286N/A
286N/A /**
286N/A * A regular expression.
286N/A * @serial
286N/A */
286N/A String regex;
286N/A /**
286N/A * @serial
286N/A */
286N/A int options;
286N/A
286N/A /**
286N/A * The number of parenthesis in the regular expression.
286N/A * @serial
286N/A */
286N/A int nofparen;
286N/A /**
286N/A * Internal representation of the regular expression.
286N/A * @serial
286N/A */
286N/A Token tokentree;
286N/A
286N/A boolean hasBackReferences = false;
286N/A
286N/A transient int minlength;
286N/A transient Op operations = null;
286N/A transient int numberOfClosures;
286N/A transient Context context = null;
286N/A transient RangeToken firstChar = null;
286N/A
286N/A transient String fixedString = null;
286N/A transient int fixedStringOptions;
286N/A transient BMPattern fixedStringTable = null;
286N/A transient boolean fixedStringOnly = false;
286N/A
286N/A static abstract class ExpressionTarget {
286N/A abstract char charAt(int index);
286N/A abstract boolean regionMatches(boolean ignoreCase, int offset, int limit, String part, int partlen);
286N/A abstract boolean regionMatches(boolean ignoreCase, int offset, int limit, int offset2, int partlen);
286N/A }
286N/A
286N/A static final class StringTarget extends ExpressionTarget {
286N/A
286N/A private String target;
286N/A
286N/A StringTarget(String target) {
286N/A this.target = target;
286N/A }
286N/A
286N/A final void resetTarget(String target) {
286N/A this.target = target;
286N/A }
286N/A
286N/A final char charAt(int index) {
286N/A return target.charAt(index);
286N/A }
286N/A
286N/A final boolean regionMatches(boolean ignoreCase, int offset, int limit,
286N/A String part, int partlen) {
286N/A if (limit-offset < partlen) {
286N/A return false;
286N/A }
286N/A return (ignoreCase) ? target.regionMatches(true, offset, part, 0, partlen) : target.regionMatches(offset, part, 0, partlen);
286N/A }
286N/A
286N/A final boolean regionMatches(boolean ignoreCase, int offset, int limit,
286N/A int offset2, int partlen) {
286N/A if (limit-offset < partlen) {
286N/A return false;
286N/A }
286N/A return (ignoreCase) ? target.regionMatches(true, offset, target, offset2, partlen)
286N/A : target.regionMatches(offset, target, offset2, partlen);
286N/A }
286N/A }
286N/A
286N/A static final class CharArrayTarget extends ExpressionTarget {
286N/A
286N/A char[] target;
286N/A
286N/A CharArrayTarget(char[] target) {
286N/A this.target = target;
286N/A }
286N/A
286N/A final void resetTarget(char[] target) {
286N/A this.target = target;
286N/A }
286N/A
286N/A char charAt(int index) {
286N/A return target[index];
286N/A }
286N/A
286N/A final boolean regionMatches(boolean ignoreCase, int offset, int limit,
286N/A String part, int partlen) {
286N/A if (offset < 0 || limit-offset < partlen) {
286N/A return false;
286N/A }
286N/A return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, part, partlen)
286N/A : regionMatches(offset, limit, part, partlen);
286N/A }
286N/A
286N/A private final boolean regionMatches(int offset, int limit, String part, int partlen) {
286N/A int i = 0;
286N/A while (partlen-- > 0) {
286N/A if (target[offset++] != part.charAt(i++)) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A
286N/A private final boolean regionMatchesIgnoreCase(int offset, int limit, String part, int partlen) {
286N/A int i = 0;
286N/A while (partlen-- > 0) {
286N/A final char ch1 = target[offset++] ;
286N/A final char ch2 = part.charAt(i++);
286N/A if (ch1 == ch2) {
286N/A continue;
286N/A }
286N/A final char uch1 = Character.toUpperCase(ch1);
286N/A final char uch2 = Character.toUpperCase(ch2);
286N/A if (uch1 == uch2) {
286N/A continue;
286N/A }
286N/A if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A
286N/A final boolean regionMatches(boolean ignoreCase, int offset, int limit, int offset2, int partlen) {
286N/A if (offset < 0 || limit-offset < partlen) {
286N/A return false;
286N/A }
286N/A return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, offset2, partlen)
286N/A : regionMatches(offset, limit, offset2, partlen);
286N/A }
286N/A
286N/A private final boolean regionMatches(int offset, int limit, int offset2, int partlen) {
286N/A int i = offset2;
286N/A while (partlen-- > 0) {
286N/A if ( target [ offset++ ] != target [ i++ ] )
286N/A return false;
286N/A }
286N/A return true;
286N/A }
286N/A
286N/A private final boolean regionMatchesIgnoreCase(int offset, int limit, int offset2, int partlen) {
286N/A int i = offset2;
286N/A while (partlen-- > 0) {
286N/A final char ch1 = target[offset++] ;
286N/A final char ch2 = target[i++] ;
286N/A if (ch1 == ch2) {
286N/A continue;
286N/A }
286N/A final char uch1 = Character.toUpperCase(ch1);
286N/A final char uch2 = Character.toUpperCase(ch2);
286N/A if (uch1 == uch2) {
286N/A continue;
286N/A }
286N/A if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A }
286N/A
286N/A static final class CharacterIteratorTarget extends ExpressionTarget {
286N/A CharacterIterator target;
286N/A
286N/A CharacterIteratorTarget(CharacterIterator target) {
286N/A this.target = target;
286N/A }
286N/A
286N/A final void resetTarget(CharacterIterator target) {
286N/A this.target = target;
286N/A }
286N/A
286N/A final char charAt(int index) {
286N/A return target.setIndex(index);
286N/A }
286N/A
286N/A final boolean regionMatches(boolean ignoreCase, int offset, int limit,
286N/A String part, int partlen) {
286N/A if (offset < 0 || limit-offset < partlen) {
286N/A return false;
286N/A }
286N/A return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, part, partlen)
286N/A : regionMatches(offset, limit, part, partlen);
286N/A }
286N/A
286N/A private final boolean regionMatches(int offset, int limit, String part, int partlen) {
286N/A int i = 0;
286N/A while (partlen-- > 0) {
286N/A if (target.setIndex(offset++) != part.charAt(i++)) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A
286N/A private final boolean regionMatchesIgnoreCase(int offset, int limit, String part, int partlen) {
286N/A int i = 0;
286N/A while (partlen-- > 0) {
286N/A final char ch1 = target.setIndex(offset++) ;
286N/A final char ch2 = part.charAt(i++);
286N/A if (ch1 == ch2) {
286N/A continue;
286N/A }
286N/A final char uch1 = Character.toUpperCase(ch1);
286N/A final char uch2 = Character.toUpperCase(ch2);
286N/A if (uch1 == uch2) {
286N/A continue;
286N/A }
286N/A if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A
286N/A final boolean regionMatches(boolean ignoreCase, int offset, int limit, int offset2, int partlen) {
286N/A if (offset < 0 || limit-offset < partlen) {
286N/A return false;
286N/A }
286N/A return (ignoreCase) ? regionMatchesIgnoreCase(offset, limit, offset2, partlen)
286N/A : regionMatches(offset, limit, offset2, partlen);
286N/A }
286N/A
286N/A private final boolean regionMatches(int offset, int limit, int offset2, int partlen) {
286N/A int i = offset2;
286N/A while (partlen-- > 0) {
286N/A if (target.setIndex(offset++) != target.setIndex(i++)) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A
286N/A private final boolean regionMatchesIgnoreCase(int offset, int limit, int offset2, int partlen) {
286N/A int i = offset2;
286N/A while (partlen-- > 0) {
286N/A final char ch1 = target.setIndex(offset++) ;
286N/A final char ch2 = target.setIndex(i++) ;
286N/A if (ch1 == ch2) {
286N/A continue;
286N/A }
286N/A final char uch1 = Character.toUpperCase(ch1);
286N/A final char uch2 = Character.toUpperCase(ch2);
286N/A if (uch1 == uch2) {
286N/A continue;
286N/A }
286N/A if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2)) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A }
286N/A
286N/A static final class ClosureContext {
286N/A
286N/A int[] offsets = new int[4];
286N/A int currentIndex = 0;
286N/A
286N/A boolean contains(int offset) {
286N/A for (int i=0; i<currentIndex;++i) {
286N/A if (offsets[i] == offset) {
286N/A return true;
286N/A }
286N/A }
286N/A return false;
286N/A }
286N/A
286N/A void reset() {
286N/A currentIndex = 0;
286N/A }
286N/A
286N/A void addOffset(int offset) {
286N/A // We do not check for duplicates, caller is responsible for that
286N/A if (currentIndex == offsets.length) {
286N/A offsets = expandOffsets();
286N/A }
286N/A offsets[currentIndex++] = offset;
286N/A }
286N/A
286N/A private int[] expandOffsets() {
286N/A final int len = offsets.length;
286N/A final int newLen = len << 1;
286N/A int[] newOffsets = new int[newLen];
286N/A
286N/A System.arraycopy(offsets, 0, newOffsets, 0, currentIndex);
286N/A return newOffsets;
286N/A }
286N/A }
286N/A
286N/A static final class Context {
286N/A int start;
286N/A int limit;
286N/A int length;
286N/A Match match;
286N/A boolean inuse = false;
286N/A ClosureContext[] closureContexts;
286N/A
286N/A private StringTarget stringTarget;
286N/A private CharArrayTarget charArrayTarget;
286N/A private CharacterIteratorTarget characterIteratorTarget;
286N/A
286N/A ExpressionTarget target;
286N/A
286N/A Context() {
286N/A }
286N/A
286N/A private void resetCommon(int nofclosures) {
286N/A this.length = this.limit-this.start;
286N/A setInUse(true);
286N/A this.match = null;
286N/A if (this.closureContexts == null || this.closureContexts.length != nofclosures) {
286N/A this.closureContexts = new ClosureContext[nofclosures];
286N/A }
286N/A for (int i = 0; i < nofclosures; i ++) {
286N/A if (this.closureContexts[i] == null) {
286N/A this.closureContexts[i] = new ClosureContext();
286N/A }
286N/A else {
286N/A this.closureContexts[i].reset();
286N/A }
286N/A }
286N/A }
286N/A
286N/A void reset(CharacterIterator target, int start, int limit, int nofclosures) {
286N/A if (characterIteratorTarget == null) {
286N/A characterIteratorTarget = new CharacterIteratorTarget(target);
286N/A }
286N/A else {
286N/A characterIteratorTarget.resetTarget(target);
286N/A }
286N/A this.target = characterIteratorTarget;
286N/A this.start = start;
286N/A this.limit = limit;
286N/A this.resetCommon(nofclosures);
286N/A }
286N/A
286N/A void reset(String target, int start, int limit, int nofclosures) {
286N/A if (stringTarget == null) {
286N/A stringTarget = new StringTarget(target);
286N/A }
286N/A else {
286N/A stringTarget.resetTarget(target);
286N/A }
286N/A this.target = stringTarget;
286N/A this.start = start;
286N/A this.limit = limit;
286N/A this.resetCommon(nofclosures);
286N/A }
286N/A
286N/A void reset(char[] target, int start, int limit, int nofclosures) {
286N/A if (charArrayTarget == null) {
286N/A charArrayTarget = new CharArrayTarget(target);
286N/A }
286N/A else {
286N/A charArrayTarget.resetTarget(target);
286N/A }
286N/A this.target = charArrayTarget;
286N/A this.start = start;
286N/A this.limit = limit;
286N/A this.resetCommon(nofclosures);
286N/A }
286N/A synchronized void setInUse(boolean inUse) {
286N/A this.inuse = inUse;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Prepares for matching. This method is called just before starting matching.
286N/A */
286N/A void prepare() {
286N/A if (Op.COUNT) Op.nofinstances = 0;
286N/A this.compile(this.tokentree);
286N/A /*
286N/A if (this.operations.type == Op.CLOSURE && this.operations.getChild().type == Op.DOT) { // .*
286N/A Op anchor = Op.createAnchor(isSet(this.options, SINGLE_LINE) ? 'A' : '@');
286N/A anchor.next = this.operations;
286N/A this.operations = anchor;
286N/A }
286N/A */
286N/A if (Op.COUNT) System.err.println("DEBUG: The number of operations: "+Op.nofinstances);
286N/A
286N/A this.minlength = this.tokentree.getMinLength();
286N/A
286N/A this.firstChar = null;
286N/A if (!isSet(this.options, PROHIBIT_HEAD_CHARACTER_OPTIMIZATION)
286N/A && !isSet(this.options, XMLSCHEMA_MODE)) {
286N/A RangeToken firstChar = Token.createRange();
286N/A int fresult = this.tokentree.analyzeFirstCharacter(firstChar, this.options);
286N/A if (fresult == Token.FC_TERMINAL) {
286N/A firstChar.compactRanges();
286N/A this.firstChar = firstChar;
286N/A if (DEBUG)
286N/A System.err.println("DEBUG: Use the first character optimization: "+firstChar);
286N/A }
286N/A }
286N/A
286N/A if (this.operations != null
286N/A && (this.operations.type == Op.STRING || this.operations.type == Op.CHAR)
286N/A && this.operations.next == null) {
286N/A if (DEBUG)
286N/A System.err.print(" *** Only fixed string! *** ");
286N/A this.fixedStringOnly = true;
286N/A if (this.operations.type == Op.STRING)
286N/A this.fixedString = this.operations.getString();
286N/A else if (this.operations.getData() >= 0x10000) { // Op.CHAR
286N/A this.fixedString = REUtil.decomposeToSurrogates(this.operations.getData());
286N/A } else {
286N/A char[] ac = new char[1];
286N/A ac[0] = (char)this.operations.getData();
286N/A this.fixedString = new String(ac);
286N/A }
286N/A this.fixedStringOptions = this.options;
286N/A this.fixedStringTable = new BMPattern(this.fixedString, 256,
286N/A isSet(this.fixedStringOptions, IGNORE_CASE));
286N/A } else if (!isSet(this.options, PROHIBIT_FIXED_STRING_OPTIMIZATION)
286N/A && !isSet(this.options, XMLSCHEMA_MODE)) {
286N/A Token.FixedStringContainer container = new Token.FixedStringContainer();
286N/A this.tokentree.findFixedString(container, this.options);
286N/A this.fixedString = container.token == null ? null : container.token.getString();
286N/A this.fixedStringOptions = container.options;
286N/A if (this.fixedString != null && this.fixedString.length() < 2)
286N/A this.fixedString = null;
286N/A // This pattern has a fixed string of which length is more than one.
286N/A if (this.fixedString != null) {
286N/A this.fixedStringTable = new BMPattern(this.fixedString, 256,
286N/A isSet(this.fixedStringOptions, IGNORE_CASE));
286N/A if (DEBUG) {
286N/A System.err.println("DEBUG: The longest fixed string: "+this.fixedString.length()
286N/A +"/" //+this.fixedString
286N/A +"/"+REUtil.createOptionString(this.fixedStringOptions));
286N/A System.err.print("String: ");
286N/A REUtil.dumpString(this.fixedString);
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * An option.
286N/A * If you specify this option, <span class="REGEX"><kbd>(</kbd><var>X</var><kbd>)</kbd></span>
286N/A * captures matched text, and <span class="REGEX"><kbd>(:?</kbd><var>X</var><kbd>)</kbd></span>
286N/A * does not capture.
286N/A *
286N/A * @see #RegularExpression(java.lang.String,int)
286N/A * @see #setPattern(java.lang.String,int)
286N/A static final int MARK_PARENS = 1<<0;
286N/A */
286N/A
286N/A /**
286N/A * "i"
286N/A */
286N/A static final int IGNORE_CASE = 1<<1;
286N/A
286N/A /**
286N/A * "s"
286N/A */
286N/A static final int SINGLE_LINE = 1<<2;
286N/A
286N/A /**
286N/A * "m"
286N/A */
286N/A static final int MULTIPLE_LINES = 1<<3;
286N/A
286N/A /**
286N/A * "x"
286N/A */
286N/A static final int EXTENDED_COMMENT = 1<<4;
286N/A
286N/A /**
286N/A * This option redefines <span class="REGEX"><kbd>\d \D \w \W \s \S</kbd></span>.
286N/A *
286N/A * @see #RegularExpression(java.lang.String,int)
286N/A * @see #setPattern(java.lang.String,int)
286N/A * @see #UNICODE_WORD_BOUNDARY
286N/A */
286N/A static final int USE_UNICODE_CATEGORY = 1<<5; // "u"
286N/A
286N/A /**
286N/A * An option.
286N/A * This enables to process locale-independent word boundary for <span class="REGEX"><kbd>\b \B \&lt; \></kbd></span>.
286N/A * <p>By default, the engine considers a position between a word character
286N/A * (<span class="REGEX"><Kbd>\w</kbd></span>) and a non word character
286N/A * is a word boundary.
286N/A * <p>By this option, the engine checks word boundaries with the method of
286N/A * 'Unicode Regular Expression Guidelines' Revision 4.
286N/A *
286N/A * @see #RegularExpression(java.lang.String,int)
286N/A * @see #setPattern(java.lang.String,int)
286N/A */
286N/A static final int UNICODE_WORD_BOUNDARY = 1<<6; // "w"
286N/A
286N/A /**
286N/A * "H"
286N/A */
286N/A static final int PROHIBIT_HEAD_CHARACTER_OPTIMIZATION = 1<<7;
286N/A /**
286N/A * "F"
286N/A */
286N/A static final int PROHIBIT_FIXED_STRING_OPTIMIZATION = 1<<8;
286N/A /**
286N/A * "X". XML Schema mode.
286N/A */
286N/A static final int XMLSCHEMA_MODE = 1<<9;
286N/A /**
286N/A * ",".
286N/A */
286N/A static final int SPECIAL_COMMA = 1<<10;
286N/A
286N/A
286N/A private static final boolean isSet(int options, int flag) {
286N/A return (options & flag) == flag;
286N/A }
286N/A
286N/A /**
286N/A * Creates a new RegularExpression instance.
286N/A *
286N/A * @param regex A regular expression
286N/A * @exception org.apache.xerces.utils.regex.ParseException <VAR>regex</VAR> is not conforming to the syntax.
286N/A */
286N/A public RegularExpression(String regex) throws ParseException {
286N/A this(regex, null);
286N/A }
286N/A
286N/A /**
286N/A * Creates a new RegularExpression instance with options.
286N/A *
286N/A * @param regex A regular expression
286N/A * @param options A String consisted of "i" "m" "s" "u" "w" "," "X"
286N/A * @exception org.apache.xerces.utils.regex.ParseException <VAR>regex</VAR> is not conforming to the syntax.
286N/A */
286N/A public RegularExpression(String regex, String options) throws ParseException {
286N/A this.setPattern(regex, options);
286N/A }
286N/A
286N/A /**
286N/A * Creates a new RegularExpression instance with options.
286N/A *
286N/A * @param regex A regular expression
286N/A * @param options A String consisted of "i" "m" "s" "u" "w" "," "X"
286N/A * @exception org.apache.xerces.utils.regex.ParseException <VAR>regex</VAR> is not conforming to the syntax.
286N/A */
286N/A public RegularExpression(String regex, String options, Locale locale) throws ParseException {
286N/A this.setPattern(regex, options, locale);
286N/A }
286N/A
286N/A RegularExpression(String regex, Token tok, int parens, boolean hasBackReferences, int options) {
286N/A this.regex = regex;
286N/A this.tokentree = tok;
286N/A this.nofparen = parens;
286N/A this.options = options;
286N/A this.hasBackReferences = hasBackReferences;
286N/A }
286N/A
286N/A /**
286N/A *
286N/A */
286N/A public void setPattern(String newPattern) throws ParseException {
286N/A this.setPattern(newPattern, Locale.getDefault());
286N/A }
286N/A
286N/A public void setPattern(String newPattern, Locale locale) throws ParseException {
286N/A this.setPattern(newPattern, this.options, locale);
286N/A }
286N/A
286N/A private void setPattern(String newPattern, int options, Locale locale) throws ParseException {
286N/A this.regex = newPattern;
286N/A this.options = options;
286N/A RegexParser rp = RegularExpression.isSet(this.options, RegularExpression.XMLSCHEMA_MODE)
286N/A ? new ParserForXMLSchema(locale) : new RegexParser(locale);
286N/A this.tokentree = rp.parse(this.regex, this.options);
286N/A this.nofparen = rp.parennumber;
286N/A this.hasBackReferences = rp.hasBackReferences;
286N/A
286N/A this.operations = null;
286N/A this.context = null;
286N/A }
286N/A /**
286N/A *
286N/A */
286N/A public void setPattern(String newPattern, String options) throws ParseException {
286N/A this.setPattern(newPattern, options, Locale.getDefault());
286N/A }
286N/A
286N/A public void setPattern(String newPattern, String options, Locale locale) throws ParseException {
286N/A this.setPattern(newPattern, REUtil.parseOptions(options), locale);
286N/A }
286N/A
286N/A /**
286N/A *
286N/A */
286N/A public String getPattern() {
286N/A return this.regex;
286N/A }
286N/A
286N/A /**
286N/A * Represents this instence in String.
286N/A */
286N/A public String toString() {
286N/A return this.tokentree.toString(this.options);
286N/A }
286N/A
286N/A /**
286N/A * Returns a option string.
286N/A * The order of letters in it may be different from a string specified
286N/A * in a constructor or <code>setPattern()</code>.
286N/A *
286N/A * @see #RegularExpression(java.lang.String,java.lang.String)
286N/A * @see #setPattern(java.lang.String,java.lang.String)
286N/A */
286N/A public String getOptions() {
286N/A return REUtil.createOptionString(this.options);
286N/A }
286N/A
286N/A /**
286N/A * Return true if patterns are the same and the options are equivalent.
286N/A */
286N/A public boolean equals(Object obj) {
286N/A if (obj == null) return false;
286N/A if (!(obj instanceof RegularExpression))
286N/A return false;
286N/A RegularExpression r = (RegularExpression)obj;
286N/A return this.regex.equals(r.regex) && this.options == r.options;
286N/A }
286N/A
286N/A boolean equals(String pattern, int options) {
286N/A return this.regex.equals(pattern) && this.options == options;
286N/A }
286N/A
286N/A /**
286N/A *
286N/A */
286N/A public int hashCode() {
286N/A return (this.regex+"/"+this.getOptions()).hashCode();
286N/A }
286N/A
286N/A /**
286N/A * Return the number of regular expression groups.
286N/A * This method returns 1 when the regular expression has no capturing-parenthesis.
286N/A *
286N/A */
286N/A public int getNumberOfGroups() {
286N/A return this.nofparen;
286N/A }
286N/A
286N/A // ================================================================
286N/A
286N/A private static final int WT_IGNORE = 0;
286N/A private static final int WT_LETTER = 1;
286N/A private static final int WT_OTHER = 2;
286N/A private static final int getWordType0(char ch, int opts) {
286N/A if (!isSet(opts, UNICODE_WORD_BOUNDARY)) {
286N/A if (isSet(opts, USE_UNICODE_CATEGORY)) {
286N/A return (Token.getRange("IsWord", true).match(ch)) ? WT_LETTER : WT_OTHER;
286N/A }
286N/A return isWordChar(ch) ? WT_LETTER : WT_OTHER;
286N/A }
286N/A
286N/A switch (Character.getType(ch)) {
286N/A case Character.UPPERCASE_LETTER: // L
286N/A case Character.LOWERCASE_LETTER: // L
286N/A case Character.TITLECASE_LETTER: // L
286N/A case Character.MODIFIER_LETTER: // L
286N/A case Character.OTHER_LETTER: // L
286N/A case Character.LETTER_NUMBER: // N
286N/A case Character.DECIMAL_DIGIT_NUMBER: // N
286N/A case Character.OTHER_NUMBER: // N
286N/A case Character.COMBINING_SPACING_MARK: // Mc
286N/A return WT_LETTER;
286N/A
286N/A case Character.FORMAT: // Cf
286N/A case Character.NON_SPACING_MARK: // Mn
286N/A case Character.ENCLOSING_MARK: // Mc
286N/A return WT_IGNORE;
286N/A
286N/A case Character.CONTROL: // Cc
286N/A switch (ch) {
286N/A case '\t':
286N/A case '\n':
286N/A case '\u000B':
286N/A case '\f':
286N/A case '\r':
286N/A return WT_OTHER;
286N/A default:
286N/A return WT_IGNORE;
286N/A }
286N/A
286N/A default:
286N/A return WT_OTHER;
286N/A }
286N/A }
286N/A
286N/A // ================================================================
286N/A
286N/A static final int LINE_FEED = 0x000A;
286N/A static final int CARRIAGE_RETURN = 0x000D;
286N/A static final int LINE_SEPARATOR = 0x2028;
286N/A static final int PARAGRAPH_SEPARATOR = 0x2029;
286N/A
286N/A private static final boolean isEOLChar(int ch) {
286N/A return ch == LINE_FEED || ch == CARRIAGE_RETURN || ch == LINE_SEPARATOR
286N/A || ch == PARAGRAPH_SEPARATOR;
286N/A }
286N/A
286N/A private static final boolean isWordChar(int ch) { // Legacy word characters
286N/A if (ch == '_') return true;
286N/A if (ch < '0') return false;
286N/A if (ch > 'z') return false;
286N/A if (ch <= '9') return true;
286N/A if (ch < 'A') return false;
286N/A if (ch <= 'Z') return true;
286N/A if (ch < 'a') return false;
286N/A return true;
286N/A }
286N/A
286N/A private static final boolean matchIgnoreCase(int chardata, int ch) {
286N/A if (chardata == ch) return true;
286N/A if (chardata > 0xffff || ch > 0xffff) return false;
286N/A char uch1 = Character.toUpperCase((char)chardata);
286N/A char uch2 = Character.toUpperCase((char)ch);
286N/A if (uch1 == uch2) return true;
286N/A return Character.toLowerCase(uch1) == Character.toLowerCase(uch2);
286N/A }
286N/A}