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  /***
38   * Represents an phonenumber in SMSj.
39   * <p>
40   * The address can be a phonenumber (+463482422) or alphanumeric
41   * ('SmsService'). Not all networks and transports supports alphanumeric
42   * sending id.
43   * <p>
44   * Max address length is <br>
45   * - 20 digits (excluding any initial '+') or<br>
46   * - 11 alphanumeric chars (if TON == TON_ALPHANUMERIC).
47   * <p>
48   * Look in SmsConstants for definitions of TON and NPI.
49   *
50   * @author Markus Eriksson
51   * @version $Id: SmsAddress.java,v 1.7 2004/11/02 17:59:49 c95men Exp $
52   */
53  public class SmsAddress
54  {
55      private static final String ALLOWED_DIGITS = "+0123456789*#ab";
56  
57      private int myTon = SmsConstants.TON_INTERNATIONAL;
58      private int myNpi = SmsConstants.NPI_ISDN_TELEPHONE;
59  
60      private String myAddress;
61  
62      /***
63       * Creates an SmsAddress object.
64       * <p>
65       * This constructor tries to be intelligent by choosing the correct
66       * NPI and TON from the given address.
67       *
68       * @param theAddress The address
69       * @throws SmsException Thrown if the address is invalid
70       */
71      public SmsAddress(String theAddress)
72          throws SmsException
73      {
74          int npi = SmsConstants.NPI_ISDN_TELEPHONE;
75          int ton = SmsConstants.TON_INTERNATIONAL;
76  
77          for (int i = 0; i < theAddress.length(); i++)
78          {
79              char ch = theAddress.charAt(i);
80              if (ALLOWED_DIGITS.indexOf(ch) == -1)
81              {
82                  ton = SmsConstants.TON_ALPHANUMERIC;
83                  npi = SmsConstants.NPI_UNKNOWN;
84                  break;
85              }
86          }
87  
88          init(theAddress, ton, npi);
89      }
90  
91      /***
92       * Creates an SmsAddress object.
93       * <p>
94       * If you choose TON_ALPHANUMERIC then the NPI will be set to NPI_UNKNOWN.
95       *
96       * @param theAddress The address
97       * @param theTon The type of number
98       * @param theNpi The number plan indication
99       * @throws SmsException Thrown if the address is invalid
100      */
101     public SmsAddress(String theAddress, int theTon, int theNpi)
102         throws SmsException
103     {
104         init(theAddress, theTon, theNpi);
105     }
106 
107     private void init(String theAddress, int theTon, int theNpi)
108         throws SmsException
109     {
110         int addressLength;
111 
112         if (theAddress == null)
113         {
114             throw new SmsException("Empty address.");
115         }
116 
117         myTon = theTon;
118         myAddress = theAddress.trim();
119         addressLength = myAddress.length();
120 
121         if (addressLength == 0)
122         {
123             throw new SmsException("Empty address.");
124         }
125 
126         if (theTon == SmsConstants.TON_ALPHANUMERIC)
127         {
128             myNpi = SmsConstants.NPI_UNKNOWN;
129 
130             if (theAddress.length() > 11)
131             {
132                 throw new SmsException("Alphanumeric address can be at most 11 chars.");
133             }
134         }
135         else
136         {
137             myNpi = theNpi;
138 
139             // Trim '+' from address
140             if (myAddress.charAt(0) == '+')
141             {
142                 myAddress = myAddress.substring(1);
143                 addressLength -= 1;
144             }
145 
146             if (addressLength > 20)
147             {
148                 throw new SmsException("Too long address, Max allowed is 20 digits (excluding any inital '+').");
149             }
150 
151             for (int i = 0; i < theAddress.length(); i++)
152             {
153                 char ch = theAddress.charAt(i);
154                 if (ALLOWED_DIGITS.indexOf(ch) == -1)
155                 {
156                     throw new SmsException("Invalid digit in address. '" + ch + "'.");
157                 }
158             }
159         }
160     }
161 
162     /***
163      * Returns the address
164      *
165      * @return The address
166      */
167     public String getAddress()
168     {
169         return myAddress;
170     }
171 
172     /***
173      * Returns the TON field
174      * <p>
175      * See SmsConstants for definitions of different TON:s
176      *
177      * @return The TON
178      */
179     public int getTypeOfNumber()
180     {
181         return myTon;
182     }
183 
184     /***
185      * Returns the NPI field
186      * <p>
187      * See SmsConstants for definitions of different TON:s
188      *
189      * @return The NPI
190      */
191     public int getNumberingPlanIdentification()
192     {
193         return myNpi;
194     }
195 }
196