KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > mail > MailContentHandlerDelegate


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.mail;
17
18 import java.io.IOException JavaDoc;
19 //import java.io.InputStream;
20
import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import javax.mail.Address JavaDoc;
24 import javax.mail.Flags JavaDoc;
25 import javax.mail.Folder JavaDoc;
26 import javax.mail.Header JavaDoc;
27 import javax.mail.Message JavaDoc;
28 import javax.mail.MessagingException JavaDoc;
29 import javax.mail.Multipart JavaDoc;
30 import javax.mail.Part JavaDoc;
31 import javax.mail.internet.ContentType JavaDoc;
32 import javax.mail.internet.InternetAddress JavaDoc;
33 import javax.mail.internet.MimeMultipart JavaDoc;
34 import javax.mail.internet.MimePart JavaDoc;
35 import javax.mail.internet.ParameterList JavaDoc;
36 import javax.mail.internet.ParseException JavaDoc;
37 import org.apache.avalon.framework.logger.AbstractLogEnabled;
38 import org.apache.excalibur.xml.sax.XMLizable;
39 import org.xml.sax.Attributes JavaDoc;
40 import org.xml.sax.ContentHandler JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.helpers.AttributesImpl JavaDoc;
43
44 /**
45  * Marshal javamail objects
46  * <p>
47  * Having one or more javamail objects, like Message, Folder, et al. emit SAX events
48  * </p>
49  *
50  * @author Bernhard Huber
51  * @since 24. Oktober 2002
52  * @version $Id: MailContentHandlerDelegate.java 164808 2005-04-26 16:07:03Z vgritsenko $
53  */

54
55 /*
56  * Generated SAX events conforming to following folder sample xml:
57  *
58  * <mail:mail xmlns:mail="http://apache.org/cocoon/mail/1.0">
59  *
60  * <mail:folder name="INBOX" full-name="INBOX" url-name="imap://user-name@host-name/INBOX"
61  * is-subscribed="yes"
62  * is-direcory="no"
63  * has-new-messages="no"
64  * total-messages="3"
65  * new-messages="0"
66  * deleted-messages="-1"
67  * unread-messages="0"
68  * >
69  * </mail:mail>
70  *
71  * Generated SAX events conforming to following message sample xml:
72  *
73  * <mail:mail xmlns:mail="http://apache.org/cocoon/mail/1.0">
74  *
75  * <mail:message-envelope>
76  * <mail:from email-address="name@a1.net">name@xxx.net</mail:from>
77  * <mail:to email-address="test@xxx.net">test@xxx.net</mail:to>
78  * <mail:reply-to email-address="xxx@test.net">test@xxx.net</mail:reply-to>
79  * <mail:subject>TEST</mail:subject>
80  * <mail:sent-date>Thu Oct 10 14:40:43 CEST 2002</mail:sent-date>
81  * <mail:received-date>Thu Oct 10 14:43:29 CEST 2002</mail:received-date>
82  * <mail:size>4440</mail:size>
83  * <mail:message-number>1</mail:message-number> <mail:flags seen="yes"/>
84  * <mail:header name="Return-path" value="<test@a1.xxx>"/>
85  * <mail:header name="Received" value="from x1"/>
86  * <mail:header name="Received" value="from x2"/>
87  * <mail:header name="Date" value="Thu, 10 Oct 2002 14:40:43 +0200 (CEST)"/>
88  * <mail:header name="From" value="test@xxx.net"/>
89  * <mail:header name="Subject" value="TEST"/>
90  * <mail:header name="To" value="test@xxx.net"/>
91  * <mail:header name="Message-id" value="<4630087.1034253643941.JavaMail.root@test>"/>
92  * <mail:header name="MIME-version" value="1.0"/>
93  * <mail:header name="Content-type" value="multipart/mixed;&#10; boundary="18119425.1034253643876.JavaMail.root@test""/>
94  * </mail:message-envelope>
95  *
96  * <mail:part content-type="multipart/MIXED; &#10; boundary="18119425.1034253643876.JavaMail.root@test"">
97  * <mail:content>
98  * <mail:part content-type="TEXT/PLAIN; charset=us-ascii">
99  * <mail:content>TEST CONTENT MESSSAGE TEST.</mail:content>
100  * </mail:part>
101  * <mail:part content-type="MESSAGE/RFC822">
102  * <mail:content>
103  * <mail:part content-type="multipart/MIXED; boundary=--1f735d241edf3a1">
104  * <mail:content>
105  * <mail:part content-type="TEXT/PLAIN; charset=us-ascii">
106  * <mail:content>Test &#13;</mail:content>
107  * </mail:part>
108  * <mail:part content-type="TEXT/X-VCARD; name=xxx.vcf; charset=windows-1252" description="Card for <xxx@a1.xxx>" disposition="ATTACHMENT" file-name="xxx.vcf">
109  * <mail:content>begin:vcard&#13; n:Name;Name&#13; title:Dr&#13; email;internet:xxx@xxx.net&#13; url:www.xxx.net&#13; fn:xXxXx&#13; end:vcard&#13; &#13; </mail:content>
110  * </mail:part>
111  * </mail:content>
112  * </mail:part>
113  * </mail:content>
114  * </mail:part>
115  * </mail:content>
116  * </mail:part>
117  *
118  * </mail:mail>
119  *
120  */

