public TreeVisitor getVisitor()

in camel-upgrade-recipes/src/main/java/org/apache/camel/upgrade/camel46/YamlDsl46Recipe.java [72:125]


    public TreeVisitor<?, ExecutionContext> getVisitor() {

        return new AbstractCamelYamlVisitor() {

            @Override
            protected void clearLocalCache() {
                //nothing to do
            }

            @Override
            public Yaml.Mapping.Entry doVisitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) {
                Yaml.Mapping.Entry e = super.doVisitMappingEntry(entry, ctx);
                //resolve path
                String path = RecipesUtil.getProperty(getCursor());

                //remove unnecessary children and switch Sequence to Mapping
                if(path.endsWith("beans.property")) {
                    //it is intentional to fail if the casting is wrong, in that case the Abstract*Visitor
                    //logs warning about failure and no migration is applied
                    Yaml.Mapping m = (Yaml.Mapping) ((Yaml.Sequence)e.getValue()).getEntries().get(0).getBlock();
                    //apply correct prefix for the new 'properties' values
                    String prefix = e.getPrefix() + "  ";
                    List<Yaml.Mapping.Entry> entries = ((Yaml.Sequence)e.getValue()).getEntries().stream()
                            //iterate over all blocks
                            .flatMap(entry1 -> ((Yaml.Mapping)entry1.getBlock()).getEntries().stream())
                            //apply correct prefix
                            .map(entry2 -> entry2.withPrefix(prefix))
                            .collect(Collectors.toList());

                    return e.withKey(((Yaml.Scalar) e.getKey().copyPaste()).withValue("properties"))
                            .withValue(m.copyPaste().withEntries(entries));
                }


                //property key is saved
                if(path.matches(".*beans.property.key") && e.getValue() instanceof Yaml.Scalar) {
                    //save value into parent context, so the value can reach it
                    getCursor().getParent(4).putMessage("key", ((Yaml.Scalar) e.getValue()).getValue());
                    //return original key, which will be removed in the code above
                    return null;
                }
                //property 'value' is replaced by the key from previous
                if(path.matches(".*beans.property.value")  && e.getValue() instanceof Yaml.Scalar && getCursor().getNearestMessage("key") != null) {
                    String key = getCursor().getNearestMessage("key");
                    if(key != null) {
                        return e.withKey(((Yaml.Scalar) e.getKey().copyPaste()).withValue(key));
                    }
                }

                return e;
            }

        };
    }