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

wmixvideo / nfe / #7176

17 Dec 2025 11:08AM UTC coverage: 50.585% (-1.6%) from 52.149%
#7176

push

web-flow
Merge branch 'wmixvideo:master' into master

8 of 884 new or added lines in 70 files covered. (0.9%)

3 existing lines in 1 file now uncovered.

14779 of 29216 relevant lines covered (50.59%)

0.51 hits per line

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

92.79
/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 tamanho15ou17N(final String string, final String info) {
359
        if (string != null) {
1✔
360
            DFStringValidador.apenasNumerico(string, info);
1✔
361
            if (string.length() != 15 && string.length() != 17) {
1✔
362
                throw new IllegalStateException(String.format("%s \"%s\" deve possuir 15 ou 17 caracteres", info, string));
1✔
363
            }
364
        }
365
    }
1✔
366

367
    public static void tamanho12(final String string, final String info) {
368
        if (string != null) {
1✔
369
            DFStringValidador.validaTamanhoMaximo(string, 12, info);
1✔
370
        }
371
    }
1✔
372

373
    public static void tamanho12N(final String string, final String info) {
374
        if (string != null) {
×
375
            DFStringValidador.apenasNumerico(string, info);
×
376
            DFStringValidador.validaTamanhoMaximo(string, 12, info);
×
377
        }
378
    }
×
379

380
    public static void tamanho120(final String string, final String info) {
381
        if (string != null) {
1✔
382
            DFStringValidador.validaTamanhoMaximo(string, 120, info);
1✔
383
        }
384
    }
1✔
385

386
    public static void tamanho128(final String string, final String info) {
387
        if (string != null) {
1✔
388
            DFStringValidador.validaTamanhoMaximo(string, 128, info);
1✔
389
        }
390
    }
1✔
391

392
    public static void tamanho160(final String string, final String info) {
393
        if (string != null) {
1✔
394
            DFStringValidador.validaTamanhoMaximo(string, 160, info);
1✔
395
        }
396
    }
1✔
397

398
    public static void tamanho10(final String string, final String info) {
399
        if (string != null) {
1✔
400
            DFStringValidador.validaTamanhoMaximo(string, 10, info);
1✔
401
        }
402
    }
1✔
403

404
    public static void tamanho10N(final String string, final String info) {
405
        if (string != null) {
1✔
406
            DFStringValidador.apenasNumerico(string, info);
1✔
407
            DFStringValidador.validaTamanhoMaximo(string, 10, info);
1✔
408
        }
409
    }
1✔
410

411
    public static void tamanho100(final String string, final String info) {
412
        if (string != null) {
1✔
413
            DFStringValidador.validaTamanhoMaximo(string, 100, info);
1✔
414
        }
415
    }
1✔
416

417
    public static void tamanho6(final String string, final String info) {
418
        if (string != null) {
1✔
419
            DFStringValidador.validaTamanhoMaximo(string, 6, info);
1✔
420
        }
421
    }
1✔
422

423
    public static void tamanho6N(final String string, final String info) {
424
        if (string != null) {
1✔
425
            DFStringValidador.validaTamanhoMaximo(string, 6, info);
1✔
426
        }
427
    }
1✔
428

429
    public static void tamanho500(final String string, final String info) {
430
        if (string != null) {
1✔
431
            DFStringValidador.validaTamanhoMaximo(string, 500, info);
1✔
432
        }
433
    }
1✔
434

435
    public static void tamanho3(final String string, final String info) {
436
        if (string != null) {
1✔
437
            DFStringValidador.validaTamanhoMaximo(string, 3, info);
1✔
438
        }
439
    }
1✔
440

441
    public static void exatamente7(final String string, final String info) {
442
        if (string != null) {
1✔
443
            DFStringValidador.validaTamanhoExato(string, 7, info);
1✔
444
        }
445
    }
1✔
446

