001: package util;
002:
003: import java.util.*; //import javax.activation.DataHandler;
004: //import javax.activation.FileDataSource;
005: //import javax.mail.*; // PLS XXX PUT THIS BACK
006: //import javax.mail.internet.*; // PLS XXX PUT THIS BACK
007: import java.io.*;
008:
009: /** utility class for sending emails and other things
010: */
011: public class Util {
012: //public static final String smtphost = "mail.XXX.com";
013:
014: /*
015: public static void sendMail(String emailid, String subject, String text)
016: throws MessagingException
017: {
018: boolean flag = false;
019: Properties properties = new Properties();
020: properties.put("mail.smtp.host", smtphost);
021:
022: Session session = Session.getDefaultInstance(properties, null);
023: session.setDebug(flag);
024: MimeMessage mimemessage = new MimeMessage(session);
025: mimemessage.setFrom(new InternetAddress("admin@XXX.com")); // XXX
026: mimemessage.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailid, false));
027: mimemessage.setSubject(subject);
028: mimemessage.setContent(text, "text/plain");
029: Transport.send(mimemessage);
030: }
031: */
032: /** splits a given string on given character separator returning
033: * an array of strings.
034: * THis would be more efficient than using string tokenizer which
035: * takes a String not char as argument.
036: * @author Rahul Kumar June 17, 2001
037: * Dealing with consecutive delimiters - I am returning blank
038: * strings.
039: * Thats okay for non-WS delims, but not so for whites.
040: * Pls use jdk 1.4's split if you want consec spaces to be treated as
041: * one.
042: * Better still use PerlWrapper.perlSplit !
043: */
044: public static String[] split(String st, char sep) {
045:
046: ArrayList alist = new ArrayList();
047:
048: int len = st.length();
049: int pos = 0;
050: int fin = 0;
051:
052: // while not end of string, and you can find a match
053: while (pos < len && (fin = st.indexOf(sep, pos)) != -1) {
054: alist.add(st.substring(pos, fin));
055: pos = fin + 1;
056: }
057: // Push remainder if it's not empty
058: String remainder = st.substring(pos);
059: if (remainder.length() != 0) {
060: alist.add(remainder);
061: }
062:
063: // Return list as an array of strings
064: String[] ret = new String[alist.size()];
065: alist.toArray(ret);
066: return ret;
067: } // end of split
068:
069: public static Properties getProperties(String filename)
070: throws java.io.FileNotFoundException, java.io.IOException {
071: Properties props = new Properties();
072: FileInputStream br = new FileInputStream(filename);
073: props.load(br);
074: br.close();
075: return props;
076: }
077:
078: /**
079: *
080: * Replaces all occurrences of patt with str in line and returns
081: * line. "replace"
082: */
083: public static String replace(String patt, String str, String line) {
084: int pos = line.indexOf(patt);
085: if (pos == -1)
086: return line;
087: int prevpos = 0;
088: StringBuffer sb = new StringBuffer(line);
089: int lenstr = str.length();
090: int lenpatt = patt.length();
091: while (true) {
092: sb.delete(pos, pos + lenpatt);
093: sb.insert(pos, str);
094: prevpos = pos + lenstr + 1;
095: if ((pos = sb.indexOf(patt, prevpos)) == -1) {
096: break;
097: }
098: }
099: return sb.toString();
100: } // strReplace
101:
102: public static int parseInt(String snum, int ndefault) {
103: int inum = ndefault;
104: try {
105: inum = Integer.parseInt(snum);
106: } catch (Exception exc) {
107: System.err.println("108 EXC:" + exc.toString());
108: exc.printStackTrace();
109: }
110:
111: return inum;
112: }
113:
114: } // class
|