1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.fourthline.cling.support.model.dlna;
16
17 import org.seamless.util.Exceptions;
18
19 import java.util.HashMap;
20 import java.util.Locale;
21 import java.util.Map;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25
26
27
28
29
30
31
32
33
34
35 public abstract class DLNAAttribute<T> {
36
37 final private static Logger log = Logger.getLogger(DLNAAttribute.class.getName());
38
39
40
41
42 public static enum Type {
43
44
45
46
47 DLNA_ORG_PN("DLNA.ORG_PN", DLNAProfileAttribute.class),
48 DLNA_ORG_OP("DLNA.ORG_OP", DLNAOperationsAttribute.class),
49 DLNA_ORG_PS("DLNA.ORG_PS", DLNAPlaySpeedAttribute.class),
50 DLNA_ORG_CI("DLNA.ORG_CI", DLNAConversionIndicatorAttribute.class),
51 DLNA_ORG_FLAGS("DLNA.ORG_FLAGS", DLNAFlagsAttribute.class);
52
53 private static Map<String, Type> byName = new HashMap<String, Type>() {
54 {
55 for (Type t : Type.values()) {
56 put(t.getAttributeName().toUpperCase(Locale.ROOT), t);
57 }
58 }
59 };
60
61 private String attributeName;
62 private Class<? extends DLNAAttribute>[] attributeTypes;
63
64 @SafeVarargs
65 private Type(String attributeName, Class<? extends DLNAAttribute>... attributeClass) {
66 this.attributeName = attributeName;
67 this.attributeTypes = attributeClass;
68 }
69
70 public String getAttributeName() {
71 return attributeName;
72 }
73
74 public Class<? extends DLNAAttribute>[] getAttributeTypes() {
75 return attributeTypes;
76 }
77
78 public static Type valueOfAttributeName(String attributeName) {
79 if (attributeName == null) {
80 return null;
81 }
82 return byName.get(attributeName.toUpperCase(Locale.ROOT));
83 }
84 }
85
86 private T value;
87
88 public void setValue(T value) {
89 this.value = value;
90 }
91
92 public T getValue() {
93 return value;
94 }
95
96
97
98
99
100
101
102 public abstract void setString(String s, String cf) throws InvalidDLNAProtocolAttributeException;
103
104
105
106
107 public abstract String getString();
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 public static DLNAAttribute newInstance(DLNAAttribute.Type type, String attributeValue, String contentFormat) {
124
125 DLNAAttribute attr = null;
126 for (int i = 0; i < type.getAttributeTypes().length && attr == null; i++) {
127 Class<? extends DLNAAttribute> attributeClass = type.getAttributeTypes()[i];
128 try {
129 log.finest("Trying to parse DLNA '" + type + "' with class: " + attributeClass.getSimpleName());
130 attr = attributeClass.newInstance();
131 if (attributeValue != null) {
132 attr.setString(attributeValue, contentFormat);
133 }
134 } catch (InvalidDLNAProtocolAttributeException ex) {
135 log.finest("Invalid DLNA attribute value for tested type: " + attributeClass.getSimpleName() + " - " + ex.getMessage());
136 attr = null;
137 } catch (Exception ex) {
138 log.severe("Error instantiating DLNA attribute of type '" + type + "' with value: " + attributeValue);
139 log.log(Level.SEVERE, "Exception root cause: ", Exceptions.unwrap(ex));
140 }
141 }
142 return attr;
143 }
144
145 @Override
146 public String toString() {
147 return "(" + getClass().getSimpleName() + ") '" + getValue() + "'";
148 }
149 }