diff --git a/pom.xml b/pom.xml index c4b100c..4510710 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ 3.1.2.RELEASE 2.1.0.RELEASE - 1.2 + 1.4.1 1.1.1 2.4 1.6.6 @@ -114,14 +114,29 @@ ${org.springframework.ws.version} - + + + + org.codehaus.castor + castor + 1.4.1 + pom + + + + + org.codehaus.castor + castor-xml + 1.4.1 + + commons-logging commons-logging @@ -216,7 +231,7 @@ - + diff --git a/src/main/java/br/com/elotech/sits/service/AbstractService.java b/src/main/java/br/com/elotech/sits/service/AbstractService.java index a8bc1c9..79af151 100644 --- a/src/main/java/br/com/elotech/sits/service/AbstractService.java +++ b/src/main/java/br/com/elotech/sits/service/AbstractService.java @@ -41,12 +41,12 @@ import org.xml.sax.SAXException; abstract class AbstractService implements Service { - + private static final String ATRIBUTO_PRINCIPAL_NOTA_FISCAL = "xmlns"; private static final String XSD_VERSAO_2_03 = "nfse_v2_03.xsd"; CastorMarshaller castorMarshaller; - + CastorMarshaller castorMarshallerNfse203; String dirToSend; @@ -56,39 +56,37 @@ abstract class AbstractService implements Service { String dirSent; WebServiceTemplate webServiceTemplate; - + WebServiceTemplate webServiceVersao203Template; public WebServiceTemplate getWebServiceTemplate() { return webServiceTemplate; } - + public WebServiceTemplate getWebServiceVersao203Template() { return webServiceVersao203Template; } - + public WebServiceTemplate getWebServiceTemplateByXSD(Node node) { - - if(Objects.nonNull(node) && - Objects.nonNull(node.getNodeValue()) && - node.getNodeValue().contains(XSD_VERSAO_2_03)){ - return webServiceVersao203Template; + + if (Objects.nonNull(node) && Objects.nonNull(node.getNodeValue()) + && node.getNodeValue().contains(XSD_VERSAO_2_03)) { + return webServiceVersao203Template; } - + return webServiceTemplate; } - + public CastorMarshaller getCastorMarshallerByXSD(Node node) { - - if(Objects.nonNull(node) && - Objects.nonNull(node.getNodeValue()) && - node.getNodeValue().contains(XSD_VERSAO_2_03)){ - return castorMarshallerNfse203; + + if (Objects.nonNull(node) && Objects.nonNull(node.getNodeValue()) + && node.getNodeValue().contains(XSD_VERSAO_2_03)) { + return castorMarshallerNfse203; } - + return castorMarshaller; } - + public List getFilesToSend() { File fileDirToSend = new File(dirToSend); @@ -114,76 +112,78 @@ public File getFileToWriteResponse(File receivedFile) { return getDestFile(receivedFile, "resp", dirReceived); } - + public File getFileToWriteSent(File sentFile) { return getDestFile(sentFile, "sent", dirSent); } - + private File getDestFile(File srcFile, String prefix, String destDir) { - return new File(String.format("%s%s%s_%s", destDir, - File.separator, prefix, srcFile.getName())); - + return new File(String.format("%s%s%s_%s", destDir, File.separator, prefix, srcFile.getName())); + } - public Object getObjectsToSend(File fileToSend, Node node) throws XmlMappingException, IOException, ParserConfigurationException, SAXException { + public Object getObjectsToSend(File fileToSend, Node node) + throws XmlMappingException, IOException, ParserConfigurationException, SAXException { CastorMarshaller castorMarshallerByXSD = getCastorMarshallerByXSD(node); - + Source source = new StreamSource(new FileInputStream(fileToSend)); - + return castorMarshallerByXSD.unmarshal(source); - + } - - public void writeReceived(File fileToWrite, Object received, Node node) throws XmlMappingException, IOException, ParserConfigurationException, SAXException { - + + public void writeReceived(File fileToWrite, Object received, Node node) + throws XmlMappingException, IOException, ParserConfigurationException, SAXException { + CastorMarshaller castorMarshallerByXSD = getCastorMarshallerByXSD(node); - - FileOutputStream fos = new FileOutputStream(fileToWrite); - + + FileOutputStream fos = new FileOutputStream(fileToWrite); + Result result = new StreamResult(fos); - + +// castorMarshallerByXSD.marshal(received, result); castorMarshallerByXSD.marshal(received, result); - + fos.flush(); - + fos.close(); - + } - + public void moveToSent(File fileSent) throws IOException { - + FileUtils.moveFile(fileSent, getFileToWriteSent(fileSent)); - + } - - public Node getXSDNotaFiscal(File file) throws ParserConfigurationException, SAXException, IOException{ - + + public Node getXSDNotaFiscal(File file) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(file); - - Node root = (Node)doc.getDocumentElement(); - + + Node root = (Node) doc.getDocumentElement(); + NamedNodeMap attributes = root.getAttributes(); - if (attributes != null){ - for (int i = 0; i < attributes.getLength(); i++){ - Node node = attributes.item(i); - if (node.getNodeType() == Node.ATTRIBUTE_NODE){ - - if(ATRIBUTO_PRINCIPAL_NOTA_FISCAL.equals(node.getNodeName())){ - return node; - } - } - } - } - - return null; - + if (attributes != null) { + for (int i = 0; i < attributes.getLength(); i++) { + Node node = attributes.item(i); + if (node.getNodeType() == Node.ATTRIBUTE_NODE) { + + if (ATRIBUTO_PRINCIPAL_NOTA_FISCAL.equals(node.getNodeName())) { + return node; + } + } + } + } + + return null; + } } diff --git a/src/main/resources/castor.properties b/src/main/resources/castor.properties new file mode 100644 index 0000000..9ff3839 --- /dev/null +++ b/src/main/resources/castor.properties @@ -0,0 +1 @@ +org.exolab.castor.indent=true \ No newline at end of file diff --git a/src/main/resources/clientWs.xml b/src/main/resources/clientWs.xml index 212cd06..6b0e632 100644 --- a/src/main/resources/clientWs.xml +++ b/src/main/resources/clientWs.xml @@ -105,10 +105,14 @@ + p:mappingLocation="classpath:castor-mapping.xml"> + + + p:mappingLocation="classpath:castor-wsnfse203-mapping.xml"> + +