1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class Attribute implements Field {
33
34
35 private static final long serialVersionUID = -6003475409338057035L;
36
37
38 private static final Pattern fieldPattern = Pattern.compile("([^:]+)(:((.+)))?");
39
40
41 private static final Pattern namePattern = Pattern.compile("(\\w)+(-\\w+)*");
42
43
44 private static final Pattern valuePattern = Pattern.compile("[^\0\\r\\n]+");
45
46
47 protected String name;
48
49
50 protected String value;
51
52
53
54
55 protected Attribute() {
56
57 super();
58 }
59
60
61
62
63
64
65
66
67 public Attribute(final String name) throws SDPException {
68
69 super();
70
71 setName(name);
72 this.value = null;
73 }
74
75
76
77
78
79
80
81
82
83
84
85 public Attribute(final String name, final String value) throws SDPException {
86
87 super();
88
89 setName(name);
90 setValue(value);
91 }
92
93
94
95
96
97
98
99
100
101
102 public static Attribute parse(final String field) throws SDPParseException {
103
104 if (!field.startsWith("a=")) {
105 throw new SDPParseException("The string \"" + field + "\" isn't an attribute field");
106 }
107
108 Attribute a = null;
109 Matcher matcher = fieldPattern.matcher(field.substring(2));
110
111
112 if (matcher.matches()) {
113
114 try {
115
116 if (matcher.group(3) != null) {
117 a = new Attribute(matcher.group(1), matcher.group(3));
118 }
119 else {
120
121 a = new Attribute(matcher.group(1));
122 }
123 }
124 catch (SDPException parseException) {
125 throw new SDPParseException("The string \"" + field + "\" isn't a valid attribute field", parseException);
126 }
127 }
128 else {
129 throw new SDPParseException("The string \"" + field + "\" isn't a valid attribute field");
130 }
131
132 return a;
133 }
134
135
136
137
138
139
140 public Object clone() {
141
142 Attribute field = new Attribute();
143 field.name = new String(this.name);
144
145 if (this.value != null) {
146 field.value = new String(this.value);
147 }
148 else {
149 field.value = null;
150 }
151
152 return field;
153 }
154
155
156
157
158
159
160 public String getName() {
161
162 return name;
163 }
164
165
166
167
168
169
170 public char getType() {
171
172 return Field.ATTRIBUTE_FIELD;
173 }
174
175
176
177
178
179
180
181 public String getValue() {
182
183 return value;
184 }
185
186
187
188
189
190
191
192 public boolean hasValue() {
193
194 return value != null;
195 }
196
197
198
199
200 public void resetValue() {
201
202 this.value = null;
203 }
204
205
206
207
208
209
210
211
212 public void setName(final String name) throws SDPException {
213
214 if (name == null) {
215 throw new SDPException("The attribute name cannot be null");
216 }
217
218 if (!namePattern.matcher(name).matches()) {
219 throw new SDPException("Invalid attribute name: " + name);
220 }
221
222 this.name = name;
223 }
224
225
226
227
228
229
230
231
232 public void setValue(final String value) throws SDPException {
233
234 if (value != null && !valuePattern.matcher(value).matches()) {
235 throw new SDPException("Invalid attribute value: " + value);
236 }
237
238 this.value = value;
239 }
240
241
242
243
244
245
246
247
248
249
250
251 public String toString() {
252
253 StringBuffer result;
254
255 result = new StringBuffer(getType() + "=");
256 result.append(name);
257
258 if (hasValue()) {
259 result.append(":" + value);
260 }
261
262 return result.toString();
263 }
264 }