121 public class MailContentHandlerDelegate extends AbstractLogEnabled {
122
123     /**
124      * URI of the generated XML elements
125      */

126     public final static String JavaDoc URI = "http://apache.org/cocoon/mail/1.0";
127
128     /**
129      * PREFIX of the generated XML elements
130      */

131     public final static String JavaDoc PREFIX = "mail";
132
133     /**
134      * Destination content handler receiving SAX events generated by
135      * this class
136      */

137     private ContentHandler JavaDoc contentHandler;
138
139     /**
140      * use this attributes for startElement attributes invocation, reusing
141      * attributes object for all elements
142      */

143     private AttributesImpl JavaDoc attributes = null;
144
145     private SimpleDateFormat JavaDoc sdf;
146
147     private ContentTypePreference alternativeMailCtPref = new MailCtPref();
148
149
150     /**
151      * Constructor for the MailContentHandler object
152      *
153      *@param contentHandler Description of Parameter
154      */

155     public MailContentHandlerDelegate(ContentHandler JavaDoc contentHandler) {
156         this.contentHandler = contentHandler;
157     }
158
159
160     /**
161      * Sets the simpleDateFormatter attribute of the MailContentHandlerDelegate object
162      *
163      *@param sdf The new simpleDateFormat value
164      */

165     public void setSimpleDateFormat(SimpleDateFormat JavaDoc sdf) {
166         this.sdf = sdf;
167     }
168
169
170     /**
171      * Gets the simpleDateFormat attribute of the MailContentHandlerDelegate object
172      *
173      *@return The simpleDateFormat value
174      */

175     public SimpleDateFormat JavaDoc getSimpleDateFormat() {
176         return sdf;
177     }
178
179
180     /**
181      * Emit starting SAX events sequence, including SAX startDocument event
182      *
183      *@exception SAXException thrown iff generating SAX events fails
184      */

185     public void startDocument() throws SAXException JavaDoc {
186         startDocumentInternal(true);
187     }
188
189
190     /**
191      * Emit starting SAX events sequence, excluding SAX startDocument event
192      *
193      *@exception SAXException thrown iff generating SAX events fails
194      */

195     public void startDocumentXMLizer() throws SAXException JavaDoc {
196         startDocumentInternal(false);
197     }
198
199
200     /**
201      * Emit starting SAX events sequence, including SAX endDocument event
202      *
203      *@exception SAXException thrown iff generating SAX events fails
204      */

205     public void endDocument() throws SAXException JavaDoc {
206         endDocumentInternal(true);
207     }
208
209
210     /**
211      * Emit starting SAX events sequence, excluding SAX endDocument event
212      *
213      *@exception SAXException thrown iff generating SAX events fails
214      */

215     public void endDocumentXMLizer() throws SAXException JavaDoc {
216         endDocumentInternal(false);
217     }
218
219
220     /**
221      * Emit a folder as a sequence of SAX events
222      *
223      *@param folder emit this folder
224      */

225     public void marshalFolderToSAX(Folder JavaDoc folder) {
226         try {
227             folderToSAX(this.contentHandler, folder);
228         } catch (Exception JavaDoc e) {
229             getLogger().error("Cannot generate SAX events from folder", e);
230         }
231     }
232
233
234     /**
235      * Emit folders as a sequence of SAX events
236      *
237      *@param folders emit these folders
238      */

239     public void marshalFolderToSAX(Folder JavaDoc[] folders) {
240         try {
241             for (int i = 0; i < folders.length; i++) {
242                 folderToSAX(this.contentHandler, folders[i]);
243             }
244         } catch (Exception JavaDoc e) {
245             getLogger().error("Cannot generate SAX events from folders", e);
246         }
247     }
248
249
250     /**
251      * Emit a message envelope as a sequence of SAX events
252      *
253      *@param message emit envelope of this message
254      */

255     public void marshalMessageEnvelopeToSAX(Message JavaDoc message) {
256         try {
257             messageEnvelopeToSAX(this.contentHandler, message);
258         } catch (Exception JavaDoc e) {
259             getLogger().error("Cannot generate SAX events from message envelope ", e);
260         }
261     }
262
263
264     /**
265      * Emit a message envelope, and message content as a sequence of SAX events
266      *
267      *@param message emit envelope, and content of this message
268      */

269     public void marshalMessageToSAX(Message JavaDoc message) {
270         try {
271             messageEnvelopeToSAX(this.contentHandler, message);
272             partToSAX(this.contentHandler, message, 0);
273         } catch (Exception JavaDoc e) {
274             getLogger().error("Cannot generate SAX events from message ", e);
275         }
276     }
277
278
279     /**
280      * Emit a message content as a sequence of SAX events
281      *
282      *@param part Description of the Parameter
283      */

284     public void marshalPartToSAX(Part JavaDoc part) {
285         try {
286             partToSAX(this.contentHandler, part, 0);
287         } catch (Exception JavaDoc e) {
288             getLogger().error("Cannot generate SAX events part", e);
289         }
290     }
291
292
293     /**
294      * Emit start document sequence
295      *
296      *@param emitStartDocument flag controlling invocation of SAX startDocument
297      *@exception SAXException thrown iff generating SAX events fails
298      */

299     protected void startDocumentInternal(boolean emitStartDocument) throws SAXException JavaDoc {
300         if (emitStartDocument) {
301             this.contentHandler.startDocument();
302         }
303         this.contentHandler.startPrefixMapping(PREFIX, URI);
304
305         attributes = new AttributesImpl JavaDoc();
306         attributes.addAttribute("", PREFIX, "xmlns:" + PREFIX, "CDATA", URI);
307         startElement("mail", attributes);
308     }
309
310
311     /**
312      * Emit end document sequence
313      *
314      *@param emitEndDocument flag controlling invocation of SAX endDocument
315      *@exception SAXException thrown iff generating SAX events fails
316      */

317     protected void endDocumentInternal(boolean emitEndDocument) throws SAXException JavaDoc {
318         endElement("mail");
319
320         this.contentHandler.endPrefixMapping(PREFIX);
321         if (emitEndDocument) {
322             this.contentHandler.endDocument();
323         }
324     }
325
326
327     /**
328      * Emit folder as sequence of SAX events
329      *
330      *@param folder emit this folder
331      *@param contentHandler specifies sink of SAX events
332      *@exception MessagingException thrown iff accessing javamail data fails
333      *@exception SAXException thrown iff generating SAX events fails
334      */

335     protected void folderToSAX(ContentHandler JavaDoc contentHandler, Folder JavaDoc folder) throws MessagingException JavaDoc, SAXException JavaDoc {
336         attributes.clear();
337         addAttribute("name", folder.getName());
338         addAttribute("full-name", folder.getFullName());
339         addAttribute("url-name", folder.getURLName().toString());
340         addAttribute("is-subscribed", folder.isSubscribed() ? "yes" : "no");
341         addAttribute("is-directory", (folder.getType() & Folder.HOLDS_FOLDERS) != 0 ? "yes" : "no");
342
343         if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) {
344             addAttribute("holds-messages", "yes");
345             addAttribute("has-new-messages", folder.hasNewMessages() ? "yes" : "no");
346             addAttribute("total-messages", String.valueOf(folder.getMessageCount()));
347             addAttribute("new-messages", String.valueOf(folder.getNewMessageCount()));
348             addAttribute("deleted-messages", String.valueOf(folder.getDeletedMessageCount()));
349             addAttribute("unread-messages", String.valueOf(folder.getUnreadMessageCount()));
350         }
351
352         startElement("folder", attributes);
353         endElement("folder");
354     }
355
356
357     /**
358      * Emit message envelope as sequence of SAX events
359      *
360      *@param message emit envelope of this message
361      *@param contentHandler specifies sink of SAX events
362      *@exception MessagingException thrown iff accessing javamail data fails
363      *@exception SAXException thrown iff generating SAX events fails
364      */

