forked from IABTechLab/iabgpp-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGppModel.java
More file actions
560 lines (486 loc) · 18 KB
/
GppModel.java
File metadata and controls
560 lines (486 loc) · 18 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package com.iab.gpp.encoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.iab.gpp.encoder.error.DecodingException;
import com.iab.gpp.encoder.error.EncodingException;
import com.iab.gpp.encoder.error.InvalidFieldException;
import com.iab.gpp.encoder.field.HeaderV1Field;
import com.iab.gpp.encoder.section.*;
public class GppModel {
private Map<String, EncodableSection> sections = new HashMap<>();
private String encodedString;
private boolean dirty = false;
private boolean decoded = true;
public GppModel() {
}
public GppModel(String encodedString) {
decode(encodedString);
}
public void setFieldValue(int sectionId, String fieldName, Object value) {
setFieldValue(Sections.SECTION_ID_NAME_MAP.get(sectionId), fieldName, value);
}
public void setFieldValue(String sectionName, String fieldName, Object value) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
EncodableSection section = null;
if (!this.sections.containsKey(sectionName)) {
if (sectionName.equals(TcfCaV1.NAME)) {
section = new TcfCaV1();
this.sections.put(TcfCaV1.NAME, section);
} else if (sectionName.equals(TcfEuV2.NAME)) {
section = new TcfEuV2();
this.sections.put(TcfEuV2.NAME, section);
} else if (sectionName.equals(UspV1.NAME)) {
section = new UspV1();
this.sections.put(UspV1.NAME, section);
} else if (sectionName.equals(UsNat.NAME)) {
section = new UsNat();
this.sections.put(UsNat.NAME, section);
} else if (sectionName.equals(UsCa.NAME)) {
section = new UsCa();
this.sections.put(UsCa.NAME, section);
} else if (sectionName.equals(UsVa.NAME)) {
section = new UsVa();
this.sections.put(UsVa.NAME, section);
} else if (sectionName.equals(UsCo.NAME)) {
section = new UsCo();
this.sections.put(UsCo.NAME, section);
} else if (sectionName.equals(UsUt.NAME)) {
section = new UsUt();
this.sections.put(UsUt.NAME, section);
} else if (sectionName.equals(UsCt.NAME)) {
section = new UsCt();
this.sections.put(UsCt.NAME, section);
} else if (sectionName.equals(UsFl.NAME)) {
section = new UsFl();
this.sections.put(UsFl.NAME, section);
} else if (sectionName.equals(UsMt.NAME)) {
section = new UsMt();
this.sections.put(UsMt.NAME, section);
} else if (sectionName.equals(UsOr.NAME)) {
section = new UsOr();
this.sections.put(UsOr.NAME, section);
} else if (sectionName.equals(UsTx.NAME)) {
section = new UsTx();
this.sections.put(UsTx.NAME, section);
} else if (sectionName.equals(UsDe.NAME)) {
section = new UsDe();
this.sections.put(UsDe.NAME, section);
} else if (sectionName.equals(UsIa.NAME)) {
section = new UsIa();
this.sections.put(UsIa.NAME, section);
} else if (sectionName.equals(UsNe.NAME)) {
section = new UsNe();
this.sections.put(UsNe.NAME, section);
} else if (sectionName.equals(UsNh.NAME)) {
section = new UsNh();
this.sections.put(UsNh.NAME, section);
} else if (sectionName.equals(UsNj.NAME)) {
section = new UsNj();
this.sections.put(UsNj.NAME, section);
} else if (sectionName.equals(UsTn.NAME)) {
section = new UsTn();
this.sections.put(UsTn.NAME, section);
} else if (sectionName.equals(UsMn.NAME)) {
section = new UsMn();
this.sections.put(UsMn.NAME, section);
}
} else {
section = this.sections.get(sectionName);
}
if (section != null) {
section.setFieldValue(fieldName, value);
this.dirty = true;
} else {
throw new InvalidFieldException(sectionName + "." + fieldName + " not found");
}
}
public Object getFieldValue(int sectionId, String fieldName) {
return getFieldValue(Sections.SECTION_ID_NAME_MAP.get(sectionId), fieldName);
}
public Object getFieldValue(String sectionName, String fieldName) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
if (this.sections.containsKey(sectionName)) {
return this.sections.get(sectionName).getFieldValue(fieldName);
} else {
return null;
}
}
public boolean hasField(int sectionId, String fieldName) {
return hasField(Sections.SECTION_ID_NAME_MAP.get(sectionId), fieldName);
}
public boolean hasField(String sectionName, String fieldName) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
if (this.sections.containsKey(sectionName)) {
return this.sections.get(sectionName).hasField(fieldName);
} else {
return false;
}
}
public boolean hasSection(int sectionId) {
return hasSection(Sections.SECTION_ID_NAME_MAP.get(sectionId));
}
public boolean hasSection(String sectionName) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
return this.sections.containsKey(sectionName);
}
public HeaderV1 getHeader() {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
HeaderV1 header = new HeaderV1();
try {
header.setFieldValue("SectionIds", this.getSectionIds());
} catch (InvalidFieldException e) {
}
return header;
}
public EncodableSection getSection(int sectionId) {
return getSection(Sections.SECTION_ID_NAME_MAP.get(sectionId));
}
public EncodableSection getSection(String sectionName) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
if (this.sections.containsKey(sectionName)) {
return this.sections.get(sectionName);
} else {
return null;
}
}
public void deleteSection(int sectionId) {
deleteSection(Sections.SECTION_ID_NAME_MAP.get(sectionId));
}
public void deleteSection(String sectionName) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
if (this.sections.containsKey(sectionName)) {
this.sections.remove(sectionName);
this.dirty = true;
}
}
public void clear() {
this.sections.clear();
this.encodedString = null;
this.dirty = false;
this.decoded = true;
}
public TcfCaV1 getTcfCaV1Section() {
return (TcfCaV1) getSection(TcfCaV1.NAME);
}
public TcfEuV2 getTcfEuV2Section() {
return (TcfEuV2) getSection(TcfEuV2.NAME);
}
public UspV1 getUspV1Section() {
return (UspV1) getSection(UspV1.NAME);
}
public UsNat getUsNatSection() {
return (UsNat) getSection(UsNat.NAME);
}
public UsCa getUsCaSection() {
return (UsCa) getSection(UsCa.NAME);
}
public UsVa getUsVaSection() {
return (UsVa) getSection(UsVa.NAME);
}
public UsCo getUsCoSection() {
return (UsCo) getSection(UsCo.NAME);
}
public UsUt getUsUtSection() {
return (UsUt) getSection(UsUt.NAME);
}
public UsCt getUsCtSection() {
return (UsCt) getSection(UsCt.NAME);
}
public UsFl getUsFlSection() {
return (UsFl) getSection(UsFl.NAME);
}
public UsMt getUsMtSection() {
return (UsMt) getSection(UsMt.NAME);
}
public UsOr getUsOrSection() {
return (UsOr) getSection(UsOr.NAME);
}
public UsTx getUsTxSection() {
return (UsTx) getSection(UsTx.NAME);
}
public UsDe getUsDeSection() {
return (UsDe) getSection(UsDe.NAME);
}
public UsIa getUsIaSection() {
return (UsIa) getSection(UsIa.NAME);
}
public UsNe getUsNeSection() {
return (UsNe) getSection(UsNe.NAME);
}
public UsNh getUsNhSection() {
return (UsNh) getSection(UsNh.NAME);
}
public UsNj getUsNjSection() {
return (UsNj) getSection(UsNj.NAME);
}
public UsTn getUsTnSection() {
return (UsTn) getSection(UsTn.NAME);
}
public UsMn getUsMnSection() {
return (UsMn) getSection(UsMn.NAME);
}
public List<Integer> getSectionIds() {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
List<Integer> sectionIds = new ArrayList<>();
for (int i = 0; i < Sections.SECTION_ORDER.size(); i++) {
String sectionName = Sections.SECTION_ORDER.get(i);
if (this.sections.containsKey(sectionName)) {
EncodableSection section = this.sections.get(sectionName);
sectionIds.add(section.getId());
}
}
return sectionIds;
}
protected String encodeModel(Map<String, EncodableSection> sections) {
List<String> encodedSections = new ArrayList<>();
List<Integer> sectionIds = new ArrayList<>();
for (int i = 0; i < Sections.SECTION_ORDER.size(); i++) {
String sectionName = Sections.SECTION_ORDER.get(i);
if (sections.containsKey(sectionName)) {
EncodableSection section = sections.get(sectionName);
encodedSections.add(section.encode());
sectionIds.add(section.getId());
}
}
HeaderV1 header = new HeaderV1();
try {
header.setFieldValue("SectionIds", getSectionIds());
} catch (InvalidFieldException e) {
throw new EncodingException(e);
}
encodedSections.add(0, header.encode());
String encodedString = encodedSections.stream().collect(Collectors.joining("~"));
return encodedString;
}
protected Map<String, EncodableSection> decodeModel(String str) {
if (str == null || str.isEmpty() || str.startsWith("DB")) {
Map<String, EncodableSection> sections = new HashMap<>();
if (str != null && !str.isEmpty()) {
String[] encodedSections = str.split("~");
HeaderV1 header = new HeaderV1(encodedSections[0]);
sections.put(HeaderV1.NAME, header);
@SuppressWarnings("unchecked")
List<Integer> sectionIds = (List<Integer>) header.getFieldValue("SectionIds");
for (int i = 0; i < sectionIds.size(); i++) {
if (sectionIds.get(i).equals(TcfEuV2.ID)) {
TcfEuV2 section = new TcfEuV2(encodedSections[i + 1]);
sections.put(TcfEuV2.NAME, section);
} else if (sectionIds.get(i).equals(TcfCaV1.ID)) {
TcfCaV1 section = new TcfCaV1(encodedSections[i + 1]);
sections.put(TcfCaV1.NAME, section);
} else if (sectionIds.get(i).equals(UspV1.ID)) {
UspV1 section = new UspV1(encodedSections[i + 1]);
sections.put(UspV1.NAME, section);
} else if (sectionIds.get(i).equals(UsCa.ID)) {
UsCa section = new UsCa(encodedSections[i + 1]);
sections.put(UsCa.NAME, section);
} else if (sectionIds.get(i).equals(UsNat.ID)) {
UsNat section = new UsNat(encodedSections[i + 1]);
sections.put(UsNat.NAME, section);
} else if (sectionIds.get(i).equals(UsVa.ID)) {
UsVa section = new UsVa(encodedSections[i + 1]);
sections.put(UsVa.NAME, section);
} else if (sectionIds.get(i).equals(UsCo.ID)) {
UsCo section = new UsCo(encodedSections[i + 1]);
sections.put(UsCo.NAME, section);
} else if (sectionIds.get(i).equals(UsUt.ID)) {
UsUt section = new UsUt(encodedSections[i + 1]);
sections.put(UsUt.NAME, section);
} else if (sectionIds.get(i).equals(UsCt.ID)) {
UsCt section = new UsCt(encodedSections[i + 1]);
sections.put(UsCt.NAME, section);
} else if (sectionIds.get(i).equals(UsFl.ID)) {
UsFl section = new UsFl(encodedSections[i + 1]);
sections.put(UsFl.NAME, section);
} else if (sectionIds.get(i).equals(UsMt.ID)) {
UsMt section = new UsMt(encodedSections[i + 1]);
sections.put(UsMt.NAME, section);
} else if (sectionIds.get(i).equals(UsOr.ID)) {
UsOr section = new UsOr(encodedSections[i + 1]);
sections.put(UsOr.NAME, section);
} else if (sectionIds.get(i).equals(UsTx.ID)) {
UsTx section = new UsTx(encodedSections[i + 1]);
sections.put(UsTx.NAME, section);
} else if (sectionIds.get(i).equals(UsDe.ID)) {
UsDe section = new UsDe(encodedSections[i + 1]);
sections.put(UsDe.NAME, section);
} else if (sectionIds.get(i).equals(UsIa.ID)) {
UsIa section = new UsIa(encodedSections[i + 1]);
sections.put(UsIa.NAME, section);
} else if (sectionIds.get(i).equals(UsNe.ID)) {
UsNe section = new UsNe(encodedSections[i + 1]);
sections.put(UsNe.NAME, section);
} else if (sectionIds.get(i).equals(UsNh.ID)) {
UsNh section = new UsNh(encodedSections[i + 1]);
sections.put(UsNh.NAME, section);
} else if (sectionIds.get(i).equals(UsNj.ID)) {
UsNj section = new UsNj(encodedSections[i + 1]);
sections.put(UsNj.NAME, section);
} else if (sectionIds.get(i).equals(UsTn.ID)) {
UsTn section = new UsTn(encodedSections[i + 1]);
sections.put(UsTn.NAME, section);
} else if (sectionIds.get(i).equals(UsMn.ID)) {
UsMn section = new UsMn(encodedSections[i + 1]);
sections.put(UsMn.NAME, section);
}
}
}
return sections;
} else if (str.startsWith("C")) {
// old tcfeu only string
Map<String, EncodableSection> sections = new HashMap<>();
TcfEuV2 section = new TcfEuV2(str);
sections.put(TcfEuV2.NAME, section);
HeaderV1 header = new HeaderV1();
header.setFieldValue(HeaderV1Field.SECTION_IDS, Arrays.asList(2));
sections.put(HeaderV1.NAME, section);
return sections;
} else {
throw new DecodingException("Unable to decode '" + str + "'");
}
}
public String encodeSection(int sectionId) {
return encodeSection(Sections.SECTION_ID_NAME_MAP.get(sectionId));
}
public String encodeSection(String sectionName) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
if (this.sections.containsKey(sectionName)) {
return this.sections.get(sectionName).encode();
} else {
return null;
}
}
public void decodeSection(int sectionId, String encodedString) {
decodeSection(Sections.SECTION_ID_NAME_MAP.get(sectionId), encodedString);
}
public void decodeSection(String sectionName, String encodedString) {
if (!this.decoded) {
this.sections = this.decodeModel(this.encodedString);
this.dirty = false;
this.decoded = true;
}
EncodableSection section = null;
if (!this.sections.containsKey(sectionName)) {
if (sectionName.equals(TcfEuV2.NAME)) {
section = new TcfEuV2();
this.sections.put(TcfEuV2.NAME, section);
} else if (sectionName.equals(TcfCaV1.NAME)) {
section = new TcfCaV1();
this.sections.put(TcfCaV1.NAME, section);
} else if (sectionName.equals(UspV1.NAME)) {
section = new UspV1();
this.sections.put(UspV1.NAME, section);
} else if (sectionName.equals(UsNat.NAME)) {
section = new UsNat();
this.sections.put(UsNat.NAME, section);
} else if (sectionName.equals(UsCa.NAME)) {
section = new UsCa();
this.sections.put(UsCa.NAME, section);
} else if (sectionName.equals(UsVa.NAME)) {
section = new UsVa();
this.sections.put(UsVa.NAME, section);
} else if (sectionName.equals(UsCo.NAME)) {
section = new UsCo();
this.sections.put(UsCo.NAME, section);
} else if (sectionName.equals(UsUt.NAME)) {
section = new UsUt();
this.sections.put(UsUt.NAME, section);
} else if (sectionName.equals(UsCt.NAME)) {
section = new UsCt();
this.sections.put(UsCt.NAME, section);
} else if (sectionName.equals(UsFl.NAME)) {
section = new UsFl();
this.sections.put(UsFl.NAME, section);
} else if (sectionName.equals(UsMt.NAME)) {
section = new UsMt();
this.sections.put(UsMt.NAME, section);
} else if (sectionName.equals(UsOr.NAME)) {
section = new UsOr();
this.sections.put(UsOr.NAME, section);
} else if (sectionName.equals(UsTx.NAME)) {
section = new UsTx();
this.sections.put(UsTx.NAME, section);
}else if (sectionName.equals(UsDe.NAME)) {
section = new UsDe();
this.sections.put(UsDe.NAME, section);
}else if (sectionName.equals(UsIa.NAME)) {
section = new UsIa();
this.sections.put(UsIa.NAME, section);
}else if (sectionName.equals(UsNe.NAME)) {
section = new UsNe();
this.sections.put(UsNe.NAME, section);
}else if (sectionName.equals(UsNh.NAME)) {
section = new UsNh();
this.sections.put(UsNh.NAME, section);
}else if (sectionName.equals(UsNj.NAME)) {
section = new UsNj();
this.sections.put(UsNj.NAME, section);
}else if (sectionName.equals(UsTn.NAME)) {
section = new UsTn();
this.sections.put(UsTn.NAME, section);
}else if (sectionName.equals(UsMn.NAME)) {
section = new UsMn();
this.sections.put(UsMn.NAME, section);
}
} else {
section = this.sections.get(sectionName);
}
if (section != null) {
section.decode(encodedString);
this.dirty = true;
}
}
public String encode() {
if (this.encodedString == null || this.encodedString.isEmpty() || this.dirty) {
this.encodedString = encodeModel(this.sections);
this.dirty = false;
this.decoded = true;
}
return this.encodedString;
}
public void decode(String encodedString) {
this.encodedString = encodedString;
this.dirty = false;
this.decoded = false;
}
}