Back to index

lightning-sunbird  0.9+nobinonly
nsMimeXmlEmitter.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdio.h>
00039 #include "nsMimeRebuffer.h"
00040 #include "nsMimeXmlEmitter.h"
00041 #include "plstr.h"
00042 #include "nsMailHeaders.h"
00043 #include "nscore.h"
00044 #include "nsEscape.h"
00045 #include "prmem.h"
00046 #include "nsEmitterUtils.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsReadableUtils.h"
00049 #include "nsUnicharUtils.h"
00050 
00051 /*
00052  * nsMimeXmlEmitter definitions....
00053  */
00054 nsMimeXmlEmitter::nsMimeXmlEmitter()
00055 {
00056 }
00057 
00058 
00059 nsMimeXmlEmitter::~nsMimeXmlEmitter(void)
00060 {
00061 }
00062 
00063 
00064 // Note - this is teardown only...you should not write
00065 // anything to the stream since these may be image data
00066 // output streams, etc...
00067 nsresult
00068 nsMimeXmlEmitter::Complete()
00069 {
00070   char  buf[16];
00071 
00072   // Now write out the total count of attachments for this message
00073   UtilityWrite("<mailattachcount>");
00074   sprintf(buf, "%d", mAttachCount);
00075   UtilityWrite(buf);
00076   UtilityWrite("</mailattachcount>");
00077 
00078   UtilityWrite("</message>");
00079 
00080   return nsMimeBaseEmitter::Complete();
00081 
00082 }
00083 
00084 nsresult
00085 nsMimeXmlEmitter::WriteXMLHeader(const char *msgID)
00086 {
00087   if ( (!msgID) || (!*msgID) )
00088     msgID = "none";
00089     
00090   char  *newValue = nsEscapeHTML(msgID);
00091   if (!newValue)
00092     return NS_ERROR_OUT_OF_MEMORY;
00093     
00094   UtilityWrite("<?xml version=\"1.0\"?>");
00095 
00096   UtilityWriteCRLF("<?xml-stylesheet href=\"chrome://messenger/skin/messageBody.css\" type=\"text/css\"?>");
00097 
00098   UtilityWrite("<message id=\"");
00099   UtilityWrite(newValue);
00100   UtilityWrite("\">");
00101 
00102   mXMLHeaderStarted = PR_TRUE;
00103   PR_FREEIF(newValue);
00104   return NS_OK;
00105 }
00106 
00107 nsresult
00108 nsMimeXmlEmitter::WriteXMLTag(const char *tagName, const char *value)
00109 {
00110   if ( (!value) || (!*value) )
00111     return NS_OK;
00112 
00113   char  *upCaseTag = NULL;
00114   char  *newValue = nsEscapeHTML(value);
00115   if (!newValue) 
00116     return NS_OK;
00117 
00118   nsString  newTagName; newTagName.AssignWithConversion(tagName);
00119   newTagName.CompressWhitespace(PR_TRUE, PR_TRUE);
00120 
00121   ToUpperCase(newTagName);
00122   upCaseTag = ToNewCString(newTagName);
00123 
00124   UtilityWrite("<header field=\"");
00125   UtilityWrite(upCaseTag);
00126   UtilityWrite("\">");
00127 
00128   // Here is where we are going to try to L10N the tagName so we will always
00129   // get a field name next to an emitted header value. Note: Default will always
00130   // be the name of the header itself.
00131   //
00132   UtilityWrite("<headerdisplayname>");
00133   char *l10nTagName = LocalizeHeaderName(upCaseTag, tagName);
00134   if ( (!l10nTagName) || (!*l10nTagName) )
00135     UtilityWrite(tagName);
00136   else
00137   {
00138     UtilityWrite(l10nTagName);
00139     PR_FREEIF(l10nTagName);
00140   }
00141 
00142   UtilityWrite(": ");
00143   UtilityWrite("</headerdisplayname>");
00144 
00145   // Now write out the actual value itself and move on!
00146   //
00147   UtilityWrite(newValue);
00148   UtilityWrite("</header>");
00149 
00150   nsCRT::free(upCaseTag);
00151   PR_FREEIF(newValue);
00152 
00153   return NS_OK;
00154 }
00155 
00156 // Header handling routines.
00157 nsresult
00158 nsMimeXmlEmitter::StartHeader(PRBool rootMailHeader, PRBool headerOnly, const char *msgID,
00159                            const char *outCharset)
00160 {
00161   mDocHeader = rootMailHeader;
00162   WriteXMLHeader(msgID);
00163   UtilityWrite("<mailheader>");
00164 
00165   return NS_OK; 
00166 }
00167 
00168 nsresult
00169 nsMimeXmlEmitter::AddHeaderField(const char *field, const char *value)
00170 {
00171   if ( (!field) || (!value) )
00172     return NS_OK;
00173 
00174   WriteXMLTag(field, value);
00175   return NS_OK;
00176 }
00177 
00178 nsresult
00179 nsMimeXmlEmitter::EndHeader()
00180 {
00181   UtilityWrite("</mailheader>");
00182   return NS_OK; 
00183 }
00184 
00185 
00186 // Attachment handling routines
00187 nsresult
00188 nsMimeXmlEmitter::StartAttachment(const char *name, const char *contentType, const char *url,
00189                                   PRBool aIsExternalAttachment)
00190 {
00191   char    buf[128];
00192 
00193   ++mAttachCount;
00194 
00195   sprintf(buf, "<mailattachment id=\"%d\">", mAttachCount);
00196   UtilityWrite(buf);
00197 
00198   AddAttachmentField(HEADER_PARM_FILENAME, name);
00199   return NS_OK;
00200 }
00201 
00202 nsresult
00203 nsMimeXmlEmitter::AddAttachmentField(const char *field, const char *value)
00204 {
00205   WriteXMLTag(field, value);
00206   return NS_OK;
00207 }
00208 
00209 nsresult
00210 nsMimeXmlEmitter::EndAttachment()
00211 {
00212   UtilityWrite("</mailattachment>");
00213   return NS_OK;
00214 }
00215 
00216