365     protected void messageEnvelopeToSAX(ContentHandler JavaDoc contentHandler, Message JavaDoc message) throws MessagingException JavaDoc, SAXException JavaDoc {
366         attributes.clear();
367         startElement("message-envelope", attributes);
368
369         Address JavaDoc[] a;
370         // FROM
371
if ((a = message.getFrom()) != null) {
372             for (int j = 0; j < a.length; j++) {
373                 emitAddress("from", a[j]);
374             }
375         }
376
377         // TO
378
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
379             for (int j = 0; j < a.length; j++) {
380                 emitAddress("to", a[j]);
381             }
382         }
383
384         // CC
385
if ((a = message.getRecipients(Message.RecipientType.CC)) != null) {
386             for (int j = 0; j < a.length; j++) {
387                 emitAddress("cc", a[j]);
388             }
389         }
390         // BCC
391
if ((a = message.getRecipients(Message.RecipientType.BCC)) != null) {
392             for (int j = 0; j < a.length; j++) {
393                 emitAddress("bcc", a[j]);
394             }
395         }
396
397         // REPLY-TO
398
if ((a = message.getReplyTo()) != null) {
399             for (int j = 0; j < a.length; j++) {
400                 emitAddress("reply-to", a[j]);
401             }
402         }
403
404         // SUBJECT
405
attributes.clear();
406         startElement("subject", attributes);
407         characters(message.getSubject());
408         endElement("subject");
409
410         // SENT-DATE
411
Date JavaDoc d;
412         d = message.getSentDate();
413         emitDate("sent-date", d);
414
415         // RECEIVED-DATE
416
d = message.getReceivedDate();
417         emitDate("received-date", d);
418
419         // SIZE
420
attributes.clear();
421         startElement("size", attributes);
422         characters(String.valueOf(message.getSize()));
423         endElement("size");
424
425         // MESSAGE NUMBER
426
attributes.clear();
427         startElement("message-number", attributes);
428         characters(String.valueOf(message.getMessageNumber()));
429         endElement("message-number");
430
431         // FLAGS:
432
Flags JavaDoc flags = message.getFlags();
433         Flags.Flag JavaDoc[] sf = flags.getSystemFlags();
434         // get the system flags
435

