Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/******************************************************************************
* Copyright 2009-2018 Exactpro (Exactpro Systems Limited)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.exactpro.sf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.io.IOUtils;
import org.gradle.api.AntBuilder;
import org.gradle.api.DefaultTask;
import org.gradle.api.Project;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.TaskExecutionException;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import groovy.lang.Closure;
/**
* @author oleg.smirnov
*
*/
public class ConvertSailfishDictionaryToQuickfixj extends DefaultTask {
@InputDirectory
private File inputDirectory;
@OutputDirectory
private File outputDirectory;
@Input
private String filterExpression;
@SuppressWarnings("serial")
@TaskAction
public void generateXmlQuicfixj()
throws TaskExecutionException, IOException, ParserConfigurationException, SAXException {
Map fileTreeArgs = new HashMap() {
{
put("dir", inputDirectory);
put("include", "*.xml");
}
};
Project project = getProject();
final AntBuilder antBuilder = project.getAnt();
FileTree dictionaries = project.fileTree(fileTreeArgs);
final String configurationPath = project.getConfigurations().getByName("compile").getAsPath();
Map xsltArgs = new HashMap() {
{
put("extension", ".xml");
put("classpath", configurationPath);
put("basedir", inputDirectory);
put("destdir", outputDirectory);
}
};
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final DocumentBuilder db = dbf.newDocumentBuilder();
ClassLoader classLoader = ConvertFixDictionary.class.getClassLoader();
try (InputStream xslResource = classLoader.getResourceAsStream("fix/slf2qfjDict.xsl");
InputStream typesResource = classLoader.getResourceAsStream("fix/types.xml")) {
File tmpDir = getTemporaryDir();
File xslFile = new File(tmpDir, "slf2qfjDict.xsl");
File typesFile = new File(tmpDir, "types.xml");
try (FileOutputStream xslOut = new FileOutputStream(xslFile);
FileOutputStream typesOut = new FileOutputStream(typesFile)) {
IOUtils.copy(xslResource, xslOut);
IOUtils.copy(typesResource, typesOut);
}
xsltArgs.put("style", xslFile);
final Pattern pattern = Pattern.compile(filterExpression);
for (File dictionary : dictionaries) {
final String dictionaryName = dictionary.getName();
if (!pattern.matcher(dictionaryName).matches()) {
continue;
}
xsltArgs.put("includes", dictionaryName);
Node node = db.parse(dictionary).getElementsByTagName("dictionary").item(0);
String[] version = node.getAttributes().getNamedItem("name").getNodeValue().split("_");
final String minor = version[version.length - 1], major = version[version.length - 2];
antBuilder.invokeMethod("xslt", new Object[] { xsltArgs, new Closure