This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 6f89f0719da Automatically transform to xml if all files are yaml (#13331) 6f89f0719da is described below commit 6f89f0719da02d2b8c124b5575a896d41621e9b8 Author: Federico Mariani <34543311+cro...@users.noreply.github.com> AuthorDate: Wed Feb 28 11:02:15 2024 +0100 Automatically transform to xml if all files are yaml (#13331) * Automatically transform to xml if all files are yaml * Add documentation --- .../modules/ROOT/pages/camel-jbang.adoc | 71 ++++++++++++++++++++++ .../dsl/jbang/core/commands/TransformRoute.java | 8 ++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc index 9911483c131..ae3e4ae6f8a 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-jbang.adoc @@ -2715,6 +2715,77 @@ TIP: You can specify multiple options by repeating the `--option` argument. NOTE: The transform message with dataformat is limited as some dataformat requires configuring complex options that cannot be set from command line. +== Transforming routes DSL + +Camel offers different DSLs, such as Java, XML, YAML, and more. Many graphical editors and code generators use XML and YAML Camel DSL, therefore, for some use cases it may be required to use one DSL or the other. +The `camel transform route` command can be used to transform a YAML route into an XML and vice versa, the command takes a list of files as input and transforms the DSL. + +For example, given the following YAML content, in a file named route.yaml + +[source:yaml] +---- +- route: + id: route-b785 + nodePrefixId: route-14b + from: + id: from-959e + uri: direct + parameters: + name: start + steps: + - choice: + id: choice-52fe + when: + - id: when-b126 + expression: + simple: + id: simple-576d + expression: ${header.foo} == 'bar' + steps: + - to: + id: to-65f9 + uri: jms + parameters: + destinationName: queue + otherwise: + id: otherwise-c07f + steps: + - to: + id: to-dc76 + uri: log + parameters: + loggerName: default +---- + +The following command + +[source:bash] +---- +camel transform route --format=xml /path/to/route.yaml +---- + +Automatically transform the route in XML DSL (Camel XML IO DSL) + +[source:xml] +---- +<camel> + <route id="route-b785" nodePrefixId="route-14b"> + <from id="from-959e" uri="direct:start"/> + <choice id="choice-52fe"> + <when id="when-b126"> + <simple>${header.foo} == 'bar'</simple> + <to id="to-65f9" uri="jms:queue"/> + </when> + <otherwise id="otherwise-c07f"> + <to id="to-dc76" uri="log:default"/> + </otherwise> + </choice> + </route> +</camel> +---- + +TIP: If all the input files have the same extension and are written with the same DSL, for example, `.yaml`, the transformation will default to the XML DSL and vice versa. + == Listing what Camel components is available Camel comes with a lot of artifacts out of the box which comes as: diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/TransformRoute.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/TransformRoute.java index 35c522d5a8b..dc95a60e70d 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/TransformRoute.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/TransformRoute.java @@ -45,7 +45,7 @@ public class TransformRoute extends CamelCommand { private String output; @CommandLine.Option(names = { "--format" }, - description = "Output format (xml or yaml)", defaultValue = "yaml") + description = "Output format (xml or yaml), if only yaml files are provided, the format defaults to xml and vice versa", defaultValue = "yaml") String format = "yaml"; @CommandLine.Option(names = { "--resolve-placeholders" }, defaultValue = "false", @@ -67,6 +67,12 @@ public class TransformRoute extends CamelCommand { @Override public Integer doCall() throws Exception { + // Automatically transform to xml if all files are yaml + if (files.stream().allMatch(file -> file.endsWith(".yaml"))) { + format = "xml"; + } else { + format = "yaml"; + } String dump = output; // if no output then we want to print to console, so we need to write to a hidden file, and dump that file afterwards