436         attributes.clear();
437         for (int i = 0; i < sf.length; i++) {
438             Flags.Flag JavaDoc flag = sf[i];
439             if (flag == Flags.Flag.ANSWERED) {
440                 addAttribute("answered", "yes");
441             } else if (flag == Flags.Flag.DELETED) {
442                 addAttribute("deleted", "yes");
443             } else if (flag == Flags.Flag.DRAFT) {
444                 addAttribute("draft", "yes");
445             } else if (flag == Flags.Flag.FLAGGED) {
446                 addAttribute("flagged", "yes");
447             } else if (flag == Flags.Flag.RECENT) {
448                 addAttribute("recent", "yes");
449             } else if (flag == Flags.Flag.SEEN) {
450                 addAttribute("seen", "yes");
451             }
452         }
453         startElement("flags", attributes);
454         endElement("flags");
455
456         String JavaDoc[] uf = flags.getUserFlags();
457         // get the user flag strings
458
for (int i = 0; i < uf.length; i++) {
459             attributes.clear();
460             startElement("user-flags", attributes);
461             characters(uf[i]);
462             endElement("user-flags");
463         }
464
465         // X-MAILER
466
//String[] hdrs = message.getHeader("X-Mailer");
467
//logger.info("X-Mailer " + (hdrs != null ? hdrs[0] : "NOT available"));
468

469         Enumeration JavaDoc allHeaders = message.getAllHeaders();
470         if (allHeaders != null) {
471             while (allHeaders.hasMoreElements()) {
472                 Header JavaDoc header = (Header JavaDoc) allHeaders.nextElement();
473                 attributes.clear();
474                 addAttribute("name", header.getName());
475                 addAttribute("value", header.getValue());
476                 startElement("header", attributes);
477
478                 endElement("header");
479             }
480         }
481
482         endElement("message-envelope");
483     }
484
485
486     /**
487      * Emit part as sequence of SAX events
488      *
489      *@param part Description of the Parameter
490      *@param contentHandler specifies sink of SAX events
491      *@param i Description of the Parameter
492      *@exception MessagingException thrown iff accessing javamail data fails
493      *@exception IOException thrown iff accessing content fails
494      *@exception SAXException thrown iff generating SAX events fails
495      */

