View Javadoc
1   /* ***** BEGIN LICENSE BLOCK *****
2    * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3    *
4    * The contents of this file are subject to the Mozilla Public License Version
5    * 1.1 (the "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    * http://www.mozilla.org/MPL/
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis,
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11   * for the specific language governing rights and limitations under the
12   * License.
13   *
14   * The Original Code is "SMS Library for the Java platform".
15   *
16   * The Initial Developer of the Original Code is Markus Eriksson.
17   * Portions created by the Initial Developer are Copyright (C) 2002
18   * the Initial Developer. All Rights Reserved.
19   *
20   * Contributor(s):
21   *
22   * Alternatively, the contents of this file may be used under the terms of
23   * either the GNU General Public License Version 2 or later (the "GPL"), or
24   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25   * in which case the provisions of the GPL or the LGPL are applicable instead
26   * of those above. If you wish to allow use of your version of this file only
27   * under the terms of either the GPL or the LGPL, and not to allow others to
28   * use your version of this file under the terms of the MPL, indicate your
29   * decision by deleting the provisions above and replace them with the notice
30   * and other provisions required by the GPL or the LGPL. If you do not delete
31   * the provisions above, a recipient may use your version of this file under
32   * the terms of any one of the MPL, the GPL or the LGPL.
33   *
34   * ***** END LICENSE BLOCK ***** */
35  package org.marre.sms;
36  
37  import java.io.*;
38  
39  /***
40   * Represents a text message.
41   * <p>
42   * The text can be sent in unicode (max 70 chars/SMS), 8-bit (max 140 chars/SMS)
43   * or GSM encoding (max 160 chars/SMS).
44   *
45   * @author Markus Eriksson
46   * @version $Id: SmsTextMessage.java,v 1.27 2005/05/12 07:49:41 c95men Exp $
47   */
48  public class SmsTextMessage extends SmsConcatMessage
49  {
50      private String text_;
51      private SmsDcs dcs_;
52      
53      /***
54       * Creates an SmsTextMessage with the given dcs.
55       * 
56       * @param theMsg The message
57       * @param theDcs The data coding scheme
58       */
59      public SmsTextMessage(String theMsg, SmsDcs theDcs)
60      {
61          setText(theMsg, theDcs);
62      }
63      
64      /***
65       * Creates an SmsTextMessage with the given alphabet and message class.
66       * <p>
67       * theAlphabet can be any of:<br>
68       * - SmsConstants.ALPHABET_GSM<br>
69       * - SmsConstants.ALPHABET_8BIT<br>
70       * - SmsConstants.ALPHABET_UCS2<br>
71       * <p>
72       * theMessageClass can be any of:<br>
73       * - SmsConstants.MSG_CLASS_0 (Often called a FLASH message)<br>
74       * - SmsConstants.MSG_CLASS_1<br>
75       * - SmsConstants.MSG_CLASS_2<br>
76       * - SmsConstants.MSG_CLASS_3<br>
77       *
78       * @param theMsg The message
79       * @param theAlphabet The alphabet
80       * @param theMessageClass The messageclass
81       */
82      public SmsTextMessage(String theMsg, int theAlphabet, byte theMessageClass)
83      {
84          this(theMsg, SmsDcs.getGeneralDataCodingDcs(theAlphabet, theMessageClass));
85      }
86  
87      /***
88       * Creates an SmsTextMessage with default 7Bit GSM Alphabet
89       *
90       * @param theMsg The message
91       */
92      public SmsTextMessage(String theMsg)
93      {
94          this(theMsg, SmsDcs.ALPHABET_GSM, SmsDcs.MSG_CLASS_UNKNOWN);
95      }
96      
97      /***
98       * Returns the text message. 
99       */
100     public String getText()
101     {
102         return text_;
103     }
104     
105     /***
106      * Sets the text.
107      * 
108      * @param text
109      */
110     public void setText(String text)
111     {
112         if (text == null)
113         {
114             throw new IllegalArgumentException("Text cannot be null, use an empty string instead.");
115         }
116         
117         text_ = text;
118     }
119 
120     /***
121      * Sets the text.
122      * 
123      * @param text
124      */
125     public void setText(String text, SmsDcs dcs)
126     {
127         // Check input for null
128         if (text == null)
129         {
130             throw new IllegalArgumentException("text cannot be null, use an empty string instead.");
131         }
132         
133         if (dcs == null)
134         {
135             throw new IllegalArgumentException("dcs cannot be null.");
136         }
137         
138         text_ = text;
139         dcs_ = dcs;
140     }
141     
142     /***
143      * Returns the dcs.
144      */
145     public SmsDcs getDcs()
146     {
147         return dcs_;
148     }
149 
150     /***
151      * Returns the user data.
152      * @return user data
153      */
154     public SmsUserData getUserData()
155     {
156         SmsUserData ud;
157         
158         try
159         {
160             switch (dcs_.getAlphabet())
161             {
162             case SmsDcs.ALPHABET_GSM:
163                 ud = new SmsUserData(SmsPduUtil.getSeptets(text_), text_.length(), dcs_);
164                 break;
165                 
166             case SmsDcs.ALPHABET_8BIT:
167                 ud = new SmsUserData(text_.getBytes("ISO-8859-1"), text_.length(), dcs_);
168                 break;
169                 
170             case SmsDcs.ALPHABET_UCS2:
171                 ud = new SmsUserData(text_.getBytes("UTF-16BE"), text_.length() * 2, dcs_);
172                 break;
173                 
174             default:
175                 ud = null;
176                 break;
177             }
178         }
179         catch (UnsupportedEncodingException ex)
180         {
181             // Shouldn't happen. According to the javadoc documentation
182             // for JDK 1.3.1 the "UTF-16BE" and "ISO-8859-1" encoding
183             // are standard...
184             throw new RuntimeException(ex);
185         }
186         
187         return ud;
188     }
189 
190     /***
191      * Returns null.
192      */
193     public SmsUdhElement[] getUdhElements()
194     {
195         return null;
196     }
197 }