View Javadoc

1   /*
2    * Copyright 2008 University Corporation for Advanced Internet Development, Inc.
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  
17  package edu.internet2.middleware.ant.util;
18  
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  
22  import org.apache.tools.ant.BuildException;
23  import org.apache.tools.ant.Task;
24  
25  /**
26   * A task that splits a string based on a regular expression.
27   * 
28   * This ant task requires three attributes:
29   * <ul>
30   * <li><strong>input</strong> - the value to be split by the regular expression</li>
31   * <li><strong>regex</strong> - regular expression, with one matching group, use to split the string</li>
32   * <li><strong>addProperty</strong> - name of the property that will receive the split string</li>
33   * <ul>
34   */
35  public class RegexSplit extends Task {
36  
37      /** The input string to be split. */
38      private String input;
39  
40      /** The regular expression used to split the input string. */
41      private String regex;
42  
43      /** The name of the property that will receive the split string. */
44      private String addProperty;
45  
46      /** {@inheritDoc} */
47      public void execute() throws BuildException {
48          Pattern regexPat = Pattern.compile(regex);
49          Matcher stringSplitter = regexPat.matcher(input);
50          stringSplitter.matches();
51          getProject().setProperty(addProperty, stringSplitter.group(1));
52      }
53  
54      /**
55       * Sets the input string to be split.
56       * 
57       * @param input input string to be split
58       */
59      public void setInput(String input) {
60          this.input = input.trim();
61      }
62  
63      /**
64       * Sets the regular expression used to split the input string.
65       * 
66       * @param regex regular expression used to split the input string
67       */
68      public void setRegex(String regex) {
69          this.regex = regex;
70      }
71  
72      /**
73       * Sets the name of the property that will receive the split string.
74       * 
75       * @param addProperty name of the property that will receive the split string
76       */
77      public void setAddProperty(String addProperty) {
78          this.addProperty = addProperty;
79      }
80  
81  }