Skip to content

Commit

Permalink
Merge pull request #6 from Elotech-Dev/fix-whiteSpacePreserve
Browse files Browse the repository at this point in the history
Correção da propriedade whiteSpacePreserve no marshaller
  • Loading branch information
rafaelgil authored Dec 16, 2019
2 parents 74b30e4 + c681b0c commit ccc1778
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 68 deletions.
25 changes: 20 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

<org.springframework.version>3.1.2.RELEASE</org.springframework.version>
<org.springframework.ws.version>2.1.0.RELEASE</org.springframework.ws.version>
<org.codehaus.castor.version>1.2</org.codehaus.castor.version>
<org.codehaus.castor.version>1.4.1</org.codehaus.castor.version>
<commons-logging.version>1.1.1</commons-logging.version>
<commons-io.version>2.4</commons-io.version>
<org.slf4j.version>1.6.6</org.slf4j.version>
Expand Down Expand Up @@ -114,14 +114,29 @@
<version>${org.springframework.ws.version}</version>
</dependency>

<dependency>
<!-- <dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor</artifactId>
<version>${org.codehaus.castor.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependency> -->

<!-- https://mvnrepository.com/artifact/org.codehaus.castor/castor -->
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor</artifactId>
<version>1.4.1</version>
<type>pom</type>
</dependency>

<!-- https://mvnrepository.com/artifact/org.codehaus.castor/castor-xml -->
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor-xml</artifactId>
<version>1.4.1</version>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
Expand Down Expand Up @@ -216,7 +231,7 @@
</extension>
</extensions>
</build>
<distributionManagement>
<!-- <distributionManagement>
<repository>
<id>Elotech</id>
<url>http://nexus.elotech.com.br/content/repositories/releases/</url>
Expand All @@ -225,5 +240,5 @@
<id>Elotech</id>
<url>http://nexus.elotech.com.br/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</distributionManagement> -->
</project>
122 changes: 61 additions & 61 deletions src/main/java/br/com/elotech/sits/service/AbstractService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<File> getFilesToSend() {

File fileDirToSend = new File(dirToSend);
Expand All @@ -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;

}

}
1 change: 1 addition & 0 deletions src/main/resources/castor.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.exolab.castor.indent=true
8 changes: 6 additions & 2 deletions src/main/resources/clientWs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,14 @@
</bean>

<bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"
p:mappingLocation="classpath:castor-mapping.xml" />
p:mappingLocation="classpath:castor-mapping.xml">
<property name="whitespacePreserve" value="true" />
</bean>

<bean id="castorMarshallerNfse203" class="org.springframework.oxm.castor.CastorMarshaller"
p:mappingLocation="classpath:castor-wsnfse203-mapping.xml" />
p:mappingLocation="classpath:castor-wsnfse203-mapping.xml">
<property name="whitespacePreserve" value="true" />
</bean>

<bean id="dirToSend" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${ISS.dirToSend}"/>
Expand Down

0 comments on commit ccc1778

Please sign in to comment.