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  package org.fourthline.cling.support.model.dlna;
16  
17  import java.util.EnumSet;
18  import java.util.Locale;
19  
20  /**
21   * @author Mario Franco
22   */
23  public class DLNAFlagsAttribute extends DLNAAttribute<EnumSet<DLNAFlags>> {
24  
25      public DLNAFlagsAttribute() {
26          setValue(EnumSet.noneOf(DLNAFlags.class));
27      }
28  
29      public DLNAFlagsAttribute(DLNAFlags... flags) {
30          if (flags != null && flags.length > 0) {
31              DLNAFlags first = flags[0];
32              if (flags.length > 1) {
33                  System.arraycopy(flags, 1, flags, 0, flags.length - 1);
34                  setValue(EnumSet.of(first, flags));
35              } else {
36                  setValue(EnumSet.of(first));
37              }
38          }
39      }
40  
41      public void setString(String s, String cf) throws InvalidDLNAProtocolAttributeException {
42          EnumSet<DLNAFlags> value = EnumSet.noneOf(DLNAFlags.class);
43          try {
44              int parseInt = Integer.parseInt(s.substring(0, s.length() - 24), 16);
45              for (DLNAFlags op : DLNAFlags.values()) {
46                  int code = op.getCode() & parseInt;
47                  if (op.getCode() == code) {
48                      value.add(op);
49                  }
50              }
51          } catch (Exception e) {
52          }
53  
54          if (value.isEmpty())
55              throw new InvalidDLNAProtocolAttributeException("Can't parse DLNA flags integer from: " + s);
56  
57          setValue(value);
58      }
59  
60      public String getString() {
61          int code = 0;
62          for (DLNAFlags op : getValue()) {
63              code |= op.getCode();
64          }
65          return String.format(Locale.ROOT, "%08x%024x", code, 0);
66      }
67  }