|
15 | 15 | import java.io.File; |
16 | 16 | import java.io.StringReader; |
17 | 17 |
|
18 | | -class Validar implements ErrorHandler { |
| 18 | +public class Validar implements ErrorHandler { |
19 | 19 |
|
20 | 20 | private String listaComErrosDeValidacao = ""; |
21 | 21 |
|
22 | | - void validaXml(ConfiguracoesNfe config, String xml, ServicosEnum servico) throws NfeException { |
| 22 | + public boolean isValidXml(String pastaSchemas, String xml, ServicosEnum servico) { |
| 23 | + return isValidXml(pastaSchemas + System.getProperty("file.separator") + servico.getXsd(), xml); |
| 24 | + } |
| 25 | + |
| 26 | + public boolean isValidXml(ConfiguracoesNfe config, String xml, ServicosEnum servico) { |
| 27 | + return isValidXml(config.getPastaSchemas(), xml, servico); |
| 28 | + } |
23 | 29 |
|
| 30 | + public boolean isValidXml(String xsd, String xml) { |
| 31 | + try { |
| 32 | + validaXml(xsd, xml); |
| 33 | + |
| 34 | + return true; |
| 35 | + } catch (NfeException ex) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + void validaXml(String xsd, String xml) throws NfeException { |
24 | 41 | System.setProperty("jdk.xml.maxOccurLimit", "99999"); |
25 | 42 | String errosValidacao; |
26 | | - String xsd = config.getPastaSchemas() + System.getProperty("file.separator") + servico.getXsd(); |
| 43 | + |
27 | 44 | if (!new File(xsd).exists()) { |
28 | 45 | throw new NfeException("Schema Nfe não Localizado: " + xsd); |
29 | 46 | } |
30 | 47 |
|
31 | 48 | errosValidacao = validateXml(xml, xsd); |
32 | | - if(ObjetoUtil.verifica(errosValidacao).isPresent()) |
33 | | - throw new NfeValidacaoException("Erro na validação: " + errosValidacao); |
| 49 | + if (ObjetoUtil.verifica(errosValidacao).isPresent()) { |
| 50 | + throw new NfeValidacaoException("Erro na validação: " + errosValidacao); |
| 51 | + } |
| 52 | + } |
34 | 53 |
|
| 54 | + void validaXml(ConfiguracoesNfe config, String xml, ServicosEnum servico) throws NfeException { |
| 55 | + validaXml(config.getPastaSchemas() + System.getProperty("file.separator") + servico.getXsd(), xml); |
35 | 56 | } |
36 | 57 |
|
37 | 58 | private String validateXml(String xml, String xsd) throws NfeException { |
@@ -59,18 +80,21 @@ private String validateXml(String xml, String xsd) throws NfeException { |
59 | 80 | return this.getListaComErrosDeValidacao(); |
60 | 81 | } |
61 | 82 |
|
| 83 | + @Override |
62 | 84 | public void error(SAXParseException exception) { |
63 | 85 |
|
64 | 86 | if (isError(exception)) { |
65 | 87 | listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage()); |
66 | 88 | } |
67 | 89 | } |
68 | 90 |
|
| 91 | + @Override |
69 | 92 | public void fatalError(SAXParseException exception) { |
70 | 93 |
|
71 | 94 | listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage()); |
72 | 95 | } |
73 | 96 |
|
| 97 | + @Override |
74 | 98 | public void warning(SAXParseException exception) { |
75 | 99 |
|
76 | 100 | listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage()); |
|
0 commit comments