01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.wicket.util.parse.metapattern;
18:
19: import java.util.regex.Matcher;
20:
21: /**
22: * A Group is a piece of a regular expression (referenced by some Java field or
23: * local variable) that forms a "capturing group" within the larger regular
24: * expression. A Group is bound to a regular expression MetaPattern when a
25: * matcher is retrieved for the pattern by calling one of the matcher() methods.
26: * Once bound, a Group cannot be rebound.
27: *
28: * @author Jonathan Locke
29: */
30: public class Group extends MetaPattern {
31: private static final long serialVersionUID = 1L;
32:
33: /** The capturing group that this Group is bound to. */
34: private int group = -1;
35:
36: /**
37: * Constructor.
38: *
39: * @param pattern
40: * MetaPattern to capture
41: */
42: public Group(final MetaPattern pattern) {
43: super (pattern);
44: }
45:
46: /**
47: * Threadsafe method to retrieve contents of this captured group.
48: *
49: * @param matcher
50: * The matcher from which to retrieve this Group's group
51: * @return The captured characters
52: */
53: public final String get(final Matcher matcher) {
54: if (group == -1) {
55: throw new GroupNotBoundException();
56: }
57:
58: return matcher.group(group);
59: }
60:
61: /**
62: * @see java.lang.Object#toString()
63: */
64: public String toString() {
65: return "(" + super .toString() + ")";
66: }
67:
68: /**
69: * Binds this capture expression if not already bound.
70: *
71: * @param bindTo
72: * The group to bind to
73: * @throws GroupAlreadyBoundException
74: * Thrown if this Group is already bound
75: */
76: final void bind(final int bindTo) {
77: if (this .group == -1) {
78: this .group = bindTo;
79: } else {
80: throw new GroupAlreadyBoundException();
81: }
82: }
83: }
|