|
| 1 | +package trang; |
| 2 | + |
| 3 | +import com.thaiopensource.relaxng.translate.Driver; |
| 4 | +import net.sf.saxon.TransformerFactoryImpl; |
| 5 | +import org.gradle.api.DefaultTask; |
| 6 | +import org.gradle.api.tasks.InputDirectory; |
| 7 | +import org.gradle.api.tasks.InputFile; |
| 8 | +import org.gradle.api.tasks.OutputDirectory; |
| 9 | +import org.gradle.api.tasks.TaskAction; |
| 10 | + |
| 11 | +import javax.xml.transform.Transformer; |
| 12 | +import javax.xml.transform.TransformerException; |
| 13 | +import javax.xml.transform.TransformerFactory; |
| 14 | +import javax.xml.transform.stream.StreamResult; |
| 15 | +import javax.xml.transform.stream.StreamSource; |
| 16 | +import java.io.File; |
| 17 | +import java.io.IOException; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.StandardCopyOption; |
| 20 | + |
| 21 | +/** |
| 22 | + * Converts .rnc files to .xsd files using trang and then applies an xsl file to cleanup the results. |
| 23 | + */ |
| 24 | +public class RncToXsd extends DefaultTask { |
| 25 | + |
| 26 | + private File rncDir; |
| 27 | + |
| 28 | + private File xslFile; |
| 29 | + |
| 30 | + private File xsdDir; |
| 31 | + |
| 32 | + @InputDirectory |
| 33 | + public File getRncDir() { |
| 34 | + return rncDir; |
| 35 | + } |
| 36 | + |
| 37 | + public void setRncDir(File rncDir) { |
| 38 | + this.rncDir = rncDir; |
| 39 | + } |
| 40 | + |
| 41 | + @InputFile |
| 42 | + public File getXslFile() { |
| 43 | + return xslFile; |
| 44 | + } |
| 45 | + |
| 46 | + public void setXslFile(File xslFile) { |
| 47 | + this.xslFile = xslFile; |
| 48 | + } |
| 49 | + |
| 50 | + @OutputDirectory |
| 51 | + public File getXsdDir() { |
| 52 | + return xsdDir; |
| 53 | + } |
| 54 | + |
| 55 | + public void setXsdDir(File xsdDir) { |
| 56 | + this.xsdDir = xsdDir; |
| 57 | + } |
| 58 | + |
| 59 | + @TaskAction |
| 60 | + public final void transform() throws IOException, TransformerException { |
| 61 | + String xslPath = xslFile.getAbsolutePath(); |
| 62 | + |
| 63 | + File[] files = rncDir.listFiles((dir, file) -> file.endsWith(".rnc")); |
| 64 | + if(files != null) { |
| 65 | + for (File rncFile : files) { |
| 66 | + File xsdFile = new File(xsdDir, rncFile.getName().replace(".rnc", ".xsd")); |
| 67 | + String xsdOutputPath = xsdFile.getAbsolutePath(); |
| 68 | + |
| 69 | + new Driver().run(new String[]{rncFile.getAbsolutePath(), xsdOutputPath}); |
| 70 | + |
| 71 | + TransformerFactory tFactory = new TransformerFactoryImpl(); |
| 72 | + Transformer transformer = tFactory.newTransformer(new StreamSource(xslPath)); |
| 73 | + |
| 74 | + File temp = File.createTempFile("gradle-trang-" + xsdFile.getName(), ".xsd"); |
| 75 | + |
| 76 | + Files.copy(xsdFile.toPath(), temp.toPath(), StandardCopyOption.REPLACE_EXISTING); |
| 77 | + StreamSource xmlSource = new StreamSource(temp); |
| 78 | + transformer.transform(xmlSource, new StreamResult(xsdFile)); |
| 79 | + temp.delete(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments