• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wmixvideo / nfe / #6748

23 Jul 2025 01:34AM UTC coverage: 51.794% (+0.3%) from 51.469%
#6748

push

web-flow
Merge 853722eff into cb8c6ab74

122 of 122 new or added lines in 1 file covered. (100.0%)

83 existing lines in 5 files now uncovered.

14360 of 27725 relevant lines covered (51.79%)

0.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.13
/src/main/java/com/fincatto/documentofiscal/validadores/DFStringValidador.java
1
package com.fincatto.documentofiscal.validadores;
2

3
import com.fincatto.documentofiscal.nfe400.classes.NFNotaInfoItemModalidadeBCICMSST;
4
import com.fincatto.documentofiscal.nfe400.classes.nota.NFNotaInfoItemImpostoICMS;
5
import org.apache.commons.codec.binary.Base64;
6
import org.apache.commons.lang3.ObjectUtils;
7
import org.apache.commons.lang3.StringUtils;
8

9
import java.lang.reflect.InvocationTargetException;
10
import java.lang.reflect.Method;
11
import java.time.format.DateTimeFormatter;
12
import java.util.Arrays;
13
import java.util.Objects;
14
import java.util.regex.Matcher;
15
import java.util.regex.Pattern;
16

17
public abstract class DFStringValidador {
×
18

19
    public static void mmaaaa(final String mmaaaa) {
20
        if (mmaaaa != null) {
1✔
21
            try {
22
                DateTimeFormatter.ofPattern("mm/yyyy").parse(mmaaaa);
1✔
23
            } catch (final Exception e) {
1✔
24
                throw new IllegalStateException(String.format("Formato invalido (mm/aaaa) (%s)", mmaaaa));
1✔
25
            }
1✔
26
        }
27
    }
1✔
28

29
    public static void aamm(final String aamm) {
30
        if (aamm != null) {
1✔
31
            try {
32
                DateTimeFormatter.ofPattern("yymm").parse(aamm);
1✔
33
            } catch (final Exception e) {
1✔
34
                throw new IllegalStateException(String.format("Formato invalido (aamm) (%s)", aamm));
1✔
35
            }
1✔
36
        }
37
    }
1✔
38

39
    public static void codigoDeBarras(final String codigoDeBarras) {
40
        if (codigoDeBarras != null) {
1✔
41
            final Matcher matcher = Pattern.compile("^([0-9]{0}|[0-9]{8}|[0-9]{12,14}|SEM GTIN)$").matcher(codigoDeBarras);
1✔
42
            if (!matcher.find()) {
1✔
43
                throw new IllegalStateException(String.format("Codigo de barras com formato invalido (%s)", codigoDeBarras));
1✔
44
            }
45
        }
46
    }
1✔
47

48
    public static void telefone(final String telefone) {
49
        if (telefone != null) {
1✔
50
            final Matcher matcher = Pattern.compile("^[0-9]{6,14}$").matcher(telefone);
1✔
51
            if (!matcher.find()) {
1✔
52
                throw new IllegalStateException(String.format("Telefone de tamanho invalido (%s)", telefone));
1✔
53
            }
54
        }
55
    }
1✔
56

57
    public static String telefone(final String telefone, final String info) {
58
        if (telefone != null) {
1✔
59
            final Matcher matcher = Pattern.compile("^[0-9]{6,14}$").matcher(telefone);
1✔
60
            if (!matcher.find()) {
1✔
61
                throw new IllegalStateException(String.format("Telefone de tamanho invalido (%s) em %s", telefone, info));
1✔
62
            }
63
        }
64
        return telefone;
1✔
65
    }
66

67
    public static void email(final String email) {
68
        if (email != null) {
1✔
69
            final String regex = "^([_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{1,6}))?$";
70
            final Matcher matcher = Pattern.compile(regex).matcher(email);
1✔
71
            if (!matcher.find()) {
1✔
72
                throw new IllegalStateException(String.format("Email invalido (%s)", email));
1✔
73
            }
74
        }
75
    }
1✔
76

77
    public static String email(final String email, final String info) {
78
        if (email != null) {
1✔
79
            final String regex = "^([_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.[a-zA-Z]{1,6}))?$";
80
            final Matcher matcher = Pattern.compile(regex).matcher(email);
1✔
81
            if (!matcher.find()) {
1✔
82
                throw new IllegalStateException(String.format("Email invalido (%s) em %s", email, info));
1✔
83
            }
84
        }
85
        return email;
1✔
86
    }
87

88
    public static void tamanho256(final String string, final String info) {
89
        if (string != null) {
1✔
90
            DFStringValidador.validaTamanhoMaximo(string, 256, info);
1✔
91
        }
92
    }
1✔
93

94
    public static void tamanho9(final String string, final String info) {
95
        if (string != null) {
1✔
96
            DFStringValidador.validaTamanhoMaximo(string, 9, info);
1✔
97
        }
98
    }
1✔
99

100
    public static void tamanho60(final String string, final String info) {
101
        if (string != null) {
1✔
102
            DFStringValidador.validaTamanhoMaximo(string, 60, info);
1✔
103
        }
104
    }
1✔
105

106
    public static void tamanho1ate8(final String string, final String info) {
107
        if (string != null) {
1✔
108
            DFStringValidador.intervalo(string, 1, 8, info);
1✔
109
        }
110
    }
1✔
111

112
    public static void tamanho2ate60(final String string, final String info) {
113
        if (string != null) {
1✔
114
            DFStringValidador.intervalo(string, 2, 60, info);
1✔
115
        }
116
    }
1✔
117

118
    public static void tamanho2ate40(final String string, final String info) {
119
        if (string != null) {
1✔
120
            DFStringValidador.intervalo(string, 2, 40, info);
1✔
121
        }
122
    }
1✔
123

124
    public static void tamanho2ate255(final String string, final String info) {
125
        if (string != null) {
1✔
126
            DFStringValidador.intervalo(string, 2, 255, info);
1✔
127
        }
128
    }
1✔
129

130
    public static void tamanho2ate2000(final String string, final String info) {
131
        if (string != null) {
×
132
            DFStringValidador.intervalo(string, 2, 2000, info);
×
133
        }
134
    }
×
135

136
    public static void tamanho25ate250(final String string, final String info) {
137
        if (string != null) {
×
138
            DFStringValidador.intervalo(string, 25, 250, info);
×
139
        }
140
    }
×
141

142
    public static void tamanho22(final String string, final String info) {
143
        if (string != null) {
1✔
144
            DFStringValidador.validaTamanhoMaximo(string, 22, info);
1✔
145
        }
146
    }
1✔
147

148
    public static void tamanho21(final String string, final String info) {
149
        if (string != null) {
1✔
150
            DFStringValidador.validaTamanhoMaximo(string, 21, info);
1✔
151
        }
152
    }
1✔
153

154
    public static void tamanho20(final String string, final String info) {
155
        if (string != null) {
1✔
156
            DFStringValidador.validaTamanhoMaximo(string, 20, info);
1✔
157
        }
158
    }
1✔
159

160
    public static void tamanho20N(final String string, final String info) {
161
        if (string != null) {
1✔
162
            DFStringValidador.apenasNumerico(string, info);
1✔
163
            DFStringValidador.tamanho20(string, info);
1✔
164
        }
165
    }
1✔
166

167
    public static void tamanho2000(final String string, final String info) {
168
        if (string != null) {
1✔
169
            DFStringValidador.validaTamanhoMaximo(string, 2000, info);
1✔
170
        }
171
    }
1✔
172

173
    public static void tamanho5000(final String string, final String info) {
174
        if (string != null) {
1✔
175
            DFStringValidador.validaTamanhoMaximo(string, 5000, info);
1✔
176
        }
177
    }
1✔
178

179
    public static void tamanho40(final String string, final String info) {
180
        if (string != null) {
1✔
181
            DFStringValidador.validaTamanhoMaximo(string, 40, info);
1✔
182
        }
183
    }
1✔
184

185
    public static void placaDeVeiculo(final String placaVeiculo) {
186
        if (placaVeiculo != null) {
1✔
187
            final Matcher matcher = Pattern.compile("^([A-Z]{2,3}[0-9]{4}|[A-Z]{3,4}[0-9]{3}|[A-Z]{3}[0-9][A-Z][0-9]{2})$").matcher(placaVeiculo);
1✔
188
            if (!matcher.find()) {
1✔
189
                throw new IllegalStateException(String.format("Placa de veiculo nao esta no padrao (%s)", placaVeiculo));
1✔
190
            }
191
        }
192
    }
1✔
193

194
    public static void placaDeVeiculo(final String placaVeiculo, final String info) {
195
        if (placaVeiculo != null) {
1✔
196
            final Matcher matcher = Pattern.compile("^([A-Z]{2,3}[0-9]{4}|[A-Z]{3,4}[0-9]{3}|[A-Z]{3}[0-9][A-Z][0-9]{2})$").matcher(placaVeiculo);
1✔
197
            if (!matcher.find()) {
1✔
198
                throw new IllegalStateException(String.format("%s nao esta no padrao (%s)", info, placaVeiculo));
1✔
199
            }
200
        }
201
    }
1✔
202

203
    public static void cnpj(final String cnpj) {
204
        if (cnpj != null) {
1✔
205
            final Matcher matcher = Pattern.compile("^[0-9]{14}$").matcher(cnpj);
1✔
206
            if (!matcher.find()) {
1✔
207
                throw new IllegalStateException(String.format("Formato CNPJ Invalido (%s)", cnpj));
1✔
208
            }
209
        }
210
    }
1✔
211

212
    public static String cnpj(final String cnpj, final String info) {
213
        if (cnpj != null) {
1✔
214
            final Matcher matcher = Pattern.compile("^[0-9]{14}$").matcher(cnpj);
1✔
215
            if (!matcher.find()) {
1✔
216
                throw new IllegalStateException(String.format("Formato CNPJ Invalido (%s) em %s", cnpj, info));
1✔
217
            }
218
        }
219
        return cnpj;
1✔
220
    }
221

222
    public static void cpf(final String cpf) {
223
        if (cpf != null) {
1✔
224
            final Matcher matcher = Pattern.compile("^[0-9]{11}$").matcher(cpf);
1✔
225
            if (!matcher.find()) {
1✔
226
                throw new IllegalStateException(String.format("Formato CPF Invalido (%s)", cpf));
1✔
227
            }
228
        }
229
    }
1✔
230

231
    public static String cpf(final String cpf, final String info) {
232
        if (cpf != null) {
1✔
233
            final Matcher matcher = Pattern.compile("^[0-9]{11}$").matcher(cpf);
1✔
234
            if (!matcher.find()) {
1✔
235
                throw new IllegalStateException(String.format("Formato CPF Invalido (%s) em %s", cpf, info));
1✔
236
            }
237
        }
238
        return cpf;
1✔
239
    }
240

241
    public static void inscricaoEstadual(final String inscricaoEstadual) {
242
        if (inscricaoEstadual != null) {
1✔
243
            final Matcher matcher = Pattern.compile("^(ISENTO|[0-9]{2,14}|)$").matcher(inscricaoEstadual);
1✔
244
            if (!matcher.find()) {
1✔
245
                throw new IllegalStateException(String.format("Inscricao estadual invalido (%s)", inscricaoEstadual));
1✔
246
            }
247
        }
248
    }
1✔
249

250
    public static void inscricaoEstadualSemIsencao(final String inscricaoEstadual) {
251
        if (inscricaoEstadual != null) {
1✔
252
            final Matcher matcher = Pattern.compile("^([0-9]{2,14}|)$").matcher(inscricaoEstadual);
1✔
253
            if (!matcher.find()) {
1✔
254
                throw new IllegalStateException(String.format("Inscricao estadual invalido (%s)", inscricaoEstadual));
1✔
255
            }
256
        }
257
    }
1✔
258

259
    public static String inscricaoEstadualSemIsencao(final String inscricaoEstadual, final String info) {
260
        if (inscricaoEstadual != null) {
1✔
261
            final Matcher matcher = Pattern.compile("^([0-9]{2,14}|)$").matcher(inscricaoEstadual);
1✔
262
            if (!matcher.find()) {
1✔
263
                throw new IllegalStateException(String.format("Inscricao estadual invalido (%s) em %s", inscricaoEstadual, info));
1✔
264
            }
265
        }
266
        return inscricaoEstadual;
1✔
267
    }
268

269
    public static void exatamente3(final String string, final String info) {
270
        if (string != null) {
1✔
271
            DFStringValidador.validaTamanhoExato(string, 3, info);
1✔
272
        }
273
    }
1✔
274

275
    public static void exatamente5(final String string, final String info) {
276
        if (string != null) {
1✔
277
            DFStringValidador.validaTamanhoExato(string, 5, info);
1✔
278
        }
279
    }
1✔
280

281
    public static void exatamente9(final String string, final String info) {
282
        if (string != null) {
1✔
283
            DFStringValidador.validaTamanhoExato(string, 9, info);
1✔
284
        }
285
    }
1✔
286

287
    public static void exatamente9N(final String string, final String info) {
288
        if (string != null) {
1✔
289
            DFStringValidador.apenasNumerico(string, info);
1✔
290
            DFStringValidador.validaTamanhoExato(string, 9, info);
1✔
291
        }
292
    }
1✔
293

294
    public static void exatamente17(final String string, final String info) {
295
        if (string != null) {
1✔
296
            DFStringValidador.validaTamanhoExato(string, 17, info);
1✔
297
        }
298
    }
1✔
299

300
    public static void exatamente4(final String string, final String info) {
301
        if (string != null) {
1✔
302
            DFStringValidador.validaTamanhoExato(string, 4, info);
1✔
303
        }
304
    }
1✔
305

306
    public static void exatamente6(final String string, final String info) {
307
        if (string != null) {
1✔
308
            DFStringValidador.validaTamanhoExato(string, 6, info);
1✔
309
        }
310
    }
1✔
311

312
    public static void exatamente21(final String string, final String info) {
313
        if (string != null) {
1✔
314
            DFStringValidador.validaTamanhoExato(string, 21, info);
1✔
315
        }
316
    }
1✔
317

318
    public static void exatamente1(final String string, final String info) {
319
        if (string != null) {
1✔
320
            DFStringValidador.validaTamanhoExato(string, 1, info);
1✔
321
        }
322
    }
1✔
323

324
    public static void exatamente13(final String string, final String info) {
325
        if (string != null) {
1✔
326
            DFStringValidador.validaTamanhoExato(string, 13, info);
1✔
327
        }
328
    }
1✔
329

330
    public static void exatamente14(final String string, final String info) {
331
        if (string != null) {
×
332
            DFStringValidador.validaTamanhoExato(string, 14, info);
×
333
        }
334
    }
×
335

336
    public static void codigoProdutoAnvisa(final String string, final String info) {
337
        if (string != null) {
1✔
338
            if (string.toUpperCase().matches("[A-Z]*")) {
1✔
339
                if (!Objects.equals(string.toUpperCase(), "ISENTO")) {
1✔
340
                    throw new IllegalStateException(String.format("C\u00f3digo produto anvisa (%s) diferente de ISENTO", string));
×
341
                }
342
            } else {
343
                if (string.length() <= 11) {
1✔
344
                    DFStringValidador.validaTamanhoExato(string, 11, info);
1✔
345
                } else {
346
                    DFStringValidador.validaTamanhoExato(string, 13, info);
1✔
347
                }
348
            }
349
        }
350
    }
1✔
351

352
    public static void tamanho15(final String string, final String info) {
353
        if (string != null) {
1✔
354
            DFStringValidador.validaTamanhoMaximo(string, 15, info);
1✔
355
        }
356
    }
1✔
357

358
    public static void tamanho12(final String string, final String info) {
359
        if (string != null) {
1✔
360
            DFStringValidador.validaTamanhoMaximo(string, 12, info);
1✔
361
        }
362
    }
1✔
363

364
    public static void tamanho12N(final String string, final String info) {
365
        if (string != null) {
×
366
            DFStringValidador.apenasNumerico(string, info);
×
367
            DFStringValidador.validaTamanhoMaximo(string, 12, info);
×
368
        }
369
    }
×
370

371
    public static void tamanho120(final String string, final String info) {
372
        if (string != null) {
1✔
373
            DFStringValidador.validaTamanhoMaximo(string, 120, info);
1✔
374
        }
375
    }
1✔
376

377
    public static void tamanho128(final String string, final String info) {
378
        if (string != null) {
1✔
379
            DFStringValidador.validaTamanhoMaximo(string, 128, info);
1✔
380
        }
381
    }
1✔
382

383
    public static void tamanho160(final String string, final String info) {
384
        if (string != null) {
1✔
385
            DFStringValidador.validaTamanhoMaximo(string, 160, info);
1✔
386
        }
387
    }
1✔
388

389
    public static void tamanho10(final String string, final String info) {
390
        if (string != null) {
1✔
391
            DFStringValidador.validaTamanhoMaximo(string, 10, info);
1✔
392
        }
393
    }
1✔
394

395
    public static void tamanho10N(final String string, final String info) {
396
        if (string != null) {
1✔
397
            DFStringValidador.apenasNumerico(string, info);
1✔
398
            DFStringValidador.validaTamanhoMaximo(string, 10, info);
1✔
399
        }
400
    }
1✔
401

402
    public static void tamanho100(final String string, final String info) {
403
        if (string != null) {
1✔
404
            DFStringValidador.validaTamanhoMaximo(string, 100, info);
1✔
405
        }
406
    }
1✔
407

408
    public static void tamanho6(final String string, final String info) {
409
        if (string != null) {
1✔
410
            DFStringValidador.validaTamanhoMaximo(string, 6, info);
1✔
411
        }
412
    }
1✔
413

414
    public static void tamanho6N(final String string, final String info) {
415
        if (string != null) {
1✔
416
            DFStringValidador.validaTamanhoMaximo(string, 6, info);
1✔
417
        }
418
    }
1✔
419

420
    public static void tamanho500(final String string, final String info) {
421
        if (string != null) {
1✔
422
            DFStringValidador.validaTamanhoMaximo(string, 500, info);
1✔
423
        }
424
    }
1✔
425

426
    public static void tamanho3(final String string, final String info) {
427
        if (string != null) {
1✔
428
            DFStringValidador.validaTamanhoMaximo(string, 3, info);
1✔
429
        }
430
    }
1✔
431

432
    public static void exatamente7(final String string, final String info) {
433
        if (string != null) {
1✔
434
            DFStringValidador.validaTamanhoExato(string, 7, info);
1✔
435
        }
436
    }
1✔
437

438
    public static void exatamente8(final String string, final String info) {
439
        if (string != null) {
1✔
440
            DFStringValidador.validaTamanhoExato(string, 8, info);
1✔
441
        }
442
    }
1✔
443

444
    public static void exatamente8N(final String string, final String info) {
445
        if (string != null) {
1✔
446
            DFStringValidador.apenasNumerico(string, info);
1✔
447
            DFStringValidador.validaTamanhoExato(string, 8, info);
1✔
448
        }
449
    }
1✔
450

451
    public static void exatamente2(final String string, final String info) {
452
        if (string != null) {
1✔
453
            DFStringValidador.validaTamanhoExato(string, 2, info);
1✔
454
        }
455
    }
1✔
456

457
    public static void exatamente2N(final String string, final String info) {
458
        if (string != null) {
1✔
459
            DFStringValidador.apenasNumerico(string, info);
1✔
460
            DFStringValidador.validaTamanhoExato(string, 2, info);
1✔
461
        }
462
    }
1✔
463

464
    public static void tamanho8a9(final String string, final String info) {
465
        if (string != null) {
1✔
466
            DFStringValidador.intervalo(string, 8, 9, info);
1✔
467
        }
468
    }
1✔
469

470
    public static void tamanho15a256(final String string, final String info) {
471
        if (string != null) {
1✔
472
            DFStringValidador.intervalo(string, 15, 256, info);
1✔
473
        }
474
    }
1✔
475

476
    public static void tamanho15a255(final String string, final String info) {
477
        if (string != null) {
1✔
478
            DFStringValidador.intervalo(string, 15, 255, info);
1✔
479
        }
480
    }
1✔
481

482
    public static void tamanho5a20(final String string, final String info) {
483
        if (string != null) {
1✔
484
            DFStringValidador.intervalo(string, 5, 20, info);
1✔
485
        }
486
    }
1✔
487

488
    public static void tamanho5a14(final String string, final String info) {
489
        if (string != null) {
1✔
490
            DFStringValidador.intervalo(string, 5, 14, info);
1✔
491
        }
492
    }
1✔
493

494
    public static void tamanho5a60(final String string, final String info) {
495
        if (string != null) {
1✔
496
            DFStringValidador.intervalo(string, 5, 60, info);
1✔
497
        }
498
    }
1✔
499

500
    public static void tamanho4a60(final String string, final String info) {
501
        if (string != null) {
1✔
502
            DFStringValidador.intervalo(string, 4, 60, info);
1✔
503
        }
504
    }
1✔
505

506
    public static void tamanho2a4(final String string, final String info) {
507
        if (string != null) {
1✔
508
            DFStringValidador.intervalo(string, 2, 4, info);
1✔
509
        }
510
    }
1✔
511

512
    public static void tamanho2a9N(final String string, final String info) {
513
        if (string != null) {
1✔
514
            DFStringValidador.apenasNumerico(string, info);
1✔
515
            DFStringValidador.intervalo(string, 2, 9, info);
1✔
516
        }
517
    }
1✔
518

519
    public static void tamanho8a9N(final String string, final String info) {
520
        if (string != null) {
1✔
521
            DFStringValidador.apenasNumerico(string, info);
1✔
522
            DFStringValidador.intervalo(string, 8, 9, info);
1✔
523
        }
524
    }
1✔
525

526
    public static void tamanho15a1000(final String string, final String info) {
527
        if (string != null) {
1✔
528
            DFStringValidador.intervalo(string, 15, 1000, info);
1✔
529
        }
530
    }
1✔
531

532
    public static void tamanho100a600(final String string, final String info) {
533
        if (string != null) {
1✔
534
            DFStringValidador.intervalo(string, 100, 600, info);
1✔
535
        }
536
    }
1✔
537

538
    public static void tamanho60a1000(final String string, final String info) {
539
        if (string != null) {
1✔
540
            DFStringValidador.intervalo(string, 60, 1000, info);
1✔
541
        }
542
    }
1✔
543

544
    public static void tamanho2a95(final String string, final String info) {
545
        if (string != null) {
1✔
546
            DFStringValidador.intervalo(string, 2, 95, info);
1✔
547
        }
548
    }
1✔
549

550
    public static void tamanho30(final String string, final String info) {
551
        if (string != null) {
1✔
552
            DFStringValidador.validaTamanhoMaximo(string, 30, info);
1✔
553
        }
554
    }
1✔
555

556
    public static void exatamente44(final String string, final String info) {
557
        if (string != null) {
1✔
558
            DFStringValidador.validaTamanhoExato(string, 44, info);
1✔
559
        }
560
    }
1✔
561

562
    public static void exatamente7N(final String string, final String info) {
563
        if (string != null) {
1✔
564
            DFStringValidador.apenasNumerico(string, info);
1✔
565
            DFStringValidador.exatamente7(string, info);
1✔
566
        }
567
    }
1✔
568

569
    public static void exatamente44N(final String string, final String info) {
570
        if (string != null) {
1✔
571
            DFStringValidador.apenasNumerico(string, info);
1✔
572
            DFStringValidador.exatamente44(string, info);
1✔
573
        }
574
    }
1✔
575

576
    public static void exatamente4N(final String string, final String info) {
577
        if (string != null) {
1✔
578
            DFStringValidador.apenasNumerico(string, info);
1✔
579
            DFStringValidador.exatamente4(string, info);
1✔
580
        }
581
    }
1✔
582

583
    public static void exatamente6N(final String string, final String info) {
584
        if (string != null) {
1✔
585
            DFStringValidador.apenasNumerico(string, info);
1✔
586
            DFStringValidador.exatamente6(string, info);
1✔
587
        }
588
    }
1✔
589

590
    public static void tamanho15N(final String string, final String info) {
591
        if (string != null) {
1✔
592
            DFStringValidador.apenasNumerico(string, info);
1✔
593
            DFStringValidador.validaTamanhoMaximo(string, 15, info);
1✔
594
        }
595
    }
1✔
596

597
    public static void tamanho14N(final String string, final String info) {
598
        if (string != null) {
1✔
599
            DFStringValidador.apenasNumerico(string, info);
1✔
600
            DFStringValidador.validaTamanhoMaximo(string, 14, info);
1✔
601
        }
602
    }
1✔
603

604
    public static void tamanho4(final String string, final String info) {
605
        if (string != null) {
1✔
606
            DFStringValidador.validaTamanhoMaximo(string, 4, info);
1✔
607
        }
608
    }
1✔
609

610
    public static void tamanho4N(final String string, final String info) {
611
        if (string != null) {
1✔
612
            DFStringValidador.apenasNumerico(string, info);
1✔
613
            DFStringValidador.validaTamanhoMaximo(string, 4, info);
1✔
614
        }
615
    }
1✔
616

617
    public static void tamanho9N(final String string, final String info) {
618
        if (string != null) {
1✔
619
            DFStringValidador.apenasNumerico(string, info);
1✔
620
            DFStringValidador.validaTamanhoMaximo(string, 9, info);
1✔
621
        }
622
    }
1✔
623

624
    public static void tamanho2ou3N(final String string, final String info) {
625
        if (string != null) {
1✔
626
            DFStringValidador.apenasNumerico(string, info);
1✔
627
            DFStringValidador.intervalo(string, 2, 3, info);
1✔
628
        }
629
    }
1✔
630

631
    public static void tamanho3N(final String string, final String info) {
632
        if (string != null) {
1✔
633
            DFStringValidador.apenasNumerico(string, info);
1✔
634
            DFStringValidador.validaTamanhoMaximo(string, 3, info);
1✔
635
        }
636
    }
1✔
637

638
    public static void tamanho2N(final String string, final String info) {
639
        if (string != null) {
1✔
640
            DFStringValidador.apenasNumerico(string, info);
1✔
641
            DFStringValidador.validaTamanhoMaximo(string, 2, info);
1✔
642
        }
643
    }
1✔
644

645
    public static void exatamente20N(final String string, final String info) {
646
        if (string != null) {
1✔
647
            DFStringValidador.apenasNumerico(string, info);
1✔
648
            DFStringValidador.validaTamanhoExato(string, 20, info);
1✔
649
        }
650
    }
1✔
651

652
    public static void tamanho25N(final String string, final String info) {
UNCOV
653
        if (string != null) {
×
UNCOV
654
            DFStringValidador.apenasNumerico(string, info);
×
UNCOV
655
            DFStringValidador.validaTamanhoMaximo(string, 25, info);
×
656
        }
UNCOV
657
    }
×
658

659
    /**
660
     * Metodo para validacao de Strings.
661
     *
662
     * @param paraValidar String a ser validada
663
     * @param info Informacao de retorno caso haja erro.
664
     * @param tamanho tamanho para validacao da {@code String} , pode ser
665
     * {@code null} :
666
     * @param exatamente <pre>
667
     * se false {@code null} a {@code String}
668
     *                   nao precisa ter o tamanho exato do parametro anterior.
669
     * </pre>
670
     *
671
     * @param numerico se true {@code null} a {@code String} precisa ser
672
     * numerica[0-9].
673
     * @param paraValidar}.
674
     * @return retorna a propria {@code String} {
675
     */
676
    public static String validador(final String paraValidar, final String info, Integer tamanho, Boolean exatamente, Boolean numerico) {
677
        tamanho = ObjectUtils.defaultIfNull(tamanho, 1);
1✔
678
        exatamente = ObjectUtils.defaultIfNull(exatamente, false);
1✔
679
        numerico = ObjectUtils.defaultIfNull(numerico, true);
1✔
680
        if (paraValidar != null) {
1✔
681
            if (numerico) {
1✔
682
                DFStringValidador.apenasNumerico(paraValidar, info);
1✔
683
            }
684
            if (exatamente) {
1✔
685
                DFStringValidador.validaTamanhoExato(paraValidar, tamanho, info);
1✔
686
            } else {
687
                DFStringValidador.validaTamanhoMaximo(paraValidar, tamanho, info);
1✔
688
            }
689
        }
690
        return paraValidar;
1✔
691
    }
692

693
    /**
694
     * @See #validador
695
     */
696
    public static String validador(final String paraValidar, final String info, final Integer tamanho, final Boolean exatamente) {
697
        return DFStringValidador.validador(paraValidar, info, tamanho, exatamente, null);
1✔
698
    }
699

700
    /**
701
     * @See #validador
702
     */
703
    public static String validador(final String paraValidar, final String info, final Integer tamanho) {
704
        return DFStringValidador.validador(paraValidar, info, tamanho, null, null);
1✔
705
    }
706

707
    public static void fci(final String numeroControleFCI) {
708
        if (numeroControleFCI != null) {
1✔
709
            final Matcher matcher = Pattern.compile("^([A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12})$").matcher(numeroControleFCI);
1✔
710
            if (!matcher.find()) {
1✔
711
                throw new IllegalStateException(String.format("FCI fora do padrao (%s)", numeroControleFCI));
1✔
712
            }
713
        }
714
    }
1✔
715

716
    public static void ncm(final String ncm) {
717
        if (ncm != null) {
1✔
718
            final Matcher matcher = Pattern.compile("^([0-9]{2}|[0-9]{8})$").matcher(ncm);
1✔
719
            if (!matcher.find()) {
1✔
720
                throw new IllegalStateException(String.format("NCM fora do padrao (%s)", ncm));
1✔
721
            }
722
        }
723
    }
1✔
724

725
    private static void apenasNumerico(final String string, final String info) {
726
        if (string != null && !StringUtils.isNumeric(string)) {
1✔
727
            throw new IllegalStateException(String.format("A string %s precisa ser numerica (%s)", info, string));
1✔
728
        }
729
    }
1✔
730

731
    public static void validaTamanhoMaximo(final String string, final int tamanho, final String info) {
732
        if (string != null && (string.length() < 1 || string.length() > tamanho)) {
1✔
733
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre 1-%s caracteres", info, string, tamanho));
1✔
734
        }
735
    }
1✔
736

737
    private static void validaTamanhoExato(final String string, final int tamanho, final String info) {
738
        if (string != null && string.length() != tamanho) {
1✔
739
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir %s caracteres", info, string, tamanho));
1✔
740
        }
741
    }
1✔
742

743
    private static void intervalo(final String string, final int inicio, final int fim, final String info) {
744
        if (string != null && (string.length() < inicio || string.length() > fim)) {
1✔
745
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre %s-%s caracteres", info, string, inicio, fim));
1✔
746
        }
747
    }
1✔
748

749
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info) {
750
        return DFStringValidador.validaIntervalo(string, inicio, fim, info, false);
1✔
751
    }