447
    public static void exatamente8(final String string, final String info) {
448
        if (string != null) {
1✔
449
            DFStringValidador.validaTamanhoExato(string, 8, info);
1✔
450
        }
451
    }
1✔
452

453
    public static void exatamente8N(final String string, final String info) {
454
        if (string != null) {
1✔
455
            DFStringValidador.apenasNumerico(string, info);
1✔
456
            DFStringValidador.validaTamanhoExato(string, 8, info);
1✔
457
        }
458
    }
1✔
459

460
    public static void exatamente2(final String string, final String info) {
461
        if (string != null) {
1✔
462
            DFStringValidador.validaTamanhoExato(string, 2, info);
1✔
463
        }
464
    }
1✔
465

466
    public static void exatamente2N(final String string, final String info) {
467
        if (string != null) {
1✔
468
            DFStringValidador.apenasNumerico(string, info);
1✔
469
            DFStringValidador.validaTamanhoExato(string, 2, info);
1✔
470
        }
471
    }
1✔
472

473
    public static void tamanho8a9(final String string, final String info) {
474
        if (string != null) {
1✔
475
            DFStringValidador.intervalo(string, 8, 9, info);
1✔
476
        }
477
    }
1✔
478

479
    public static void tamanho15a256(final String string, final String info) {
480
        if (string != null) {
1✔
481
            DFStringValidador.intervalo(string, 15, 256, info);
1✔
482
        }
483
    }
1✔
484

485
    public static void tamanho15a255(final String string, final String info) {
486
        if (string != null) {
1✔
487
            DFStringValidador.intervalo(string, 15, 255, info);
1✔
488
        }
489
    }
1✔
490

491
    public static void tamanho5a20(final String string, final String info) {
492
        if (string != null) {
1✔
493
            DFStringValidador.intervalo(string, 5, 20, info);
1✔
494
        }
495
    }
1✔
496

497
    public static void tamanho5a14(final String string, final String info) {
498
        if (string != null) {
1✔
499
            DFStringValidador.intervalo(string, 5, 14, info);
1✔
500
        }
501
    }
1✔
502

503
    public static void tamanho5a60(final String string, final String info) {
504
        if (string != null) {
1✔
505
            DFStringValidador.intervalo(string, 5, 60, info);
1✔
506
        }
507
    }
1✔
508

509
    public static void tamanho4a60(final String string, final String info) {
510
        if (string != null) {
1✔
511
            DFStringValidador.intervalo(string, 4, 60, info);
1✔
512
        }
513
    }
1✔
514

515
    public static void tamanho2a4(final String string, final String info) {
516
        if (string != null) {
1✔
517
            DFStringValidador.intervalo(string, 2, 4, info);
1✔
518
        }
519
    }
1✔
520

521
    public static void tamanho2a9N(final String string, final String info) {
522
        if (string != null) {
1✔
523
            DFStringValidador.apenasNumerico(string, info);
1✔
524
            DFStringValidador.intervalo(string, 2, 9, info);
1✔
525
        }
526
    }
1✔
527

528
    public static void tamanho8a9N(final String string, final String info) {
529
        if (string != null) {
1✔
530
            DFStringValidador.apenasNumerico(string, info);
1✔
531
            DFStringValidador.intervalo(string, 8, 9, info);
1✔
532
        }
533
    }
1✔
534

535
    public static void tamanho15a1000(final String string, final String info) {
536
        if (string != null) {
1✔
537
            DFStringValidador.intervalo(string, 15, 1000, info);
1✔
538
        }
539
    }
1✔
540

541
    public static void tamanho100a600(final String string, final String info) {
542
        if (string != null) {
1✔
543
            DFStringValidador.intervalo(string, 100, 600, info);
1✔
544
        }
545
    }
1✔
546

547
    public static void tamanho60a1000(final String string, final String info) {
548
        if (string != null) {
1✔
549
            DFStringValidador.intervalo(string, 60, 1000, info);
1✔
550
        }
551
    }
