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

wmixvideo / nfe / #6749

24 Jun 2025 01:09AM UTC coverage: 51.41% (-0.4%) from 51.794%
#6749

push

web-flow
Merge a03513cf8 into bb9f583b3

0 of 19 new or added lines in 1 file covered. (0.0%)

47 existing lines in 5 files now uncovered.

14003 of 27238 relevant lines covered (51.41%)

0.51 hits per line

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

94.09
/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 tamanho2a95(final String string, final String info) {
539
        if (string != null) {
1✔
540
            DFStringValidador.intervalo(string, 2, 95, info);
1✔
541
        }
542
    }
1✔
543

544
    public static void tamanho30(final String string, final String info) {
545
        if (string != null) {
1✔
546
            DFStringValidador.validaTamanhoMaximo(string, 30, info);
1✔
547
        }
548
    }
1✔
549

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

556
    public static void exatamente7N(final String string, final String info) {
557
        if (string != null) {
1✔
558
            DFStringValidador.apenasNumerico(string, info);
1✔
559
            DFStringValidador.exatamente7(string, info);
1✔
560
        }
561
    }
1✔
562

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

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

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

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

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

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

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

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

618
    public static void tamanho2ou3N(final String string, final String info) {
619
        if (string != null) {
1✔
620
            DFStringValidador.apenasNumerico(string, info);
1✔
621
            DFStringValidador.intervalo(string, 2, 3, info);
1✔
622
        }
623
    }
1✔
624

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

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

639
    public static void exatamente20N(final String string, final String info) {
640
        if (string != null) {
1✔
641
            DFStringValidador.apenasNumerico(string, info);
1✔
642
            DFStringValidador.validaTamanhoExato(string, 20, info);
1✔
643
        }
644
    }
1✔
645

646
    public static void tamanho25N(final String string, final String info) {
UNCOV
647
        if (string != null) {
×
UNCOV
648
            DFStringValidador.apenasNumerico(string, info);
×
UNCOV
649
            DFStringValidador.validaTamanhoMaximo(string, 25, info);
×
650
        }
UNCOV
651
    }
×
652

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

687
    /**
688
     * @See #validador
689
     */
690
    public static String validador(final String paraValidar, final String info, final Integer tamanho, final Boolean exatamente) {
691
        return DFStringValidador.validador(paraValidar, info, tamanho, exatamente, null);
1✔
692
    }
693

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

701
    public static void fci(final String numeroControleFCI) {
702
        if (numeroControleFCI != null) {
1✔
703
            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✔
704
            if (!matcher.find()) {
1✔
705
                throw new IllegalStateException(String.format("FCI fora do padrao (%s)", numeroControleFCI));
1✔
706
            }
707
        }
708
    }
1✔
709

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

719
    private static void apenasNumerico(final String string, final String info) {
720
        if (string != null && !StringUtils.isNumeric(string)) {
1✔
721
            throw new IllegalStateException(String.format("A string %s precisa ser numerica (%s)", info, string));
1✔
722
        }
723
    }
1✔
724

725
    public static void validaTamanhoMaximo(final String string, final int tamanho, final String info) {
726
        if (string != null && (string.length() < 1 || string.length() > tamanho)) {
1✔
727
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre 1-%s caracteres", info, string, tamanho));
1✔
728
        }
729
    }
1✔
730

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

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

743
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info) {
744
        return DFStringValidador.validaIntervalo(string, inicio, fim, info, false);
1✔
745
    }
746

747
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info, Boolean isNumeric) {
748
        if (string != null) {
1✔
749
            isNumeric = ObjectUtils.defaultIfNull(isNumeric, false);
1✔
750
            if (isNumeric) {
1✔
751
                DFStringValidador.apenasNumerico(string, info);
1✔
752
            }
753
            DFStringValidador.intervalo(string, inicio, fim, info);
1✔
754
        }
755
        return string;
1✔
756
    }
757

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

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

793
    public static void itemListaServico(final String itemListaServicos) {
794
        if (itemListaServicos != null) {
1✔
795
            final Matcher matcher = Pattern.compile("^\\d{2}\\.\\d{2}$").matcher(itemListaServicos);
1✔
796
            if (!matcher.find()) {
1✔
797
                throw new IllegalStateException(String.format("Item Lista de servico fora do padrao (%s)", itemListaServicos));
1✔
798
            }
799
        }
800
    }
1✔
801

802
    public static void exatamente54(final String string, final String info) {
803
        DFStringValidador.validaTamanhoExato(string, 54, info);
1✔
804
    }
1✔
805

806
    public static void exatamente55(final String string, final String info) {
UNCOV
807
        DFStringValidador.validaTamanhoExato(string, 55, info);
×
UNCOV
808
    }
×
809

810
    public static void exatamente15N(final String string, final String info) {
811
        DFStringValidador.validaTamanhoExato(string, 15, info);
1✔
812
        DFStringValidador.apenasNumerico(string, info);
1✔
813
    }
1✔
814

815
    public static void exatamente11N(final String string, final String info) {
816
        DFStringValidador.apenasNumerico(string, info);
1✔
817
        DFStringValidador.validaTamanhoExato(string, 11, info);
1✔
818
    }
1✔
819

820
    public static void modeloDocumentoFiscal(final String modeloDocumentoFiscal) {
821
        if (!modeloDocumentoFiscal.equals("55") && !modeloDocumentoFiscal.equals("65")) {
1✔
822
            throw new IllegalStateException(String.format("Modelo Fiscal Invalido (%s)", modeloDocumentoFiscal));
1✔
823
        }
824
    }
1✔
825

826
    public static void identificador(final String identificador) {
827
        final Matcher matcher = Pattern.compile("^ID\\d{41}$").matcher(identificador);
1✔
828
        if (!matcher.find()) {
1✔
829
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
1✔
830
        }
831
    }
1✔
832

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

840
    public static void equals(final String test, final String tested) {
841
        if (!StringUtils.equals(test, tested)) {
1✔
842
            throw new IllegalStateException(String.format("Valor('%s') nao corresponde com o padrao('%s')", tested, test));
1✔
843
        }
844
    }
1✔
845

846
    public static void isBase64(final String string, final String info) {
847
        if (!Base64.isArrayByteBase64(string.getBytes())) {
1✔
UNCOV
848
            throw new IllegalStateException(String.format("A string %s com o valor = '%s' precisa ser codificada em Base64. ", info, string));
×
849
        }
850
    }
1✔
851

852
    /**
853
     * Validacao conforme nota tecnica 2019.001 Versao 1.00 – Abril de 2019
854
     */
855
    public static void validaCodigoRandomico(final String string, final String info) {
856
        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✔
857
        if (StringUtils.containsAny(string, codigosInvalidos)) {
1✔
858
            throw new IllegalStateException(String.format("%s \"%s\" inválido", info, string));
1✔
859
        }
860
    }
1✔
861

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