752

753
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info, Boolean isNumeric) {
754
        if (string != null) {
1✔
755
            isNumeric = ObjectUtils.defaultIfNull(isNumeric, false);
1✔
756
            if (isNumeric) {
1✔
757
                DFStringValidador.apenasNumerico(string, info);
1✔
758
            }
759
            DFStringValidador.intervalo(string, inicio, fim, info);
1✔
760
        }
761
        return string;
1✔
762
    }
763

764
    /**
765
     * Valida um numero com N {
766
     *
767
     * <pre>
768
     *  StringValidador.capacidadeNDigitos("10000", "info" , 5)   = "10000"
769
     *  StringValidador.capacidadeNDigitos("5", "info" , 2)   = "5"
770
     * </pre>
771
     *
772
     * @param capacidade
773
     * @param info
774
     * @param digitos
775
     * @return
776
     * @throws IllegalStateException se<br>
777
     * {@code capacidade = "10000" } & {@code digitos = 3}, ou seja , {@code capacidade.length()-1 > digitos
778
     * }
779
     */
780
    public static String capacidadeNDigitos(final String capacidade, final String info, final int digitos) {
781
        if (capacidade != null) {
1✔
782
            final Matcher matcher = Pattern.compile("^(0|[1-9]{1}[0-9]{0," + digitos + "})$").matcher(capacidade);
1✔
783
            if (!matcher.find()) {
1✔
784
                throw new IllegalStateException(String.format("%s fora do padrao (%s)", info, capacidade));
1✔
785
            }
786
        }
787
        return capacidade;
1✔
788
    }
