1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model.message.header;
17
18 import java.util.regex.Pattern;
19 import java.util.regex.Matcher;
20
21
22
23
24 public class MANHeader extends UpnpHeader<String> {
25
26 public static final Pattern PATTERN = Pattern.compile("\"(.+?)\"(;.+?)??");
27 public static final Pattern NAMESPACE_PATTERN = Pattern.compile(";\\s?ns\\s?=\\s?([0-9]{2})");
28
29 public String namespace;
30
31 public MANHeader() {
32 }
33
34 public MANHeader(String value) {
35 setValue(value);
36 }
37
38 public MANHeader(String value, String namespace) {
39 this(value);
40 this.namespace = namespace;
41 }
42
43 public void setString(String s) throws InvalidHeaderException {
44
45 Matcher matcher = PATTERN.matcher(s);
46 if (matcher.matches()) {
47 setValue(matcher.group(1));
48
49 if (matcher.group(2) != null) {
50 Matcher nsMatcher = NAMESPACE_PATTERN.matcher(matcher.group(2));
51 if (nsMatcher.matches()) {
52 setNamespace(nsMatcher.group(1));
53 } else {
54 throw new InvalidHeaderException("Invalid namespace in MAN header value: " + s);
55 }
56 }
57
58 } else {
59 throw new InvalidHeaderException("Invalid MAN header value: " + s);
60 }
61 }
62
63 public String getString() {
64 if (getValue() == null) return null;
65 StringBuilder s = new StringBuilder();
66 s.append("\"").append(getValue()).append("\"");
67 if (getNamespace() != null) s.append("; ns=").append(getNamespace());
68 return s.toString();
69 }
70
71 public String getNamespace() {
72 return namespace;
73 }
74
75 public void setNamespace(String namespace) {
76 this.namespace = namespace;
77 }
78 }