Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
414 espaco 1
package nfe;
2
 
3
import br.com.swconsultoria.nfe.dom.ConfiguracoesNfe;
4
import br.com.swconsultoria.nfe.dom.enuns.ServicosEnum;
5
import br.com.swconsultoria.nfe.exception.NfeException;
6
import br.com.swconsultoria.nfe.exception.NfeValidacaoException;
7
import br.com.swconsultoria.nfe.util.ObjetoUtil;
8
import org.xml.sax.ErrorHandler;
9
import org.xml.sax.InputSource;
10
import org.xml.sax.SAXParseException;
11
 
12
import javax.xml.parsers.DocumentBuilder;
13
import javax.xml.parsers.DocumentBuilderFactory;
14
import javax.xml.parsers.ParserConfigurationException;
15
import java.io.File;
16
import java.io.StringReader;
17
 
18
public class Validar implements ErrorHandler {
19
 
20
    private String listaComErrosDeValidacao = "";
21
 
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
    }
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 {
41
        System.setProperty("jdk.xml.maxOccurLimit", "99999");
42
        String errosValidacao;
43
 
44
        if (!new File(xsd).exists()) {
45
            throw new NfeException("Schema Nfe não Localizado: " + xsd);
46
        }
47
 
48
        errosValidacao = validateXml(xml, xsd);
49
        if (ObjetoUtil.verifica(errosValidacao).isPresent()) {
50
            throw new NfeValidacaoException("Erro na validação: " + errosValidacao);
51
        }
52
    }
53
 
54
    void validaXml(ConfiguracoesNfe config, String xml, ServicosEnum servico) throws NfeException {
55
        validaXml(config.getPastaSchemas() + System.getProperty("file.separator") + servico.getXsd(), xml);
56
    }
57
 
58
    private String validateXml(String xml, String xsd) throws NfeException {
59
 
60
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
61
        docBuilderFactory.setValidating(true);
62
        docBuilderFactory.setNamespaceAware(true);
63
        docBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
64
                "http://www.w3.org/2001/XMLSchema");
65
        docBuilderFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd);
66
        DocumentBuilder builder;
67
        try {
68
            builder = docBuilderFactory.newDocumentBuilder();
69
            builder.setErrorHandler(this);
70
        } catch (ParserConfigurationException ex) {
71
            throw new NfeException(ex.getMessage());
72
        }
73
 
74
        try {
75
            builder.parse(new InputSource(new StringReader(xml)));
76
        } catch (Exception ex) {
77
            throw new NfeException(ex.toString());
78
        }
79
 
80
        return this.getListaComErrosDeValidacao();
81
    }
82
 
83
    @Override
84
    public void error(SAXParseException exception) {
85
 
86
        if (isError(exception)) {
87
            listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage());
88
        }
89
    }
90
 
91
    @Override
92
    public void fatalError(SAXParseException exception) {
93
 
94
        listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage());
95
    }
96
 
97
    @Override
98
    public void warning(SAXParseException exception) {
99
 
100
        listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage());
101
    }
102
 
103
    private String tratamentoRetorno(String message) {
104
 
105
        message = message.replaceAll("cvc-type.3.1.3:", "-");
106
        message = message.replaceAll("cvc-attribute.3:", "-");
107
        message = message.replaceAll("cvc-complex-type.2.4.a:", "-");
108
        message = message.replaceAll("cvc-complex-type.2.4.b:", "-");
109
        message = message.replaceAll("cvc-complex-type.2.4.c:", "-");
110
        message = message.replaceAll("cvc-complex-type.2.4.d:", "-");
111
        message = message.replaceAll("cvc-complex-type.4:", "-");
112
        message = message.replaceAll("cvc-minLength-valid:", "-");
113
        message = message.replaceAll("The value", "O valor");
114
        message = message.replaceAll("Value", "Valor");
115
        message = message.replaceAll("with length", "com tamanho");
116
        message = message.replaceAll("is not facet-valid with respect to minLength", "não equivale ao tamanho mínimo");
117
        message = message.replaceAll("for type", "para o tipo");
118
        message = message.replaceAll("The content", "O conteúdo");
119
        message = message.replaceAll("of element", "do campo");
120
        message = message.replaceAll("is not complete", "não está completo");
121
        message = message.replaceAll("is not valid", "não é válido");
122
        message = message.replaceAll("Attribute", "Campo");
123
        message = message.replaceAll("must appear on element", "precisa estar em");
124
        message = message.replaceAll("Invalid content was found starting with element", "Conteúdo inválido encontrado iniciando com o campo");
125
        message = message.replaceAll("One of", "Um dos Campos");
126
        message = message.replaceAll("is expected", "é esperado");
127
        message = message.replaceAll("\\{", "");
128
        message = message.replaceAll("\\}", "");
129
        message = message.replaceAll("\"", "");
130
        message = message.replaceAll("http://www.portalfiscal.inf.br/nfe:", "");
131
        return System.getProperty("line.separator") + message.trim();
132
    }
133
 
134
    private String getListaComErrosDeValidacao() {
135
 
136
        return listaComErrosDeValidacao;
137
    }
138
 
139
    private boolean isError(SAXParseException exception) {
140
 
141
        return !exception.getMessage().startsWith("cvc-enumeration-valid")
142
                && !exception.getMessage().startsWith("cvc-pattern-valid")
143
                && !exception.getMessage().startsWith("cvc-maxLength-valid")
144
                && !exception.getMessage().startsWith("cvc-datatype");
145
    }
146
 
147
}