br.com.swconsultoria.mdfe.util.XmlMdfeUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-mdfe Show documentation
Show all versions of java-mdfe Show documentation
API Java para consumo do WebService de MDFe
The newest version!
/**
*
*/
package br.com.swconsultoria.mdfe.util;
import br.com.swconsultoria.mdfe.exception.MdfeException;
import br.com.swconsultoria.mdfe.schema_300.procMDFe.TMDFe;
import javax.xml.bind.*;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.GregorianCalendar;
import java.util.zip.GZIPInputStream;
/**
* Classe Responsavel por Metodos referentes ao XML
*
* @author Samuel Oliveira
*/
public class XmlMdfeUtil {
/**
* Transforma o String do XML em Objeto
*
* @param xml
* @param classe
* @return T
*/
public static T xmlToObject(String xml, Class classe) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(classe);
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(new StreamSource(new StringReader(xml)), classe).getValue();
}
/**
* Le o Arquivo XML e retona String
*
* @return String
* @throws MdfeException
*/
public static String leXml(String arquivo) throws MdfeException {
StringBuilder xml = new StringBuilder();
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(arquivo), "UTF-8"));
String linha;
while ((linha = in.readLine()) != null) {
xml.append(linha);
}
in.close();
} catch (IOException e) {
throw new MdfeException("Ler Xml: " + e.getMessage());
}
return xml.toString();
}
public static String objectMdfeToXml(Object obj) throws JAXBException, MdfeException {
JAXBContext context = null;
JAXBElement> element = null;
switch (obj.getClass().getSimpleName()) {
case ConstantesMDFe.XML.MDFE:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.enviMDFe.TMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.enviMDFe.ObjectFactory()
.createMdfe((br.com.swconsultoria.mdfe.schema_300.enviMDFe.TMDFe) obj);
break;
case ConstantesMDFe.XML.ENVIO_MDFE:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.enviMDFe.TEnviMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.enviMDFe.ObjectFactory()
.createEnviMDFe((br.com.swconsultoria.mdfe.schema_300.enviMDFe.TEnviMDFe) obj);
break;
case ConstantesMDFe.XML.STATUS_SERVICO:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.consStatServMDFe.TConsStatServ.class);
element = new br.com.swconsultoria.mdfe.schema_300.consStatServMDFe.ObjectFactory()
.createConsStatServMDFe((br.com.swconsultoria.mdfe.schema_300.consStatServMDFe.TConsStatServ) obj);
break;
case ConstantesMDFe.XML.CONSULTA_PROTOCOLO:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.consSitMDFe.TConsSitMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.consSitMDFe.ObjectFactory()
.createConsSitMDFe((br.com.swconsultoria.mdfe.schema_300.consSitMDFe.TConsSitMDFe) obj);
break;
case ConstantesMDFe.XML.CONSULTA_RECIBO:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.consReciMDFe.TConsReciMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.consReciMDFe.ObjectFactory()
.createConsReciMDFe((br.com.swconsultoria.mdfe.schema_300.consReciMDFe.TConsReciMDFe) obj);
break;
case ConstantesMDFe.XML.CONSULTA_NAO_ENCERRADO:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.consMDFeNaoEnc.TConsMDFeNaoEnc.class);
element = new br.com.swconsultoria.mdfe.schema_300.consMDFeNaoEnc.ObjectFactory()
.createConsMDFeNaoEnc((br.com.swconsultoria.mdfe.schema_300.consMDFeNaoEnc.TConsMDFeNaoEnc) obj);
break;
case ConstantesMDFe.XML.PROC_MDFE:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.procMDFe.MdfeProc.class);
element = new br.com.swconsultoria.mdfe.schema_300.procMDFe.ObjectFactory()
.createMdfeProc((br.com.swconsultoria.mdfe.schema_300.procMDFe.MdfeProc) obj);
break;
case ConstantesMDFe.XML.PROT_MDFE:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.retConsReciMDFe.TProtMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.retConsReciMDFe.ObjectFactory()
.createTProtMDFe((br.com.swconsultoria.mdfe.schema_300.retConsReciMDFe.TProtMDFe) obj);
break;
case ConstantesMDFe.XML.RET_ENVIMDFE:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.retEnviMDFe.TRetEnviMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.retEnviMDFe.ObjectFactory()
.createRetEnviMDFe((br.com.swconsultoria.mdfe.schema_300.retEnviMDFe.TRetEnviMDFe) obj);
break;
case ConstantesMDFe.XML.RET_CONSULTA_RECIBO:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.retConsReciMDFe.TRetConsReciMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.retConsReciMDFe.ObjectFactory()
.createRetConsReciMDFe((br.com.swconsultoria.mdfe.schema_300.retConsReciMDFe.TRetConsReciMDFe) obj);
break;
case ConstantesMDFe.XML.RET_CONSULTA_NAO_ENCERRADO:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.retConsMDFeNaoEnc.TRetConsMDFeNaoEnc.class);
element = new br.com.swconsultoria.mdfe.schema_300.retConsMDFeNaoEnc.ObjectFactory()
.createRetConsMDFeNaoEnc((br.com.swconsultoria.mdfe.schema_300.retConsMDFeNaoEnc.TRetConsMDFeNaoEnc) obj);
break;
case ConstantesMDFe.XML.RET_CONSULTA_PROTOCOLO:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.retConsSitMDFe.TRetConsSitMDFe.class);
element = new br.com.swconsultoria.mdfe.schema_300.retConsSitMDFe.ObjectFactory()
.createRetConsSitMDFe((br.com.swconsultoria.mdfe.schema_300.retConsSitMDFe.TRetConsSitMDFe) obj);
break;
case ConstantesMDFe.XML.EVENTO:
switch (obj.getClass().getName()) {
case ConstantesMDFe.XML.CANCELAR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TEvento.class);
element = new br.com.swconsultoria.mdfe.schema_300.evCancMDFe.ObjectFactory()
.createTEvento((br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TEvento) obj);
break;
case ConstantesMDFe.XML.ENCERRAR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TEvento.class);
element = new br.com.swconsultoria.mdfe.schema_300.evEncMDFe.ObjectFactory()
.createTEvento((br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TEvento) obj);
break;
case ConstantesMDFe.XML.INCLUIR_CONDUTOR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TEvento.class);
element = new br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.ObjectFactory()
.createTEvento((br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TEvento) obj);
break;
}
break;
case ConstantesMDFe.XML.RET_EVENTO:
switch (obj.getClass().getName()) {
case ConstantesMDFe.XML.RET_EVENTO_CANCELAR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TRetEvento.class);
element = XsdUtil.retEvento.createTRetEvent((br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TRetEvento) obj);
break;
case ConstantesMDFe.XML.RET_EVENTO_CONDUTOR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TRetEvento.class);
element = XsdUtil.retEvento.createTRetEvent((br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TRetEvento) obj);
break;
case ConstantesMDFe.XML.RET_EVENTO_ENCERRAR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TRetEvento.class);
element = XsdUtil.retEvento.createTRetEvent((br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TRetEvento) obj);
break;
}
break;
case ConstantesMDFe.XML.PROC_EVENTO:
switch (obj.getClass().getName()) {
case ConstantesMDFe.XML.PROC_CANCELAR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TProcEvento.class);
element = new br.com.swconsultoria.mdfe.schema_300.evCancMDFe.ObjectFactory()
.createTProcEvento((br.com.swconsultoria.mdfe.schema_300.evCancMDFe.TProcEvento) obj);
break;
case ConstantesMDFe.XML.PROC_ENCERRAR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TProcEvento.class);
element = new br.com.swconsultoria.mdfe.schema_300.evEncMDFe.ObjectFactory()
.createTProcEvento((br.com.swconsultoria.mdfe.schema_300.evEncMDFe.TProcEvento) obj);
break;
case ConstantesMDFe.XML.PROC_INCLUIR_CONDUTOR:
context = JAXBContext.newInstance(br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TProcEvento.class);
element = new br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.ObjectFactory()
.createTProcEvento((br.com.swconsultoria.mdfe.schema_300.evIncCondutorMDF.TProcEvento) obj);
break;
default:
throw new MdfeException("Objeto não mapeado no XmlMdfeUtil:" + obj.getClass().getName());
}
break;
default:
throw new MdfeException("Objeto não mapeado no XmlMdfeUtil:" + obj.getClass().getName());
}
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.encoding", "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
StringWriter sw = new StringWriter();
marshaller.marshal(element, sw);
return replacesMdfe("" + sw.toString());
}
public static String gZipToXml(byte[] conteudo) throws IOException {
if (conteudo == null || conteudo.length == 0) {
return "";
}
GZIPInputStream gis;
gis = new GZIPInputStream(new ByteArrayInputStream(conteudo));
BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
StringBuilder outStr = new StringBuilder();
String line;
while ((line = bf.readLine()) != null) {
outStr.append(line);
}
return outStr.toString();
}
public static String criaMdfeProc(br.com.swconsultoria.mdfe.schema_300.enviMDFe.TEnviMDFe enviMdfe, Object retorno) throws JAXBException, MdfeException {
br.com.swconsultoria.mdfe.schema_300.procMDFe.MdfeProc mdfeProc = new br.com.swconsultoria.mdfe.schema_300.procMDFe.MdfeProc();
mdfeProc.setVersao("3.00");
mdfeProc.setMDFe(xmlToObject(objectMdfeToXml(enviMdfe.getMDFe()), TMDFe.class));
mdfeProc.setProtMDFe(xmlToObject(objectMdfeToXml(retorno), br.com.swconsultoria.mdfe.schema_300.procMDFe.TProtMDFe.class));
return objectMdfeToXml(mdfeProc);
}
private static String replacesMdfe(String xml) {
xml = xml.replaceAll("ns2:", "");
xml = xml.replaceAll("ns3:", "");
xml = xml.replaceAll("<", "<");
xml = xml.replaceAll(">", ">");
xml = xml.replaceAll("", "");
xml = xml.replaceAll(" xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\"", "");
xml = xml.replaceAll(" xmlns:ns3=\"http://www.portalfiscal.inf.br/mdfe\"", "");
xml = xml.replaceAll(" xmlns:ns2=\"http://www.portalfiscal.inf.br/mdfe\"", "");
xml = xml.replaceAll(" xmlns=\"\"", "");
return xml;
}
public static String dataMDFe(LocalDateTime data) throws MdfeException {
XMLGregorianCalendar xmlCalendar;
try {
GregorianCalendar calendar = GregorianCalendar.from(data.atZone(ZoneId.of("Brazil/East")));
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
xmlCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
} catch (DatatypeConfigurationException e) {
throw new MdfeException(e.getMessage());
}
return (xmlCalendar.toString());
}
public static int modulo11(String chave) {
int total = 0;
int peso = 2;
for (int i = 0; i < chave.length(); i++) {
total += (chave.charAt((chave.length() - 1) - i) - '0') * peso;
peso++;
if (peso == 10)
peso = 2;
}
int resto = total % 11;
return (resto == 0 || resto == 1) ? 0 : (11 - resto);
}
}