1✔
552

553
    public static void tamanho2a95(final String string, final String info) {
554
        if (string != null) {
1✔
555
            DFStringValidador.intervalo(string, 2, 95, info);
1✔
556
        }
557
    }
1✔
558

559
    public static void tamanho2a98(final String string, final String info) {
560
        if (string != null) {
1✔
561
            DFStringValidador.intervalo(string, 2, 98, info);
1✔
562
        }
563
    }
1✔
564

565
    public static void tamanho30(final String string, final String info) {
566
        if (string != null) {
1✔
567
            DFStringValidador.validaTamanhoMaximo(string, 30, info);
1✔
568
        }
569
    }
1✔
570

571
    public static void tamanho2a85(final String string, final String info) {
UNCOV
572
        if (string != null) {
×
UNCOV
573
            DFStringValidador.intervalo(string, 2, 85, info);
×
574
        }
UNCOV
575
    }
×
576

577
    public static void exatamente44(final String string, final String info) {
578
        if (string != null) {
1✔
579
            DFStringValidador.validaTamanhoExato(string, 44, info);
1✔
580
        }
581
    }
1✔
582

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

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

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

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

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

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

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

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

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

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

652
    public static void tamanho2ou3N(final String string, final String info) {
653
        if (string != null) {
1✔
654
            DFStringValidador.apenasNumerico(string, info);
1✔
655
            DFStringValidador.intervalo(string, 2, 3, info);
1✔
656
        }
657
    }
1✔
658

659
    public static void tamanho3N(final String string, final String info) {
660
        if (string != null) {
1✔
661
            DFStringValidador.apenasNumerico(string, info);
1✔
662
            DFStringValidador.validaTamanhoMaximo(string, 3, info);
1✔
663
        }
664
    }
1✔
665

666
    public static void tamanho2N(final String string, final String info) {
667
        if (string != null) {
1✔
668
            DFStringValidador.apenasNumerico(string, info);
1✔
669
            DFStringValidador.validaTamanhoMaximo(string, 2, info);
1✔
670
        }
671
    }
1✔
672

673
    public static void exatamente20N(final String string, final String info) {
674
        if (string != null) {
1✔
675
            DFStringValidador.apenasNumerico(string, info);
1✔
676
            DFStringValidador.validaTamanhoExato(string, 20, info);
1✔
677
        }
678
    }
1✔
679

680
    public static void tamanho25N(final String string, final String info) {
681
        if (string != null) {
×
682
            DFStringValidador.apenasNumerico(string, info);
×
683
            DFStringValidador.validaTamanhoMaximo(string, 25, info);
×
684
        }
685
    }
×
686

687
    /**
688
     * Metodo para validacao de Strings.
689
     *
690
     * @param paraValidar String a ser validada
691
     * @param info Informacao de retorno caso haja erro.
692
     * @param tamanho tamanho para validacao da {@code String} , pode ser
693
     * {@code null} :
694
     * @param exatamente <pre>
695
     * se false {@code null} a {@code String}
696
     *                   nao precisa ter o tamanho exato do parametro anterior.
697
     * </pre>
698
     *
699
     * @param numerico se true {@code null} a {@code String} precisa ser
700
     * numerica[0-9].
701
     * @param paraValidar}.
702
     * @return retorna a propria {@code String} {
703
     */
704
    public static String validador(final String paraValidar, final String info, Integer tamanho, Boolean exatamente, Boolean numerico) {
705
        tamanho = ObjectUtils.defaultIfNull(tamanho, 1);
1✔
706
        exatamente = ObjectUtils.defaultIfNull(exatamente, false);
1✔
707
        numerico = ObjectUtils.defaultIfNull(numerico, true);
1✔
708
        if (paraValidar != null) {
1✔
709
            if (numerico) {
1✔
710
                DFStringValidador.apenasNumerico(paraValidar, info);
1✔
711
            }
712
            if (exatamente) {
1✔
713
                DFStringValidador.validaTamanhoExato(paraValidar, tamanho, info);
1✔
714
            } else {
715
                DFStringValidador.validaTamanhoMaximo(paraValidar, tamanho, info);
1✔
716
            }
717
        }
718
        return paraValidar;
1✔
719
    }
