use of org.apache.camel.quarkus.component.xslt.CamelXsltErrorListener in project camel-quarkus by apache.
the class XsltProcessor method xsltResources.
@BuildStep
void xsltResources(CamelXsltConfig config, BuildProducer<XsltGeneratedClassBuildItem> generatedNames, BuildProducer<GeneratedClassBuildItem> generatedClasses, BuildProducer<UriResolverEntryBuildItem> uriResolverEntries) throws Exception {
final Path destination = Files.createTempDirectory(XsltFeature.FEATURE);
final Set<String> translets = new LinkedHashSet<>();
try {
final BuildTimeUriResolver resolver = new BuildTimeUriResolver();
for (String uri : config.sources.orElse(Collections.emptyList())) {
ResolutionResult resolvedUri = resolver.resolve(uri);
uriResolverEntries.produce(resolvedUri.toBuildItem());
if (translets.contains(resolvedUri.transletClassName)) {
throw new RuntimeException("XSLT translet name clash: cannot add '" + resolvedUri.transletClassName + "' to previously added translets " + translets);
}
translets.add(resolvedUri.transletClassName);
try {
TransformerFactory tf = new XalanTransformerFactory();
for (Map.Entry<String, Boolean> entry : config.features.entrySet()) {
tf.setFeature(entry.getKey(), entry.getValue());
}
tf.setAttribute("generate-translet", true);
tf.setAttribute("translet-name", resolvedUri.transletClassName);
tf.setAttribute("package-name", config.packageName);
tf.setAttribute("destination-directory", destination.toString());
tf.setErrorListener(new CamelXsltErrorListener());
tf.newTemplates(resolvedUri.source);
} catch (TransformerException e) {
throw new RuntimeException("Could not compile XSLT " + uri, e);
}
}
Files.walk(destination).sorted(Comparator.reverseOrder()).filter(Files::isRegularFile).filter(path -> path.toString().endsWith(".class")).forEach(path -> {
try {
final Path rel = destination.relativize(path);
final String fqcn = StringUtils.removeEnd(rel.toString(), ".class").replace(File.separatorChar, '.');
final byte[] data = Files.readAllBytes(path);
generatedClasses.produce(new GeneratedClassBuildItem(false, fqcn, data));
generatedNames.produce(new XsltGeneratedClassBuildItem(fqcn));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} finally {
Files.walk(destination).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}
}
Aggregations