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.net.MalformedURLException;
8 import java.net.URL;
9
10 /**
11 * An <tt>Uri</tt> represents a <b>u=<i><field value></i></b> field
12 * contained in a SDP message. The uri field should be a pointer to additional
13 * information about the conference.
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 Uri implements Field {
22
23 /** The class Stream Unique Identifier, SUID */
24 private static final long serialVersionUID = 5886238353833030564L;
25
26 /** The uri value */
27 protected URL url;
28
29 /**
30 * Creates a new <tt>Uri</tt>.
31 */
32 protected Uri() {
33
34 super();
35 }
36
37 /**
38 * Creates a new <tt>Uri</tt>.
39 *
40 * @param url the uri value
41 *
42 * @throws SDPException if the value specified is a not valid uri
43 */
44 public Uri(final String url) throws SDPException {
45
46 super();
47 setURL(url);
48 }
49
50 /**
51 * Creates a new <tt>Uri</tt>.
52 *
53 * @param url the resource url
54 */
55 public Uri(final URL url) {
56
57 super();
58 this.url = url;
59 }
60
61 /**
62 * Parse an input string and constructs the equivalent uri field.
63 *
64 * @param field the string to parse
65 *
66 * @return a new <tt>Uri</tt> instance
67 *
68 * @throws SDPParseException if an error occurs while parsing
69 */
70 public static Uri parse(final String field) throws SDPParseException {
71
72 if (!field.startsWith("u=")) {
73 throw new SDPParseException("The string \"" + field + "\" isn't an uri field");
74 }
75
76 /* The parsed field */
77 Uri u = null;
78
79 try {
80 /* Create th field */
81 u = new Uri(field.substring(2));
82 }
83 catch (SDPException parseException) {
84 throw new SDPParseException("The string \"" + field + "\" isn't a valid uri field", parseException);
85 }
86
87 return u;
88 }
89
90 /**
91 * Returns a clone of this field.
92 *
93 * @return a clone of this field
94 */
95 public Object clone() {
96
97 Uri field = new Uri();
98
99 try {
100 field.url = new URL(this.url.toExternalForm());
101 }
102 /* Never thrown */
103 catch (MalformedURLException malformedURL) {
104 malformedURL.printStackTrace();
105 }
106
107 return field;
108 }
109
110 /**
111 * Returns the type character for the field.
112 *
113 * @return the field type character: <tt>u</tt>
114 */
115 public char getType() {
116
117 return Field.URI_FIELD;
118 }
119
120 /**
121 * Returns the resource url.
122 *
123 * @return an url
124 */
125 public String getURL() {
126
127 return url.toExternalForm();
128 }
129
130 /**
131 * Sets the uri value.
132 *
133 * @param url the resource url
134 *
135 * @throws IllegalArgumentException if the url is <tt>null</tt>
136 *
137 * @throws SDPException if the value specified is a not valid uri
138 */
139 public void setURL(final String url) throws IllegalArgumentException, SDPException {
140
141 if (url == null) {
142 throw new IllegalArgumentException("The resource url cannot be null");
143 }
144
145 try {
146 this.url = new URL(url);
147 }
148 catch (MalformedURLException malformedURL) {
149 throw new SDPException("Invalid URL: " + url);
150 }
151 }
152
153 /**
154 * Sets the uri value.
155 *
156 * @param url the resource url
157 *
158 * @throws IllegalArgumentException if the url is <tt>null</tt>
159 */
160 public void setURL(final URL url) throws IllegalArgumentException {
161
162 if (url == null) {
163 throw new IllegalArgumentException("The resource url cannot be null");
164 }
165
166 this.url = url;
167 }
168
169 /**
170 * Returns a string representation of the field. The representation has the
171 * form: <b>u=<i><uri></i></b>.
172 *
173 * @return the string representation of the field
174 */
175 public String toString() {
176
177 return getType() + "=" + url.toExternalForm();
178 }
179 }