001: /*
002: * $Id: substituteExample.java,v 1.8 2003/11/07 20:16:23 dfs Exp $
003: *
004: * ====================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 2000 The Apache Software Foundation. All rights
008: * reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution,
023: * if any, must include the following acknowledgment:
024: * "This product includes software developed by the
025: * Apache Software Foundation (http://www.apache.org/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
030: * must not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact apache@apache.org.
033: *
034: * 5. Products derived from this software may not be called "Apache"
035: * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
036: * name, without prior written permission of the Apache Software Foundation.
037: *
038: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: * ====================================================================
051: *
052: * This software consists of voluntary contributions made by many
053: * individuals on behalf of the Apache Software Foundation. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package examples.awk;
059:
060: import org.apache.oro.text.regex.*;
061: import org.apache.oro.text.awk.*;
062:
063: /**
064: * This is a test program demonstrating the use of the Util.substitute()
065: * method. It is the same as the version in the OROMatcher distribution
066: * except that it uses Awk classes instead of Awk classes.
067: *
068: * @version @version@
069: */
070: public final class substituteExample {
071:
072: /**
073: * A good way for you to understand the substitute() method is to play around
074: * with it by using this test program. The program takes 3 to 5 arguments
075: * as follows:
076: * java substituteExample
077: * regex substitution input [sub limit] [interpolation limit]
078: * regex - A regular expression used to find parts of the input to be
079: * substituted.
080: * sub limit - An optional argument limiting the number of substitutions.
081: * If no limit is given, the limit used is Util.SUBSTITUTE_ALL.
082: * input - A string to be used as input for substitute().
083: * interpolation limit - An optional argument limiting the number of
084: * interpolations performed.
085: *
086: * Try the following command line for a simple example of subsitute().
087: * It changes (2,3) to (3,2) in the input.
088: * java substituteExample '\(2,3\)' '(3, 2)' '(1,2) (2,3) (4,5)'
089: *
090: * The following command line shows the substitute limit at work. It
091: * changed the first four 1's in the input to 4's.
092: * java substituteExample '1' '4' '381298175 1111'
093: */
094: public static final void main(String args[]) {
095: int limit;
096: PatternMatcher matcher = new AwkMatcher();
097: Pattern pattern = null;
098: PatternCompiler compiler = new AwkCompiler();
099: String regularExpression, input, result;
100: Substitution sub;
101:
102: // Make sure there are sufficient arguments
103: if (args.length < 3) {
104: System.err
105: .println("Usage: substituteExample regex substitution "
106: + "input [sub limit]");
107: System.exit(1);
108: }
109:
110: limit = Util.SUBSTITUTE_ALL;
111:
112: regularExpression = args[0];
113: sub = new StringSubstitution(args[1]);
114: input = args[2];
115:
116: if (args.length > 3)
117: limit = Integer.parseInt(args[3]);
118:
119: try {
120: pattern = compiler.compile(regularExpression);
121: System.out
122: .println("substitute regex: " + regularExpression);
123: } catch (MalformedPatternException e) {
124: System.err.println("Bad pattern.");
125: System.err.println(e.getMessage());
126: System.exit(1);
127: }
128:
129: // Perform substitution and print result.
130: result = Util.substitute(matcher, pattern, sub, input, limit);
131: System.out.println("result: " + result);
132: }
133: }
|