720

721
    /**
722
     * @See #validador
723
     */
724
    public static String validador(final String paraValidar, final String info, final Integer tamanho, final Boolean exatamente) {
725
        return DFStringValidador.validador(paraValidar, info, tamanho, exatamente, null);
1✔
726
    }
727

728
    /**
729
     * @See #validador
730
     */
731
    public static String validador(final String paraValidar, final String info, final Integer tamanho) {
732
        return DFStringValidador.validador(paraValidar, info, tamanho, null, null);
1✔
733
    }
734

735
    public static void fci(final String numeroControleFCI) {
736
        if (numeroControleFCI != null) {
1✔
737
            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✔
738
            if (!matcher.find()) {
1✔
739
                throw new IllegalStateException(String.format("FCI fora do padrao (%s)", numeroControleFCI));
1✔
740
            }
741
        }
742
    }
1✔
743

744
    public static void ncm(final String ncm) {
745
        if (ncm != null) {
1✔
746
            final Matcher matcher = Pattern.compile("^([0-9]{2}|[0-9]{8})$").matcher(ncm);
1✔
747
            if (!matcher.find()) {
1✔
748
                throw new IllegalStateException(String.format("NCM fora do padrao (%s)", ncm));
1✔
749
            }
750
        }
751
    }
1✔
752

753
    private static void apenasNumerico(final String string, final String info) {
754
        if (string != null && !StringUtils.isNumeric(string)) {
1✔
755
            throw new IllegalStateException(String.format("A string %s precisa ser numerica (%s)", info, string));
1✔
756
        }
757
    }
1✔
758

759
    public static void validaTamanhoMaximo(final String string, final int tamanho, final String info) {
760
        if (string != null && (string.length() < 1 || string.length() > tamanho)) {
1✔
761
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre 1-%s caracteres", info, string, tamanho));
1✔
762
        }
763
    }
1✔
764

765
    private static void validaTamanhoExato(final String string, final int tamanho, final String info) {
766
        if (string != null && string.length() != tamanho) {
1✔
767
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir %s caracteres", info, string, tamanho));
1✔
768
        }
769
    }
1✔
770

771
    private static void intervalo(final String string, final int inicio, final int fim, final String info) {
772
        if (string != null && (string.length() < inicio || string.length() > fim)) {
1✔
773
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre %s-%s caracteres", info, string, inicio, fim));
1✔
774
        }
775
    }
1✔
776

777
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info) {
778
        return DFStringValidador.validaIntervalo(string, inicio, fim, info, false);
1✔
779
    }
780

781
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info, Boolean isNumeric) {
782
        if (string != null) {
1✔
783
            isNumeric = ObjectUtils.defaultIfNull(isNumeric, false);
1✔
784
            if (isNumeric) {
1✔
785
                DFStringValidador.apenasNumerico(string, info);
1✔
786
            }
787
            DFStringValidador.intervalo(string, inicio, fim, info);
1✔
788
        }
789
        return string;
1✔
790
    }
791

792
    /**
793
     * Valida um numero com N {
794
     *
795
     * <pre>
796
     *  StringValidador.capacidadeNDigitos("10000", "info" , 5)   = "10000"
797
     *  StringValidador.capacidadeNDigitos("5", "info" , 2)   = "5"
798
     * </pre>
799
     *
800
     * @param capacidade
801
     * @param info
802
     * @param digitos
803
     * @return
804
     * @throws IllegalStateException se<br>
805
     * {@code capacidade = "10000" } & {@code digitos = 3}, ou seja , {@code capacidade.length()-1 > digitos
806
     * }
807
     */
