private Document LoadXML(string path) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); return(factory.newDocumentBuilder().parse(new BufferedInputStream(new FileInputStream(path)))); }
public void Sign(string unsignedXmlPath, string signedXmlPath, string pfxPath, string pfxPassword) { PrivateKey privateKey; Provider provider; X509Certificate certificate = _LoadCertificate(pfxPath, pfxPassword, out privateKey, out provider); if (certificate != null) { TrustFactory.instance = es.mityc.javasign.trust.TrustExtendFactory.newInstance(); TrustFactory.truster = es.mityc.javasign.trust.MyPropsTruster.getInstance(); PoliciesManager.POLICY_SIGN = new es.mityc.javasign.xml.xades.policy.facturae.Facturae31Manager(); PoliciesManager.POLICY_VALIDATION = new es.mityc.javasign.xml.xades.policy.facturae.Facturae31Manager(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); Document unsignedDocument = dbf.newDocumentBuilder().parse(new BufferedInputStream(new FileInputStream(unsignedXmlPath))); DataToSign dataToSign = new DataToSign(); dataToSign.setXadesFormat(EnumFormatoFirma.XAdES_BES); dataToSign.setEsquema(XAdESSchemas.XAdES_132); dataToSign.setPolicyKey("facturae31"); dataToSign.setAddPolicy(true); dataToSign.setXMLEncoding("UTF-8"); dataToSign.setEnveloped(true); dataToSign.addObject(new ObjectToSign(new AllXMLToSign(), "Description", null, "text/xml", null)); dataToSign.setDocument(unsignedDocument); Object[] res = new FirmaXML().signFile(certificate, dataToSign, privateKey, provider); UtilidadTratarNodo.saveDocumentToOutputStream((Document)res[0], new FileOutputStream(signedXmlPath), true); } }
public static Document LoadXML(string path) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); return(dbf.newDocumentBuilder().parse(new BufferedInputStream(new FileInputStream(path)))); }
public static Document Erp90w(string path) { SAXParserFactoryImpl sAXParserFactoryImpl = new SAXParserFactoryImpl(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); Document document = documentBuilderFactory.newDocumentBuilder().parse(new BufferedInputStream(new FileInputStream(path))); return(document); }
public static void firmar(string Archivo) { string path = @"C:\Firma\fabricio_fortunato_mero_mosquera.p12"; PrivateKey privatekey; Provider provider; java.security.cert.X509Certificate certificate = LayerLogic.ClassLibrary.Complementos.Firmar.loadCertificate(path, "FFmm_1978", out privatekey, out provider); if (certificate != null) { //Creamos el documento a firmar DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); //C# var base64 = System.Convert.FromBase64String(Archivo); string bytes = System.Text.Encoding.UTF8.GetString(base64); ByteArrayInputStream bs = new ByteArrayInputStream(System.Text.Encoding.UTF8.GetBytes(bytes)); Document documento = dbf.newDocumentBuilder().parse(bs); //Creamos datos a firmar DataToSign dataToSign = new DataToSign(); dataToSign.setXadesFormat(EnumFormatoFirma.XAdES_BES); //XAdES-EPES dataToSign.setAddPolicy(false); dataToSign.setXMLEncoding(encoding); dataToSign.setEnveloped(true); dataToSign.addObject(new ObjectToSign(new InternObjectToSign(nodoFirma), "comprobante", null, "text/xml", null)); dataToSign.setParentSignNode(nodoFirma); //dataToSign.setDocument(LoadXML(NombreArchivo)); dataToSign.setDocument(documento); //Firmar Object[] res = new FirmaXML().signFile(certificate, dataToSign, privatekey, provider); Document doc = (Document)res[0]; //Transformar a string org.w3c.dom.ls.DOMImplementationLS domImplementation = (org.w3c.dom.ls.DOMImplementationLS)doc.getImplementation(); org.w3c.dom.ls.LSSerializer lsSerializer = domImplementation.createLSSerializer(); Archivo = lsSerializer.writeToString(doc).Replace("UTF-16", "UTF-8"); //C# var ArchivoFirmado = Encoding.UTF8.GetBytes(Archivo); string firmado = Convert.ToBase64String(ArchivoFirmado); var base642 = System.Convert.FromBase64String(firmado); string bytes2 = System.Text.Encoding.UTF8.GetString(base642); } }
private Document parseaDoc(InputStream fichero) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder1 = (DocumentBuilder)null; DocumentBuilder documentBuilder2; try { documentBuilder2 = documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException ex) { int num = (int)XtraMessageBox.Show("Error al realizar validacion a documento = " + ex.toString()); return((Document)null); } Document document = (Document)null; try { return(documentBuilder2.parse(fichero)); } catch (SAXException ex) { document = (Document)null; } catch (System.IO.IOException ex) { int num = (int)XtraMessageBox.Show("Error al realizar validacion a documento = " + ex.ToString()); } finally { documentBuilder1 = (DocumentBuilder)null; } return((Document)null); }
public void parse() { DocumentBuilderFactory factory = null; DocumentBuilder builder = null; Document document = null; factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); //otherwise we can not act namespace independend, i.e. use document.getElementsByTagNameNS("*",... try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException ex3) { // TODO Auto-generated catch block ex3.printStackTrace(); } try { InputStream bais = new ByteArrayInputStream(rawXML); document = builder.parse(bais); } catch (SAXException ex1) { ex1.printStackTrace(); } catch (IOException ex2) { ex2.printStackTrace(); } NodeList ndList; // rootNode = document.getDocumentElement(); // ApplicableSupplyChainTradeSettlement ndList = document.getDocumentElement() .getElementsByTagNameNS("*", "PaymentReference"); //$NON-NLS-1$ for (int bookingIndex = 0; bookingIndex < ndList .getLength(); bookingIndex++) { Node booking = ndList.item(bookingIndex); // if there is a attribute in the tag number:value setForeignReference(booking.getTextContent()); } /* * ndList = document * .getElementsByTagName("GermanBankleitzahlID"); //$NON-NLS-1$ * for (int bookingIndex = 0; bookingIndex < ndList * .getLength(); bookingIndex++) { * Node booking = ndList.item(bookingIndex); * // if there is a attribute in the tag number:value * setBIC(booking.getTextContent()); * } * ndList = document.getElementsByTagName("ProprietaryID"); //$NON-NLS-1$ * for (int bookingIndex = 0; bookingIndex < ndList * .getLength(); bookingIndex++) { * Node booking = ndList.item(bookingIndex); * // if there is a attribute in the tag number:value * setIBAN(booking.getTextContent()); * } * <ram:PayeePartyCreditorFinancialAccount> * <ram:IBANID>DE1234</ram:IBANID> * </ram:PayeePartyCreditorFinancialAccount> * <ram:PayeeSpecifiedCreditorFinancialInstitution> * <ram:BICID>DE5656565</ram:BICID> * <ram:Name>Commerzbank</ram:Name> * </ram:PayeeSpecifiedCreditorFinancialInstitution> */ ndList = document.getElementsByTagNameNS("*", "PayeePartyCreditorFinancialAccount"); //$NON-NLS-1$ for (int bookingIndex = 0; bookingIndex < ndList .getLength(); bookingIndex++) { Node booking = ndList.item(bookingIndex); // there are many "name" elements, so get the one below // SellerTradeParty NodeList bookingDetails = booking.getChildNodes(); for (int detailIndex = 0; detailIndex < bookingDetails .getLength(); detailIndex++) { Node detail = bookingDetails.item(detailIndex); if ((detail.getLocalName() != null) && (detail.getLocalName().Equals("IBANID"))) { //$NON-NLS-1$ setIBAN(detail.getTextContent()); } } } ndList = document.getElementsByTagNameNS("*", "PayeeSpecifiedCreditorFinancialInstitution"); //$NON-NLS-1$ for (int bookingIndex = 0; bookingIndex < ndList .getLength(); bookingIndex++) { Node booking = ndList.item(bookingIndex); // there are many "name" elements, so get the one below // SellerTradeParty NodeList bookingDetails = booking.getChildNodes(); for (int detailIndex = 0; detailIndex < bookingDetails .getLength(); detailIndex++) { Node detail = bookingDetails.item(detailIndex); if ((detail.getLocalName() != null) && (detail.getLocalName().Equals("BICID"))) { //$NON-NLS-1$ setBIC(detail.getTextContent()); } if ((detail.getLocalName() != null) && (detail.getLocalName().Equals("Name"))) { //$NON-NLS-1$ setBankName(detail.getTextContent()); } } } // ndList = document.getElementsByTagNameNS("*", "SellerTradeParty"); //$NON-NLS-1$ for (int bookingIndex = 0; bookingIndex < ndList .getLength(); bookingIndex++) { Node booking = ndList.item(bookingIndex); // there are many "name" elements, so get the one below // SellerTradeParty NodeList bookingDetails = booking.getChildNodes(); for (int detailIndex = 0; detailIndex < bookingDetails .getLength(); detailIndex++) { Node detail = bookingDetails.item(detailIndex); if ((detail.getLocalName() != null) && (detail.getLocalName().Equals("Name"))) { //$NON-NLS-1$ setHolder(detail.getTextContent()); } } } ndList = document.getElementsByTagNameNS("*", "DuePayableAmount"); //$NON-NLS-1$ for (int bookingIndex = 0; bookingIndex < ndList .getLength(); bookingIndex++) { Node booking = ndList.item(bookingIndex); // if there is a attribute in the tag number:value amountFound = true; setAmount(booking.getTextContent()); } if (!amountFound) { /* there is apparently no requirement to mention DuePayableAmount,, * if it's not there, check for GrandTotalAmount */ ndList = document.getElementsByTagNameNS("*", "GrandTotalAmount"); //$NON-NLS-1$ for (int bookingIndex = 0; bookingIndex < ndList .getLength(); bookingIndex++) { Node booking = ndList.item(bookingIndex); // if there is a attribute in the tag number:value amountFound = true; setAmount(booking.getTextContent()); } } }