diff --git a/buildSrc/src/main/groovy/trang/TrangPlugin.groovy b/buildSrc/src/main/groovy/trang/TrangPlugin.groovy deleted file mode 100644 index e8bb3d8077..0000000000 --- a/buildSrc/src/main/groovy/trang/TrangPlugin.groovy +++ /dev/null @@ -1,59 +0,0 @@ -package trang; - -import com.thaiopensource.relaxng.translate.Driver - -import javax.xml.transform.Transformer -import javax.xml.transform.TransformerFactory -import javax.xml.transform.stream.StreamSource -import javax.xml.transform.stream.StreamResult - -import org.gradle.api.*; -import org.gradle.api.tasks.* -import org.gradle.api.file.FileCollection - -/** - * Used for converting .rnc files to .xsd files. - * @author Rob Winch - */ -class TrangPlugin implements Plugin { - public void apply(Project project) { - Task rncToXsd = project.tasks.create('rncToXsd', RncToXsd.class) - rncToXsd.description = 'Converts .rnc to .xsd' - rncToXsd.group = 'Build' - } -} - -/** - * Converts .rnc files to .xsd files using trang and then applies an xsl file to cleanup the results. - */ -public class RncToXsd extends DefaultTask { - @InputDirectory - File rncDir - - @InputFile - File xslFile - - @OutputDirectory - File xsdDir - - @TaskAction - public final void transform() { - String xslPath = xslFile.absolutePath - rncDir.listFiles( { dir, file -> file.endsWith('.rnc')} as FilenameFilter).each { rncFile -> - File xsdFile = new File(xsdDir, rncFile.name.replace('.rnc', '.xsd')) - String xsdOutputPath = xsdFile.absolutePath - new Driver().run([rncFile.absolutePath, xsdOutputPath] as String[]); - - TransformerFactory tFactory = new net.sf.saxon.TransformerFactoryImpl() - Transformer transformer = - tFactory.newTransformer(new StreamSource(xslPath)) - File temp = File.createTempFile("gradle-trang-" + xsdFile.name, ".xsd") - xsdFile.withInputStream { is -> - temp << is - } - StreamSource xmlSource = new StreamSource(temp) - transformer.transform(xmlSource, new StreamResult(xsdFile)) - temp.delete() - } - } -} \ No newline at end of file diff --git a/buildSrc/src/main/java/trang/RncToXsd.java b/buildSrc/src/main/java/trang/RncToXsd.java new file mode 100644 index 0000000000..03185d5d33 --- /dev/null +++ b/buildSrc/src/main/java/trang/RncToXsd.java @@ -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(); + } + } + } +} diff --git a/buildSrc/src/main/java/trang/TrangPlugin.java b/buildSrc/src/main/java/trang/TrangPlugin.java new file mode 100644 index 0000000000..39feb412ed --- /dev/null +++ b/buildSrc/src/main/java/trang/TrangPlugin.java @@ -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 { + @Override + public void apply(Project project) { + Task rncToXsd = project.getTasks().create("rncToXsd", RncToXsd.class); + rncToXsd.setDescription("Converts .rnc to .xsd"); + rncToXsd.setGroup("Build"); + } +}