Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1 | espaco | 1 | package nfe.util; |
| 2 | |||
| 3 | import java.io.BufferedReader; |
||
| 4 | import java.io.ByteArrayInputStream; |
||
| 5 | import java.io.FileInputStream; |
||
| 6 | import java.io.IOException; |
||
| 7 | import java.io.InputStreamReader; |
||
| 8 | import java.io.StringReader; |
||
| 9 | import java.io.StringWriter; |
||
| 10 | import java.text.Normalizer; |
||
| 11 | import java.time.LocalDateTime; |
||
| 12 | import java.time.ZoneId; |
||
| 13 | import java.util.GregorianCalendar; |
||
| 14 | import java.util.zip.GZIPInputStream; |
||
| 15 | |||
| 16 | import javax.xml.bind.JAXBContext; |
||
| 17 | import javax.xml.bind.JAXBElement; |
||
| 18 | import javax.xml.bind.JAXBException; |
||
| 19 | import javax.xml.bind.Marshaller; |
||
| 20 | import javax.xml.bind.Unmarshaller; |
||
| 21 | import javax.xml.datatype.DatatypeConfigurationException; |
||
| 22 | import javax.xml.datatype.DatatypeConstants; |
||
| 23 | import javax.xml.datatype.DatatypeFactory; |
||
| 24 | import javax.xml.datatype.XMLGregorianCalendar; |
||
| 25 | import javax.xml.transform.stream.StreamSource; |
||
| 26 | |||
| 27 | import br.inf.portalfiscal.nfe.schema.consCad.TConsCad; |
||
| 28 | import br.inf.portalfiscal.nfe.schema.distdfeint.DistDFeInt; |
||
| 29 | import br.inf.portalfiscal.nfe.schema.envEventoCancNFe.TEnvEvento; |
||
| 30 | import br.inf.portalfiscal.nfe.schema_4.consReciNFe.TConsReciNFe; |
||
| 31 | import br.inf.portalfiscal.nfe.schema_4.consSitNFe.TConsSitNFe; |
||
| 32 | import br.inf.portalfiscal.nfe.schema_4.consStatServ.TConsStatServ; |
||
| 33 | import br.inf.portalfiscal.nfe.schema_4.enviNFe.TEnviNFe; |
||
| 34 | import br.inf.portalfiscal.nfe.schema_4.enviNFe.TNfeProc; |
||
| 35 | import br.inf.portalfiscal.nfe.schema_4.enviNFe.TProtNFe; |
||
| 36 | import br.inf.portalfiscal.nfe.schema_4.inutNFe.TInutNFe; |
||
| 37 | import br.inf.portalfiscal.nfe.schema_4.inutNFe.TProcInutNFe; |
||
| 38 | import br.inf.portalfiscal.nfe.schema_4.util.XsdUtil; |
||
| 39 | import nfe.exception.NfeException; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Classe Responsavel por Metodos referentes ao XML |
||
| 43 | * |
||
| 44 | * @author Samuel Oliveira |
||
| 45 | */ |
||
| 46 | public class XmlUtil { |
||
| 47 | |||
| 48 | private static final String STATUS = "TConsStatServ"; |
||
| 49 | private static final String SITUACAO_NFE = "TConsSitNFe"; |
||
| 50 | private static final String ENVIO_NFE = "TEnviNFe"; |
||
| 51 | private static final String DIST_DFE = "DistDFeInt"; |
||
| 52 | private static final String INUTILIZACAO = "TInutNFe"; |
||
| 53 | private static final String NFEPROC = "TNfeProc"; |
||
| 54 | private static final String EVENTO = "TEnvEvento"; |
||
| 55 | private static final String TPROCEVENTO = "TProcEvento"; |
||
| 56 | private static final String TCONSRECINFE = "TConsReciNFe"; |
||
| 57 | private static final String TConsCad = "TConsCad"; |
||
| 58 | private static final String TPROCINUT = "TProcInutNFe"; |
||
| 59 | |||
| 60 | private static final String TPROCCANCELAR = "br.inf.portalfiscal.nfe.schema.envEventoCancNFe.TProcEvento"; |
||
| 61 | private static final String TPROCCCE = "br.inf.portalfiscal.nfe.schema.envcce.TProcEvento"; |
||
| 62 | |||
| 63 | private static final String TProtNFe = "TProtNFe"; |
||
| 64 | private static final String TProtEnvi = "br.inf.portalfiscal.nfe.schema_4.enviNFe.TProtNFe"; |
||
| 65 | private static final String TProtCons = "br.inf.portalfiscal.nfe.schema_4.retConsSitNFe.TProtNFe"; |
||
| 66 | private static final String TProtReci = "br.inf.portalfiscal.nfe.schema_4.retConsReciNFe.TProtNFe"; |
||
| 67 | |||
| 68 | private static final String CANCELAR = "br.inf.portalfiscal.nfe.schema.envEventoCancNFe.TEnvEvento"; |
||
| 69 | private static final String CCE = "br.inf.portalfiscal.nfe.schema.envcce.TEnvEvento"; |
||
| 70 | private static final String MANIFESTAR = "br.inf.portalfiscal.nfe.schema.envConfRecebto.TEnvEvento"; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Transforma o String do XML em Objeto |
||
| 74 | * |
||
| 75 | * @param xml |
||
| 76 | * @param classe |
||
| 77 | * @return T |
||
| 78 | */ |
||
| 79 | public static <T> T xmlToObject(String xml, Class<T> classe) throws JAXBException { |
||
| 80 | |||
| 81 | JAXBContext context = JAXBContext.newInstance(classe); |
||
| 82 | Unmarshaller unmarshaller = context.createUnmarshaller(); |
||
| 83 | |||
| 84 | return unmarshaller.unmarshal(new StreamSource(new StringReader(xml)), classe).getValue(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Transforma o Objeto em XML(String) |
||
| 89 | * |
||
| 90 | * @param obj |
||
| 91 | * @return |
||
| 92 | * @throws JAXBException |
||
| 93 | * @throws NfeException |
||
| 94 | */ |
||
| 95 | public static <T> String objectToXml(Object obj) throws JAXBException, NfeException { |
||
| 96 | |||
| 97 | JAXBContext context = null; |
||
| 98 | JAXBElement<?> element = null; |
||
| 99 | |||
| 100 | switch (obj.getClass().getSimpleName()) { |
||
| 101 | |||
| 102 | case STATUS: |
||
| 103 | context = JAXBContext.newInstance(TConsStatServ.class); |
||
| 104 | element = new br.inf.portalfiscal.nfe.schema_4.consStatServ.ObjectFactory().createConsStatServ((TConsStatServ) obj); |
||
| 105 | break; |
||
| 106 | |||
| 107 | case ENVIO_NFE: |
||
| 108 | context = JAXBContext.newInstance(TEnviNFe.class); |
||
| 109 | element = new br.inf.portalfiscal.nfe.schema_4.enviNFe.ObjectFactory().createEnviNFe((TEnviNFe) obj); |
||
| 110 | break; |
||
| 111 | |||
| 112 | case SITUACAO_NFE: |
||
| 113 | context = JAXBContext.newInstance(TConsSitNFe.class); |
||
| 114 | element = new br.inf.portalfiscal.nfe.schema_4.consSitNFe.ObjectFactory().createConsSitNFe((TConsSitNFe) obj); |
||
| 115 | break; |
||
| 116 | |||
| 117 | case DIST_DFE: |
||
| 118 | context = JAXBContext.newInstance(DistDFeInt.class); |
||
| 119 | element = new br.inf.portalfiscal.nfe.schema.distdfeint.ObjectFactory().createDistDFeInt((DistDFeInt) obj); |
||
| 120 | break; |
||
| 121 | |||
| 122 | case TCONSRECINFE: |
||
| 123 | context = JAXBContext.newInstance(TConsReciNFe.class); |
||
| 124 | element = new br.inf.portalfiscal.nfe.schema_4.consReciNFe.ObjectFactory().createConsReciNFe((TConsReciNFe) obj); |
||
| 125 | break; |
||
| 126 | |||
| 127 | case TConsCad: |
||
| 128 | context = JAXBContext.newInstance(TConsCad.class); |
||
| 129 | element = new br.inf.portalfiscal.nfe.schema.consCad.ObjectFactory().createConsCad((TConsCad) obj); |
||
| 130 | break; |
||
| 131 | |||
| 132 | case INUTILIZACAO: |
||
| 133 | context = JAXBContext.newInstance(TInutNFe.class); |
||
| 134 | element = new br.inf.portalfiscal.nfe.schema_4.inutNFe.ObjectFactory().createInutNFe((TInutNFe) obj); |
||
| 135 | break; |
||
| 136 | |||
| 137 | case TPROCEVENTO: |
||
| 138 | if (obj.getClass().getName().equals(TPROCCANCELAR)) { |
||
| 139 | context = JAXBContext.newInstance(br.inf.portalfiscal.nfe.schema.envEventoCancNFe.TProcEvento.class); |
||
| 140 | element = new br.inf.portalfiscal.nfe.schema.envEventoCancNFe.ObjectFactory().createTProcEvento((br.inf.portalfiscal.nfe.schema.envEventoCancNFe.TProcEvento) obj); |
||
| 141 | } else if (obj.getClass().getName().equals(TPROCCCE)) { |
||
| 142 | context = JAXBContext.newInstance(br.inf.portalfiscal.nfe.schema.envcce.TProcEvento.class); |
||
| 143 | element = new br.inf.portalfiscal.nfe.schema.envcce.ObjectFactory().createTProcEvento((br.inf.portalfiscal.nfe.schema.envcce.TProcEvento) obj); |
||
| 144 | } |
||
| 145 | |||
| 146 | break; |
||
| 147 | |||
| 148 | case NFEPROC: |
||
| 149 | context = JAXBContext.newInstance(TNfeProc.class); |
||
| 150 | element = XsdUtil.enviNfe.createTNfeProc((TNfeProc) obj); |
||
| 151 | break; |
||
| 152 | |||
| 153 | case TPROCINUT: |
||
| 154 | context = JAXBContext.newInstance(TProcInutNFe.class); |
||
| 155 | element = XsdUtil.inutNfe.createTProcInutNFe((TProcInutNFe) obj); |
||
| 156 | break; |
||
| 157 | |||
| 158 | case EVENTO: |
||
| 159 | if (obj.getClass().getName().equals(CANCELAR)) { |
||
| 160 | context = JAXBContext.newInstance(TEnvEvento.class); |
||
| 161 | element = new br.inf.portalfiscal.nfe.schema.envEventoCancNFe.ObjectFactory().createEnvEvento((TEnvEvento) obj); |
||
| 162 | } else if (obj.getClass().getName().equals(CCE)) { |
||
| 163 | context = JAXBContext.newInstance(br.inf.portalfiscal.nfe.schema.envcce.TEnvEvento.class); |
||
| 164 | element = new br.inf.portalfiscal.nfe.schema.envcce.ObjectFactory().createEnvEvento((br.inf.portalfiscal.nfe.schema.envcce.TEnvEvento) obj); |
||
| 165 | } else if (obj.getClass().getName().equals(MANIFESTAR)) { |
||
| 166 | context = JAXBContext.newInstance(br.inf.portalfiscal.nfe.schema.envConfRecebto.TEnvEvento.class); |
||
| 167 | element = new br.inf.portalfiscal.nfe.schema.envConfRecebto.ObjectFactory().createEnvEvento((br.inf.portalfiscal.nfe.schema.envConfRecebto.TEnvEvento) obj); |
||
| 168 | } |
||
| 169 | break; |
||
| 170 | |||
| 171 | case TProtNFe: |
||
| 172 | if (obj.getClass().getName().equals(TProtEnvi)) { |
||
| 173 | context = JAXBContext.newInstance(TProtNFe.class); |
||
| 174 | element = XsdUtil.enviNfe.createTProtNFe((br.inf.portalfiscal.nfe.schema_4.enviNFe.TProtNFe) obj); |
||
| 175 | } else if (obj.getClass().getName().equals(TProtCons)) { |
||
| 176 | context = JAXBContext.newInstance(br.inf.portalfiscal.nfe.schema_4.retConsSitNFe.TProtNFe.class); |
||
| 177 | element = XsdUtil.retConsSitNfe.createTProtNFe((br.inf.portalfiscal.nfe.schema_4.retConsSitNFe.TProtNFe) obj); |
||
| 178 | } else if (obj.getClass().getName().equals(TProtReci)) { |
||
| 179 | context = JAXBContext.newInstance(br.inf.portalfiscal.nfe.schema_4.retConsReciNFe.TProtNFe.class); |
||
| 180 | element = XsdUtil.retConsReciNfe.createTProtNFe((br.inf.portalfiscal.nfe.schema_4.retConsReciNFe.TProtNFe) obj); |
||
| 181 | } |
||
| 182 | break; |
||
| 183 | |||
| 184 | default: |
||
| 185 | throw new NfeException("Objeto não mapeado no XmlUtil:" + obj.getClass().getSimpleName()); |
||
| 186 | } |
||
| 187 | Marshaller marshaller = context.createMarshaller(); |
||
| 188 | |||
| 189 | marshaller.setProperty("jaxb.encoding", "Unicode"); |
||
| 190 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); |
||
| 191 | marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); |
||
| 192 | |||
| 193 | StringWriter sw = new StringWriter(); |
||
| 194 | |||
| 195 | if (obj.getClass().getSimpleName().equals(ENVIO_NFE) || obj.getClass().getSimpleName().equals(NFEPROC)) { |
||
| 196 | try { |
||
| 197 | CDATAContentHandler cdataHandler = new CDATAContentHandler(sw, "utf-8"); |
||
| 198 | marshaller.marshal(element, cdataHandler); |
||
| 199 | } catch (IOException e) { |
||
| 200 | throw new NfeException(e.getMessage()); |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | marshaller.marshal(element, sw); |
||
| 204 | } |
||
| 205 | StringBuilder xml = new StringBuilder(); |
||
| 206 | xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append(sw.toString()); |
||
| 207 | |||
| 208 | if ((obj.getClass().getSimpleName().equals(TPROCEVENTO))) { |
||
| 209 | return replacesNfe(xml.toString().replaceAll("procEvento", "procEventoNFe")); |
||
| 210 | } else { |
||
| 211 | return replacesNfe(xml.toString()); |
||
| 212 | } |
||
| 213 | |||
| 214 | } |
||
| 215 | |||
| 216 | public static String gZipToXml(byte[] conteudo) throws IOException { |
||
| 217 | if (conteudo == null || conteudo.length == 0) { |
||
| 218 | return ""; |
||
| 219 | } |
||
| 220 | GZIPInputStream gis; |
||
| 221 | gis = new GZIPInputStream(new ByteArrayInputStream(conteudo)); |
||
| 222 | BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8")); |
||
| 223 | StringBuilder outStr = new StringBuilder(); |
||
| 224 | String line; |
||
| 225 | while ((line = bf.readLine()) != null) { |
||
| 226 | outStr.append(line); |
||
| 227 | } |
||
| 228 | |||
| 229 | return outStr.toString(); |
||
| 230 | } |
||
| 231 | |||
| 232 | public static String criaNfeProc(TEnviNFe enviNfe, Object retorno) throws JAXBException, NfeException { |
||
| 233 | |||
| 234 | TNfeProc nfeProc = new TNfeProc(); |
||
| 235 | nfeProc.setVersao("4.00"); |
||
| 236 | nfeProc.setNFe(enviNfe.getNFe().get(0)); |
||
| 237 | String xml = XmlUtil.objectToXml(retorno); |
||
| 238 | nfeProc.setProtNFe(XmlUtil.xmlToObject(xml, TProtNFe.class)); |
||
| 239 | |||
| 240 | return XmlUtil.objectToXml(nfeProc); |
||
| 241 | } |
||
| 242 | |||
| 243 | public static String removeAcentos(String str) { |
||
| 244 | |||
| 245 | str = str.replaceAll("\r", ""); |
||
| 246 | str = str.replaceAll("\t", ""); |
||
| 247 | str = str.replaceAll("\n", ""); |
||
| 248 | str = str.replaceAll("&", "E"); |
||
| 249 | str = str.replaceAll(">\\s+<", "><"); |
||
| 250 | CharSequence cs = new StringBuilder(str == null ? "" : str); |
||
| 251 | return Normalizer.normalize(cs, Normalizer.Form.NFKD).replaceAll("\\p{InCombiningDiacriticalMarks}+", ""); |
||
| 252 | |||
| 253 | } |
||
| 254 | |||
| 255 | private static String replacesNfe(String xml) { |
||
| 256 | |||
| 257 | xml = xml.replaceAll("ns2:", ""); |
||
| 258 | xml = xml.replaceAll("<!\\[CDATA\\[<!\\[CDATA\\[", "<!\\[CDATA\\["); |
||
| 259 | xml = xml.replaceAll("\\]\\]>\\]\\]>", "\\]\\]>"); |
||
| 260 | xml = xml.replaceAll("ns3:", ""); |
||
| 261 | xml = xml.replaceAll("<", "<"); |
||
| 262 | xml = xml.replaceAll("&", "&"); |
||
| 263 | xml = xml.replaceAll(">", ">"); |
||
| 264 | xml = xml.replaceAll("<Signature>", "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\">"); |
||
| 265 | xml = xml.replaceAll(" xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\"", ""); |
||
| 266 | xml = xml.replaceAll(" xmlns=\"\" xmlns:ns3=\"http://www.portalfiscal.inf.br/nfe\"", ""); |
||
| 267 | |||
| 268 | return xml; |
||
| 269 | |||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Le o Arquivo XML e retona String |
||
| 274 | * |
||
| 275 | * @return String |
||
| 276 | * @throws NfeException |
||
| 277 | */ |
||
| 278 | public static String leXml(String arquivo) throws NfeException { |
||
| 279 | |||
| 280 | StringBuilder xml = new StringBuilder(); |
||
| 281 | BufferedReader in; |
||
| 282 | try { |
||
| 283 | in = new BufferedReader(new InputStreamReader(new FileInputStream(arquivo), "UTF-8")); |
||
| 284 | String linha; |
||
| 285 | |||
| 286 | while ((linha = in.readLine()) != null) { |
||
| 287 | xml.append(linha); |
||
| 288 | |||
| 289 | } |
||
| 290 | in.close(); |
||
| 291 | } catch (IOException e) { |
||
| 292 | throw new NfeException("Ler Xml: " + e.getMessage()); |
||
| 293 | } |
||
| 294 | return xml.toString(); |
||
| 295 | } |
||
| 296 | |||
| 297 | public static String dataNfe() throws NfeException { |
||
| 298 | try { |
||
| 299 | LocalDateTime dataASerFormatada = LocalDateTime.now(); |
||
| 300 | GregorianCalendar calendar = GregorianCalendar.from(dataASerFormatada.atZone(ZoneId.of("Brazil/East"))); |
||
| 301 | |||
| 302 | XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar); |
||
| 303 | xmlCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED); |
||
| 304 | |||
| 305 | return (xmlCalendar.toString()); |
||
| 306 | } catch (DatatypeConfigurationException e) { |
||
| 307 | throw new NfeException(e.getMessage()); |
||
| 308 | } |
||
| 309 | |||
| 310 | } |
||
| 311 | |||
| 312 | } |