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

wmixvideo / nfe / #7324

18 Nov 2025 12:44PM UTC coverage: 52.488%. Remained the same
#7324

push

web-flow
Merge 5aad707ce into 2d4409cab

272 of 387 new or added lines in 14 files covered. (70.28%)

704 existing lines in 45 files now uncovered.

14639 of 27890 relevant lines covered (52.49%)

0.52 hits per line

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

94.2
/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
import org.apache.commons.lang3.Strings;
9

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

UNCOV
18
public abstract class DFStringValidador {
×
19

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

186
    public static void placaDeVeiculo(final String placaVeiculo) {
187
        if (placaVeiculo != null) {
1✔
188
            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✔
189
            if (!matcher.find()) {
1✔
190
                throw new IllegalStateException(String.format("Placa de veiculo nao esta no padrao (%s)", placaVeiculo));
1✔
191
            }
192
        }
193
    }
1✔
194

195
    public static void placaDeVeiculo(final String placaVeiculo, final String info) {
196
        if (placaVeiculo != null) {
1✔
197
            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✔
198
            if (!matcher.find()) {
1✔
199
                throw new IllegalStateException(String.format("%s nao esta no padrao (%s)", info, placaVeiculo));
1✔
200
            }
201
        }
202
    }
1✔
203

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

359
    public static void tamanho15ou17N(final String string, final String info) {
360
        if (string != null) {
1✔
361
            DFStringValidador.apenasNumerico(string, info);
1✔
362
            if (string.length() != 15 && string.length() != 17) {
1✔
363
                throw new IllegalStateException(String.format("%s \"%s\" deve possuir 15 ou 17 caracteres", info, string));
1✔
364
            }
365
        }
366
    }
1✔
367

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

560
    public static void tamanho30(final String string, final String info) {
561
        if (string != null) {
1✔
562
            DFStringValidador.validaTamanhoMaximo(string, 30, info);
1✔
563
        }
564
    }
1✔
565

566
    public static void exatamente44(final String string, final String info) {
567
        if (string != null) {
1✔
568
            DFStringValidador.validaTamanhoExato(string, 44, info);
1✔
569
        }
570
    }
1✔
571

572
    public static void exatamente7N(final String string, final String info) {
573
        if (string != null) {
1✔
574
            DFStringValidador.apenasNumerico(string, info);
1✔
575
            DFStringValidador.exatamente7(string, info);
1✔
576
        }
577
    }
1✔
578

579
    public static void exatamente44N(final String string, final String info) {
580
        if (string != null) {
1✔
581
            DFStringValidador.apenasNumerico(string, info);
1✔
582
            DFStringValidador.exatamente44(string, info);
1✔
583
        }
584
    }
1✔
585

586
    public static void exatamente4N(final String string, final String info) {
587
        if (string != null) {
1✔
588
            DFStringValidador.apenasNumerico(string, info);
1✔
589
            DFStringValidador.exatamente4(string, info);
1✔
590
        }
591
    }
1✔
592

593
    public static void exatamente6N(final String string, final String info) {
594
        if (string != null) {
1✔
595
            DFStringValidador.apenasNumerico(string, info);
1✔
596
            DFStringValidador.exatamente6(string, info);
1✔
597
        }
598
    }
1✔
599

600
    public static void tamanho15N(final String string, final String info) {
601
        if (string != null) {
1✔
602
            DFStringValidador.apenasNumerico(string, info);
1✔
603
            DFStringValidador.validaTamanhoMaximo(string, 15, info);
1✔
604
        }
605
    }
1✔
606

607
    public static void tamanho14N(final String string, final String info) {
608
        if (string != null) {
1✔
609
            DFStringValidador.apenasNumerico(string, info);
1✔
610
            DFStringValidador.validaTamanhoMaximo(string, 14, info);
1✔
611
        }
612
    }
1✔
613

614
    public static void tamanho4(final String string, final String info) {
615
        if (string != null) {
1✔
616
            DFStringValidador.validaTamanhoMaximo(string, 4, info);
1✔
617
        }
618
    }
1✔
619

620
    public static void tamanho4N(final String string, final String info) {
621
        if (string != null) {
1✔
622
            DFStringValidador.apenasNumerico(string, info);
1✔
623
            DFStringValidador.validaTamanhoMaximo(string, 4, info);
1✔
624
        }
625
    }
1✔
626

627
    public static void tamanho9N(final String string, final String info) {
628
        if (string != null) {
1✔
629
            DFStringValidador.apenasNumerico(string, info);
1✔
630
            DFStringValidador.validaTamanhoMaximo(string, 9, info);
1✔
631
        }
632
    }
1✔
633

634
    public static void tamanho2ou3N(final String string, final String info) {
635
        if (string != null) {
1✔
636
            DFStringValidador.apenasNumerico(string, info);
1✔
637
            DFStringValidador.intervalo(string, 2, 3, info);
1✔
638
        }
639
    }
1✔
640

641
    public static void tamanho3N(final String string, final String info) {
642
        if (string != null) {
1✔
643
            DFStringValidador.apenasNumerico(string, info);
1✔
644
            DFStringValidador.validaTamanhoMaximo(string, 3, info);
1✔
645
        }
646
    }
1✔
647

648
    public static void tamanho2N(final String string, final String info) {
649
        if (string != null) {
1✔
650
            DFStringValidador.apenasNumerico(string, info);
1✔
651
            DFStringValidador.validaTamanhoMaximo(string, 2, info);
1✔
652
        }
653
    }
1✔
654

655
    public static void exatamente20N(final String string, final String info) {
656
        if (string != null) {
1✔
657
            DFStringValidador.apenasNumerico(string, info);
1✔
658
            DFStringValidador.validaTamanhoExato(string, 20, info);
1✔
659
        }
660
    }
1✔
661

662
    public static void tamanho25N(final String string, final String info) {
UNCOV
663
        if (string != null) {
×
664
            DFStringValidador.apenasNumerico(string, info);
×
665
            DFStringValidador.validaTamanhoMaximo(string, 25, info);
×
666
        }
UNCOV
667
    }
×
668

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

703
    /**
704
     * @See #validador
705
     */
706
    public static String validador(final String paraValidar, final String info, final Integer tamanho, final Boolean exatamente) {
707
        return DFStringValidador.validador(paraValidar, info, tamanho, exatamente, null);
1✔
708
    }
709

710
    /**
711
     * @See #validador
712
     */
713
    public static String validador(final String paraValidar, final String info, final Integer tamanho) {
714
        return DFStringValidador.validador(paraValidar, info, tamanho, null, null);
1✔
715
    }
716

717
    public static void fci(final String numeroControleFCI) {
718
        if (numeroControleFCI != null) {
1✔
719
            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✔
720
            if (!matcher.find()) {
1✔
721
                throw new IllegalStateException(String.format("FCI fora do padrao (%s)", numeroControleFCI));
1✔
722
            }
723
        }
724
    }
1✔
725

726
    public static void ncm(final String ncm) {
727
        if (ncm != null) {
1✔
728
            final Matcher matcher = Pattern.compile("^([0-9]{2}|[0-9]{8})$").matcher(ncm);
1✔
729
            if (!matcher.find()) {
1✔
730
                throw new IllegalStateException(String.format("NCM fora do padrao (%s)", ncm));
1✔
731
            }
732
        }
733
    }
1✔
734

735
    private static void apenasNumerico(final String string, final String info) {
736
        if (string != null && !StringUtils.isNumeric(string)) {
1✔
737
            throw new IllegalStateException(String.format("A string %s precisa ser numerica (%s)", info, string));
1✔
738
        }
739
    }
1✔
740

741
    public static void validaTamanhoMaximo(final String string, final int tamanho, final String info) {
742
        if (string != null && (string.length() < 1 || string.length() > tamanho)) {
1✔
743
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre 1-%s caracteres", info, string, tamanho));
1✔
744
        }
745
    }
1✔
746

747
    private static void validaTamanhoExato(final String string, final int tamanho, final String info) {
748
        if (string != null && string.length() != tamanho) {
1✔
749
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir %s caracteres", info, string, tamanho));
1✔
750
        }
751
    }
1✔
752

753
    private static void intervalo(final String string, final int inicio, final int fim, final String info) {
754
        if (string != null && (string.length() < inicio || string.length() > fim)) {
1✔
755
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre %s-%s caracteres", info, string, inicio, fim));
1✔
756
        }
757
    }
1✔
758

759
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info) {
760
        return DFStringValidador.validaIntervalo(string, inicio, fim, info, false);
1✔
761
    }
