Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate TrangPlugin groovy->java #7077

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions buildSrc/src/main/groovy/trang/TrangPlugin.groovy

This file was deleted.

83 changes: 83 additions & 0 deletions buildSrc/src/main/java/trang/RncToXsd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package trang;

import com.thaiopensource.relaxng.translate.Driver;
import net.sf.saxon.TransformerFactoryImpl;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.InputDirectory;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
* Converts .rnc files to .xsd files using trang and then applies an xsl file to cleanup the results.
*/
public class RncToXsd extends DefaultTask {

private File rncDir;

private File xslFile;

private File xsdDir;

@InputDirectory
public File getRncDir() {
return rncDir;
}

public void setRncDir(File rncDir) {
this.rncDir = rncDir;
}

@InputFile
public File getXslFile() {
return xslFile;
}

public void setXslFile(File xslFile) {
this.xslFile = xslFile;
}

@OutputDirectory
public File getXsdDir() {
return xsdDir;
}

public void setXsdDir(File xsdDir) {
this.xsdDir = xsdDir;
}

@TaskAction
public final void transform() throws IOException, TransformerException {
String xslPath = xslFile.getAbsolutePath();

File[] files = rncDir.listFiles((dir, file) -> file.endsWith(".rnc"));
if(files != null) {
for (File rncFile : files) {
File xsdFile = new File(xsdDir, rncFile.getName().replace(".rnc", ".xsd"));
String xsdOutputPath = xsdFile.getAbsolutePath();

new Driver().run(new String[]{rncFile.getAbsolutePath(), xsdOutputPath});

TransformerFactory tFactory = new TransformerFactoryImpl();
Transformer transformer = tFactory.newTransformer(new StreamSource(xslPath));

File temp = File.createTempFile("gradle-trang-" + xsdFile.getName(), ".xsd");

Files.copy(xsdFile.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
StreamSource xmlSource = new StreamSource(temp);
transformer.transform(xmlSource, new StreamResult(xsdFile));
temp.delete();
}
}
}
}
18 changes: 18 additions & 0 deletions buildSrc/src/main/java/trang/TrangPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package trang;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;

/**
* Used for converting .rnc files to .xsd files.
* @author Rob Winch
*/
public class TrangPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
Task rncToXsd = project.getTasks().create("rncToXsd", RncToXsd.class);
rncToXsd.setDescription("Converts .rnc to .xsd");
rncToXsd.setGroup("Build");
}
}