001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.util;
010:
011: import java.util.Arrays;
012: import java.util.Properties;
013: import java.util.ArrayList;
014:
015: /**
016: * @author Gennady Krizhevsky
017: */
018: public class StringUtil {
019:
020: public static String lpadNumber(Number number, int maxDigits) {
021: if (number == null) {
022: return null;
023: }
024: String num = number.toString();
025: return lpadNumber(num, maxDigits);
026: }
027:
028: public static String lpadNumber(String number, int maxDigits) {
029: return lpadString(number, maxDigits, '0');
030: }
031:
032: public static String lpadString(String number, int maxDigits,
033: char padChar) {
034: if (number == null || number.trim().length() == 0) {
035: return null;
036: }
037: if (number.length() >= maxDigits) {
038: return number;
039: }
040: char[] chars = new char[maxDigits - number.length()];
041:
042: String pad = "";
043: if (chars.length > 0) {
044: Arrays.fill(chars, padChar);
045: pad = new String(chars);
046: }
047: return pad + number;
048: }
049:
050: public static String indent(int numberOfSpaces, String s) {
051: char[] chars = new char[numberOfSpaces];
052: String pad = "";
053: if (chars.length > 0) {
054: Arrays.fill(chars, ' ');
055: pad = new String(chars);
056: }
057: return pad + s;
058: }
059:
060: public static boolean containsOnly(String value, char ch) {
061: if (value == null || value.length() == 0) {
062: return true;
063: }
064: boolean rc = true;
065: for (int i = 0; i < value.length(); i++) {
066: if (value.charAt(i) != ch) {
067: rc = false;
068: break;
069: }
070: }
071: return rc;
072: }
073:
074: public static String capitalizeFirst(String token) {
075: if (token == null || token.length() == 0) {
076: return token;
077: }
078: StringBuffer buffer = new StringBuffer(token);
079: buffer.setCharAt(0, (Character.toString(buffer.charAt(0))
080: .toUpperCase()).charAt(0));
081: return buffer.toString();
082: }
083:
084: public static String uncapitalizeFirst(String token) {
085: if (token == null || token.length() == 0) {
086: return token;
087: }
088: StringBuffer buffer = new StringBuffer(token);
089: buffer.setCharAt(0, (Character.toString(buffer.charAt(0))
090: .toLowerCase()).charAt(0));
091: return buffer.toString();
092: }
093:
094: public static boolean isEmpty(String s) {
095: return s == null || s.trim().length() == 0;
096: }
097:
098: public static String toString(Object o, String nullString) {
099: return o == null ? nullString : o.toString();
100: }
101:
102: public static String toString(Object o) {
103: return toString(o, "");
104: }
105:
106: /**
107: * Replaces expressions ${token} with System.getProperties().get(token) values
108: *
109: * @param value value to replace
110: * @return value with replced ${token} parameters
111: */
112: public static String resolveParameters(String value) {
113: return resolveParameters(value, null);
114: }
115:
116: /**
117: * Replaces expressions ${token} with properties.get(token) values
118: *
119: * @param value value to replace
120: * @param properties replacement properties. If null System.getProperties() are used.
121: * @return value with replced ${token} parameters
122: */
123: public static String resolveParameters(String value,
124: Properties properties) {
125: if (value == null || value.length() == 0) {
126: return value;
127: }
128: properties = properties == null ? System.getProperties()
129: : properties;
130:
131: ArrayList parameters = new ArrayList();
132: collectTokens(value, parameters, 0);
133: for (int i = 0; i < parameters.size(); i++) {
134: String token = (String) parameters.get(i);
135: String replacementValue = properties.getProperty(token);
136: if (replacementValue != null) {
137: String fullToken = "\\$\\{" + token + "\\}";
138: value = value.replaceAll(fullToken, replacementValue);
139: } else {
140: throw new IllegalArgumentException(
141: "Cannot find system parameter by name ["
142: + token + "]");
143: }
144: }
145: return value;
146: }
147:
148: private static int collectTokens(String value,
149: ArrayList parameters, int searchFromIndex) {
150: int startIndex;
151: if ((startIndex = value.indexOf('$', searchFromIndex)) >= 0) {
152: if (startIndex < value.length() - 1
153: && '{' == value.charAt(startIndex + 1)) {
154: int endIndex = value.indexOf('}', startIndex);
155: if (endIndex < 0) {
156: throw new IllegalArgumentException(
157: "Cannot find end ('}') of parameter");
158: }
159: String parameter = value.substring(startIndex + 2,
160: endIndex);
161: parameters.add(parameter);
162: collectTokens(value, parameters, endIndex);
163: }
164: }
165: return searchFromIndex;
166: }
167:
168: public static String shortClassName(String className) {
169: if (className == null) {
170: return null;
171: }
172: String[] tokens = className.split("\\.");
173: return tokens[tokens.length - 1];
174:
175: }
176:
177: public static void main(String[] args) {
178: String value = "${token1}:${token2}";
179: Properties properties = new Properties();
180: properties.put("token1", "aaa");
181: properties.put("token2", "bbb");
182:
183: String s = resolveParameters(value, properties);
184: System.out.println(s);
185: }
186:
187: }
|