Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Managing ezines with JavaMail and XSLT, Part 2

Use XML and XSLT to automatically produce both plain text and HTML newsletters

Return to article


Listing 3. ConfigHandler.java

package com.psol.xslist;

import java.io.*;
import java.util.*;
import javax.mail.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.mail.internet.*;
import javax.xml.transform.*;
import javax.xml.transform.sax.*;
import javax.xml.transform.stream.*;

public class ConfigHandler
   extends DefaultHandler
{
   protected static final String
      NAMESPACE_URI = "http://www.psol.com/xns/xslist/config";
   protected static final int NONE = 0,
                              EMAIL = 1,
                              HEADER = 2,
                              BODY = 3,
                              PART = 4;
   protected int state;
   protected File source;
   protected Multipart multipart;
   protected Message message;

   public void startElement(String uri,
                            String name,
                            String qualifiedName,
                            Attributes atts)
      throws SAXException
   {
      try
      {
         if(!uri.equals(NAMESPACE_URI))
            return;
         if(state == BODY &&
            (name.equals("part") || name.equals("text")))
         {
            state = PART;
            String styleSheet = atts.getValue("styleSheet"),
                   contentType = atts.getValue("contentType");
            BodyPart part = createBodyPart(source,
                                           new File(styleSheet),
                                           contentType,
                                           name.equals("text"));
            multipart.addBodyPart(part);
         }
         else if(state == EMAIL && name.equals("body"))
         {
            state = BODY;
            multipart = new MimeMultipart("alternative");
            source = new File(atts.getValue("source"));
         }
         else if(state == EMAIL && name.equals("header"))
         {
            state = HEADER;
            InternetAddress from =
                new InternetAddress(atts.getValue("from"));
            InternetAddress to[] =
               InternetAddress.parse(atts.getValue("to"));
            message.setFrom(from);
            message.setRecipients(Message.RecipientType.TO,to);
            message.setSubject(atts.getValue("subject"));
            message.setSentDate(new Date());
         }
         else if(state == NONE && name.equals("email"))
         {
            state = EMAIL;
            Properties props = System.getProperties();
            props.put("mail.smtp.host",atts.getValue("smtp"));
            Session session = Session.getDefaultInstance(props);
            message = new MimeMessage(session);
         }
      }
      catch(MessagingException e)
      {
         throw new SAXException("Messaging exception",e);
      }
      catch(TransformerException e)
      {
         throw new SAXException("Transformer exception",e);
      }
   }

   public void endElement(String uri,
                          String name,
                          String qualifiedName)
      throws SAXException
   {
      try
      {
         if(!uri.equals(NAMESPACE_URI))
            return;
         if(state == PART &&
            (name.equals("part") || name.equals("text")))
         {
            state = BODY;
         }
         else if(state == BODY && name.equals("body"))
         {
            state = EMAIL;
            message.setContent(multipart);
         }
         else if(state == HEADER && name.equals("header"))
         {
            state = EMAIL;
         }
         else if(state == EMAIL && name.equals("email"))
         {
            state = NONE;
            Transport.send(message);
         }
      }
      catch(MessagingException e)
      {
         throw new SAXException("Messaging exception",e);
      }
   }

   public void startDocument()
   {
      state = NONE;
      source = null;
      multipart = null;
      message = null;
   }

   protected BodyPart createBodyPart(File source,
                                     File styleSheet,
                                     String contentType,
                                     boolean toText)
      throws MessagingException, TransformerException
   {
      TransformerFactory factory =
         TransformerFactory.newInstance();
      Transformer transformer =
         factory.newTransformer(new StreamSource(styleSheet));
      StringWriter stringWriter = new StringWriter();
      PrintWriter printWriter = new PrintWriter(stringWriter);
      if(toText)
      {
         Xml2Text xml2Text = new Xml2Text(printWriter);
         WhitespaceFilter spaceFilter = new WhitespaceFilter();
         spaceFilter.setContentHandler(xml2Text);
         AsciiFilter asciiFilter = new AsciiFilter();
         asciiFilter.setContentHandler(spaceFilter);
         transformer.transform(new StreamSource(source),
                               new SAXResult(asciiFilter));
         printWriter.close();
         String st = stringWriter.toString();
         BodyPart part = new MimeBodyPart();
         part.setText(st);
         return part;
      }
      else
      {
         transformer.transform(new StreamSource(source),
                               new StreamResult(stringWriter));
         printWriter.close();
         String st = stringWriter.toString();
         MimeBodyPart part = new MimeBodyPart();
         part.setContent(st,"text/html");
         return part;
      }
   }
}

Return to article