1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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