496     protected void partToSAX(ContentHandler JavaDoc contentHandler, Part JavaDoc part, int i) throws MessagingException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
497         attributes.clear();
498         String JavaDoc v;
499         if ((v = part.getContentType()) != null) {
500             // content type as-is
501
addAttribute("content-type", v);
502             try {
503                 ContentType JavaDoc ct = new ContentType JavaDoc(v);
504                 String JavaDoc s;
505
506                 // primary part only
507
s = ct.getPrimaryType();
508                 if (s != null) {
509                     addAttribute("primary-type", s.toLowerCase());
510                 }
511
512                 // secondary part only
513
s = ct.getSubType();
514                 if (s != null) {
515                     addAttribute("secondary-type", s.toLowerCase());
516                 }
517
518                 // primary part '/' secondary part
519
s = ct.getBaseType();
520                 if (s != null) {
521                     addAttribute("base-type", s.toLowerCase());
522                 }
523
524                 // list of parameters : parameter-name parameter-value
525
ParameterList JavaDoc pl = ct.getParameterList();
526                 Enumeration JavaDoc names = pl.getNames();
527                 while (names.hasMoreElements()) {
528                     String JavaDoc key = (String JavaDoc) names.nextElement();
529                     String JavaDoc value = pl.get(key);
530                     addAttribute(key, value);
531                 }
532             } catch (ParseException JavaDoc pe) {
533                 String JavaDoc message = "Cannot parse content-type " + String.valueOf(v);
534                 getLogger().error(message, pe);
535             }
536         }
537
538         if (i > 0) {
539             addAttribute("part-num", String.valueOf(i));
540
541             if (part.getDescription() != null) {
542                 addAttribute("description", part.getDescription());
543             }
544             if (part.getDisposition() != null) {
545                 addAttribute("disposition", part.getDisposition());
546                 addAttribute("disposition-inline", String.valueOf(part.getDisposition().equals(Part.INLINE)));
547             }
548             if (part.getFileName() != null) {
549                 addAttribute("file-name", part.getFileName());
550             }
551         } else {
552             boolean hasAttachments = false;
553             if (part.isMimeType("multipart/*")) {
554                 Multipart JavaDoc mp = (Multipart JavaDoc) part.getContent();
555                 if (mp.getCount() > 1) {
556                     hasAttachments = true;
557                     addAttribute("num-parts", String.valueOf(mp.getCount()));
558                 }
559             }
560             addAttribute("has-attachments", String.valueOf(hasAttachments));
561         }
562
563         startElement("part", attributes);
564         contentToSAX(contentHandler, part);
565         endElement("part");
566     }
567
568
569     /**
570      * Emit content of message part as sequence of SAX events
571      *
572      *@param contentHandler specifies sink of SAX events
573      *@param part emit this part as sequence of SAX events
574      *@exception SAXException thrown iff generating SAX events fails
575      *@exception MessagingException thrown iff accessing javamail data fails
576      *@exception IOException thrown iff accessing content fails
577      */

578     protected void contentToSAX(ContentHandler JavaDoc contentHandler, Part JavaDoc part) throws MessagingException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
579         attributes.clear();
580         startElement("content", attributes);
581
582         /*
583          * Using isMimeType to determine the content type avoids
584          * fetching the actual content data until we need it.
585          *
586          * todo: recheck this code for all circumstances.........
587          */

