View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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   * @author Christian Bauer
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  }