Added backend support for multiple files in XSLT. #272
@@ -4,15 +4,11 @@ import com.google.gson.Gson;
|
||||
import com.r11.tools.controller.internal.*;
|
||||
import com.r11.tools.model.XMLMultipleFilesBody;
|
||||
import com.r11.tools.model.XMLResponseBody;
|
||||
import com.r11.tools.xml.Saxon;
|
||||
import com.r11.tools.xml.XmlEngine;
|
||||
import net.sf.saxon.s9api.SaxonApiException;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import spark.Request;
|
||||
import spark.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@GlobalControllerManifest
|
||||
public class MultipleXMLController implements RestController {
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.r11.tools.controller;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.r11.tools.controller.internal.*;
|
||||
import com.r11.tools.model.XMLMultipleFilesBody;
|
||||
import com.r11.tools.model.XMLRequestBody;
|
||||
|
|
||||
import com.r11.tools.model.XMLResponseBody;
|
||||
import com.r11.tools.model.XPathQueryResult;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.r11.tools.controller.internal;
|
||||
|
||||
import com.r11.tools.model.XMLMultipleFilesBody;
|
||||
import com.r11.tools.model.XMLRequestBody;
|
||||
import com.r11.tools.xml.Saxon;
|
||||
import com.r11.tools.xml.XmlEngine;
|
||||
|
bema
commented
Unused import, removal needed Unused import, removal needed
|
||||
import spark.Response;
|
||||
|
bema
commented
Same as above Same as above
|
||||
|
||||
|
||||
@@ -24,27 +24,16 @@ public class Saxon implements XmlEngine{
|
||||
* @param transform XSLT
|
||||
* @return transformed xml
|
||||
* @throws SaxonApiException thrown on stylesheet or transformation error
|
||||
* @throws IOException thrown when file does not exist, or cannot be read.
|
||||
*/
|
||||
|
bema
commented
I would extract some parts of the code from this method to separate private methods to increase readability I would extract some parts of the code from this method to separate private methods to increase readability
|
||||
public String processXSLT(XMLMultipleFilesData[] data, String transform) throws SaxonApiException, IOException{
|
||||
Processor processor = new Processor(false);
|
||||
|
||||
XsltCompiler compiler = processor.newXsltCompiler();
|
||||
|
||||
String filesPath = "/tmp/"+UUID.randomUUID()+"/";
|
||||
try{
|
||||
Path processPath = Paths.get(filesPath);
|
||||
Files.createDirectories(processPath);
|
||||
|
||||
for (XMLMultipleFilesData fileData : data) {
|
||||
Path filePath = Files.createFile( Paths.get(filesPath + fileData.getFilename() ) );
|
||||
try (FileWriter writer = new FileWriter(filePath.toFile())) {
|
||||
writer.write(fileData.getData());
|
||||
}
|
||||
}
|
||||
Path transformPath = Files.createFile( Paths.get(filesPath + "transform.xsl") );
|
||||
FileWriter writer = new FileWriter(transformPath.toFile());
|
||||
writer.write(transform);
|
||||
writer.close();
|
||||
createXMLFilesFromData(data, filesPath);
|
||||
Path transformPath = createXSLTFileAndReturnPath(transform,filesPath);
|
||||
XsltExecutable stylesheet = compiler.compile( new StreamSource( transformPath.toFile() ));
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
@@ -54,17 +43,39 @@ public class Saxon implements XmlEngine{
|
||||
Xslt30Transformer transformer = stylesheet.load30();
|
||||
transformer.transform( new StreamSource( new File(filesPath+data[0].getFilename()) ) , out );
|
||||
return sw.toString();
|
||||
|
||||
} finally {
|
||||
Files
|
||||
.walk( Paths.get(filesPath) )
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
deleteTemporaryFiles(filesPath);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void createXMLFilesFromData( XMLMultipleFilesData[] data , String filesPath ) throws IOException {
|
||||
Files.createDirectories(Paths.get(filesPath));
|
||||
for (XMLMultipleFilesData fileData : data) {
|
||||
Path filePath = Files.createFile( Paths.get(filesPath + fileData.getFilename() ) );
|
||||
try (FileWriter writer = new FileWriter(filePath.toFile())) {
|
||||
writer.write(fileData.getData());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Path createXSLTFileAndReturnPath( String xsltTransform, String filesPath ) throws IOException {
|
||||
Path transformPath = Files.createFile( Paths.get(filesPath + "transform.xsl") );
|
||||
FileWriter writer = new FileWriter(transformPath.toFile());
|
||||
writer.write(xsltTransform);
|
||||
writer.close();
|
||||
|
||||
return transformPath;
|
||||
}
|
||||
|
||||
private void deleteTemporaryFiles(String filesPath) throws IOException {
|
||||
Files
|
||||
.walk( Paths.get(filesPath) )
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms string containing xml document via xslt
|
||||
* @param data xml to be transformed
|
||||
|
||||
Reference in New Issue
Block a user
This is unneeded, so should be removed