588         if (part.getContent() instanceof String JavaDoc && (part.isMimeType("text/plain"))) {
589             characters((String JavaDoc) part.getContent());
590         } else if (part.isMimeType("multipart/alternative")) {
591             MimeMultipart JavaDoc mp = (MimeMultipart JavaDoc) part.getContent();
592             MimePart JavaDoc bestPart = null;
593             int ctMax = 0;
594             for (int i = 0; i < mp.getCount(); i++) {
595                 MimePart JavaDoc p = (MimePart JavaDoc) mp.getBodyPart(i);
596                 int ctPrefN = alternativeMailCtPref.preference(p);
597                 if (ctPrefN > ctMax) {
598                     ctMax = ctPrefN;
599                     bestPart = p;
600                 }
601             }
602             if (bestPart != null) {
603                 partToSAX(contentHandler, bestPart, 0);
604             }
605         } else if (part.isMimeType("multipart/*")) {
606             Multipart JavaDoc mp = (Multipart JavaDoc) part.getContent();
607
608             int count = mp.getCount();
609             for (int i = 0; i < count; i++) {
610                 partToSAX(contentHandler, mp.getBodyPart(i), i);
611             }
612         } else if (part.isMimeType("message/rfc822")) {
613             partToSAX(contentHandler, (Part JavaDoc) part.getContent(), 0);
614         } else {
615             /*
616              * If we actually want to see the data, and it's not a
617              * MIME type we know, fetch it and check its Java type.
618             Object o = part.getContent();
619             if (o instanceof String) {
620                 characters((String) o);
621             } else if (o instanceof InputStream) {
622                 encodeInputStreamForXML((InputStream) o);
623             } else {
624                 // unknown type
625                 InputStream is = part.getInputStream();
626                 encodeInputStreamForXML(is);
627             }
628             */

629         }
630         endElement("content");
631     }
632
633
634     /**
635      * Helper method emitting SAX events representing an internet address
636      *
637      *@param nodeName emitted element node name
638      *@param address emitted address data
639      *@exception SAXException thrown iff generating SAX events fails
640      */

641     protected void emitAddress(String JavaDoc nodeName, Address JavaDoc address) throws SAXException JavaDoc {
642
643         attributes.clear();
644
645         if (address instanceof InternetAddress JavaDoc) {
646             InternetAddress JavaDoc internetAddress = (InternetAddress JavaDoc) address;
647             String JavaDoc personal = internetAddress.getPersonal();
648             if (personal != null) {
649                 addAttribute("personal", personal);
650             }
651             String JavaDoc emailAddress = internetAddress.getAddress();
652             if (emailAddress != null) {
653                 addAttribute("email-address", emailAddress);
654             }
655         }
656
657         startElement(nodeName, attributes);
658         String JavaDoc addressAsString = address.toString();
659         characters(addressAsString);
660         endElement(nodeName);
661     }
662
663
664     /**
665      * Helper method emitting SAX events representing a date
666      *
667      *@param nodeName emitted element node name
668      *@param d Description of the Parameter
669      *@exception SAXException thrown iff generating SAX events fails
670      */

671     protected void emitDate(String JavaDoc nodeName, Date JavaDoc d) throws SAXException JavaDoc {
672         attributes.clear();
673         startElement(nodeName, attributes);
674         if (d != null) {
675             if (sdf != null) {
676                 String JavaDoc formattedDate = sdf.format(d);
677                 characters(formattedDate);
678             } else {
679                 characters(d.toString());
680             }
681         }
682         endElement(nodeName);
683     }
684
685
686     /**
687      * Helper method emitting SAX startElement event
688      *
689      *@param nodeName Description of the Parameter
690      *@param attributes Description of the Parameter
691      *@exception SAXException thrown iff generating SAX events fails
692      */

693     private void startElement(String JavaDoc nodeName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
694         this.contentHandler.startElement(URI, nodeName, PREFIX + ":" + nodeName, attributes);
695     }
696
697
698     /**
699      * Helper method emitting SAX characters event
700      *
701      *@param s Description of the Parameter
702      *@exception SAXException thrown iff generating SAX events fails
703      */

704     private void characters(String JavaDoc s) throws SAXException JavaDoc {
705         if (s != null) {
706             // replace 0d0a by 0a
707
// any better idea ?
708
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
709             char[] stringCharacters = s.toCharArray();
710             for (int i = 0; i < stringCharacters.length; i++) {
711                 if (stringCharacters[i] != 0x0d) {
712                     sb.append(stringCharacters[i]);
713                 }
714             }
715             stringCharacters = sb.toString().toCharArray();
716
717             this.contentHandler.characters(stringCharacters, 0, stringCharacters.length);
718         }
719     }
720
721
722     /**
723      * Helper method emitting SAX endElement event
724      *
725      *@param nodeName Description of the Parameter
726      *@exception SAXException thrown iff generating SAX events fails
727      */

