Search in sources :

Example 1 with XalanTransformerFactory

use of org.apache.camel.quarkus.support.xalan.XalanTransformerFactory 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);
    }
}
Also used : Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Record(io.quarkus.deployment.annotations.Record) TransformerException(javax.xml.transform.TransformerException) CamelXsltErrorListener(org.apache.camel.quarkus.component.xslt.CamelXsltErrorListener) XalanTransformerFactory(org.apache.camel.quarkus.support.xalan.XalanTransformerFactory) StringUtils(org.apache.commons.lang3.StringUtils) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) BuildStep(io.quarkus.deployment.annotations.BuildStep) RuntimeValue(io.quarkus.runtime.RuntimeValue) Map(java.util.Map) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Builder(org.apache.camel.quarkus.component.xslt.RuntimeUriResolver.Builder) CamelXsltConfig(org.apache.camel.quarkus.component.xslt.CamelXsltConfig) Files(java.nio.file.Files) CamelBeanBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelBeanBuildItem) ExecutionTime(io.quarkus.deployment.annotations.ExecutionTime) Set(java.util.Set) IOException(java.io.IOException) CamelServiceFilterBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelServiceFilterBuildItem) File(java.io.File) List(java.util.List) ResolutionResult(org.apache.camel.quarkus.component.xslt.deployment.BuildTimeUriResolver.ResolutionResult) GeneratedClassBuildItem(io.quarkus.deployment.builditem.GeneratedClassBuildItem) XsltComponent(org.apache.camel.component.xslt.XsltComponent) TransformerFactory(javax.xml.transform.TransformerFactory) Comparator(java.util.Comparator) CamelXsltRecorder(org.apache.camel.quarkus.component.xslt.CamelXsltRecorder) Collections(java.util.Collections) CamelServiceFilter(org.apache.camel.quarkus.core.deployment.spi.CamelServiceFilter) XalanTransformerFactory(org.apache.camel.quarkus.support.xalan.XalanTransformerFactory) TransformerFactory(javax.xml.transform.TransformerFactory) ResolutionResult(org.apache.camel.quarkus.component.xslt.deployment.BuildTimeUriResolver.ResolutionResult) CamelXsltErrorListener(org.apache.camel.quarkus.component.xslt.CamelXsltErrorListener) IOException(java.io.IOException) GeneratedClassBuildItem(io.quarkus.deployment.builditem.GeneratedClassBuildItem) XalanTransformerFactory(org.apache.camel.quarkus.support.xalan.XalanTransformerFactory) Files(java.nio.file.Files) Map(java.util.Map) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 ExecutionTime (io.quarkus.deployment.annotations.ExecutionTime)1 Record (io.quarkus.deployment.annotations.Record)1 GeneratedClassBuildItem (io.quarkus.deployment.builditem.GeneratedClassBuildItem)1 RuntimeValue (io.quarkus.runtime.RuntimeValue)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 XsltComponent (org.apache.camel.component.xslt.XsltComponent)1 CamelXsltConfig (org.apache.camel.quarkus.component.xslt.CamelXsltConfig)1