Custom splitter

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom splitter

Ron Cecchini
Hi, all.  Since the list is quiet I thought I'd run something by you.

I wrote a custom splitter that's working just fine, so I really don't need any help.

But I was curious if I could have done things simpler with some combination of split(), tokenize() and Simple, etc.?

~~~

I hit an endpoint that returns a String representing a list of lists of the form:

[ [ "line 1", "line 2" ], [ "line 1", "line 2" ], ... [ "line 1", "line 2" ] ]

The goal was to iterate on the inner-lists and pass each "line 1"/"line 2" pair to a processor.

I couldn't quite get split() to work as-is, and tried playing around a bit with tokenize().

Ultimately, I created the following custom splitter, which creates an array of strings which can be split on, and added it to my route with .split().method("MySplitter", "splitList").

But could I have done something simpler?

Thanks.

~~~

    /* Converts a string representing a lists of lists into an array of strings of the form:
       "line 1", "line 2"
       ...
       "line 1", "line 2"
    */
    public List<String> splitList (String body)
    {
        String lists = body.trim();
        lists = lists.substring(1, lists.length()-1);
        List<String> answer = new ArrayList<String>();
        for ( String list : lists.split("],") ) {
            answer.add(list.replace("[", "").replace("]", "").trim());
        }
        return answer;
    }
Reply | Threaded
Open this post in threaded view
|

Re: Custom splitter

Claus Ibsen-2
Hi

Probably not as if your input data is string based then you need to
parse it and then a one-liner is not easy when its all sorts of combos
of lists enclosed in brackets etc.

If you are a regexp ninja you may come up with something that can
iterate it in a one liner. But then again 5 lines of java code you can
make into a util class and have an unit test is a great solution -
that anybody can understand and maintain.

On Thu, Jul 9, 2020 at 5:26 PM Ron Cecchini <[hidden email]> wrote:

>
> Hi, all.  Since the list is quiet I thought I'd run something by you.
>
> I wrote a custom splitter that's working just fine, so I really don't need any help.
>
> But I was curious if I could have done things simpler with some combination of split(), tokenize() and Simple, etc.?
>
> ~~~
>
> I hit an endpoint that returns a String representing a list of lists of the form:
>
> [ [ "line 1", "line 2" ], [ "line 1", "line 2" ], ... [ "line 1", "line 2" ] ]
>
> The goal was to iterate on the inner-lists and pass each "line 1"/"line 2" pair to a processor.
>
> I couldn't quite get split() to work as-is, and tried playing around a bit with tokenize().
>
> Ultimately, I created the following custom splitter, which creates an array of strings which can be split on, and added it to my route with .split().method("MySplitter", "splitList").
>
> But could I have done something simpler?
>
> Thanks.
>
> ~~~
>
>     /* Converts a string representing a lists of lists into an array of strings of the form:
>        "line 1", "line 2"
>        ...
>        "line 1", "line 2"
>     */
>     public List<String> splitList (String body)
>     {
>         String lists = body.trim();
>         lists = lists.substring(1, lists.length()-1);
>         List<String> answer = new ArrayList<String>();
>         for ( String list : lists.split("],") ) {
>             answer.add(list.replace("[", "").replace("]", "").trim());
>         }
>         return answer;
>     }



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2