1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.mail.model;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.util.Html;
31  
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  /**
36   * <a href="MailContent.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Alexander Chow
39   *
40   */
41  public class MailContent {
42  
43      public MailContent() {
44      }
45  
46      public MailContent(String body) {
47          setHtmlBody(body);
48      }
49  
50      public String getPlainBody() {
51          return GetterUtil.getString(_plainBody);
52      }
53  
54      public void setPlainBody(String plainBody) {
55          _plainBody = plainBody;
56      }
57  
58      public void appendPlainBody(String plainBody) {
59          if (Validator.isNull(_plainBody)) {
60              _plainBody = plainBody;
61          }
62          else {
63              _plainBody += StringPool.NEW_LINE + StringPool.NEW_LINE + plainBody;
64          }
65      }
66  
67      public String getHtmlBody() {
68          if (Validator.isNotNull(_htmlBody)) {
69              return _htmlBody;
70          }
71          else {
72              String body = GetterUtil.getString(_plainBody);
73  
74              if (Validator.isNotNull(body)) {
75                  body = Html.escape(body);
76                  body = StringUtil.replace(body, "\t", "    ");
77                  body = StringUtil.replace(body, "  ", " &nbsp;");
78                  body = StringUtil.replace(body, "\n", "<BR />");
79                  body =
80                      "<div style=\"font-family: courier, monospace; " +
81                          "font-size: 12\">" + body + "</div>";
82              }
83  
84              return body;
85          }
86      }
87  
88      public void setHtmlBody(String htmlBody) {
89          _htmlBody = htmlBody;
90      }
91  
92      public void appendHtmlBody(String htmlBody) {
93          if (Validator.isNull(_htmlBody)) {
94              _htmlBody = htmlBody;
95          }
96          else {
97              _htmlBody += "<HR/>" + htmlBody;
98          }
99      }
100 
101     public List getSubContent() {
102         return _subContent;
103     }
104 
105     public void appendSubContent(MailContent mc) {
106         _subContent.add(mc);
107     }
108 
109     public String toString() {
110         StringMaker sm = new StringMaker();
111 
112         sm.append(getHtmlBody());
113 
114         for (int i = 0; i < _subContent.size(); i++) {
115             sm.append("<HR/>" + _subContent.get(i).toString());
116         }
117 
118         return sm.toString();
119     }
120 
121     private String _plainBody;
122     private String _htmlBody;
123     private List _subContent = new ArrayList();
124 
125 }