View Javadoc

1   /*
2    * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3    * Vita
4    */
5   package net.sourceforge.jsdp;
6   
7   import java.util.regex.Pattern;
8   
9   /**
10   * A <tt>Phone</tt> represents a <b>p=<i>&lt;field value&gt;</i></b> field
11   * contained in a SDP message. A phone field specify contact information for the
12   * person responsible for the conference. This is not necessarily the same
13   * person that created the conference announcement.
14   * 
15   * @since 0.1.0
16   * 
17   * @version 1.0
18   * 
19   * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
20   */
21  public class Phone implements Field {
22  
23      /** The class Stream Unique Identifier, SUID */
24      private static final long serialVersionUID = 4792028746712967047L;
25  
26      /** The pattern used to validate the phone number */
27      private static final Pattern phoneMatcher = Pattern.compile("\\+([1-9])+[ -](\\d)+([ -](\\d)+)*");
28  
29      /** The phone user */
30      protected String name;
31  
32      /** The phone number */
33      protected String phoneNumber;
34  
35      /**
36       * Creates a new <tt>Phone</tt>.
37       */
38      private Phone() {
39  
40          super();
41      }
42  
43      /**
44       * Creates a new <tt>Phone</tt>.
45       * 
46       * @param number the phone number
47       * 
48       * @throws SDPException if the phone number is not valid
49       */
50      public Phone(final String number) throws SDPException {
51  
52          super();
53          setPhoneNumber(number);
54      }
55  
56      /**
57       * Parse an input string and constructs the equivalent phone field.
58       * 
59       * @param field the string to parse
60       * 
61       * @return a new <tt>Phone</tt> instance
62       * 
63       * @throws SDPParseException if an error occurs while parsing
64       */
65      public static Phone parse(final String field) throws SDPParseException {
66  
67          if (!field.startsWith("p=")) {
68              throw new SDPParseException("The string \"" + field + "\" isn't a phone field");
69          }
70  
71          Phone p = null;
72  
73          try {
74              // TODO Add support for the contact name parsing
75              p = new Phone(field.substring(2));
76          }
77          catch (SDPException parseException) {
78              throw new SDPParseException("The string \"" + field + "\" isn't a valid phone field", parseException);
79          }
80  
81          return p;
82      }
83  
84      /**
85       * Returns a clone of this field.
86       * 
87       * @return a clone of this field
88       */
89      public Object clone() {
90  
91          Phone field = new Phone();
92  
93          field.phoneNumber = this.phoneNumber;
94          if (this.name != null) {
95              field.name = new String(this.name);
96          }
97  
98          return field;
99      }
100 
101     /**
102      * Returns the name of the phone user.
103      * 
104      * @return the phone user
105      */
106     public String getName() {
107 
108         return name;
109     }
110 
111     /**
112      * Returns the phone number.
113      * 
114      * @return the phone number
115      */
116     public String getPhoneNumber() {
117 
118         return phoneNumber;
119     }
120 
121     /**
122      * Returns the type character for the field.
123      * 
124      * @return the field type character: <b>p</b>
125      */
126     public char getType() {
127 
128         return Field.PHONE_FIELD;
129     }
130 
131     /**
132      * Sets phone user name.
133      * 
134      * @param name the phone user name
135      * 
136      * @throws SDPException if the phone user name is not valid
137      */
138     public void setName(final String name) throws SDPException {
139 
140         // TODO Validate the user name
141         this.name = name;
142     }
143 
144     /**
145      * Sets the phone number.
146      * 
147      * @param number the phone number
148      * 
149      * @throws SDPException if the phone number is not valid
150      */
151     public void setPhoneNumber(final String number) throws SDPException {
152 
153         if (!phoneMatcher.matcher(number).matches()) {
154             throw new SDPException("Invalid phone number");
155         }
156 
157         this.phoneNumber = number;
158     }
159 
160     /**
161      * Returns a string representation of the field. The representation has the
162      * form: <b>p=<i>&lt;value&gt;</i></b>
163      * 
164      * @return the string representation of the field
165      */
166     public String toString() {
167 
168         // TODO Check the string representation of the field
169         StringBuffer result = new StringBuffer(getType() + "=");
170 
171         if (name != null) {
172             result.append(name);
173             result.append("<" + phoneNumber + ">");
174         }
175         else {
176             result.append(phoneNumber);
177         }
178 
179         return result.toString();
180     }
181 }