762

763
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info, Boolean isNumeric) {
764
        if (string != null) {
1✔
765
            isNumeric = ObjectUtils.getIfNull(isNumeric, false);
1✔
766
            if (isNumeric) {
1✔
767
                DFStringValidador.apenasNumerico(string, info);
1✔
768
            }
769
            DFStringValidador.intervalo(string, inicio, fim, info);
1✔
770
        }
771
        return string;
1✔
772
    }
773

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

800
    public static void nve(final String nve) {
801
        if (nve != null) {
1✔
802
            final Matcher matcher = Pattern.compile("^[A-Z]{2}[0-9]{4}$").matcher(nve);
1✔
803
            if (!matcher.find()) {
1✔
804
                throw new IllegalStateException(String.format("NVE fora do padrao (%s)", nve));
1✔
805
            }
806
        }
807
    }
1✔
808

809
    public static void itemListaServico(final String itemListaServicos) {
810
        if (itemListaServicos != null) {
1✔
811
            final Matcher matcher = Pattern.compile("^\\d{2}\\.\\d{2}$").matcher(itemListaServicos);
1✔
812
            if (!matcher.find()) {
1✔
813
                throw new IllegalStateException(String.format("Item Lista de servico fora do padrao (%s)", itemListaServicos));
1✔
814
            }
815
        }
816
    }
