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

wmixvideo / nfe / #6269

27 Mar 2025 11:03AM UTC coverage: 52.718% (+0.05%) from 52.671%
#6269

push

web-flow
Merge pull request #4 from dennnisk/4.0.80-SNAPSHOT-JJW

Correções para compilar com o jdk 1.8 e publicar em repositorio interno

14002 of 26560 relevant lines covered (52.72%)

0.53 hits per line

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

94.74
/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 codigoProdutoAnvisa(final String string, final String info) {
331
        if (string != null) {
1✔
332
            if (string.toUpperCase().matches("[A-Z]*")) {
1✔
333
                if (!Objects.equals(string.toUpperCase(), "ISENTO")) {
1✔
334
                    throw new IllegalStateException(String.format("C\u00f3digo produto anvisa (%s) diferente de ISENTO", string));
×
335
                }
336
            } else {
337
                if (string.length() <= 11) {
1✔
338
                    DFStringValidador.validaTamanhoExato(string, 11, info);
1✔
339
                } else {
340
                    DFStringValidador.validaTamanhoExato(string, 13, info);
1✔
341
                }
342
            }
343
        }
344
    }
1✔
345

346
    public static void tamanho15(final String string, final String info) {
347
        if (string != null) {
1✔
348
            DFStringValidador.validaTamanhoMaximo(string, 15, info);
1✔
349
        }
350
    }
1✔
351

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

358
    public static void tamanho12N(final String string, final String info) {
359
        if (string != null) {
×
360
            DFStringValidador.apenasNumerico(string, info);
×
361
            DFStringValidador.validaTamanhoMaximo(string, 12, info);
×
362
        }
363
    }
×
364

365
    public static void tamanho120(final String string, final String info) {
366
        if (string != null) {
1✔
367
            DFStringValidador.validaTamanhoMaximo(string, 120, info);
1✔
368
        }
369
    }
1✔
370

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

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

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

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

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

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

408
    public static void tamanho6N(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 tamanho500(final String string, final String info) {
415
        if (string != null) {
1✔
416
            DFStringValidador.validaTamanhoMaximo(string, 500, info);
1✔
417
        }
418
    }
1✔
419

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

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

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

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

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

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

458
    public static void tamanho8a9(final String string, final String info) {
459
        if (string != null) {
1✔
460
            DFStringValidador.intervalo(string, 8, 9, info);
1✔
461
        }
462
    }
1✔
463

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

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

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

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

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

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

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

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

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

520
    public static void tamanho15a1000(final String string, final String info) {
521
        if (string != null) {
1✔
522
            DFStringValidador.intervalo(string, 15, 1000, info);
1✔
523
        }
524
    }
1✔
525

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

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

538
    public static void tamanho30(final String string, final String info) {
539
        if (string != null) {
1✔
540
            DFStringValidador.validaTamanhoMaximo(string, 30, info);
1✔
541
        }
542
    }
1✔
543

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

550
    public static void exatamente7N(final String string, final String info) {
551
        if (string != null) {
1✔
552
            DFStringValidador.apenasNumerico(string, info);
1✔
553
            DFStringValidador.exatamente7(string, info);
1✔
554
        }
555
    }
1✔
556

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

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

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

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

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

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

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

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

612
    public static void tamanho2ou3N(final String string, final String info) {
613
        if (string != null) {
1✔
614
            DFStringValidador.apenasNumerico(string, info);
1✔
615
            DFStringValidador.intervalo(string, 2, 3, info);
1✔
616
        }
617
    }
1✔
618

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

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

633
    public static void exatamente20N(final String string, final String info) {
634
        if (string != null) {
1✔
635
            DFStringValidador.apenasNumerico(string, info);
1✔
636
            DFStringValidador.validaTamanhoExato(string, 20, info);
1✔
637
        }
638
    }
1✔
639

640
    public static void tamanho25N(final String string, final String info) {
641
        if (string != null) {
×
642
            DFStringValidador.apenasNumerico(string, info);
×
643
            DFStringValidador.validaTamanhoMaximo(string, 25, info);
×
644
        }
645
    }
×
646

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

681
    /**
682
     * @See #validador
683
     */
684
    public static String validador(final String paraValidar, final String info, final Integer tamanho, final Boolean exatamente) {
685
        return DFStringValidador.validador(paraValidar, info, tamanho, exatamente, null);
1✔
686
    }
687

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

695
    public static void fci(final String numeroControleFCI) {
696
        if (numeroControleFCI != null) {
1✔
697
            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✔
698
            if (!matcher.find()) {
1✔
699
                throw new IllegalStateException(String.format("FCI fora do padrao (%s)", numeroControleFCI));
1✔
700
            }
701
        }
702
    }
1✔
703

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

713
    private static void apenasNumerico(final String string, final String info) {
714
        if (string != null && !StringUtils.isNumeric(string)) {
1✔
715
            throw new IllegalStateException(String.format("A string %s precisa ser numerica (%s)", info, string));
1✔
716
        }
717
    }
1✔
718

719
    public static void validaTamanhoMaximo(final String string, final int tamanho, final String info) {
720
        if (string != null && (string.length() < 1 || string.length() > tamanho)) {
1✔
721
            throw new IllegalStateException(String.format("%s \"%s\" deve possuir entre 1-%s caracteres", info, string, tamanho));
1✔
722
        }
723
    }
1✔
724

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

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

737
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info) {
738
        return DFStringValidador.validaIntervalo(string, inicio, fim, info, false);
1✔
739
    }
