Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RegexQuoter |
|
| 2.3333333333333335;2,333 |
1 | /* | |
2 | * Copyright (c) 2006 - 2011 Michael Schierl | |
3 | * | |
4 | * All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * | |
10 | * - Redistributions of source code must retain the above copyright notice, | |
11 | * this list of conditions and the following disclaimer. | |
12 | * | |
13 | * - Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * | |
17 | * - Neither name of the copyright holders nor the names of its | |
18 | * contributors may be used to endorse or promote products derived from | |
19 | * this software without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS | |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
25 | * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
27 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
28 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
29 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
30 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | |
31 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
32 | */ | |
33 | package net.sf.jmatchparser.util; | |
34 | ||
35 | import java.util.regex.Pattern; | |
36 | ||
37 | /** | |
38 | * Utility class for quoting regular expressions for matching literal text. | |
39 | * | |
40 | * This class was very useful in Java 1.4, where {@link Pattern#quote(String)} | |
41 | * did not exist yet. Nowadays, only its {@link #quotePartially(String)} | |
42 | * function is useful. | |
43 | */ | |
44 | 0 | public class RegexQuoter { |
45 | ||
46 | /** | |
47 | * Quote an expression. | |
48 | * | |
49 | * @param expression | |
50 | * any text | |
51 | * @return a regular expression that matches exactly the input text | |
52 | * @deprecated as of Java 1.5, use {@link Pattern#quote(String)} instead | |
53 | */ | |
54 | @Deprecated | |
55 | public static String quote(String expression) { | |
56 | 0 | return Pattern.quote(expression); |
57 | } | |
58 | ||
59 | /** | |
60 | * Quote an expression partially. Parts between « and » are not | |
61 | * quoted, but left inside the expression. | |
62 | */ | |
63 | public static String quotePartially(String expression) { | |
64 | 2 | return quotePartially(expression, "«", "»"); |
65 | } | |
66 | ||
67 | /** | |
68 | * Quote an expression partially. Parts between <i>startDelimiter</i> and | |
69 | * <i>endDelimiter</i> are not quoted, but left inside the expression. | |
70 | */ | |
71 | public static String quotePartially(String expression, String startDelimiter, String endDelimiter) { | |
72 | 2 | StringBuilder sb = new StringBuilder(); |
73 | int pos; | |
74 | 6 | while ((pos = expression.indexOf(startDelimiter)) != -1) { |
75 | 4 | sb.append(Pattern.quote(expression.substring(0, pos))).append("("); |
76 | 4 | expression = expression.substring(pos + startDelimiter.length()); |
77 | 4 | pos = expression.indexOf(endDelimiter); |
78 | 4 | if (pos == -1) |
79 | 0 | throw new RuntimeException("Missing delimiter"); |
80 | 4 | if (pos == 0) |
81 | 1 | sb.append(".*?"); |
82 | 4 | sb.append(expression.substring(0, pos)).append(")"); |
83 | 4 | expression = expression.substring(pos + endDelimiter.length()); |
84 | } | |
85 | 2 | return sb.append(Pattern.quote(expression)).toString(); |
86 | } | |
87 | } |