1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.tools

File RegexTool.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
25
9
2
182
73
14
0.56
2.78
4.5
1.56

Classes

Class Line # Actions
RegexTool 34 19 0% 10 0
1.0100%
RegexTool.RegexResult 39 6 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.velocity.tools;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24    import java.util.regex.Matcher;
25    import java.util.regex.Pattern;
26    import java.util.regex.PatternSyntaxException;
27   
28    /**
29    * Velocity Tool offering various Regex-based APIs to make it easy to manipulate regular expressions from Velocity.
30    *
31    * @version $Id: 97c88bdced62ea395e94a51224de2a171a6f6df3 $
32    * @since 2.0RC2
33    */
 
34    public class RegexTool
35    {
36    /**
37    * Result of a Regex search.
38    */
 
39    public class RegexResult
40    {
41    /**
42    * @see #getStart()
43    */
44    public int start;
45   
46    /**
47    * @see #getEnd()
48    */
49    public int end;
50   
51    /**
52    * @see #getGroup()
53    */
54    public String group;
55   
56    /**
57    * @param start see {@link #getStart()}
58    * @param end see {@link #getEnd()}
59    * @param group see {@link #getGroup()}
60    */
 
61  227 toggle public RegexResult(int start, int end, String group)
62    {
63  227 this.start = start;
64  227 this.end = end;
65  227 this.group = group;
66    }
67   
68    /**
69    * @return the captured group
70    */
 
71  113 toggle public String getGroup()
72    {
73  113 return this.group;
74    }
75   
76    /**
77    * @return the capture group's start position
78    */
 
79  11 toggle public int getStart()
80    {
81  11 return this.start;
82    }
83   
84    /**
85    * @return the capture group's end position
86    */
 
87  20 toggle public int getEnd()
88    {
89  20 return this.end;
90    }
91    }
92   
93    /**
94    * @param content the content to parse
95    * @param regex the regex to look for in the passed content
96    * @return empty list if the passed regex doesn't match the content or several {@link RegexResult} objects
97    * containing the matched position and matched content for all capturing groups, the first group
98    * representing the whole . The first object is represents the entire pattern
99    */
 
100  110 toggle public List<RegexResult> find(String content, String regex)
101    {
102  110 List<RegexResult> result = new ArrayList<RegexResult>();
103  110 Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(content);
104  110 if (matcher.find()) {
105  303 for (int i = 0; i < matcher.groupCount() + 1; i++) {
106  221 result.add(new RegexResult(matcher.start(i), matcher.end(i), matcher.group(i)));
107    }
108    }
109  110 return result;
110    }
111   
112    /**
113    * @param content the content to parse
114    * @param regex the regular expression to look for in the passed content
115    * @return an empty list if the passed regular expression doesn't match the content, several {@link RegexResult}
116    * objects containing the matched position and matched content for all capturing groups and sub-groups
117    * otherwise
118    */
 
119  2 toggle public List<List<RegexResult>> findAll(String content, String regex)
120    {
121  2 List<List<RegexResult>> result = new ArrayList<>();
122  2 Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(content);
123  4 while (matcher.find()) {
124  2 List<RegexResult> match = new ArrayList<>();
125  8 for (int i = 0; i < matcher.groupCount() + 1; i++) {
126  6 match.add(new RegexResult(matcher.start(i), matcher.end(i), matcher.group(i)));
127    }
128  2 result.add(match);
129    }
130  2 return result;
131    }
132   
133    /**
134    * Compiles a regular expression into a java {@code Pattern} object.
135    *
136    * @param regex the textual representation of the regular expression
137    * @return the {@code Pattern} object corresponding to the regular expression, or {@code null} if the expression is
138    * invalid
139    * @since 2.3M1
140    */
 
141  3 toggle public Pattern compile(String regex)
142    {
143  3 try {
144  3 return Pattern.compile(regex);
145    } catch (PatternSyntaxException ex) {
146  1 return null;
147    }
148    }
149   
150    /**
151    * Returns a literal pattern <code>String</code> for the specified <code>String</code>.
152    * <p>
153    * This method produces a <code>String</code> that can be used to create a <code>Pattern</code> that would match the
154    * string <code>s</code> as if it were a literal pattern.
155    * </p>
156    * Metacharacters or escape sequences in the input sequence will be given no special meaning.
157    *
158    * @param s The string to be literalized
159    * @return A literal string replacement
160    * @since 2.4M2
161    */
 
162  271 toggle public String quote(String s)
163    {
164  271 return Pattern.quote(s);
165    }
166   
167    /**
168    * Returns a literal replacement {@code String} for the specified {@code String}. This method produces a
169    * {@code String} that will work as a literal replacement {@code s} in
170    * {@code String#replaceAll(regularExpression, s)}. The {@code String} produced will match the sequence of
171    * characters in {@code s} treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no
172    * special meaning.
173    *
174    * @param s the string to be literalized
175    * @return a literal string replacement
176    * @since 8.2RC1
177    */
 
178  5 toggle public String quoteReplacement(String s)
179    {
180  5 return Matcher.quoteReplacement(s);
181    }
182    }