Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package nfe; |
| 2 | |||
| 3 | import java.io.ByteArrayInputStream; |
||
| 4 | import java.io.File; |
||
| 5 | |||
| 6 | import javax.xml.parsers.DocumentBuilder; |
||
| 7 | import javax.xml.parsers.DocumentBuilderFactory; |
||
| 8 | import javax.xml.parsers.ParserConfigurationException; |
||
| 9 | |||
| 10 | import org.xml.sax.ErrorHandler; |
||
| 11 | import org.xml.sax.SAXException; |
||
| 12 | import org.xml.sax.SAXParseException; |
||
| 13 | |||
| 14 | import nfe.dom.ConfiguracoesIniciaisNfe; |
||
| 15 | import nfe.exception.NfeException; |
||
| 16 | import nfe.util.SchemaUtil; |
||
| 17 | |||
| 18 | class Validar implements ErrorHandler { |
||
| 19 | |||
| 20 | private static String xsd; |
||
| 21 | |||
| 22 | private String listaComErrosDeValidacao; |
||
| 23 | |||
| 24 | static final String STATUS = "status"; |
||
| 25 | static final String CONSULTA_XML = "consultaXml"; |
||
| 26 | static final String CONSULTA_CADASTRO = "br/inf/portalfiscal/nfe/schema/consCad"; |
||
| 27 | static final String ENVIO = "envio"; |
||
| 28 | static final String DIST_DFE = "destDfe"; |
||
| 29 | static final String INUTILIZACAO = "inutilizacao"; |
||
| 30 | static final String CANCELAR = "cancelar"; |
||
| 31 | static final String MANIFESTAR = "manifestar"; |
||
| 32 | static final String CCE = "br/inf/portalfiscal/nfe/schema/cce"; |
||
| 33 | static final String CONSULTA_RECIBO = "consultaRecibo"; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Construtor privado |
||
| 37 | */ |
||
| 38 | private Validar() { |
||
| 39 | this.listaComErrosDeValidacao = ""; |
||
| 40 | } |
||
| 41 | |||
| 42 | static String validaXml(String xml, String tipo) throws NfeException { |
||
| 43 | |||
| 44 | String errosValidacao = null; |
||
| 45 | |||
| 46 | ConfiguracoesIniciaisNfe configuracoesNfe = ConfiguracoesIniciaisNfe.getInstance(); |
||
| 47 | |||
| 48 | switch (tipo) { |
||
| 49 | case STATUS: |
||
| 50 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.STATUS; |
||
| 51 | break; |
||
| 52 | case ENVIO: |
||
| 53 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.ENVIO; |
||
| 54 | break; |
||
| 55 | case CONSULTA_XML: |
||
| 56 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.CONSULTA_XML; |
||
| 57 | break; |
||
| 58 | case CONSULTA_CADASTRO: |
||
| 59 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.CONSULTA_CADASTRO; |
||
| 60 | break; |
||
| 61 | case DIST_DFE: |
||
| 62 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.DIST_DFE; |
||
| 63 | break; |
||
| 64 | case INUTILIZACAO: |
||
| 65 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.INUTILIZACAO; |
||
| 66 | break; |
||
| 67 | case CANCELAR: |
||
| 68 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.CANCELAR; |
||
| 69 | break; |
||
| 70 | case CCE: |
||
| 71 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.CCE; |
||
| 72 | break; |
||
| 73 | case MANIFESTAR: |
||
| 74 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.MANIFESTAR; |
||
| 75 | break; |
||
| 76 | case CONSULTA_RECIBO: |
||
| 77 | xsd = configuracoesNfe.getPastaSchemas()+ "/" + SchemaUtil.CONSULTA_RECIBO; |
||
| 78 | break; |
||
| 79 | default: |
||
| 80 | break; |
||
| 81 | } |
||
| 82 | |||
| 83 | if(!new File(xsd).exists()){ |
||
| 84 | throw new NfeException("Schema Nfe não Localizado: "+xsd); |
||
| 85 | } |
||
| 86 | |||
| 87 | Validar validar= new Validar(); |
||
| 88 | |||
| 89 | errosValidacao = validar.validateXml(xml, xsd); |
||
| 90 | |||
| 91 | return errosValidacao; |
||
| 92 | } |
||
| 93 | |||
| 94 | private String validateXml(String xml, String xsd) throws NfeException { |
||
| 95 | |||
| 96 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
||
| 97 | factory.setNamespaceAware(true); |
||
| 98 | factory.setValidating(true); |
||
| 99 | factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); |
||
| 100 | factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd); |
||
| 101 | DocumentBuilder builder; |
||
| 102 | try { |
||
| 103 | builder = factory.newDocumentBuilder(); |
||
| 104 | builder.setErrorHandler(this); |
||
| 105 | } catch (ParserConfigurationException ex) { |
||
| 106 | throw new NfeException(ex.getMessage()); |
||
| 107 | } |
||
| 108 | |||
| 109 | try { |
||
| 110 | builder.parse(new ByteArrayInputStream(xml.getBytes())); |
||
| 111 | } catch (Exception ex) { |
||
| 112 | throw new NfeException(ex.toString()); |
||
| 113 | } |
||
| 114 | |||
| 115 | return this.getListaComErrosDeValidacao(); |
||
| 116 | } |
||
| 117 | |||
| 118 | public void error(SAXParseException exception) throws SAXException { |
||
| 119 | |||
| 120 | if (isError(exception)) { |
||
| 121 | listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage()); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | public void fatalError(SAXParseException exception) throws SAXException { |
||
| 126 | |||
| 127 | listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage()); |
||
| 128 | } |
||
| 129 | |||
| 130 | public void warning(SAXParseException exception) throws SAXException { |
||
| 131 | |||
| 132 | listaComErrosDeValidacao += tratamentoRetorno(exception.getMessage()); |
||
| 133 | } |
||
| 134 | |||
| 135 | private String tratamentoRetorno(String message) { |
||
| 136 | |||
| 137 | message = message.replaceAll("cvc-type.3.1.3:", "-"); |
||
| 138 | message = message.replaceAll("cvc-attribute.3:", "-"); |
||
| 139 | message = message.replaceAll("cvc-complex-type.2.4.a:", "-"); |
||
| 140 | message = message.replaceAll("cvc-complex-type.2.4.b:", "-"); |
||
| 141 | message = message.replaceAll("The value", "O valor"); |
||
| 142 | message = message.replaceAll("of element", "do campo"); |
||
| 143 | message = message.replaceAll("is not valid", "nao é valido"); |
||
| 144 | message = message.replaceAll("Invalid content was found starting with element", "Encontrado o campo"); |
||
| 145 | message = message.replaceAll("One of", "Campo(s)"); |
||
| 146 | message = message.replaceAll("is expected", "é obrigatorio"); |
||
| 147 | message = message.replaceAll("\\{", ""); |
||
| 148 | message = message.replaceAll("\\}", ""); |
||
| 149 | message = message.replaceAll("\"", ""); |
||
| 150 | message = message.replaceAll("http://www.portalfiscal.inf.br/nfe:", ""); |
||
| 151 | return System.getProperty("line.separator") + message.trim(); |
||
| 152 | } |
||
| 153 | |||
| 154 | private String getListaComErrosDeValidacao() { |
||
| 155 | |||
| 156 | return listaComErrosDeValidacao; |
||
| 157 | } |
||
| 158 | |||
| 159 | private boolean isError(SAXParseException exception) { |
||
| 160 | |||
| 161 | return !exception.getMessage().startsWith("cvc-enumeration-valid") && !exception.getMessage().startsWith("cvc-pattern-valid") && !exception.getMessage().startsWith("cvc-maxLength-valid") && !exception.getMessage().startsWith("cvc-datatype"); |
||
| 162 | } |
||
| 163 | |||
| 164 | } |