740

741
    public static String validaIntervalo(final String string, final int inicio, final int fim, final String info, Boolean isNumeric) {
742
        if (string != null) {
1✔
743
            isNumeric = ObjectUtils.defaultIfNull(isNumeric, false);
1✔
744
            if (isNumeric) {
1✔
745
                DFStringValidador.apenasNumerico(string, info);
1✔
746
            }
747
            DFStringValidador.intervalo(string, inicio, fim, info);
1✔
748
        }
749
        return string;
1✔
750
    }
751

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

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

787
    public static void itemListaServico(final String itemListaServicos) {
788
        if (itemListaServicos != null) {
1✔
789
            final Matcher matcher = Pattern.compile("^\\d{2}\\.\\d{2}$").matcher(itemListaServicos);
1✔
790
            if (!matcher.find()) {
1✔
791
                throw new IllegalStateException(String.format("Item Lista de servico fora do padrao (%s)", itemListaServicos));
1✔
792
            }
793
        }
794
    }
1✔
795

796
    public static void exatamente54(final String string, final String info) {
797
        DFStringValidador.validaTamanhoExato(string, 54, info);
1✔
798
    }
1✔
799

800
    public static void exatamente55(final String string, final String info) {
801
        DFStringValidador.validaTamanhoExato(string, 55, info);
×
802
    }
×
803

804
    public static void exatamente15N(final String string, final String info) {
805
        DFStringValidador.validaTamanhoExato(string, 15, info);
1✔
806
        DFStringValidador.apenasNumerico(string, info);
1✔
807
    }
1✔
808

809
    public static void exatamente11N(final String string, final String info) {
810
        DFStringValidador.apenasNumerico(string, info);
1✔
811
        DFStringValidador.validaTamanhoExato(string, 11, info);
1✔
812
    }
1✔
813

814
    public static void modeloDocumentoFiscal(final String modeloDocumentoFiscal) {
815
        if (!modeloDocumentoFiscal.equals("55") && !modeloDocumentoFiscal.equals("65")) {
1✔
816
            throw new IllegalStateException(String.format("Modelo Fiscal Invalido (%s)", modeloDocumentoFiscal));
1✔
817
        }
818
    }
1✔
819

820
    public static void identificador(final String identificador) {
821
        final Matcher matcher = Pattern.compile("^ID\\d{41}$").matcher(identificador);
1✔
822
        if (!matcher.find()) {
1✔
823
            throw new IllegalStateException(String.format("Identificador fora do padrao (%s)", identificador));
1✔
824
        }
825
    }
1✔
826

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

834
    public static void equals(final String test, final String tested) {
835
        if (!StringUtils.equals(test, tested)) {
1✔
836
            throw new IllegalStateException(String.format("Valor('%s') nao corresponde com o padrao('%s')", tested, test));
1✔
837
        }
838
    }
1✔
839

840
    public static void isBase64(final String string, final String info) {
841
        if (!Base64.isArrayByteBase64(string.getBytes())) {
1✔
842
            throw new IllegalStateException(String.format("A string %s com o valor = '%s' precisa ser codificada em Base64. ", info, string));
×
843
        }
844
    }
1✔
845

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

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