808
    public static String capacidadeNDigitos(final String capacidade, final String info, final int digitos) {
809
        if (capacidade != null) {
1✔
810
            final Matcher matcher = Pattern.compile("^(0|[1-9]{1}[0-9]{0," + digitos + "})$").matcher(capacidade);
1✔
811
            if (!matcher.find()) {
1✔
812
                throw new IllegalStateException(String.format("%s fora do padrao (%s)", info, capacidade));
1✔
813
            }
814
        }
815
        return capacidade;
1✔
816
    }
817

818
    public static void nve(final String nve) {
819
        if (nve != null) {
1✔
820
            final Matcher matcher = Pattern.compile("^[A-Z]{2}[0-9]{4}$").matcher(nve);
1✔
821
            if (!matcher.find()) {
1✔
822
                throw new IllegalStateException(String.format("NVE fora do padrao (%s)", nve));
1✔
823
            }
824
        }
825
    }
1✔
826

827
    public static void itemListaServico(final String itemListaServicos) {
828
        if (itemListaServicos != null) {
1✔
829
            final Matcher matcher = Pattern.compile("^\\d{2}\\.\\d{2}$").matcher(itemListaServicos);
1✔
830
            if (!matcher.find()) {
1✔
831
                throw new IllegalStateException(String.format("Item Lista de servico fora do padrao (%s)", itemListaServicos));
1✔
832
            }
833
        }
834
    }
1✔
835

836
    public static void exatamente54(final String string, final String info) {
837
        DFStringValidador.validaTamanhoExato(string, 54, info);
1✔
838
    }
1✔
839

840
    public static void exatamente55(final String string, final String info) {
841
        DFStringValidador.validaTamanhoExato(string, 55, info);
×
842
    }
×
843

844
    public static void exatamente15N(final String string, final String info) {
845
        DFStringValidador.validaTamanhoExato(string, 15, info);
1✔
846
        DFStringValidador.apenasNumerico(string, info);
1✔
847
    }
1✔
848

849
    public static void exatamente11N(final String string, final String info) {
850
        DFStringValidador.apenasNumerico(string, info);
1✔
851
        DFStringValidador.validaTamanhoExato(string, 11, info);
1✔
852
    }
1✔
853

854
    public static void modeloDocumentoFiscal(final String modeloDocumentoFiscal) {
855
        if (!modeloDocumentoFiscal.equals("55") && !modeloDocumentoFiscal.equals("65")) {
1✔
856
            throw new IllegalStateException(String.format("Modelo Fiscal Invalido (%s)", modeloDocumentoFiscal));
1✔
857
        }
858
    }
1✔
859

860
    public static void identificador(final String identificador) {
861
        final Matcher matcher = Pattern.compile("^ID\\d{41}$").matcher(identificador);
1✔
862
        if (!matcher.find()) {
1✔
863
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
1✔
864
        }
865
    }
1✔
866

867
    public static void identificadorCTe(final String identificador) {
868
        final Matcher matcher = Pattern.compile("^ID\\d{39}$").matcher(identificador);
×
869
        if (!matcher.find()) {
×
870
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
×
871
        }
872
    }
×
873

874
    public static void equals(final String test, final String tested) {
875
        if (!StringUtils.equals(test, tested)) {
1✔
876
            throw new IllegalStateException(String.format("Valor('%s') nao corresponde com o padrao('%s')", tested, test));
1✔
877
        }
878
    }
1✔
879

880
    public static void isBase64(final String string, final String info) {
881
        if (!Base64.isArrayByteBase64(string.getBytes())) {
1✔
882
            throw new IllegalStateException(String.format("A string %s com o valor = '%s' precisa ser codificada em Base64. ", info, string));
×
883
        }
884
    }
1✔
885

886
    /**
887
     * Validacao conforme nota tecnica 2019.001 Versao 1.00 – Abril de 2019
888
     */
