1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.net.MalformedURLException;
8 import java.net.URL;
9
10
11
12
13
14
15
16
17
18
19
20
21 public class Uri implements Field {
22
23
24 private static final long serialVersionUID = 5886238353833030564L;
25
26
27 protected URL url;
28
29
30
31
32 protected Uri() {
33
34 super();
35 }
36
37
38
39
40
41
42
43
44 public Uri(final String url) throws SDPException {
45
46 super();
47 setURL(url);
48 }
49
50
51
52
53
54
55 public Uri(final URL url) {
56
57 super();
58 this.url = url;
59 }
60
61
62
63
64
65
66
67
68
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
77 Uri u = null;
78
79 try {
80
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
92
93
94
95 public Object clone() {
96
97 Uri field = new Uri();
98
99 try {
100 field.url = new URL(this.url.toExternalForm());
101 }
102
103 catch (MalformedURLException malformedURL) {
104 malformedURL.printStackTrace();
105 }
106
107 return field;
108 }
109
110
111
112
113
114
115 public char getType() {
116
117 return Field.URI_FIELD;
118 }
119
120
121
122
123
124
125 public String getURL() {
126
127 return url.toExternalForm();
128 }
129
130
131
132
133
134
135
136
137
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
155
156
157
158
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
171
172
173
174
175 public String toString() {
176
177 return getType() + "=" + url.toExternalForm();
178 }
179 }