789

790
    public static void nve(final String nve) {
791
        if (nve != null) {
1✔
792
            final Matcher matcher = Pattern.compile("^[A-Z]{2}[0-9]{4}$").matcher(nve);
1✔
793
            if (!matcher.find()) {
1✔
794
                throw new IllegalStateException(String.format("NVE fora do padrao (%s)", nve));
1✔
795
            }
796
        }
797
    }
1✔
798

799
    public static void itemListaServico(final String itemListaServicos) {
800
        if (itemListaServicos != null) {
1✔
801
            final Matcher matcher = Pattern.compile("^\\d{2}\\.\\d{2}$").matcher(itemListaServicos);
1✔
802
            if (!matcher.find()) {
1✔
803
                throw new IllegalStateException(String.format("Item Lista de servico fora do padrao (%s)", itemListaServicos));
1✔
804
            }
805
        }
806
    }
1✔
807

808
    public static void exatamente54(final String string, final String info) {
809
        DFStringValidador.validaTamanhoExato(string, 54, info);
1✔
810
    }
1✔
811

812
    public static void exatamente55(final String string, final String info) {
UNCOV
813
        DFStringValidador.validaTamanhoExato(string, 55, info);
×
UNCOV
814
    }
×
815

816
    public static void exatamente15N(final String string, final String info) {
817
        DFStringValidador.validaTamanhoExato(string, 15, info);
1✔
818
        DFStringValidador.apenasNumerico(string, info);
1✔
819
    }