728     private void endElement(String JavaDoc nodeName) throws SAXException JavaDoc {
729         this.contentHandler.endElement(URI, nodeName, PREFIX + ":" + nodeName);
730     }
731
732
733     /**
734      * Helper method adding an attribute name-value pair
735      *
736      *@param nodeName The feature to be added to the Attribute attribute
737      *@param nodeValue The feature to be added to the Attribute attribute
738      */

739     private void addAttribute(String JavaDoc nodeName, String JavaDoc nodeValue) {
740         attributes.addAttribute("", nodeName, nodeName, "CDATA", nodeValue);
741     }
742
743
744     /*
745      * Description of the Method
746      *
747      *@param is Description of Parameter
748      *@exception IOException Description of Exception
749      *@exception SAXException thrown iff generating SAX events fails
750      */

751     /* FIXME (SM) This method doesn't appear to be used
752      private void encodeInputStreamForXML(InputStream is) throws IOException, SAXException {
753         int contentLength = is.available();
754         if (contentLength < 16) {
755             contentLength = 2048;
756         }
757         attributes.clear();
758         addAttribute("type", "hex");
759         startElement("encoding", attributes);
760         byte content[] = new byte[contentLength];
761         int readLength;
762         while ((readLength = is.read(content, 0, content.length)) != -1) {
763             String strContent = encodeBytes(content, 0, readLength);
764             characters(strContent);
765         }
766         endElement("encoding");
767     } */

768
769
770     /*
771      * A simple byte as hex encodeing
772      *
773      *@param bytes Description of Parameter
774      *@param offset Description of Parameter
775      *@param length Description of Parameter
776      *@return Description of the Returned Value
777      */

778     /* FIXME (SM) This method doesn't appear to be used
779     private String encodeBytes(final byte[] bytes, final int offset, final int length) {
780         StringBuffer sb = new StringBuffer();
781         final String ENCODE_TABLE[] = {
782                 "0", "1", "2", "3", "4", "5", "6", "7", "8",
783                 "9", "a", "b", "c", "d", "e", "f"};
784         final int l = offset + length;
785         for (int i = offset; i < l; i++) {
786             byte b = bytes[i];
787             int upperNibble = ((b >> 4) & 0x0f);
788             int lowerNibble = (b & 0x0f);
789
790             sb.append(ENCODE_TABLE[upperNibble]);
791             sb.append(ENCODE_TABLE[lowerNibble]);
792             sb.append(" ");
793         }
794         return sb.toString();
795     } */

796
797
798     /**
799      * XMLizable Wrapper for one or more folders, saxing folders.
800      *
801      *@author Bernhard Huber
802      *@created 30. Dezember 2002
803      *@version CVS Version: $Id: MailContentHandlerDelegate.java 164808 2005-04-26 16:07:03Z vgritsenko $
804      */

805     static class FolderXMLizer extends AbstractLogEnabled
806              implements XMLizable {
807         //private Folder folder;
808
private Folder JavaDoc[] folders;
809
810
811         /**
812          *Constructor for the FolderSAX object
813          *
814          *@param folder Description of the Parameter
815          */

816         FolderXMLizer(Folder JavaDoc folder) {
817             this.folders = new Folder JavaDoc[]{folder};
818         }
819
820
821         /**
822          *Constructor for the FolderXMLizer object
823          *
824          *@param folders Description of the Parameter
825          */

826         FolderXMLizer(Folder JavaDoc[] folders) {
827             this.folders = folders;
828         }
829
830
831         /**
832          * Generate SAX events from one or more folders
833          *
834          *@param handler Description of the Parameter
835          *@exception SAXException thrown iff generating SAX events fails
836          */

837         public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
838             MailContentHandlerDelegate mailContentHandlerDelegate = new MailContentHandlerDelegate(handler);
839             mailContentHandlerDelegate.enableLogging(getLogger());
840             mailContentHandlerDelegate.startDocumentXMLizer();
841             for (int i = 0; i < folders.length; i++) {
842                 mailContentHandlerDelegate.marshalFolderToSAX(folders[i]);
843             }
844             mailContentHandlerDelegate.endDocumentXMLizer();
845         }
846     }
847
848
849     /**
850      * XMLizable Wrapper for one or more messages, saxing envelope only of messages.
851      *
852      *@author Bernhard Huber
853      *@created 30. Dezember 2002
854      *@version CVS Version: $Id: MailContentHandlerDelegate.java 164808 2005-04-26 16:07:03Z vgritsenko $
855      */

