001: /* CVS ID: $Id: XMLMessage.java,v 1.1.1.1 2002/10/02 18:42:54 wastl Exp $ */
002: package net.wastl.webmail.xml;
003:
004: import java.util.*;
005:
006: import org.w3c.dom.*;
007:
008: /*
009: * XMLMessage.java
010: *
011: * Created: Tue Apr 18 14:02:30 2000
012: *
013: * Copyright (C) 2000 Sebastian Schaffert
014: *
015: * This program is free software; you can redistribute it and/or
016: * modify it under the terms of the GNU General Public License
017: * as published by the Free Software Foundation; either version 2
018: * of the License, or (at your option) any later version.
019: *
020: * This program is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * You should have received a copy of the GNU General Public License
026: * along with this program; if not, write to the Free Software
027: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
028: *
029: */
030:
031: /**
032: * Represents an XML message object (part of the XMLUserModel)
033: * @see XMLUserModel
034: */
035: public class XMLMessage extends XMLMessagePart {
036:
037: protected Element message;
038:
039: public XMLMessage(Element message) {
040: super (message);
041: this .message = message;
042: }
043:
044: public Element getMessageElement() {
045: return message;
046: }
047:
048: public boolean messageCompletelyCached() {
049: NodeList nl = message.getElementsByTagName("PART");
050: return nl.getLength() > 0;
051: }
052:
053: public void prepareReply(String prefix_subject,
054: String postfix_subject, String prefix_message,
055: String postfix_message) {
056:
057: String subject = getHeader("SUBJECT");
058: // Test whether this is already a reply (prefixed with RE or AW)
059: if (!isReply(subject)) {
060: subject = prefix_subject + " " + getHeader("SUBJECT") + " "
061: + postfix_subject;
062: }
063: setHeader("SUBJECT", subject);
064:
065: XMLMessagePart firstpart = new XMLMessagePart(
066: getFirstMessageTextPart(message));
067:
068: firstpart.quoteContent();
069:
070: firstpart.insertContent(prefix_message + "\n", 0);
071: firstpart.addContent(postfix_message, 0);
072:
073: removeAllParts();
074:
075: XMLMessagePart newmainpart = createPart("multi");
076:
077: newmainpart.appendPart(firstpart);
078: }
079:
080: public void prepareForward(String prefix_subject, String postfix_subject,
081: String prefix_message, String postfix_message) {
082:
083: String subject=getHeader("SUBJECT");
084: subject=prefix_subject+" "
085: +getHeader("SUBJECT")+" "
086: +postfix_subject;
087: setHeader("SUBJECT",subject);
088:
089: XMLMessagePart mainpart=null;
090: NodeList nl=message.getChildNodes();
091: for(int i=0;i<nl.getLength();i++) {
092: Element elem=(Element)nl.item(i);
093: if(elem.getTagName().equals("PART")) {
094: mainpart=new XMLMessagePart(elem);
095: break;
096: }
097: }
098: XMLMessagePart firstpart=new XMLMessagePart(getFirstMessageTextPart(message));
099:
100: firstpart.insertContent(prefix_message+"\n",0);
101: firstpart.addContent(postfix_message,0);
102:
103: removeAllParts();
104:
105: XMLMessagePart newmainpart=createPart("multi");
106:
107: newmainpart.appendPart(firstpart);
108:
109: if(mainpart != null) {
110: Enumeration enum=mainpart.getParts();
111: while(enum.hasMoreElements()) {
112: newmainpart.appendPart((XMLMessagePart)enum.nextElement());
113: }
114: }
115: }
116:
117: public Element getHeader() {
118: NodeList nl = message.getElementsByTagName("HEADER");
119: Element header = nl.getLength() == 0 ? null : (Element) nl
120: .item(0);
121: if (header == null) {
122: header = root.createElement("HEADER");
123: message.appendChild(header);
124: }
125: return header;
126: }
127:
128: public String getHeader(String header) {
129: Element xml_header = getHeader();
130: NodeList nl = xml_header.getElementsByTagName(header);
131: Element element;
132: if (nl.getLength() == 0) {
133: element = root.createElement(header);
134: xml_header.appendChild(element);
135: } else {
136: element = (Element) nl.item(0);
137: }
138: return XMLCommon.getElementTextValue(element);
139: }
140:
141: public void setHeader(String header, String value) {
142: Element xml_header = getHeader();
143: NodeList nl = xml_header.getElementsByTagName(header);
144: Element element;
145: if (nl.getLength() == 0) {
146: element = root.createElement(header);
147: xml_header.appendChild(element);
148: } else {
149: element = (Element) nl.item(0);
150: }
151: XMLCommon.setElementTextValue(element, value);
152: }
153:
154: protected boolean isReply(String subject) {
155: String s = subject.toUpperCase();
156: return s.startsWith("RE") || s.startsWith("AW");
157: }
158:
159: protected Element getFirstMessageTextPart(Element parent) {
160: NodeList nl = parent.getChildNodes();
161: // Modified by exce, start
162: /**
163: * Maybe here we should modify the algorithm:
164: * If no appropriate text/plain is found, try to search for text/html.
165: */
166: // Modified by exce, end
167: for (int i = 0; i < nl.getLength(); i++) {
168: Element elem = (Element) nl.item(i);
169: if (elem.getTagName().equals("PART")) {
170: if (elem.getAttribute("type").equals("multi")) {
171: Element retval = getFirstMessageTextPart(elem);
172: if (retval != null) {
173: return retval;
174: }
175: } else if (elem.getAttribute("type").equals("text")) {
176: return elem;
177: }
178: }
179: }
180: // Modified by exce, start
181: for (int i = 0; i < nl.getLength(); i++) {
182: Element elem = (Element) nl.item(i);
183: if (elem.getTagName().equals("PART")) {
184: if (elem.getAttribute("type").equals("multi")) {
185: Element retval = getFirstMessageTextPart(elem);
186: if (retval != null) {
187: return retval;
188: }
189: } else if (elem.getAttribute("type").equals("html")) {
190: return elem;
191: }
192: }
193: }
194: // Modified by exce, end
195: return null;
196: }
197:
198: public XMLMessagePart getFirstMessageTextPart() {
199: return new XMLMessagePart(getFirstMessageTextPart(message));
200: }
201:
202: public XMLMessagePart getFirstMessageMultiPart() {
203: NodeList nl = message.getChildNodes();
204: for (int i = 0; i < nl.getLength(); i++) {
205: Element elem = (Element) nl.item(i);
206: if (elem.getTagName().equals("PART")) {
207: if (elem.getAttribute("type").equals("multi")) {
208: return new XMLMessagePart(elem);
209: }
210: }
211: }
212: return null;
213: }
214:
215: } // XMLMessage
|