1 package wjhk.jupload2.filedata.helper;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import wjhk.jupload2.exception.JUploadException;
7 import wjhk.jupload2.policies.UploadPolicy;
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public class ImageFileConversionInfo {
22
23 private static final String FORMAT_SEPARATOR = ",";
24
25 private static final String RELATION_SEPARATOR = ";";
26
27 private static final String RELATION_ASSIGNMENT = ":";
28
29
30
31
32
33
34
35
36 private Map<String, String> formatRelations = new HashMap<String, String>();
37
38
39
40
41
42
43
44
45
46 public ImageFileConversionInfo(String conversionList)
47 throws JUploadException {
48 parseConversionList(conversionList);
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62 public String getTargetFormatOrNull(String sourceFormat) {
63 if (sourceFormat == null) {
64 return null;
65 }
66 String mapValue = this.formatRelations.get(sourceFormat.toLowerCase());
67 return mapValue;
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public String getTargetFormat(String sourceFormat) {
88 if (sourceFormat == null) {
89 return null;
90 }
91 String targetFormatOrNull = getTargetFormatOrNull(sourceFormat);
92 if (targetFormatOrNull == null) {
93 return sourceFormat.toLowerCase();
94 }
95 return targetFormatOrNull;
96 }
97
98
99
100
101
102
103
104
105 private void parseConversionList(String conversionList)
106 throws JUploadException {
107 if (conversionList == null || conversionList.equals("")) {
108 return;
109 }
110
111
112
113
114
115
116
117
118
119 if (!conversionList.endsWith(RELATION_SEPARATOR)) {
120 conversionList += RELATION_SEPARATOR;
121 }
122
123
124
125
126
127 String[] relations = conversionList.split(RELATION_SEPARATOR);
128 for (String relation : relations) {
129
130
131
132 String[] assignmentDetails = relation.split(RELATION_ASSIGNMENT);
133 if (assignmentDetails.length != 2) {
134 throw new JUploadException("Invalid format: relation '"
135 + relation + "' should contain exatly one '"
136 + RELATION_ASSIGNMENT + "'");
137 }
138 String sourceFormatList = assignmentDetails[0];
139
140
141
142 String targetFormat = assignmentDetails[1].toLowerCase();
143
144
145
146 String[] sourceFormats = sourceFormatList.split(FORMAT_SEPARATOR);
147 for (String sourceFormat : sourceFormats) {
148
149
150
151 String lcSourceFormat = sourceFormat.toLowerCase();
152
153
154
155 if (lcSourceFormat.equals(targetFormat)) {
156 throw new JUploadException("format '" + sourceFormat
157 + "' is assigned to itself");
158 }
159 String putResult = this.formatRelations.put(lcSourceFormat,
160 targetFormat);
161 if (putResult != null) {
162 throw new JUploadException("format '" + lcSourceFormat
163 + "' is assigned to multiple target formats: '"
164 + targetFormat + "', '" + putResult + "'");
165 }
166 }
167 }
168 }
169
170
171
172
173 @Override
174 public String toString() {
175 StringBuilder sb = new StringBuilder("ImageFileConversionInfo (");
176 for (Map.Entry<String, String> formatRelation : this.formatRelations
177 .entrySet()) {
178 sb.append(formatRelation.getKey());
179 sb.append("-->");
180 sb.append(formatRelation.getValue());
181 sb.append(";");
182 }
183 sb.append(")");
184
185 return sb.toString();
186 }
187 }