1✔
820

821
    public static void exatamente11N(final String string, final String info) {
822
        DFStringValidador.apenasNumerico(string, info);
1✔
823
        DFStringValidador.validaTamanhoExato(string, 11, info);
1✔
824
    }
1✔
825

826
    public static void modeloDocumentoFiscal(final String modeloDocumentoFiscal) {
827
        if (!modeloDocumentoFiscal.equals("55") && !modeloDocumentoFiscal.equals("65")) {
1✔
828
            throw new IllegalStateException(String.format("Modelo Fiscal Invalido (%s)", modeloDocumentoFiscal));
1✔
829
        }
830
    }
1✔
831

832
    public static void identificador(final String identificador) {
833
        final Matcher matcher = Pattern.compile("^ID\\d{41}$").matcher(identificador);
1✔
834
        if (!matcher.find()) {
1✔
835
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
1✔
836
        }
837
    }
1✔
838

839
    public static void identificadorCTe(final String identificador) {
UNCOV
840
        final Matcher matcher = Pattern.compile("^ID\\d{39}$").matcher(identificador);
×
UNCOV
841
        if (!matcher.find()) {
×
UNCOV
842
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
×
843
        }
UNCOV
844
    }
×
845

846
    public static void equals(final String test, final String tested) {
847
        if (!StringUtils.equals(test, tested)) {
1✔
848
            throw new IllegalStateException(String.format("Valor('%s') nao corresponde com o padrao('%s')", tested, test));
1✔
849
        }
850
    }
