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.snmp.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: if (empty(s)) {
207: return;
208: }
209:
210: try {
211: // Load
212: Properties p = new Properties();
213: ByteArrayInputStream inp = new ByteArrayInputStream(s
214: .getBytes("ISO-8859-1"));
215: p.load(inp);
216:
217: // Copy
218: for (Iterator iter = p.entrySet().iterator(); iter
219: .hasNext();) {
220: Map.Entry element = (Map.Entry) iter.next();
221: toAdd.put(element.getKey(), element.getValue());
222: }
223: } catch (Exception e) {
224: throw new RuntimeException("Failed to load properties: "
225: + e, e);
226: }
227: }
228:
229: /**
230: * Serializes a properties set to a String
231: *
232: * @param p properties to serialize
233: * @return String
234: */
235: public static String serializeProperties(Properties p) {
236: try {
237: ByteArrayOutputStream out = new ByteArrayOutputStream();
238: p.store(out, "");
239: return out.toString("ISO-8859-1");
240: } catch (Exception e) {
241: throw new RuntimeException(
242: "Failed to serialize properties: " + e, e);
243: }
244: }
245:
246: // /**
247: // * Serializes a properties set to a String
248: // *
249: // * @param p properties to serialize
250: // * @return String
251: // */
252: // public static String propertiesToString(Properties p) {
253: // StringBuffer ret = new StringBuffer();
254: // for (Iterator iter = p.entrySet().iterator(); iter.hasNext();) {
255: // Map.Entry x = (Map.Entry) iter.next();
256: // ret.append(x.getKey()).append(" = ").append(x.getValue());
257: // }
258: // return ret.toString();
259: // }
260:
261: /**
262: * Concatenates string components
263: *
264: * @param strs components
265: * @param delim delimeter, e.g. ", "
266: * @return concatenated string
267: */
268: public static String concat(Object[] strs, String delim) {
269: StringBuffer ret = new StringBuffer();
270: for (int i = 0; i < strs.length; i++) {
271: if (i != 0) {
272: ret.append(delim);
273: }
274: ret.append(strs[i]);
275: }
276: return ret.toString();
277: }
278:
279: /**
280: * Returns if a string is empty or null
281: *
282: * @param s string to test
283: * @return true if null or empty
284: */
285: public boolean isEmpty(String s) {
286: return s == null || s.length() == 0;
287: }
288: }
|