public TreeVisitor getVisitor()

in camel-upgrade-recipes/src/main/java/org/apache/camel/upgrade/camel40/yaml/CamelYamlStepsInFromRecipe.java [79:142]


    public TreeVisitor<?, ExecutionContext> getVisitor() {

        return new AbstractCamelYamlVisitor() {
            //both variables has to be set to null, to mark the migration done
            Yaml.Mapping from = null;
            Yaml.Mapping.Entry steps = null;

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

            @Override
            public Yaml.Mapping.Entry doVisitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext context) {
                Yaml.Mapping.Entry e = super.doVisitMappingEntry(entry, context);

                if (steps == null && (MATCHER_WITH_ROUTE.matches(getCursor()) || MATCHER_WITHOUT_ROUTE.matches(getCursor()))) {
                    steps = e;
                    if (from != null) {
                        moveSteps();
                    }
                    return null;
                }
                return e;

            }

            @Override
            public Yaml.Mapping doVisitMapping(Yaml.Mapping mapping, ExecutionContext context) {
                Yaml.Mapping m = super.doVisitMapping(mapping, context);

                String prop = RecipesUtil.getProperty(getCursor());
                if (("route.from".equals(prop) || "from".equals(prop)) && from == null) {
                    from = m;
                    if (steps != null) {
                        moveSteps();
                    }
                }

                return m;
            }

            private void moveSteps() {
                doAfterVisit(new YamlIsoVisitor<ExecutionContext>() {

                    @Override
                    public Yaml.Mapping visitMapping(Yaml.Mapping mapping, ExecutionContext c) {
                        Yaml.Mapping m = super.visitMapping(mapping, c);

                        if (m == from) {
                            List<Yaml.Mapping.Entry> entries = new ArrayList<>(m.getEntries());
                            entries.add(steps.copyPaste().withPrefix("\n"));
                            m = m.withEntries(entries);
                        }

                        return m;
                    }
                });

                //TODO might probably change indent in original file, may this happen?
                doAfterVisit(new IndentsVisitor(new IndentsStyle(2), null));
            }
        };
    }