856     static class MessageEnvelopeXMLizer extends AbstractLogEnabled
857              implements XMLizable {
858         private Message JavaDoc[] messages;
859
860         private SimpleDateFormat JavaDoc sdf;
861
862
863         /**
864          *Constructor for the MessageEnvelopeXMLizer object
865          *
866          *@param message Description of the Parameter
867          */

868         public MessageEnvelopeXMLizer(Message JavaDoc message) {
869             this.messages = new Message JavaDoc[1];
870             this.messages[0] = message;
871         }
872
873
874         /**
875          *Constructor for the MessageEnvelopeXMLize object
876          *
877          *@param messages Description of the Parameter
878          */

879         public MessageEnvelopeXMLizer(Message JavaDoc[] messages) {
880             this.messages = messages;
881         }
882
883
884         /**
885          * Sets the simpleDateFormat attribute of the MessageEnvelopeXMLizer object
886          *
887          *@param sdf The new simpleDateFormat value
888          */

889         public void setSimpleDateFormat(SimpleDateFormat JavaDoc sdf) {
890             this.sdf = sdf;
891         }
892
893
894         /**
895          * Gets the simpleDateFormat attribute of the MessageEnvelopeXMLizer object
896          *
897          *@param sdf Description of the Parameter
898          */

899         public void getSimpleDateFormat(SimpleDateFormat JavaDoc sdf) {
900             this.sdf = sdf;
901         }
902
903
904         /**
905          * Generate SAX events from one or more messages
906          *
907          *@param handler Description of the Parameter
908          *@exception SAXException thrown iff generating SAX events fails
909          */

910         public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
911             MailContentHandlerDelegate mailContentHandlerDelegate = new MailContentHandlerDelegate(handler);
912             mailContentHandlerDelegate.enableLogging(getLogger());
913             mailContentHandlerDelegate.setSimpleDateFormat(sdf);
914             mailContentHandlerDelegate.startDocumentXMLizer();
915
916             for (int i = 0; i < messages.length; i++) {
917                 mailContentHandlerDelegate.marshalMessageEnvelopeToSAX(messages[i]);
918             }
919
920             mailContentHandlerDelegate.endDocumentXMLizer();
921         }
922     }
923
924
925     /**
926      * XMLizable Wrapper for a message, saxing a message envelope, plus content.
927      *
928      *@author Bernhard Huber
929      *@created 30. Dezember 2002
930      *@version CVS Version: $Id: MailContentHandlerDelegate.java 164808 2005-04-26 16:07:03Z vgritsenko $
931      */

932     static class MessageXMLizer extends AbstractLogEnabled
933              implements XMLizable {
934         private Message JavaDoc message;
935
936         private SimpleDateFormat JavaDoc sdf;
937
938
939         /**
940          *Constructor for the MessageXMLizer object
941          *
942          *@param message Description of the Parameter
943          */

944         public MessageXMLizer(Message JavaDoc message) {
945             this.message = message;
946         }
947
948
949         /**
950          * Sets the simpleDateFormat attribute of the MessageXMLizer object
951          *
952          *@param sdf The new simpleDateFormat value
953          */

954         public void setSimpleDateFormat(SimpleDateFormat JavaDoc sdf) {
955             this.sdf = sdf;
956         }
957
958
959         /**
960          * Gets the simpleDateFormat attribute of the MessageXMLizer object
961          *
962          *@param sdf Description of the Parameter
963          */

964         public void getSimpleDateFormat(SimpleDateFormat JavaDoc sdf) {
965             this.sdf = sdf;
966         }
967
968
969         /**
970          * Generate SAX events from a message
971          *
972          *@param handler Description of the Parameter
973          *@exception SAXException thrown iff generating SAX events fails
974          */

975         public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
976             MailContentHandlerDelegate mailContentHandlerDelegate = new MailContentHandlerDelegate(handler);
977             mailContentHandlerDelegate.enableLogging(getLogger());
978             mailContentHandlerDelegate.setSimpleDateFormat(sdf);
979             mailContentHandlerDelegate.startDocumentXMLizer();
980             mailContentHandlerDelegate.marshalMessageToSAX(message);
981             mailContentHandlerDelegate.endDocumentXMLizer();
982         }
983     }
984 }
985
986
Popular Tags