001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.email.faux;
031:
032: import java.io.BufferedReader;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.InputStreamReader;
036: import java.util.ArrayList;
037: import java.util.Calendar;
038: import java.util.Date;
039: import java.util.GregorianCalendar;
040: import java.util.List;
041: import java.util.StringTokenizer;
042:
043: import javax.mail.Address;
044: import javax.mail.MessagingException;
045: import javax.mail.internet.AddressException;
046: import javax.mail.internet.InternetAddress;
047:
048: /**
049: * Generates random fake content for the fake JavaMail <code>Message</code>
050: * objects.
051: */
052: public class MessageGenerator {
053:
054: private static final String DOMAIN = "nextapp.com";
055: private static final Address YOUR_ADDRESS;
056: static {
057: try {
058: YOUR_ADDRESS = new InternetAddress(
059: "Joe Smith <joe.smith@nextapp.com>");
060: } catch (AddressException ex) {
061: throw new RuntimeException(ex);
062: }
063: }
064:
065: private static final int MINIMUM_DATE_DELTA = -365;
066: private static final int MAXIMUM_DATE_DELTA = 0;
067:
068: /**
069: * Generates a text document from random artificial text.
070: *
071: * @param sentenceArray the source array from which strings are randomly
072: * retrieved
073: * @param minimumSentences the minimum number of sentences each paragraph
074: * may contain
075: * @param maximumSentences the maximum number of sentences each paragraph
076: * may contain
077: * @param minimumParagraphs the minimum number of paragraphs the document
078: * may contain
079: * @param maximumParagraphs the maximum number of paragraphs the document
080: * may contain
081: * @return the document
082: */
083: private static String generateText(String[] sentenceArray,
084: int minimumSentences, int maximumSentences,
085: int minimumParagraphs, int maximumParagraphs) {
086:
087: int paragraphCount = randomInteger(minimumParagraphs,
088: maximumParagraphs);
089: StringBuffer text = new StringBuffer();
090: for (int paragraphIndex = 0; paragraphIndex < paragraphCount; ++paragraphIndex) {
091: int sentenceCount = randomInteger(minimumSentences,
092: maximumSentences);
093: for (int sentenceIndex = 0; sentenceIndex < sentenceCount; ++sentenceIndex) {
094: text
095: .append(sentenceArray[randomInteger(sentenceArray.length)]);
096: text.append(".");
097: if (sentenceIndex < sentenceCount - 1) {
098: text.append(" ");
099: }
100: }
101: if (paragraphIndex < paragraphCount - 1) {
102: text.append("\n\n");
103: }
104: }
105: return text.toString();
106: }
107:
108: /**
109: * Returns a text resource as an array of strings.
110: *
111: * @param resourceName the name of resource available from the
112: * <code>CLASSPATH</code>
113: * @return the resource as an array of strings
114: */
115: private static String[] loadStrings(String resourceName) {
116: InputStream in = null;
117: List strings = new ArrayList();
118: try {
119: in = MessageGenerator.class
120: .getResourceAsStream(resourceName);
121: if (in == null) {
122: throw new IllegalArgumentException(
123: "Resource does not exist: \"" + resourceName
124: + "\"");
125: }
126: BufferedReader reader = new BufferedReader(
127: new InputStreamReader(in));
128: String line;
129: while ((line = reader.readLine()) != null) {
130: line = line.trim();
131: if (line.length() > 0) {
132: strings.add(Character.toUpperCase(line.charAt(0))
133: + line.substring(1).toLowerCase());
134: }
135: }
136: return (String[]) strings
137: .toArray(new String[strings.size()]);
138: } catch (IOException ex) {
139: throw new IllegalArgumentException(
140: "Cannot get resource: \"" + resourceName + "\": "
141: + ex);
142: } finally {
143: if (in != null) {
144: try {
145: in.close();
146: } catch (IOException ex) {
147: }
148: }
149: }
150: }
151:
152: /**
153: * Returns a random boolean value.
154: *
155: * @param truePercent The percentage of 'true' values to be returned.
156: * @return The random boolean value.
157: */
158: private static boolean randomBoolean(int truePercent) {
159: int number = (int) (Math.random() * 100);
160: return number < truePercent;
161: }
162:
163: /**
164: * Returns a random integer between the specified bounds.
165: *
166: * @param minimum the minimum possible value
167: * @param maximum the maximum possible value
168: * @return the random integer
169: */
170: private static int randomInteger(int minimum, int maximum) {
171: return minimum
172: + (int) (Math.random() * (maximum - minimum + 1));
173: }
174:
175: /**
176: * Returns a random integer between 0 and <code>size</code> - 1.
177: *
178: * @param size the number of possible random values
179: * @return a random integer between 0 and <code>size</code> - 1
180: */
181: private static int randomInteger(int size) {
182: return (int) (Math.random() * size);
183: }
184:
185: /**
186: * Returns a version of a sentence with every word capitalized.
187: *
188: * @param title The title sentence to capitalize.
189: * @return A version of the sentence with every word capitalized.
190: */
191: private static String titleCapitalize(String title) {
192: StringTokenizer st = new StringTokenizer(title);
193: StringBuffer sb = new StringBuffer();
194: while (st.hasMoreTokens()) {
195: String token = st.nextToken();
196: sb.append(Character.toUpperCase(token.charAt(0)));
197: sb.append(token.substring(1));
198: if (st.hasMoreTokens()) {
199: sb.append(" ");
200: }
201: }
202: return sb.toString();
203: }
204:
205: private String[] lastNames = loadStrings("resource/LastNames.txt");
206: private String[] maleFirstNames = loadStrings("resource/MaleFirstNames.txt");
207: private String[] femaleFirstNames = loadStrings("resource/FemaleFirstNames.txt");
208: private String[] latinSentences = loadStrings("resource/LatinSentences.txt");
209:
210: private Address createAddress() throws MessagingException {
211: // Create Name.
212: String firstName;
213:
214: boolean male = randomBoolean(50);
215: if (male) {
216: int firstNameIndex = randomInteger(maleFirstNames.length);
217: firstName = maleFirstNames[firstNameIndex];
218: } else {
219: int firstNameIndex = randomInteger(femaleFirstNames.length);
220: firstName = femaleFirstNames[firstNameIndex];
221: }
222: String lastName = lastNames[randomInteger(lastNames.length)];
223: String email = firstName + "." + lastName + "@" + DOMAIN;
224: InternetAddress address = new InternetAddress(firstName + " "
225: + lastName + " <" + email + ">");
226: return address;
227: }
228:
229: public Date randomDate() {
230: Calendar cal = new GregorianCalendar();
231: cal.add(Calendar.DAY_OF_MONTH, randomInteger(
232: MINIMUM_DATE_DELTA, MAXIMUM_DATE_DELTA));
233: cal.add(Calendar.SECOND, -randomInteger(86400));
234: return cal.getTime();
235: }
236:
237: public FauxMessage generateMessage() throws MessagingException {
238:
239: Date receivedDate = randomDate();
240: Address from = createAddress();
241:
242: Address[] to = null;
243: Address[] cc = null;
244: Address[] bcc = null;
245: switch (randomInteger(0, 3)) {
246: case 0:
247: to = new Address[] { YOUR_ADDRESS };
248: break;
249: case 1:
250: to = new Address[] { createAddress(), createAddress() };
251: cc = new Address[] { createAddress(), createAddress(),
252: createAddress() };
253: bcc = new Address[] { YOUR_ADDRESS };
254: break;
255: case 2:
256: to = new Address[] { YOUR_ADDRESS };
257: cc = new Address[] { createAddress(), createAddress() };
258: break;
259: case 3:
260: to = new Address[] { createAddress() };
261: cc = new Address[] { createAddress(), YOUR_ADDRESS,
262: createAddress() };
263: break;
264: }
265: // Create Article Title
266: String subject = latinSentences[randomInteger(latinSentences.length)];
267: if (subject.length() > 40) {
268: subject = subject
269: .substring(0, subject.lastIndexOf(" ", 40));
270: }
271: if (subject.endsWith(",")) {
272: subject = subject.substring(0, subject.length() - 1);
273: }
274: subject = titleCapitalize(subject);
275:
276: String content = generateText(latinSentences, 2, 8, 6, 26);
277:
278: return new FauxMessage(from, receivedDate, to, cc, bcc,
279: subject, content);
280:
281: }
282: }
|