001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.wsdlextensions.jms.validator;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.text.MessageFormat;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.Properties;
028:
029: /**
030: * Provides for easy string formatting
031: */
032: public class Str {
033: /**
034: * Formats a msg
035: *
036: * @return formatted string
037: * @param msg String
038: * @param args Object[]
039: */
040: public static String msg(String msg, Object[] args) {
041: return MessageFormat.format(msg, args);
042: }
043:
044: /**
045: * Formats a msg
046: *
047: * @return formatted string
048: * @param msg String
049: */
050: public static String msg(String msg) {
051: return msg(msg, new Object[] {});
052: }
053:
054: /**
055: * Formats a msg
056: *
057: * @return formatted string
058: * @param msg String
059: * @param arg1 Object
060: */
061: public static String msg(String msg, Object arg1) {
062: return msg(msg, new Object[] { arg1 });
063: }
064:
065: /**
066: * Formats a msg
067: *
068: * @return formatted string
069: * @param msg String
070: * @param arg1 Object
071: * @param arg2 Object
072: */
073: public static String msg(String msg, Object arg1, Object arg2) {
074: return msg(msg, new Object[] { arg1, arg2 });
075: }
076:
077: /**
078: * Formats a msg
079: *
080: * @return formatted string
081: * @param msg String
082: * @param arg1 Object
083: * @param arg2 Object
084: * @param arg3 Object
085: */
086: public static String msg(String msg, Object arg1, Object arg2,
087: Object arg3) {
088: return msg(msg, new Object[] { arg1, arg2, arg3 });
089: }
090:
091: /**
092: * Formats a msg
093: *
094: * @return formatted string
095: * @param msg String
096: * @param arg1 Object
097: * @param arg2 Object
098: * @param arg3 Object
099: * @param arg4 Object
100: */
101: public static String msg(String msg, Object arg1, Object arg2,
102: Object arg3, Object arg4) {
103: return msg(msg, new Object[] { arg1, arg2, arg3, arg4 });
104: }
105:
106: /**
107: * Formats a msg
108: *
109: * @return formatted string
110: * @param msg String
111: * @param arg1 Object
112: * @param arg2 Object
113: * @param arg3 Object
114: * @param arg4 Object
115: * @param arg5 Object
116: */
117: public static String msg(String msg, Object arg1, Object arg2,
118: Object arg3, Object arg4, Object arg5) {
119: return msg(msg, new Object[] { arg1, arg2, arg3, arg4, arg5 });
120: }
121:
122: /**
123: * Converts a password to a string suitable to display in log files
124: *
125: * @param inp password
126: * @return neutralized string
127: */
128: public static String password(String inp) {
129: if (inp == null) {
130: return "null";
131: } else if (inp.length() == 0) {
132: return "zero-length";
133: } else {
134: return "###";
135: }
136: }
137:
138: /**
139: * Returns true if the specified string is empty (null, "" or just spaces)
140: *
141: * @param s String
142: * @return boolean true if empty
143: */
144: public static boolean empty(String s) {
145: if (s == null || s.length() == 0) {
146: return true;
147: }
148:
149: for (int i = 0; i < s.length(); i++) {
150: if (!Character.isSpaceChar(s.charAt(i))) {
151: return false;
152: }
153: }
154:
155: return true;
156: }
157:
158: /**
159: * isEqual
160: *
161: * @param a String
162: * @param b String
163: * @return boolean
164: */
165: public static boolean isEqual(String a, String b) {
166: if (a == null) {
167: return (b == null);
168: } else {
169: return a.equals(b);
170: }
171: }
172:
173: /**
174: * hash
175: *
176: * @param seed int
177: * @param o Object
178: * @return int
179: */
180: public static int hash(int seed, Object o) {
181: if (o == null) {
182: return seed + 17;
183: }
184: return seed * 37 + o.hashCode();
185: }
186:
187: /**
188: * Hash tool
189: *
190: * @param seed int
191: * @param o boolean
192: * @return int
193: */
194: public static int hash(int seed, boolean o) {
195: return seed * 37 + (o ? 3 : 7);
196: }
197:
198: /**
199: * Parses the specified properties and merges them into the
200: * specified properties set.
201: *
202: * @param s serialized properties; may be empty
203: * @param toAdd properties set to merge into
204: */
205: public static void deserializeProperties(String s, Properties toAdd)
206: throws ValidationException {
207: if (empty(s)) {
208: return;
209: }
210:
211: try {
212: // Load
213: Properties p = new Properties();
214: ByteArrayInputStream inp = new ByteArrayInputStream(s
215: .getBytes("ISO-8859-1"));
216: p.load(inp);
217:
218: // Copy
219: for (Iterator iter = p.entrySet().iterator(); iter
220: .hasNext();) {
221: Map.Entry element = (Map.Entry) iter.next();
222: toAdd.put(element.getKey(), element.getValue());
223: }
224: } catch (Exception e) {
225: throw new ValidationException("Failed to load properties: "
226: + e, e);
227: }
228: }
229:
230: /**
231: * Serializes a properties set to a String
232: *
233: * @param p properties to serialize
234: * @return String
235: */
236: public static String serializeProperties(Properties p)
237: throws ValidationException {
238: try {
239: ByteArrayOutputStream out = new ByteArrayOutputStream();
240: p.store(out, "");
241: return out.toString("ISO-8859-1");
242: } catch (Exception e) {
243: throw new ValidationException(
244: "Failed to serialize properties: " + e, e);
245: }
246: }
247:
248: // /**
249: // * Serializes a properties set to a String
250: // *
251: // * @param p properties to serialize
252: // * @return String
253: // */
254: // public static String propertiesToString(Properties p) {
255: // StringBuffer ret = new StringBuffer();
256: // for (Iterator iter = p.entrySet().iterator(); iter.hasNext();) {
257: // Map.Entry x = (Map.Entry) iter.next();
258: // ret.append(x.getKey()).append(" = ").append(x.getValue());
259: // }
260: // return ret.toString();
261: // }
262:
263: /**
264: * Concatenates string components
265: *
266: * @param strs components
267: * @param delim delimeter, e.g. ", "
268: * @return concatenated string
269: */
270: public static String concat(Object[] strs, String delim) {
271: StringBuffer ret = new StringBuffer();
272: for (int i = 0; i < strs.length; i++) {
273: if (i != 0) {
274: ret.append(delim);
275: }
276: ret.append(strs[i]);
277: }
278: return ret.toString();
279: }
280:
281: /**
282: * Returns if a string is empty or null
283: *
284: * @param s string to test
285: * @return true if null or empty
286: */
287: public boolean isEmpty(String s) {
288: return s == null || s.length() == 0;
289: }
290: }
|