Skip to content

Commit

Permalink
Merge pull request #264 from evertonramos/master
Browse files Browse the repository at this point in the history
Pequenas modificações no Validar
  • Loading branch information
Samuel-Oliveira authored Apr 25, 2023
2 parents 9789318 + 9a895ff commit 0628a17
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
34 changes: 29 additions & 5 deletions src/main/java/br/com/swconsultoria/nfe/Validar.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,44 @@
import java.io.File;
import java.io.StringReader;

class Validar implements ErrorHandler {
public class Validar implements ErrorHandler {

private String listaComErrosDeValidacao = "";

void validaXml(ConfiguracoesNfe config, String xml, ServicosEnum servico) throws NfeException {
public boolean isValidXml(String pastaSchemas, String xml, ServicosEnum servico) {
return isValidXml(pastaSchemas + System.getProperty("file.separator") + servico.getXsd(), xml);
}

public boolean isValidXml(ConfiguracoesNfe config, String xml, ServicosEnum servico) {
return isValidXml(config.getPastaSchemas(), xml, servico);
}

public boolean isValidXml(String xsd, String xml) {
try {
validaXml(xsd, xml);

return true;
} catch (NfeException ex) {
return false;
}
}

void validaXml(String xsd, String xml) throws NfeException {
System.setProperty("jdk.xml.maxOccurLimit", "99999");
String errosValidacao;
String xsd = config.getPastaSchemas() + System.getProperty("file.separator") + servico.getXsd();

if (!new File(xsd).exists()) {
throw new NfeException("Schema Nfe não Localizado: " + xsd);
}

errosValidacao = validateXml(xml, xsd);
if(ObjetoUtil.verifica(errosValidacao).isPresent())
throw new NfeValidacaoException("Erro na validação: " + errosValidacao);
if (ObjetoUtil.verifica(errosValidacao).isPresent()) {
throw new NfeValidacaoException("Erro na validação: " + errosValidacao);
}
}

void validaXml(ConfiguracoesNfe config, String xml, ServicosEnum servico) throws NfeException {
validaXml(config.getPastaSchemas() + System.getProperty("file.separator") + servico.getXsd(), xml);
}

private String validateXml(String xml, String xsd) throws NfeException {
Expand Down Expand Up @@ -59,18 +80,21 @@ private String validateXml(String xml, String xsd) throws NfeException {
return this.getListaComErrosDeValidacao();
}

@Override
public void error(SAXParseException exception) {

if (isError(exception)) {
listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage());
}
}

@Override
public void fatalError(SAXParseException exception) {

listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage());
}

@Override
public void warning(SAXParseException exception) {

listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum ServicosEnum {
CONSULTA_XML("nfeconsultaprotocolo_4.00", "consSitNFe_v4.00.xsd"),
DISTRIBUICAO_DFE("nfedistribuicaodfe_1.01", "distDFeInt_v1.01.xsd"),
URL_QRCODE("url-qrcode",null),
URL_CONSULTANFCE("url-consultanfce",null);
URL_CONSULTANFCE("url-consultanfce",null),
PROC("procNFe_v4.00","procNFe_v4.00.xsd");

private final String servico;
private final String xsd;
Expand Down
Loading

0 comments on commit 0628a17

Please sign in to comment.