Source Code Cross Referenced for EmailUtils.java in  » Net » apache-common-Email » org » apache » commons » mail » 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 » Net » apache common Email » org.apache.commons.mail 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2005 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.mail;
018:
019:        import java.util.Random;
020:
021:        /**
022:         * Utility methods used by commons-email.
023:         *
024:         * <p>
025:         * These methods are copied from other commons components (commons-lang) to avoid creating a dependency for such a small component.
026:         * </p>
027:         *
028:         * <p>
029:         * This is a package scoped class, and should not be used directly by users.
030:         * </p>
031:         *
032:         * @author jakarta-commons
033:         * @version $Id: EmailUtils.java 279313 2005-09-07 12:41:58Z henning $
034:         *
035:         * @since 1.0
036:         */
037:        final class EmailUtils {
038:            /**
039:             * <p>
040:             * Random object used by random method. This has to be not local to the random method so as to not return the same value in the
041:             * same millisecond.
042:             * </p>
043:             */
044:            private static final Random RANDOM = new Random();
045:
046:            /**
047:             * Constructs a new <code>EmailException</code> with no detail message.
048:             */
049:            private EmailUtils() {
050:                super ();
051:            }
052:
053:            /**
054:             * <p>
055:             * Checks if a String is empty ("") or null.
056:             * </p>
057:             *
058:             * @param str the String to check, may be null
059:             *
060:             * @return <code>true</code> if the String is empty or null
061:             *
062:             * @since Commons Lang v2.1, svn 240418
063:             */
064:            static boolean isEmpty(String str) {
065:                return (str == null) || (str.length() == 0);
066:            }
067:
068:            /**
069:             * <p>
070:             * Checks if a String is not empty ("") and not null.
071:             * </p>
072:             *
073:             * @param str the String to check, may be null
074:             *
075:             * @return <code>true</code> if the String is not empty and not null
076:             *
077:             * @since Commons Lang v2.1, svn 240418
078:             */
079:            static boolean isNotEmpty(String str) {
080:                return (str != null) && (str.length() > 0);
081:            }
082:
083:            /**
084:             * <p>
085:             * Validate an argument, throwing <code>IllegalArgumentException</code> if the argument is <code>null</code>.
086:             * </p>
087:             *
088:             * @param object the object to check is not <code>null</code>
089:             * @param message the exception message you would like to see if the object is <code>null</code>
090:             *
091:             * @throws IllegalArgumentException if the object is <code>null</code>
092:             *
093:             * @since Commons Lang v2.1, svn 201930
094:             */
095:            static void notNull(Object object, String message) {
096:                if (object == null) {
097:                    throw new IllegalArgumentException(message);
098:                }
099:            }
100:
101:            /**
102:             * <p>
103:             * Creates a random string whose length is the number of characters specified.
104:             * </p>
105:             *
106:             * <p>
107:             * Characters will be chosen from the set of alphabetic characters.
108:             * </p>
109:             *
110:             * @param count the length of random string to create
111:             *
112:             * @return the random string
113:             *
114:             * @since Commons Lang v2.1, svn 201930
115:             */
116:            static String randomAlphabetic(int count) {
117:                return random(count, 0, 0, true, false, null, RANDOM);
118:            }
119:
120:            /**
121:             * <p>
122:             * Creates a random string based on a variety of options, using supplied source of randomness.
123:             * </p>
124:             *
125:             * <p>
126:             * If start and end are both <code>0</code>, start and end are set to <code>' '</code> and <code>'z'</code>, the ASCII
127:             * printable characters, will be used, unless letters and numbers are both <code>false</code>, in which case, start and end
128:             * are set to <code>0</code> and <code>Integer.MAX_VALUE</code>.
129:             * </p>
130:             *
131:             * <p>
132:             * If set is not <code>null</code>, characters between start and end are chosen.
133:             * </p>
134:             *
135:             * <p>
136:             * This method accepts a user-supplied {@link Random} instance to use as a source of randomness. By seeding a single {@link
137:             * Random} instance with a fixed seed and using it for each call, the same random sequence of strings can be generated
138:             * repeatedly and predictably.
139:             * </p>
140:             *
141:             * @param count the length of random string to create
142:             * @param start the position in set of chars to start at
143:             * @param end the position in set of chars to end before
144:             * @param letters only allow letters?
145:             * @param numbers only allow numbers?
146:             * @param chars the set of chars to choose randoms from. If <code>null</code>, then it will use the set of all chars.
147:             * @param random a source of randomness.
148:             *
149:             * @return the random string
150:             *
151:             * @throws IllegalArgumentException if <code>count</code> &lt; 0.
152:             *
153:             * @since Commons Lang v2.1, svn 201930
154:             */
155:            private static String random(int count, int start, int end,
156:                    boolean letters, boolean numbers, char[] chars,
157:                    Random random) {
158:                if (count == 0) {
159:                    return "";
160:                } else if (count < 0) {
161:                    throw new IllegalArgumentException(
162:                            "Requested random string length " + count
163:                                    + " is less than 0.");
164:                }
165:
166:                if ((start == 0) && (end == 0)) {
167:                    end = 'z' + 1;
168:                    start = ' ';
169:
170:                    if (!letters && !numbers) {
171:                        start = 0;
172:                        end = Integer.MAX_VALUE;
173:                    }
174:                }
175:
176:                StringBuffer buffer = new StringBuffer();
177:                int gap = end - start;
178:
179:                while (count-- != 0) {
180:                    char ch;
181:
182:                    if (chars == null) {
183:                        ch = (char) (random.nextInt(gap) + start);
184:                    } else {
185:                        ch = chars[random.nextInt(gap) + start];
186:                    }
187:
188:                    if ((letters && numbers && Character.isLetterOrDigit(ch))
189:                            || (letters && Character.isLetter(ch))
190:                            || (numbers && Character.isDigit(ch))
191:                            || (!letters && !numbers)) {
192:                        buffer.append(ch);
193:                    } else {
194:                        count++;
195:                    }
196:                }
197:
198:                return buffer.toString();
199:            }
200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.