1✔
851

852
    public static void isBase64(final String string, final String info) {
853
        if (!Base64.isArrayByteBase64(string.getBytes())) {
1✔
UNCOV
854
            throw new IllegalStateException(String.format("A string %s com o valor = '%s' precisa ser codificada em Base64. ", info, string));
×
855
        }
856
    }
1✔
857

858
    /**
859
     * Validacao conforme nota tecnica 2019.001 Versao 1.00 – Abril de 2019
860
     */
861
    public static void validaCodigoRandomico(final String string, final String info) {
862
        String[] codigosInvalidos = new String[]{"00000000", "11111111", "22222222", "33333333", "44444444", "55555555", "66666666", "77777777", "88888888", "99999999", "12345678", "23456789", "34567890", "45678901", "56789012", "67890123", "78901234", "89012345", "90123456", "01234567"};
1✔
863
        if (StringUtils.containsAny(string, codigosInvalidos)) {
1✔
864
            throw new IllegalStateException(String.format("%s \"%s\" inválido", info, string));
1✔
865
        }
866
    }
1✔
867

868
    /**
869
     * Metodo para regra de validacao N18-10 e N18-20, da nota tecnica :
870
     * 2019.001 Versao 1.00 – Abril de 2019 Utilizasse Java reflection para
871
     * acessar os metodos necessarios.
872
     */
