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 org.fourthline.cling.model.types.InvalidValueException;
18  import org.fourthline.cling.support.model.Protocol;
19  import org.fourthline.cling.support.model.ProtocolInfo;
20  import org.seamless.util.MimeType;
21  
22  import java.util.EnumMap;
23  import java.util.Map;
24  
25  /**
26   * Encaspulates a MIME type (content format) and transport, protocol, additional information.
27   * <p/>
28   * Parses DLNA attributes in the additional information.
29   *
30   * @author Mario Franco
31   */
32  public class DLNAProtocolInfo extends ProtocolInfo {
33  
34      protected final Map<DLNAAttribute.Type, DLNAAttribute> attributes = new EnumMap<>(DLNAAttribute.Type.class);
35  
36      public DLNAProtocolInfo(String s) throws InvalidValueException {
37          super(s);
38          parseAdditionalInfo();
39      }
40  
41      public DLNAProtocolInfo(MimeType contentFormatMimeType) {
42          super(contentFormatMimeType);
43      }
44  
45      public DLNAProtocolInfo(DLNAProfiles profile) {
46          super(MimeType.valueOf(profile.getContentFormat()));
47          this.attributes.put(DLNAAttribute.Type.DLNA_ORG_PN, new DLNAProfileAttribute(profile));
48          this.additionalInfo = this.getAttributesString();
49      }
50  
51      public DLNAProtocolInfo(DLNAProfiles profile, EnumMap<DLNAAttribute.Type, DLNAAttribute> attributes) {
52          super(MimeType.valueOf(profile.getContentFormat()));
53          this.attributes.putAll(attributes);
54          this.attributes.put(DLNAAttribute.Type.DLNA_ORG_PN, new DLNAProfileAttribute(profile));
55          this.additionalInfo = this.getAttributesString();
56      }
57      
58      public DLNAProtocolInfo(Protocol protocol, String network, String contentFormat, String additionalInfo) {
59          super(protocol, network, contentFormat, additionalInfo);
60          parseAdditionalInfo();
61      }
62  
63      public DLNAProtocolInfo(Protocol protocol, String network, String contentFormat, EnumMap<DLNAAttribute.Type, DLNAAttribute> attributes) {
64          super(protocol, network, contentFormat, "");
65          this.attributes.putAll(attributes);
66          this.additionalInfo = this.getAttributesString();
67      }
68  
69      public DLNAProtocolInfo(ProtocolInfo template) {
70          this(template.getProtocol(),
71               template.getNetwork(),
72               template.getContentFormat(),
73               template.getAdditionalInfo()
74          );
75      }
76  
77      public boolean contains(DLNAAttribute.Type type) {
78          return attributes.containsKey(type);
79      }
80  
81      public DLNAAttribute getAttribute(DLNAAttribute.Type type) {
82          return attributes.get(type);
83      }
84  
85      public Map<DLNAAttribute.Type, DLNAAttribute> getAttributes() {
86          return attributes;
87      }
88  
89      protected String getAttributesString() {
90          String s = "";
91          for (DLNAAttribute.Type type : DLNAAttribute.Type.values() ) {
92              String value = attributes.containsKey(type)?attributes.get(type).getString():null;
93              if (value!=null && value.length() != 0)
94                  s += (s.length() == 0 ? "" : ";") + type.getAttributeName() + "=" + value;
95          }
96          return s;
97      }
98  
99      protected void parseAdditionalInfo() {
100         if (additionalInfo != null) {
101             String[] atts = additionalInfo.split(";");
102             for (String att : atts) {
103                 String[] attNameValue = att.split("=");
104                 if (attNameValue.length == 2) {
105                     DLNAAttribute.Type type =
106                             DLNAAttribute.Type.valueOfAttributeName(attNameValue[0]);
107                     if (type != null) {
108                         DLNAAttribute dlnaAttrinute =
109                                 DLNAAttribute.newInstance(type, attNameValue[1], this.getContentFormat());
110                         attributes.put(type, dlnaAttrinute);
111                     }
112                 }
113             }
114         }
115     }
116 
117 }