001: /*
002: * @(#)MessageUtils.java 1.16 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.misc;
029:
030: /**
031: * MessageUtils: miscellaneous utilities for handling error and status
032: * properties and messages.
033: *
034: * @version 1.12, 08/19/02
035: * @author Herb Jellinek
036: */
037:
038: public class MessageUtils {
039: // can instantiate it for to allow less verbose use - via instance
040: // instead of classname
041:
042: public MessageUtils() {
043: }
044:
045: public static String subst(String patt, String arg) {
046: String args[] = { arg };
047: return subst(patt, args);
048: }
049:
050: public static String subst(String patt, String arg1, String arg2) {
051: String args[] = { arg1, arg2 };
052: return subst(patt, args);
053: }
054:
055: public static String subst(String patt, String arg1, String arg2,
056: String arg3) {
057: String args[] = { arg1, arg2, arg3 };
058: return subst(patt, args);
059: }
060:
061: public static String subst(String patt, String args[]) {
062: StringBuffer result = new StringBuffer();
063: int len = patt.length();
064: for (int i = 0; i >= 0 && i < len; i++) {
065: char ch = patt.charAt(i);
066: if (ch == '%') {
067: if (i != len) {
068: int index = Character.digit(patt.charAt(i + 1), 10);
069: if (index == -1) {
070: result.append(patt.charAt(i + 1));
071: i++;
072: } else if (index < args.length) {
073: result.append(args[index]);
074: i++;
075: }
076: }
077: } else {
078: result.append(ch);
079: }
080: }
081: return result.toString();
082: }
083:
084: public static String substProp(String propName, String arg) {
085: return subst(System.getProperty(propName), arg);
086: }
087:
088: public static String substProp(String propName, String arg1,
089: String arg2) {
090: return subst(System.getProperty(propName), arg1, arg2);
091: }
092:
093: public static String substProp(String propName, String arg1,
094: String arg2, String arg3) {
095: return subst(System.getProperty(propName), arg1, arg2, arg3);
096: }
097:
098: public static void main(String args[]) {
099: String stringArgs[] = new String[args.length - 1];
100: System.arraycopy(args, 1, stringArgs, 0, args.length - 1);
101: System.out.println("> result = " + subst(args[0], stringArgs));
102: }
103: }
|