Rev 414 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 414 | espaco | 1 | /** |
| 2 | * |
||
| 3 | */ |
||
| 415 | espaco | 4 | package nfe.util; |
| 414 | espaco | 5 | |
| 6 | import br.com.swconsultoria.nfe.exception.NfeException; |
||
| 7 | import br.com.swconsultoria.nfe.schema.consCad.TConsCad; |
||
| 8 | import br.com.swconsultoria.nfe.schema.distdfeint.DistDFeInt; |
||
| 9 | import br.com.swconsultoria.nfe.schema.envEventoCancNFe.TEnvEvento; |
||
| 10 | import br.com.swconsultoria.nfe.schema.envEventoCancNFe.TRetEnvEvento; |
||
| 11 | import br.com.swconsultoria.nfe.schema_4.consReciNFe.TConsReciNFe; |
||
| 12 | import br.com.swconsultoria.nfe.schema_4.consSitNFe.TConsSitNFe; |
||
| 13 | import br.com.swconsultoria.nfe.schema_4.consStatServ.TConsStatServ; |
||
| 14 | import br.com.swconsultoria.nfe.schema_4.enviNFe.*; |
||
| 15 | import br.com.swconsultoria.nfe.schema_4.inutNFe.TInutNFe; |
||
| 16 | import br.com.swconsultoria.nfe.schema_4.inutNFe.TProcInutNFe; |
||
| 17 | import br.com.swconsultoria.nfe.schema_4.inutNFe.TRetInutNFe; |
||
| 18 | import br.com.swconsultoria.nfe.schema_4.retConsSitNFe.TRetConsSitNFe; |
||
| 19 | import lombok.extern.java.Log; |
||
| 20 | |||
| 21 | import javax.xml.bind.*; |
||
| 22 | import javax.xml.datatype.DatatypeConfigurationException; |
||
| 23 | import javax.xml.datatype.DatatypeConstants; |
||
| 24 | import javax.xml.datatype.DatatypeFactory; |
||
| 25 | import javax.xml.datatype.XMLGregorianCalendar; |
||
| 26 | import javax.xml.namespace.QName; |
||
| 27 | import javax.xml.transform.stream.StreamSource; |
||
| 28 | import java.io.*; |
||
| 29 | import java.nio.charset.Charset; |
||
| 30 | import java.nio.file.Files; |
||
| 31 | import java.nio.file.Paths; |
||
| 32 | import java.security.InvalidParameterException; |
||
| 33 | import java.security.MessageDigest; |
||
| 34 | import java.security.NoSuchAlgorithmException; |
||
| 35 | import java.time.LocalDateTime; |
||
| 36 | import java.time.ZoneId; |
||
| 37 | import java.util.Base64; |
||
| 38 | import java.util.GregorianCalendar; |
||
| 39 | import java.util.List; |
||
| 40 | import java.util.StringJoiner; |
||
| 415 | espaco | 41 | import java.util.logging.Logger; |
| 414 | espaco | 42 | import java.util.zip.GZIPInputStream; |
| 43 | |||
| 44 | /** |
||
| 45 | * Classe Responsavel por Metodos referentes ao XML |
||
| 46 | * |
||
| 47 | * @author Samuel Oliveira |
||
| 48 | */ |
||
| 49 | @Log |
||
| 50 | public class XmlNfeUtil { |
||
| 51 | |||
| 52 | private static final String STATUS = "TConsStatServ"; |
||
| 53 | private static final String SITUACAO_NFE = "TConsSitNFe"; |
||
| 54 | private static final String ENVIO_NFE = "TEnviNFe"; |
||
| 55 | private static final String DIST_DFE = "DistDFeInt"; |
||
| 56 | private static final String INUTILIZACAO = "TInutNFe"; |
||
| 57 | private static final String NFEPROC = "TNfeProc"; |
||
| 58 | private static final String NFE = "TNFe"; |
||
| 59 | private static final String EVENTO = "TEnvEvento"; |
||
| 60 | private static final String TPROCEVENTO = "TProcEvento"; |
||
| 61 | private static final String TCONSRECINFE = "TConsReciNFe"; |
||
| 62 | private static final String TCONS_CAD = "TConsCad"; |
||
| 63 | private static final String TPROCINUT = "TProcInutNFe"; |
||
| 64 | private static final String RETORNO_ENVIO = "TRetEnviNFe"; |
||
| 65 | private static final String SITUACAO_NFE_RET = "TRetConsSitNFe"; |
||
| 66 | private static final String RET_RECIBO_NFE = "TRetConsReciNFe"; |
||
| 67 | private static final String RET_STATUS_SERVICO = "TRetConsStatServ"; |
||
| 68 | private static final String RET_CONS_CAD = "TRetConsCad"; |
||
| 69 | |||
| 70 | private static final String RET_ENV_EVENTO = "TRetEnvEvento"; |
||
| 71 | |||
| 72 | private static final String RET_INUT_NFE = "TRetInutNFe"; |
||
| 73 | |||
| 74 | private static final String TPROCCANCELAR = "br.com.swconsultoria.nfe.schema.envEventoCancNFe.TProcEvento"; |
||
| 75 | private static final String TPROCATORINTERESSADO = "br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TProcEvento"; |
||
| 76 | private static final String TPROCCANCELARSUBST = "br.com.swconsultoria.nfe.schema.envEventoCancSubst.TProcEvento"; |
||
| 77 | private static final String TPROCCCE = "br.com.swconsultoria.nfe.schema.envcce.TProcEvento"; |
||
| 78 | private static final String TPROCEPEC = "br.com.swconsultoria.nfe.schema.envEpec.TProcEvento"; |
||
| 79 | private static final String TPROCMAN = "br.com.swconsultoria.nfe.schema.envConfRecebto.TProcEvento"; |
||
| 80 | |||
| 81 | private static final String TProtNFe = "TProtNFe"; |
||
| 82 | private static final String TProtEnvi = "br.com.swconsultoria.nfe.schema_4.enviNFe.TProtNFe"; |
||
| 83 | private static final String TProtCons = "br.com.swconsultoria.nfe.schema_4.retConsSitNFe.TProtNFe"; |
||
| 84 | private static final String TProtReci = "br.com.swconsultoria.nfe.schema_4.retConsReciNFe.TProtNFe"; |
||
| 85 | |||
| 86 | private static final String CANCELAR = "br.com.swconsultoria.nfe.schema.envEventoCancNFe.TEnvEvento"; |
||
| 87 | private static final String ATOR_INTERESSADO = "br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TEnvEvento"; |
||
| 88 | private static final String CANCELAR_SUBSTITUICAO = "br.com.swconsultoria.nfe.schema.envEventoCancSubst.TEnvEvento"; |
||
| 89 | private static final String CCE = "br.com.swconsultoria.nfe.schema.envcce.TEnvEvento"; |
||
| 90 | private static final String EPEC = "br.com.swconsultoria.nfe.schema.envEpec.TEnvEvento"; |
||
| 91 | private static final String MANIFESTAR = "br.com.swconsultoria.nfe.schema.envConfRecebto.TEnvEvento"; |
||
| 92 | |||
| 93 | private static final String RET_CANCELAR = "br.com.swconsultoria.nfe.schema.envEventoCancNFe.TRetEnvEvento"; |
||
| 94 | private static final String RET_ATOR_INTERESSADO = "br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TRetEnvEvento"; |
||
| 95 | private static final String RET_CANCELAR_SUBSTITUICAO = "br.com.swconsultoria.nfe.schema.envEventoCancSubst.TRetEnvEvento"; |
||
| 96 | private static final String RET_CCE = "br.com.swconsultoria.nfe.schema.envcce.TRetEnvEvento"; |
||
| 97 | private static final String RET_EPEC = "br.com.swconsultoria.nfe.schema.envEpec.TRetEnvEvento"; |
||
| 98 | private static final String RET_MANIFESTAR = "br.com.swconsultoria.nfe.schema.envConfRecebto.TRetEnvEvento"; |
||
| 415 | espaco | 99 | |
| 100 | private final static Logger logger = Logger.getLogger(WebServiceUtil.class.getName()); |
||
| 414 | espaco | 101 | |
| 102 | /** |
||
| 103 | * Transforma o String do XML em Objeto |
||
| 104 | * |
||
| 105 | * @param xml |
||
| 106 | * @param classe |
||
| 107 | * @return T |
||
| 108 | */ |
||
| 109 | public static <T> T xmlToObject(String xml, Class<T> classe) throws JAXBException { |
||
| 110 | return JAXB.unmarshal(new StreamSource(new StringReader(xml)), classe); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Transforma o Objeto em XML(String) |
||
| 115 | * |
||
| 116 | * @param obj |
||
| 117 | * @return |
||
| 118 | * @throws JAXBException |
||
| 119 | * @throws NfeException |
||
| 120 | */ |
||
| 121 | public static <T> String objectToXml(Object obj) throws JAXBException, NfeException { |
||
| 122 | return objectToXml(obj, Charset.forName("UTF-8")); |
||
| 123 | } |
||
| 124 | |||
| 125 | public static <T> String objectToXml(Object obj, Charset encode) throws JAXBException, NfeException { |
||
| 126 | |||
| 127 | JAXBContext context; |
||
| 128 | JAXBElement<?> element; |
||
| 129 | |||
| 130 | switch (obj.getClass().getSimpleName()) { |
||
| 131 | |||
| 132 | case STATUS: |
||
| 133 | context = JAXBContext.newInstance(TConsStatServ.class); |
||
| 134 | element = new br.com.swconsultoria.nfe.schema_4.consStatServ.ObjectFactory().createConsStatServ((TConsStatServ) obj); |
||
| 135 | break; |
||
| 136 | |||
| 137 | case ENVIO_NFE: |
||
| 138 | context = JAXBContext.newInstance(TEnviNFe.class); |
||
| 139 | element = new br.com.swconsultoria.nfe.schema_4.enviNFe.ObjectFactory().createEnviNFe((TEnviNFe) obj); |
||
| 140 | break; |
||
| 141 | |||
| 142 | case RETORNO_ENVIO: |
||
| 143 | context = JAXBContext.newInstance(TRetEnviNFe.class); |
||
| 144 | element = XsdUtil.enviNfe.createTRetEnviNFe((TRetEnviNFe) obj); |
||
| 145 | break; |
||
| 146 | |||
| 147 | case SITUACAO_NFE: |
||
| 148 | context = JAXBContext.newInstance(TConsSitNFe.class); |
||
| 149 | element = new br.com.swconsultoria.nfe.schema_4.consSitNFe.ObjectFactory().createConsSitNFe((TConsSitNFe) obj); |
||
| 150 | break; |
||
| 151 | |||
| 152 | case DIST_DFE: |
||
| 153 | context = JAXBContext.newInstance(DistDFeInt.class); |
||
| 154 | element = XsdUtil.distDFeInt.createDistDFeInt((DistDFeInt) obj); |
||
| 155 | break; |
||
| 156 | |||
| 157 | case TCONSRECINFE: |
||
| 158 | context = JAXBContext.newInstance(TConsReciNFe.class); |
||
| 159 | element = new br.com.swconsultoria.nfe.schema_4.consReciNFe.ObjectFactory().createConsReciNFe((TConsReciNFe) obj); |
||
| 160 | break; |
||
| 161 | |||
| 162 | case TCONS_CAD: |
||
| 163 | context = JAXBContext.newInstance(TConsCad.class); |
||
| 164 | element = new br.com.swconsultoria.nfe.schema.consCad.ObjectFactory().createConsCad((TConsCad) obj); |
||
| 165 | break; |
||
| 166 | |||
| 167 | case INUTILIZACAO: |
||
| 168 | context = JAXBContext.newInstance(TInutNFe.class); |
||
| 169 | element = new br.com.swconsultoria.nfe.schema_4.inutNFe.ObjectFactory().createInutNFe((TInutNFe) obj); |
||
| 170 | break; |
||
| 171 | |||
| 172 | case RET_INUT_NFE: |
||
| 173 | context = JAXBContext.newInstance(TRetInutNFe.class); |
||
| 174 | element = XsdUtil.inutNfe.createTRetInutNfe((TRetInutNFe) obj); |
||
| 175 | break; |
||
| 176 | |||
| 177 | case SITUACAO_NFE_RET: |
||
| 178 | context = JAXBContext.newInstance(TRetConsSitNFe.class); |
||
| 179 | element = new br.com.swconsultoria.nfe.schema_4.retConsSitNFe.ObjectFactory().createRetConsSitNFe((TRetConsSitNFe) obj); |
||
| 180 | break; |
||
| 181 | |||
| 182 | case RET_RECIBO_NFE: |
||
| 183 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema_4.retConsReciNFe.TRetConsReciNFe.class); |
||
| 184 | element = new br.com.swconsultoria.nfe.schema_4.retConsReciNFe.ObjectFactory().createRetConsReciNFe((br.com.swconsultoria.nfe.schema_4.retConsReciNFe.TRetConsReciNFe) obj); |
||
| 185 | break; |
||
| 186 | |||
| 187 | case RET_STATUS_SERVICO: |
||
| 188 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema_4.retConsStatServ.TRetConsStatServ.class); |
||
| 189 | element = new br.com.swconsultoria.nfe.schema_4.retConsStatServ.ObjectFactory().createRetConsStatServ((br.com.swconsultoria.nfe.schema_4.retConsStatServ.TRetConsStatServ) obj); |
||
| 190 | break; |
||
| 191 | |||
| 192 | case RET_CONS_CAD: |
||
| 193 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.retConsCad.TRetConsCad.class); |
||
| 194 | element = new br.com.swconsultoria.nfe.schema.retConsCad.ObjectFactory().createRetConsCad((br.com.swconsultoria.nfe.schema.retConsCad.TRetConsCad) obj); |
||
| 195 | break; |
||
| 196 | |||
| 197 | case TPROCEVENTO: |
||
| 198 | switch (obj.getClass().getName()) { |
||
| 199 | case TPROCCANCELAR: |
||
| 200 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEventoCancNFe.TProcEvento.class); |
||
| 201 | element = XsdUtil.envEventoCancNFe.createTProcEvento((br.com.swconsultoria.nfe.schema.envEventoCancNFe.TProcEvento) obj); |
||
| 202 | break; |
||
| 203 | case TPROCATORINTERESSADO: |
||
| 204 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TProcEvento.class); |
||
| 205 | element = XsdUtil.envEventoAtorInteressado.createTProcEvento((br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TProcEvento) obj); |
||
| 206 | break; |
||
| 207 | case TPROCCANCELARSUBST: |
||
| 208 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEventoCancSubst.TProcEvento.class); |
||
| 209 | element = XsdUtil.envEventoCancSubst.createTProcEvento((br.com.swconsultoria.nfe.schema.envEventoCancSubst.TProcEvento) obj); |
||
| 210 | break; |
||
| 211 | case TPROCCCE: |
||
| 212 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envcce.TProcEvento.class); |
||
| 213 | element = XsdUtil.envcce.createTProcEvento((br.com.swconsultoria.nfe.schema.envcce.TProcEvento) obj); |
||
| 214 | break; |
||
| 215 | case TPROCEPEC: |
||
| 216 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEpec.TProcEvento.class); |
||
| 217 | element = XsdUtil.epec.createTProcEvento((br.com.swconsultoria.nfe.schema.envEpec.TProcEvento) obj); |
||
| 218 | break; |
||
| 219 | case TPROCMAN: |
||
| 220 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envConfRecebto.TProcEvento.class); |
||
| 221 | element = XsdUtil.manifestacao.createTProcEvento((br.com.swconsultoria.nfe.schema.envConfRecebto.TProcEvento) obj); |
||
| 222 | break; |
||
| 223 | default: |
||
| 224 | throw new NfeException("Objeto não mapeado no XmlUtil:" + obj.getClass().getSimpleName()); |
||
| 225 | } |
||
| 226 | |||
| 227 | break; |
||
| 228 | |||
| 229 | case NFEPROC: |
||
| 230 | context = JAXBContext.newInstance(TNfeProc.class); |
||
| 231 | element = XsdUtil.enviNfe.createTNfeProc((TNfeProc) obj); |
||
| 232 | break; |
||
| 233 | |||
| 234 | case NFE: |
||
| 235 | context = JAXBContext.newInstance(TNFe.class); |
||
| 236 | element = new JAXBElement<>(new QName("http://www.portalfiscal.inf.br/nfe", "NFe"), TNFe.class, null, (br.com.swconsultoria.nfe.schema_4.enviNFe.TNFe) obj); |
||
| 237 | break; |
||
| 238 | |||
| 239 | case TPROCINUT: |
||
| 240 | context = JAXBContext.newInstance(TProcInutNFe.class); |
||
| 241 | element = XsdUtil.inutNfe.createTProcInutNFe((TProcInutNFe) obj); |
||
| 242 | break; |
||
| 243 | |||
| 244 | case EVENTO: |
||
| 245 | switch (obj.getClass().getName()) { |
||
| 246 | case CANCELAR: |
||
| 247 | context = JAXBContext.newInstance(TEnvEvento.class); |
||
| 248 | element = new br.com.swconsultoria.nfe.schema.envEventoCancNFe.ObjectFactory().createEnvEvento((TEnvEvento) obj); |
||
| 249 | break; |
||
| 250 | case CANCELAR_SUBSTITUICAO: |
||
| 251 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEventoCancSubst.TEnvEvento.class); |
||
| 252 | element = new br.com.swconsultoria.nfe.schema.envEventoCancSubst.ObjectFactory().createEnvEvento((br.com.swconsultoria.nfe.schema.envEventoCancSubst.TEnvEvento) obj); |
||
| 253 | break; |
||
| 254 | case CCE: |
||
| 255 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envcce.TEnvEvento.class); |
||
| 256 | element = new br.com.swconsultoria.nfe.schema.envcce.ObjectFactory().createEnvEvento((br.com.swconsultoria.nfe.schema.envcce.TEnvEvento) obj); |
||
| 257 | break; |
||
| 258 | case EPEC: |
||
| 259 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEpec.TEnvEvento.class); |
||
| 260 | element = new br.com.swconsultoria.nfe.schema.envEpec.ObjectFactory().createEnvEvento((br.com.swconsultoria.nfe.schema.envEpec.TEnvEvento) obj); |
||
| 261 | break; |
||
| 262 | case MANIFESTAR: |
||
| 263 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envConfRecebto.TEnvEvento.class); |
||
| 264 | element = new br.com.swconsultoria.nfe.schema.envConfRecebto.ObjectFactory().createEnvEvento((br.com.swconsultoria.nfe.schema.envConfRecebto.TEnvEvento) obj); |
||
| 265 | break; |
||
| 266 | case ATOR_INTERESSADO: |
||
| 267 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TEnvEvento.class); |
||
| 268 | element = new br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.ObjectFactory().createEnvEvento((br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TEnvEvento) obj); |
||
| 269 | break; |
||
| 270 | default: |
||
| 271 | throw new NfeException("Objeto não mapeado no XmlUtil:" + obj.getClass().getSimpleName()); |
||
| 272 | } |
||
| 273 | break; |
||
| 274 | |||
| 275 | case RET_ENV_EVENTO: |
||
| 276 | switch (obj.getClass().getName()) { |
||
| 277 | case RET_CANCELAR: |
||
| 278 | context = JAXBContext.newInstance(TRetEnvEvento.class); |
||
| 279 | element = XsdUtil.retEnvEvento.createTRetEnvEvento((TRetEnvEvento) obj); |
||
| 280 | break; |
||
| 281 | case RET_CANCELAR_SUBSTITUICAO: |
||
| 282 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEventoCancSubst.TRetEnvEvento.class); |
||
| 283 | element = XsdUtil.retEnvEvento.createTRetEnvEvento((br.com.swconsultoria.nfe.schema.envEventoCancSubst.TRetEnvEvento) obj); |
||
| 284 | break; |
||
| 285 | case RET_CCE: |
||
| 286 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envcce.TRetEnvEvento.class); |
||
| 287 | element = XsdUtil.retEnvEvento.createTRetEnvEvento((br.com.swconsultoria.nfe.schema.envcce.TRetEnvEvento) obj); |
||
| 288 | break; |
||
| 289 | case RET_EPEC: |
||
| 290 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEpec.TRetEnvEvento.class); |
||
| 291 | element = XsdUtil.retEnvEvento.createTRetEnvEvento((br.com.swconsultoria.nfe.schema.envEpec.TRetEnvEvento) obj); |
||
| 292 | break; |
||
| 293 | case RET_MANIFESTAR: |
||
| 294 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envConfRecebto.TRetEnvEvento.class); |
||
| 295 | element = XsdUtil.retEnvEvento.createTRetEnvEvento((br.com.swconsultoria.nfe.schema.envConfRecebto.TRetEnvEvento) obj); |
||
| 296 | break; |
||
| 297 | case RET_ATOR_INTERESSADO: |
||
| 298 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TRetEnvEvento.class); |
||
| 299 | element = XsdUtil.retEnvEvento.createTRetEnvEvento((br.com.swconsultoria.nfe.schema.envEventoAtorInteressado.TRetEnvEvento) obj); |
||
| 300 | break; |
||
| 301 | default: |
||
| 302 | throw new NfeException("Objeto não mapeado no XmlUtil:" + obj.getClass().getSimpleName()); |
||
| 303 | } |
||
| 304 | break; |
||
| 305 | |||
| 306 | case TProtNFe: |
||
| 307 | switch (obj.getClass().getName()) { |
||
| 308 | case TProtEnvi: |
||
| 309 | context = JAXBContext.newInstance(TProtNFe.class); |
||
| 310 | element = XsdUtil.enviNfe.createTProtNFe((br.com.swconsultoria.nfe.schema_4.enviNFe.TProtNFe) obj); |
||
| 311 | break; |
||
| 312 | case TProtCons: |
||
| 313 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema_4.retConsSitNFe.TProtNFe.class); |
||
| 314 | element = XsdUtil.retConsSitNfe.createTProtNFe((br.com.swconsultoria.nfe.schema_4.retConsSitNFe.TProtNFe) obj); |
||
| 315 | break; |
||
| 316 | case TProtReci: |
||
| 317 | context = JAXBContext.newInstance(br.com.swconsultoria.nfe.schema_4.retConsReciNFe.TProtNFe.class); |
||
| 318 | element = XsdUtil.retConsReciNfe.createTProtNFe((br.com.swconsultoria.nfe.schema_4.retConsReciNFe.TProtNFe) obj); |
||
| 319 | break; |
||
| 320 | default: |
||
| 321 | throw new NfeException("Objeto não mapeado no XmlUtil:" + obj.getClass().getSimpleName()); |
||
| 322 | } |
||
| 323 | break; |
||
| 324 | |||
| 325 | default: |
||
| 326 | throw new NfeException("Objeto não mapeado no XmlUtil:" + obj.getClass().getSimpleName()); |
||
| 327 | } |
||
| 328 | assert context != null; |
||
| 329 | Marshaller marshaller = context.createMarshaller(); |
||
| 330 | |||
| 331 | marshaller.setProperty("jaxb.encoding", "Unicode"); |
||
| 332 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); |
||
| 333 | marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); |
||
| 334 | |||
| 335 | StringWriter sw = new StringWriter(4096); |
||
| 336 | |||
| 337 | String encodeXml = encode == null || !Charset.isSupported(encode.displayName()) ? "UTF-8" : encode.displayName(); |
||
| 338 | |||
| 339 | sw.append("<?xml version=\"1.0\" encoding=\"" + encodeXml + "\"?>"); |
||
| 340 | |||
| 341 | marshaller.marshal(element, sw); |
||
| 342 | |||
| 343 | if ((obj.getClass().getSimpleName().equals(TPROCEVENTO))) { |
||
| 344 | return replacesNfe(sw.toString().replace("procEvento", "procEventoNFe")); |
||
| 345 | } |
||
| 346 | |||
| 347 | return replacesNfe(sw.toString()); |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | public static String gZipToXml(byte[] conteudo) throws IOException { |
||
| 352 | if (conteudo == null || conteudo.length == 0) { |
||
| 353 | return ""; |
||
| 354 | } |
||
| 355 | GZIPInputStream gis; |
||
| 356 | gis = new GZIPInputStream(new ByteArrayInputStream(conteudo)); |
||
| 357 | BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8")); |
||
| 358 | StringBuilder outStr = new StringBuilder(); |
||
| 359 | String line; |
||
| 360 | while ((line = bf.readLine()) != null) { |
||
| 361 | outStr.append(line); |
||
| 362 | } |
||
| 363 | |||
| 364 | return outStr.toString(); |
||
| 365 | } |
||
| 366 | |||
| 367 | public static String criaNfeProc(TEnviNFe enviNfe, Object retorno) throws JAXBException, NfeException { |
||
| 368 | |||
| 369 | TNfeProc nfeProc = new TNfeProc(); |
||
| 370 | nfeProc.setVersao("4.00"); |
||
| 371 | nfeProc.setNFe(enviNfe.getNFe().get(0)); |
||
| 372 | String xml = XmlNfeUtil.objectToXml(retorno); |
||
| 373 | nfeProc.setProtNFe(XmlNfeUtil.xmlToObject(xml, TProtNFe.class)); |
||
| 374 | |||
| 375 | return XmlNfeUtil.objectToXml(nfeProc); |
||
| 376 | } |
||
| 377 | |||
| 378 | private static String replacesNfe(String xml) { |
||
| 379 | |||
| 380 | return xml.replace("<!\\[CDATA\\[<!\\[CDATA\\[", "<!\\[CDATA\\[") |
||
| 381 | .replace("\\]\\]>\\]\\]>", "\\]\\]>") |
||
| 382 | .replace("ns2:", "") |
||
| 383 | .replace("ns3:", "") |
||
| 384 | .replace("<", "<") |
||
| 385 | .replace(">", ">") |
||
| 386 | .replace("<Signature>", "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">") |
||
| 387 | .replace(" xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\"", "") |
||
| 388 | .replace(" xmlns=\"\" xmlns:ns3=\"http://www.portalfiscal.inf.br/nfe\"", "") |
||
| 389 | .replace("<NFe>", "<NFe xmlns=\"http://www.portalfiscal.inf.br/nfe\">"); |
||
| 390 | |||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Le o Arquivo XML e retona String |
||
| 395 | * |
||
| 396 | * @return String |
||
| 397 | * @throws NfeException |
||
| 398 | */ |
||
| 399 | public static String leXml(String arquivo) throws IOException { |
||
| 400 | |||
| 401 | ObjetoUtil.verifica(arquivo).orElseThrow(() -> new IllegalArgumentException("Arquivo xml não pode ser nulo/vazio.")); |
||
| 402 | if (!Files.exists(Paths.get(arquivo))) { |
||
| 403 | throw new FileNotFoundException("Arquivo " + arquivo + " não encontrado."); |
||
| 404 | } |
||
| 405 | List<String> list = Files.readAllLines(Paths.get(arquivo)); |
||
| 406 | StringJoiner joiner = new StringJoiner("\n"); |
||
| 407 | list.forEach(joiner::add); |
||
| 408 | |||
| 409 | return joiner.toString(); |
||
| 410 | } |
||
| 411 | |||
| 412 | public static String dataNfe(LocalDateTime dataASerFormatada) { |
||
| 413 | return dataNfe(dataASerFormatada, ZoneId.systemDefault()); |
||
| 414 | } |
||
| 415 | |||
| 416 | public static String dataNfe(LocalDateTime dataASerFormatada, ZoneId zoneId) { |
||
| 417 | try { |
||
| 418 | GregorianCalendar calendar = GregorianCalendar.from(dataASerFormatada.atZone(ObjetoUtil.verifica(zoneId).orElse(ZoneId.of("Brazil/East")))); |
||
| 419 | |||
| 420 | XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar); |
||
| 421 | xmlCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED); |
||
| 422 | return xmlCalendar.toString(); |
||
| 423 | |||
| 424 | } catch (DatatypeConfigurationException e) { |
||
| 415 | espaco | 425 | logger.warning(e.getMessage()); |
| 414 | espaco | 426 | } |
| 427 | return null; |
||
| 428 | } |
||
| 429 | |||
| 430 | public static byte[] geraHashCSRT(String chave, String csrt) throws NoSuchAlgorithmException { |
||
| 431 | |||
| 432 | ObjetoUtil.verifica(chave).orElseThrow(() -> new InvalidParameterException("Chave não deve ser nula ou vazia")); |
||
| 433 | ObjetoUtil.verifica(csrt).orElseThrow(() -> new InvalidParameterException("CSRT não deve ser nulo ou vazio")); |
||
| 434 | if (chave.length() != 44) { |
||
| 435 | throw new InvalidParameterException("Chave deve conter 44 caracteres."); |
||
| 436 | } |
||
| 437 | MessageDigest md = MessageDigest.getInstance("SHA-1"); |
||
| 438 | md.update((csrt + chave).getBytes()); |
||
| 439 | return Base64.getEncoder().encode(md.digest()); |
||
| 440 | } |
||
| 441 | } |