Source Code Cross Referenced for CommonUtils.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » uPortal_rel 2 6 1 GA » org.jasig.portal.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2002 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal.utils;
007:
008:        import java.io.PrintWriter;
009:        import java.io.StringWriter;
010:        import java.util.Enumeration;
011:        import java.util.Hashtable;
012:        import java.util.Properties;
013:        import java.util.StringTokenizer;
014:        import javax.servlet.http.HttpServletResponse;
015:
016:        /**
017:         * CommonUtils class contains base useful utilities
018:         * @author <a href="mailto:mvi@immagic.com">Michael Ivanov</a>
019:         * @version $Revision: 34793 $
020:         */
021:        public class CommonUtils {
022:
023:            /**
024:             * Sets "no cache" directives to the given response
025:             * @param res <code>HttpServletResponse</code> response
026:             */
027:            public static void setNoCache(HttpServletResponse res) {
028:                res.setHeader("Pragma", "no-cache");
029:                res.setHeader("Cache-Control", "no-cache");
030:                res.setDateHeader("Expires", 0);
031:            }
032:
033:            /**
034:             * Replaces "sourceString" with "replaceString" if sourceString equals null
035:             * @param sourceStr the string to replace
036:             * @param replaceStr the replacement string
037:             * @return the processed string
038:             */
039:            public static String nvl(String sourceStr, String replaceStr) {
040:                return (sourceStr != null) ? sourceStr : replaceStr;
041:            }
042:
043:            /**
044:             * Replaces "sourceString" with "replaceString" if sourceString is null or empty
045:             * @param sourceStr the string to replace
046:             * @param replaceStr the replacement string
047:             * @return the processed string
048:             */
049:            public static String envl(String sourceStr, String replaceStr) {
050:                return (sourceStr != null && sourceStr.trim().length() > 0) ? sourceStr
051:                        : replaceStr;
052:            }
053:
054:            /**
055:             * Replaces "sourceString" with "replaceString" if sourceString equals null
056:             * @param sourceStr the string to replace
057:             * @param replaceStr the replacement string
058:             * @param prefix the prefix
059:             * @return the processed string
060:             */
061:            public static String nvl(String sourceStr, String replaceStr,
062:                    String prefix) {
063:                String nvlStr = nvl(sourceStr, replaceStr);
064:                if (!nvlStr.trim().equals(""))
065:                    return new String(prefix + nvlStr);
066:                else
067:                    return nvlStr;
068:            }
069:
070:            /**
071:             * Replaces "sourceString" with "" if sourceString equals null
072:             * @param sourceStr string to replace
073:             * @return the processed string
074:             */
075:            public static String nvl(String sourceStr) {
076:                return nvl(sourceStr, "");
077:            }
078:
079:            /**
080:             * replaces "replacedString" with "newString" in "text"
081:             * @param text text where to replace
082:             * @param replacedString string to replace
083:             * @param newString new string
084:             * @return new text
085:             */
086:            public static String replaceText(String text,
087:                    String replacedString, String newString) {
088:                int i = text.indexOf(replacedString);
089:                if (i >= 0) {
090:                    StringBuffer buffer = new StringBuffer(text.length());
091:                    int from = 0;
092:                    int rl = replacedString.length();
093:                    while (i >= 0) {
094:                        buffer.append(text.substring(from, i))
095:                                .append(newString);
096:                        from = i + rl;
097:                        i = text.indexOf(replacedString, from);
098:                    }
099:                    buffer.append(text.substring(from));
100:                    return buffer.toString();
101:                }
102:                return text;
103:            }
104:
105:            public static void replaceSubstVariables(Hashtable original,
106:                    Hashtable subst) {
107:
108:                for (Enumeration original_keys = original.keys(); original_keys
109:                        .hasMoreElements();) {
110:                    String original_key = (String) original_keys.nextElement();
111:                    String original_value = (String) original.get(original_key);
112:
113:                    for (Enumeration subst_keys = subst.keys(); subst_keys
114:                            .hasMoreElements();) {
115:                        String subst_key = (String) subst_keys.nextElement();
116:                        String subst_value = (String) subst.get(subst_key);
117:
118:                        original_value = replaceText(original_value, subst_key,
119:                                subst_value);
120:                    }
121:
122:                    original.put(original_key, original_value);
123:
124:                }
125:
126:            }
127:
128:            public static String stackTraceToString(Exception e) {
129:                StringWriter strwrt = new StringWriter();
130:                e.printStackTrace(new PrintWriter(strwrt));
131:                return strwrt.toString();
132:            }
133:
134:            /**
135:             * This method gets an array of strings from given string splitted by commas.
136:             * @param str a string value
137:             * @param delim a delimeter
138:             * @return an array of strings
139:             */
140:            public static String[] getSplitStringByCommas(String str,
141:                    String delim) {
142:                if (str == null)
143:                    return null;
144:                StringTokenizer st = new StringTokenizer(str, delim);
145:                String[] strArray = new String[st.countTokens()];
146:                for (int i = 0; st.hasMoreTokens(); i++) {
147:                    strArray[i] = st.nextToken().trim();
148:                }
149:                return strArray;
150:            }
151:
152:            /**
153:             * This method gets a properties of strings from given string splitted by commas.
154:             * @param keys a string keys for properties
155:             * @param values a string value
156:             * @param delim a delimeter
157:             * @return an array of strings
158:             */
159:            public static Properties getSplitStringByCommas(String keys,
160:                    String values, String delim) {
161:                if (values == null || keys == null)
162:                    return null;
163:                StringTokenizer stValues = new StringTokenizer(values, delim);
164:                StringTokenizer stKeys = new StringTokenizer(keys, delim);
165:                Properties props = new Properties();
166:                while (stValues.hasMoreTokens() && stKeys.hasMoreTokens()) {
167:                    props.put(stKeys.nextToken().trim(), stValues.nextToken()
168:                            .trim());
169:                }
170:                return props;
171:            }
172:
173:            /**
174:             * This method gets a properties of int from given string splitted by commas.
175:             * @param str a string keys for properties
176:             * @param delim a delimeter
177:             * @param def a default value
178:             * @return an array of int
179:             */
180:            public static int[] getSplitIntByCommas(String str, String delim,
181:                    int def) {
182:                if (str == null)
183:                    str = "";
184:                String[] strarr = getSplitStringByCommas(str, delim);
185:                int[] intarr = new int[strarr.length];
186:                for (int i = 0; i < strarr.length; i++) {
187:                    intarr[i] = parseInt(strarr[i], def);
188:                }
189:                return intarr;
190:            }
191:
192:            public static Hashtable getFamilyProps(Properties props,
193:                    String prefix, String delim, Properties defaultProps) {
194:                Hashtable hash = new Hashtable();
195:                Enumeration en = props.propertyNames();
196:                String propName = null;
197:                String propShort = null;
198:                String itemName = null;
199:                String itemPropName = null;
200:                String itemPropValue = null;
201:                StringTokenizer st = null;
202:                Properties itemProps = null;
203:                while (en.hasMoreElements()) {
204:                    propName = (String) en.nextElement();
205:                    if (propName.startsWith(prefix)) {
206:                        propShort = propName.substring(prefix.length());
207:                        st = new StringTokenizer(propShort, delim);
208:                        itemName = null;
209:                        itemPropName = null;
210:                        itemPropValue = null;
211:                        if (st.hasMoreTokens()) {
212:                            itemName = st.nextToken().trim();
213:                            if (st.hasMoreTokens()) {
214:                                itemPropName = st.nextToken().trim();
215:                            }
216:                        }
217:                        if ((itemName != null) && (itemPropName != null)) {
218:                            itemPropValue = props.getProperty(propName, "");
219:                            itemProps = (Properties) hash.get(itemName);
220:                            if (itemProps == null) {
221:                                itemProps = new Properties(defaultProps);
222:                            }
223:                            itemProps.put(itemPropName, itemPropValue);
224:                            hash.put(itemName, itemProps);
225:                        }
226:                    }
227:                }
228:                return hash;
229:            }
230:
231:            public static String[] getFamilyPropertyArrayString(Hashtable hash,
232:                    String[] keys, String propName, String[] def) {
233:                String[] st = new String[keys.length];
234:                for (int i = 0; i < st.length; i++) {
235:                    st[i] = getFamilyPropertyString(hash, keys[i], propName,
236:                            null);
237:                    if ((def != null) && (st[i] == null) && (def.length > i)) {
238:                        st[i] = def[i];
239:                    }
240:                }
241:                return st;
242:            }
243:
244:            public static String getFamilyPropertyString(Hashtable hash,
245:                    String key, String propName, String def) {
246:                return ((Properties) hash.get(key)).getProperty(propName, def);
247:            }
248:
249:            public static String[] getFamilyPropertyArrayString(Hashtable hash,
250:                    String[] keys, String propName, String def) {
251:                String[] st = new String[keys.length];
252:                for (int i = 0; i < st.length; i++) {
253:                    st[i] = def;
254:                }
255:                return getFamilyPropertyArrayString(hash, keys, propName, st);
256:            }
257:
258:            public static String[] getFamilyPropertyArrayString(Hashtable hash,
259:                    String[] keys, String propName) {
260:                return getFamilyPropertyArrayString(hash, keys, propName,
261:                        (String[]) null);
262:            }
263:
264:            public static boolean[] getFamilyPropertyArrayBoolean(
265:                    Hashtable hash, String[] keys, String propName, boolean def) {
266:                boolean[] st = new boolean[keys.length];
267:                for (int i = 0; i < st.length; i++) {
268:                    st[i] = parseBoolean(((Properties) hash.get(keys[i]))
269:                            .getProperty(propName), def);
270:                }
271:                return st;
272:            }
273:
274:            public static boolean[] getFamilyPropertyArrayBoolean(
275:                    Hashtable hash, String[] keys, String propName) {
276:                return getFamilyPropertyArrayBoolean(hash, keys, propName,
277:                        false);
278:            }
279:
280:            public static int[] getFamilyPropertyArrayInt(Hashtable hash,
281:                    String[] keys, String propName) {
282:                int[] st = new int[keys.length];
283:                for (int i = 0; i < st.length; i++) {
284:                    st[i] = parseInt(((Properties) hash.get(keys[i]))
285:                            .getProperty(propName));
286:                }
287:                return st;
288:            }
289:
290:            // parse "yes" and "no"
291:            public static boolean parseBoolean(String str, boolean defaultValue) {
292:                boolean res = defaultValue;
293:                if ("yes".equalsIgnoreCase(str))
294:                    res = true;
295:                if ("no".equalsIgnoreCase(str))
296:                    res = false;
297:                return res;
298:            }
299:
300:            /**
301:             * This method returns the String representation of the given boolean value.
302:             * @param bool a value of boolean type
303:             * @return "true" if bool is true, "false" - otherwise
304:             */
305:            public static String boolToStr(boolean bool) {
306:                return (bool) ? "true" : "false";
307:            }
308:
309:            /**
310:             * This method returns the boolean value for the given String representation ("true"-"false").
311:             * @param bool a <code>String</code> value
312:             * @return true if bool is "true", false - otherwise
313:             */
314:            public static boolean strToBool(String bool) {
315:                return ("true".equalsIgnoreCase(bool)) ? true : false;
316:            }
317:
318:            /**
319:             * This method returns the boolean value for the given String representation ("yes"-"no").
320:             * @param str a <code>String</code> value
321:             * @return true if str is "yes", false - otherwise
322:             */
323:            public static boolean parseBoolean(String str) {
324:                return parseBoolean(str, false);
325:            }
326:
327:            // Parse integer string value
328:            public static int parseInt(String str, int defaultValue) {
329:                try {
330:                    return Integer.parseInt(str);
331:                } catch (Exception e) {
332:                    return defaultValue;
333:                }
334:            }
335:
336:            public static int parseInt(String str) {
337:                return parseInt(str, -1);
338:            }
339:
340:            public static boolean odd(int i) {
341:                return (i >> 1 << 1 != i);
342:            }
343:
344:            /**
345:             * This method checks if an array of objects is empty or not.
346:             * @param objects an array of objects
347:             * @return true if array is empty, false - otherwise
348:             */
349:            public static boolean isArrayEmpty(Object objects[]) {
350:                if (objects == null)
351:                    return true;
352:                for (int i = 0; i < objects.length; i++) {
353:                    if (objects[i] != null)
354:                        return false;
355:                }
356:                return true;
357:            }
358:
359:            /**
360:             * This method checks if an array of strings is empty or not.
361:             * @param objects an array of strings
362:             * @return true if array is empty, false - otherwise
363:             */
364:            public static boolean isArrayEmpty(String objects[]) {
365:                if (objects == null)
366:                    return true;
367:                for (int i = 0; i < objects.length; i++) {
368:                    if (objects[i] != null && !"".equals(objects[i]))
369:                        return false;
370:                }
371:                return true;
372:            }
373:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.