873
    //este metodo esta muito ruim, nao faz nada e ainda estoura um stack trace que nao pode ser capturado!
874
    //(pq está ruim?, ele faz a validacao da nota tecnica 2019.001 Versao 1.00 descrita no comentario do metodo)
875
    public static void validaPreenchimentoDeMargemValorAgregado(NFNotaInfoItemImpostoICMS impostoICMS) throws InvocationTargetException, IllegalAccessException {
876
        if (impostoICMS != null) {
1✔
877
            //seleciona todos os metodos da classe de ICMS
878
            for (Method method : impostoICMS.getClass().getMethods()) {
1✔
879
                final Class<?> returnType = method.getReturnType();
1✔
880
                Method[] typeMethods = returnType.getMethods();
1✔
881
                //verifica se a classe de ICMS tem o item NFNotaInfoItemModalidadeBCICMSST.
882
                final boolean present = Arrays.stream(typeMethods).anyMatch(method1 -> method1.getReturnType().equals(NFNotaInfoItemModalidadeBCICMSST.class));
1✔
883
                if (present) {
1✔
884
                    //invoca o metodo para verificar qual classe de ICMS esta preenchida(objectValue!=null)
885
                    Object objectValue = method.invoke(impostoICMS);
1✔
886
                    if (objectValue != null) {
1✔
887
                        // retorna o metodo necessario para extrair o valor de ModalidadeMVA.
888
                        Method modalidadeMethod = Arrays.stream(typeMethods).filter(method1 -> method1.getReturnType().equals(NFNotaInfoItemModalidadeBCICMSST.class)).findAny().get();
1✔
889
                        NFNotaInfoItemModalidadeBCICMSST modalidadeBCICMSST = (NFNotaInfoItemModalidadeBCICMSST) modalidadeMethod.invoke(objectValue, new Object[]{});
1✔
890
                        // retorna o metodo necessario para extrair o valor da percentualMargemValorAdicionadoICMSST(pMVAST).
891
                        Method percentualMethod = Arrays.stream(typeMethods).filter(method1 -> method1.getName().contains("getPercentualMargemValorAdicionadoICMSST")).findAny().orElse(null);
1✔
892
                        String percentualValue = null;
1✔
893
                        if (percentualMethod != null) {
1✔
894
                            percentualValue = (String) percentualMethod.invoke(objectValue, new Object[]{});
1✔
895
                        }
896
                        //verificacoes conforme a regra de validacao
897
                        if (modalidadeBCICMSST != null && modalidadeBCICMSST.equals(NFNotaInfoItemModalidadeBCICMSST.MARGEM_VALOR_AGREGADO) && StringUtils.isBlank(percentualValue)) {
1✔
898
                            throw new IllegalStateException("Informada modalidade de determinacao da BC da ST como MVA(modBCST=4)" + " e nao informado o campo pMVAST!");
1✔
899
                        } else if (StringUtils.isNotBlank(percentualValue) && (modalidadeBCICMSST == null || !modalidadeBCICMSST.equals(NFNotaInfoItemModalidadeBCICMSST.MARGEM_VALOR_AGREGADO))) {
1✔
900
                            throw new IllegalStateException(String.format("Informada modalidade de determinacao da BC da ST diferente de MVA(informado[%s]) e informado o campo pMVAST", (modalidadeBCICMSST != null ? modalidadeBCICMSST.toString() : "modBCST<>4")));
1✔
901
                        }
902
                    }
903
                }
904
            }
905
        }
906
    }
1✔
907
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc