View Javadoc

1   /*
2    * jSDP: A Java implementation of SDP protocol Copyright (C) 2007 Claudio Di
3    * Vita
4    */
5   package net.sourceforge.jsdp.util;
6   
7   import java.io.Serializable;
8   import java.util.StringTokenizer;
9   import java.util.regex.Matcher;
10  import java.util.regex.Pattern;
11  
12  import net.sourceforge.jsdp.SDPException;
13  
14  /**
15   * A representation of a contact information.
16   * 
17   * @since 1.0
18   * 
19   * @version 1.0
20   * 
21   * @author <a href="mailto:cdivita@users.sourceforge.net">Claudio Di Vita</a>
22   */
23  public class Contact implements Cloneable, Serializable {
24  
25      /** The class Stream Unique Identifier, SUID */
26      private static final long serialVersionUID = -1082887678662975345L;
27  
28      /** Contact e-mail address regular expression */
29      private static final String EMAIL_REGEX = "([\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+(\\.[\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+)*)@([\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+(\\.[\\w'-/:?#\\$&\\*;=\\[\\]\\^_`\\{\\}\\+~]+)*)";
30  
31      /** Contact personal information regular expression */
32      private static final String PERSONAL_REGEX = "[^\\(\\)<>\\r\\n]+";
33  
34      /** The pattern used to validate an e-mail address */
35      private static final Pattern emailPattern = Pattern.compile(EMAIL_REGEX);
36  
37      /** The pattern used to validate a contact personal information */
38      private static final Pattern personalPattern = Pattern.compile(PERSONAL_REGEX);
39  
40      /** The e-mail address of the contact */
41      protected String address;
42  
43      /** The personal information of the contact */
44      protected String personal;
45  
46      /**
47       * Creates a new <tt>Contact</tt>.
48       */
49      protected Contact() {
50  
51          super();
52      }
53  
54      /**
55       * Creates a new <tt>Contact</tt>.
56       * 
57       * @param info the contact information
58       * 
59       * @throws SDPException if the contact information aren't valid
60       */
61      public Contact(final String info) throws SDPException {
62  
63          super();
64  
65          Matcher emailMatcher = emailPattern.matcher(info);
66          if (emailMatcher.lookingAt()) {
67  
68              int position = emailMatcher.end();
69              if (position == info.length()) {
70                  this.address = info;
71              }
72              else {
73                  this.address = info.substring(0, position);
74  
75                  StringTokenizer tokenizer = new StringTokenizer(info.substring(position), "()");
76                  StringBuffer temp = new StringBuffer();
77                  while (tokenizer.hasMoreTokens()) {
78                      temp.append(tokenizer.nextToken());
79                  }
80  
81                  String name = temp.toString().trim();
82                  if (name.length() > 0) {
83                      setPersonal(name);
84                  }
85                  else {
86                      throw new SDPException("Invalid e-mail address");
87                  }
88              }
89          }
90          else {
91              StringTokenizer tokenizer = new StringTokenizer(info, "<>");
92              setPersonal(tokenizer.nextToken().trim());
93              setAddress(tokenizer.nextToken());
94          }
95      }
96  
97      /**
98       * Creates a new <tt>Contact</tt>.
99       * 
100      * @param address the e-mail address of the contact
101      * 
102      * @param personal the personal information of the contact
103      * 
104      * @throws SDPException if the e-mail address or the personal information
105      *         aren't valid
106      */
107     public Contact(final String address, final String personal) throws SDPException {
108 
109         super();
110         setAddress(address);
111         setPersonal(personal);
112     }
113 
114     /**
115      * Returns a clone of this contact.
116      * 
117      * @return a clone of this object
118      */
119     public Object clone() {
120 
121         Contact email = new Contact();
122         email.address = new String(this.address);
123 
124         if (this.personal != null) {
125             email.personal = new String(personal);
126         }
127         else {
128             email.personal = null;
129         }
130 
131         return email;
132     }
133 
134     /**
135      * Returns the e-mail address.
136      * 
137      * @return the e-mail address of the contact
138      */
139     public String getAddress() {
140 
141         return address;
142     }
143 
144     /**
145      * Returns the personal information.
146      * 
147      * @return the personal information of the contact
148      */
149     public String getPersonal() {
150 
151         return personal;
152     }
153 
154     /**
155      * Sets the e-mail address of the contact.
156      * 
157      * @param address the e-mail address to set
158      * 
159      * @throws SDPException if the e-mail address is not valid
160      */
161     public void setAddress(final String address) throws SDPException {
162 
163         if (!emailPattern.matcher(address).matches()) {
164             throw new SDPException("Invalid e-mail address:" + address);
165         }
166 
167         this.address = address;
168     }
169 
170     /**
171      * Sets the personal information of the contact.
172      * 
173      * @param personal the personal information to set
174      * 
175      * @throws SDPException if the personal information are not valid
176      */
177     public void setPersonal(final String personal) throws SDPException {
178 
179         if (!personalPattern.matcher(personal).matches()) {
180             throw new SDPException("Invalid personal information:" + personal);
181         }
182 
183         this.personal = personal.trim();
184     }
185 
186     /**
187      * Returns a string representation of the contact. The representation has
188      * the form: <b><i>personal</i> <i>&lt;e-mail&gt;</i></b>
189      * 
190      * @return the string representation of the contact
191      */
192     public String toString() {
193 
194         StringBuffer result;
195 
196         result = new StringBuffer();
197 
198         if (personal != null) {
199             result.append(personal);
200             result.append(" <" + getAddress() + ">");
201         }
202         else {
203             result.append(getAddress());
204         }
205 
206         return result.toString();
207     }
208 }