forked from weblicht/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfile.java
More file actions
265 lines (228 loc) · 8.6 KB
/
Profile.java
File metadata and controls
265 lines (228 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package eu.clarin.switchboard.profiler.api;
import com.google.common.collect.ImmutableMap;
import org.jetbrains.annotations.NotNull;
import jakarta.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
/**
* A simple data object for storing a data profile (e.g. mediatype, language, version, other features).
* Use the nested Builder to build a profile, or the nested Flat class for serialization/deserialization.
*/
public class Profile implements Comparable<Profile> {
/**
* feature name for the document's main language (when appropriate), encoded as ISO 639-3
*/
public final static String FEATURE_LANGUAGE = "language";
/**
* feature flag set if the document's main document (if exists and is text) encoding is UTF-8
*/
public final static String FEATURE_IS_UTF8 = "isUTF8Encoding";
// how sure we are this is the right profile
private final Confidence confidence;
// profile mediatype, e.g. application/xml
private final String mediaType;
// dynamic parameters or features
// e.g. document format version, used when appropriate (e.g. TCF4/5, XML 1.0/1.1)
private Map<String, String> features;
private Profile(Confidence confidence, String mediaType, Map<String, String> features) {
this.confidence = Objects.requireNonNull(confidence);
this.mediaType = stripSpaces(Objects.requireNonNull(mediaType));
this.features = ImmutableMap.copyOf(Objects.requireNonNull(features));
}
public static Profile.Builder builder() {
return new Profile.Builder(null);
}
public static Profile.Builder builder(Profile profile) {
return new Profile.Builder(profile);
}
public Confidence getConfidence() {
return confidence;
}
public String getMediaType() {
return mediaType;
}
public Map<String, String> getFeatures() {
return features;
}
public boolean hasFeature(String featureKey) {
return features.containsKey(featureKey);
}
public String getFeature(String featureKey) {
return features.get(featureKey);
}
public boolean isMediaType(String mediaType) {
return this.mediaType.equalsIgnoreCase(mediaType);
}
public boolean isXmlMediaType() {
// mediatype format is: type "/" [tree "."] subtype ["+" suffix]* [";" parameter]*
// 1. remove parameter suffix
String mediaType = this.mediaType;
int formatStartIndex = mediaType.indexOf(';');
if (formatStartIndex > 0) {
mediaType = mediaType.substring(0, formatStartIndex);
}
// 2. split in type and subtypes
String[] typeSubtype = mediaType.split("/");
if (typeSubtype.length == 2) {
String subtype = typeSubtype[1];
// 3. find and remove tree name
int i = subtype.indexOf(".");
if (i >= 0) {
subtype = typeSubtype[1].substring(i + 1);
}
// 4. find suffix
String suffix = "";
i = subtype.lastIndexOf("+");
if (i >= 0) {
suffix = subtype.substring(i + 1);
subtype = subtype.substring(0, i);
}
return "xml".equalsIgnoreCase(subtype) || "xml".equalsIgnoreCase(suffix);
}
return false;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Profile profile = (Profile) o;
return Objects.equals(confidence, profile.confidence) &&
Objects.equals(mediaType, profile.mediaType) &&
Objects.equals(features, profile.features);
}
@Override
public int hashCode() {
return Objects.hash(mediaType, features);
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder("Profile { confidence=");
buf.append(confidence).append("; mediaType=").append(mediaType);
for (String k : features.keySet()) {
buf.append("; ").append(k);
String v = features.get(k);
if (v != null && !v.isEmpty()) {
buf.append("=").append(v);
}
}
buf.append(" }");
return buf.toString();
}
@Override
public int compareTo(@NotNull Profile o) {
int x = confidence.compareTo(o.confidence);
if (x == 0 && mediaType != null) {
x = mediaType.compareToIgnoreCase(o.mediaType);
}
return x;
}
/**
* Builder class pattern for a Profile.
*/
public static class Builder {
Confidence confidence = Confidence.Uncertain;
String mediaType = MediaType.APPLICATION_OCTET_STREAM;
// dynamic parameters or features
Map<String, String> features = new HashMap<>();
public Builder(Profile p) {
if (p != null) {
this.confidence = p.confidence;
this.mediaType = p.mediaType;
p.features.forEach(this::feature);
}
}
public Builder certain() {
this.confidence = Confidence.Certain;
return this;
}
public Builder mediaType(String mediaType) {
Objects.requireNonNull(mediaType);
if (mediaType.isEmpty()) {
throw new IllegalArgumentException("bad mediaType argument: empty");
}
this.mediaType = mediaType;
return this;
}
/// @param language ISO 639-3 code of the language
public Builder language(String language) {
Objects.requireNonNull(language);
if (language.length() != 3) {
throw new IllegalArgumentException("Profile.Builder: language is not ISO 639-3: " + language);
}
features.put(FEATURE_LANGUAGE, language);
return this;
}
public Builder feature(String featureName) {
Objects.requireNonNull(featureName);
if (FEATURE_LANGUAGE.equals(featureName)) {
throw new IllegalArgumentException("Profile.Builder: language is a special feature and must have a value");
}
features.put(featureName, "");
return this;
}
public Builder feature(String featureName, String value) {
Objects.requireNonNull(featureName);
Objects.requireNonNull(value);
if (FEATURE_LANGUAGE.equals(featureName)) {
return language(value);
}
features.put(featureName, value);
return this;
}
//allow some introspection during building
public boolean hasFeature(String featureKey) {
return features.containsKey(featureKey);
}
//allow some introspection during building
public String getFeature(String featureKey) {
return features.get(featureKey);
}
public Profile build() {
return new Profile(confidence, mediaType, features);
}
}
public Flat flat() {
return Flat.fromProfile(this);
}
/**
* Utility class for serializing and deserializing a Profile
*/
public static class Flat extends LinkedHashMap<String, String> {
public static Flat fromProfile(Profile p) {
Flat flat = new Flat();
flat.put("confidence", p.getConfidence().name());
flat.put("mediaType", p.getMediaType());
flat.putAll(p.getFeatures());
return flat;
}
public Profile toProfile() {
for (String value : this.values()) {
if (value == null) {
throw new IllegalArgumentException("null values in Profile are illegal");
}
}
Map<String, String> features = new LinkedHashMap<>(this);
String confidence = features.get("confidence");
features.remove("confidence");
String mediaType = features.get("mediaType");
features.remove("mediaType");
return new Profile(
confidence == null ? Confidence.Uncertain : Confidence.Certain,
mediaType == null ? MediaType.APPLICATION_OCTET_STREAM : mediaType,
features);
}
}
private static String stripSpaces(String s) {
StringBuilder sb = new StringBuilder();
s.chars().forEachOrdered(c -> {
if (!Character.isSpaceChar(c)) {
sb.append((char)c);
}
});
return sb.toString();
}
}