Skip to content

Commit 3d2542c

Browse files
larsgreferrwinch
authored andcommitted
Migrate TrangPlugin groovy->java
Issue: gh-4939
1 parent 3ea9d37 commit 3d2542c

File tree

3 files changed

+101
-59
lines changed

3 files changed

+101
-59
lines changed

buildSrc/src/main/groovy/trang/TrangPlugin.groovy

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package trang;
2+
3+
import org.gradle.api.Plugin;
4+
import org.gradle.api.Project;
5+
import org.gradle.api.Task;
6+
7+
/**
8+
* Used for converting .rnc files to .xsd files.
9+
* @author Rob Winch
10+
*/
11+
public class TrangPlugin implements Plugin<Project> {
12+
@Override
13+
public void apply(Project project) {
14+
Task rncToXsd = project.getTasks().create("rncToXsd", RncToXsd.class);
15+
rncToXsd.setDescription("Converts .rnc to .xsd");
16+
rncToXsd.setGroup("Build");
17+
}
18+
}

0 commit comments

Comments
 (0)