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.types;
17  
18  import org.fourthline.cling.model.Constants;
19  
20  import java.util.regex.Pattern;
21  import java.util.regex.Matcher;
22  
23  /**
24   * Service type with a fixed <code>schemas-upnp-org</code> namespace.
25   *
26   * @author Christian Bauer
27   */
28  public class UDAServiceType extends ServiceType {
29  
30      public static final String DEFAULT_NAMESPACE = "schemas-upnp-org";
31  
32      // This pattern also accepts decimal versions, not only integers (as would be required by UDA), but cuts off fractions
33      public static final Pattern PATTERN =
34              Pattern.compile("urn:" + DEFAULT_NAMESPACE + ":service:(" + Constants.REGEX_TYPE + "):([0-9]+).*");
35  
36      public UDAServiceType(String type) {
37          this(type, 1);
38      }
39  
40      public UDAServiceType(String type, int version) {
41          super(DEFAULT_NAMESPACE, type, version);
42      }
43  
44      public static UDAServiceType valueOf(String s) throws InvalidValueException {
45          Matcher matcher = UDAServiceType.PATTERN.matcher(s);
46  
47          try {
48              if (matcher.matches())
49                  return new UDAServiceType(matcher.group(1), Integer.valueOf(matcher.group(2)));
50          } catch (RuntimeException e) {
51              throw new InvalidValueException(String.format(
52                  "Can't parse UDA service type string (namespace/type/version) '%s': %s", s, e.toString()));
53          }
54          throw new InvalidValueException("Can't parse UDA service type string (namespace/type/version): " + s);
55      }
56  
57  }