889
    public static void validaCodigoRandomico(final String string, final String info) {
890
        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✔
891
        if (StringUtils.containsAny(string, codigosInvalidos)) {
1✔
892
            throw new IllegalStateException(String.format("%s \"%s\" inválido", info, string));
1✔
893
        }
894
    }
1✔
895

896
    /**
897
     * Metodo para regra de validacao N18-10 e N18-20, da nota tecnica :
898
     * 2019.001 Versao 1.00 – Abril de 2019 Utilizasse Java reflection para
899
     * acessar os metodos necessarios.
900
     */
901
    //este metodo esta muito ruim, nao faz nada e ainda estoura um stack trace que nao pode ser capturado!
902
    //(pq está ruim?, ele faz a validacao da nota tecnica 2019.001 Versao 1.00 descrita no comentario do metodo)
903
    public static void validaPreenchimentoDeMargemValorAgregado(NFNotaInfoItemImpostoICMS impostoICMS) throws InvocationTargetException, IllegalAccessException {
904
        if (impostoICMS != null) {
1✔
905
            //seleciona todos os metodos da classe de ICMS
906
            for (Method method : impostoICMS.getClass().getMethods()) {
1✔
907
                final Class<?> returnType = method.getReturnType();
1✔
908
                Method[] typeMethods = returnType.getMethods();
1✔
909
                //verifica se a classe de ICMS tem o item NFNotaInfoItemModalidadeBCICMSST.
910
                final boolean present = Arrays.stream(typeMethods).anyMatch(method1 -> method1.getReturnType().equals(NFNotaInfoItemModalidadeBCICMSST.class));
1✔
911
                if (present) {
1✔
912
                    //invoca o metodo para verificar qual classe de ICMS esta preenchida(objectValue!=null)
913
                    Object objectValue = method.invoke(impostoICMS);
1✔
914
                    if (objectValue != null) {
1✔
915
                        // retorna o metodo necessario para extrair o valor de ModalidadeMVA.
916
                        Method modalidadeMethod = Arrays.stream(typeMethods).filter(method1 -> method1.getReturnType().equals(NFNotaInfoItemModalidadeBCICMSST.class)).findAny().get();
1✔
917
                        NFNotaInfoItemModalidadeBCICMSST modalidadeBCICMSST = (NFNotaInfoItemModalidadeBCICMSST) modalidadeMethod.invoke(objectValue, new Object[]{});
1✔
918
                        // retorna o metodo necessario para extrair o valor da percentualMargemValorAdicionadoICMSST(pMVAST).
919
                        Method percentualMethod = Arrays.stream(typeMethods).filter(method1 -> method1.getName().contains("getPercentualMargemValorAdicionadoICMSST")).findAny().orElse(null);
1✔
920
                        String percentualValue = null;
1✔
921
                        if (percentualMethod != null) {
1✔
922
                            percentualValue = (String) percentualMethod.invoke(objectValue, new Object[]{});
1✔
923
                        }
924
                        //verificacoes conforme a regra de validacao
925
                        if (modalidadeBCICMSST != null && modalidadeBCICMSST.equals(NFNotaInfoItemModalidadeBCICMSST.MARGEM_VALOR_AGREGADO) && StringUtils.isBlank(percentualValue)) {
1✔
926
                            throw new IllegalStateException("Informada modalidade de determinacao da BC da ST como MVA(modBCST=4)" + " e nao informado o campo pMVAST!");
1✔
927
                        } else if (StringUtils.isNotBlank(percentualValue) && (modalidadeBCICMSST == null || !modalidadeBCICMSST.equals(NFNotaInfoItemModalidadeBCICMSST.MARGEM_VALOR_AGREGADO))) {
1✔
928
                            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✔
929
                        }
930
                    }
931
                }
932
            }
933
        }
934
    }
1✔
935
}
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

© 2025 Coveralls, Inc