Debian Codegraph @

src/main/java/net/sf/antcontrib/property/RegexUtil.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.property;
17
18 import java.util.Enumeration;
19 import java.util.Vector;
20
21 /****************************************************************************
22 * Regular Expression utilities
23 *
24 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
25 *
26 ****************************************************************************/
27
28
29 public class RegexUtil
30 {
31
32 /***
33 * An abritrary node in a select expression
34 */
35 private static interface SelectNode
36 {
37 /***
38 * Select the value based on the groups
39 * @param groups The groups found in the match
40 */
41 public String select(Vector groups);
42 }
43
44 /***
45 * A group node in a select expression
46 */
47 private static class GroupSelectNode
48 implements SelectNode
49 {
50 private int groupNumber;
51
52 public GroupSelectNode(int groupNumber)
53 {
54 this.groupNumber = groupNumber;
55 }
56
57 public String select(Vector groups)
58 {
59 if ( groupNumber < groups.size())
60 return (String)groups.elementAt(groupNumber);
61 else
62 return "\\" + groupNumber;
63 }
64
65 public String toString()
66 {
67 return "group: " + groupNumber;
68 }
69 }
70
71 /***
72 * An abritrary node in a select expression
73 */
74 private static class StringSelectNode
75 implements SelectNode
76 {
77 private String text;
78
79 public StringSelectNode(String text)
80 {
81 this.text = text;
82 }
83
84 public String select(Vector groups)
85 {
86 return text;
87 }
88
89 public String toString()
90 {
91 return "string: " + text;
92 }
93 }
94
95 /***
96 * Parses a select string into a List of SelectNode objects.
97 * These objects can then be merged with a group list to produce
98 * an output string (using the "select" method)
99 * @param input The select string
100 * @return a List of SelectNode objects
101 */
102 private static Vector parseSelectString(String input)
103 {
104 Vector nodes = new Vector();
105 StringBuffer buf = new StringBuffer();
106 char c[] = input.toCharArray();
107 for (int i=0;i<c.length;i++)
108 {
109 if (c[i] == '\\')
110 {
111 if (buf.length() > 0)
112 {
113 nodes.addElement(new StringSelectNode(buf.toString()));
114 buf.setLength(0);
115 }
116
117 while (i+1 < c.length && Character.isDigit(c[i+1]))
118 {
119 buf.append(c[i+1]);
120 i++;
121 }
122
123 int groupNum = Integer.parseInt(buf.toString());
124 buf.setLength(0);
125 nodes.addElement(new GroupSelectNode(groupNum));
126 }
127 else
128 {
129 buf.append(c[i]);
130 }
131 }
132
133
134 if (buf.length() > 0)
135 {
136 nodes.addElement(new StringSelectNode(buf.toString()));
137 buf.setLength(0);
138 }
139
140 return nodes;
141 }
142
143 /***
144 * Parse a select string, and merge it with a match groups
145 * vector to produce an output string. Each group placehold
146 * in the select string is replaced with the group at the
147 * corresponding index in the match groups vector
148 * @param select The select string
149 * @param groups The match groups
150 * @return The output string with the merged selection
151 */
152 public static String select(String select, Vector groups)
153 {
154 Vector nodes = parseSelectString(select);
155
156 StringBuffer buf = new StringBuffer();
157 Enumeration e = nodes.elements();
158 SelectNode node = null;
159 while (e.hasMoreElements())
160 {
161 node = (SelectNode)e.nextElement();
162 buf.append(node.select(groups));
163 }
164 return buf.toString();
165 }
166 }