1✔
817

818
    public static void exatamente54(final String string, final String info) {
819
        DFStringValidador.validaTamanhoExato(string, 54, info);
1✔
820
    }
1✔
821

822
    public static void exatamente55(final String string, final String info) {
UNCOV
823
        DFStringValidador.validaTamanhoExato(string, 55, info);
×
824
    }
×
825

826
    public static void exatamente15N(final String string, final String info) {
827
        DFStringValidador.validaTamanhoExato(string, 15, info);
1✔
828
        DFStringValidador.apenasNumerico(string, info);
1✔
829
    }
1✔
830

831
    public static void exatamente11N(final String string, final String info) {
832
        DFStringValidador.apenasNumerico(string, info);
1✔
833
        DFStringValidador.validaTamanhoExato(string, 11, info);
1✔
834
    }
1✔
835

836
    public static void modeloDocumentoFiscal(final String modeloDocumentoFiscal) {
837
        if (!modeloDocumentoFiscal.equals("55") && !modeloDocumentoFiscal.equals("65")) {
1✔
838
            throw new IllegalStateException(String.format("Modelo Fiscal Invalido (%s)", modeloDocumentoFiscal));
1✔
839
        }
840
    }
1✔
841

842
    public static void identificador(final String identificador) {
843
        final Matcher matcher = Pattern.compile("^ID\\d{41}$").matcher(identificador);
1✔
844
        if (!matcher.find()) {
1✔
845
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
1✔
846
        }
847
    }
1✔
848

849
    public static void identificadorCTe(final String identificador) {
UNCOV
850
        final Matcher matcher = Pattern.compile("^ID\\d{39}$").matcher(identificador);
×
851
        if (!matcher.find()) {
×
852
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
×
853
        }
UNCOV
854
    }
×
855

856
    public static void equals(final String test, final String tested) {
857
        if (!Strings.CS.equals(test, tested)) {
1✔
858
            throw new IllegalStateException(String.format("Valor('%s') nao corresponde com o padrao('%s')", tested, test));
1✔
859
        }
860
    }
1✔
861

862
    public static void isBase64(final String string, final String info) {
863
        if (!Base64.isBase64(string.getBytes())) {
1✔
UNCOV
864
            throw new IllegalStateException(String.format("A string %s com o valor = '%s' precisa ser codificada em Base64. ", info, string));
×
865
        }
866
    }
1✔
867

868
    /**
869
     * Validacao conforme nota tecnica 2019.001 Versao 1.00 – Abril de 2019
870
     */
871
    public static void validaCodigoRandomico(final String string, final String info) {
872
        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✔
873
        if (Strings.CS.containsAny(string, codigosInvalidos)) {
1✔
874
            throw new IllegalStateException(String.format("%s \"%s\" inválido", info, string));
1✔
875
        }
876
    }
1✔
877

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