0

I am using Spring Integration. I get a string (payload) like below:

<Element>
<Sub-Element>5</Sub-Element>
</Element>

I need to test if above string starts with <Element><Sub-Element> which is actually <Element>\r\n <Sub-Element>.

<int:recipient-list-router id="customRouter" input-channel="routingChannel">
    <int:recipient channel="channel1" selector-expression="payload.startsWith('&lt;Element&gt;&lt;Sub-Element&gt;')"/>
    <int:recipient channel="channel2" selector-expression="!payload.startsWith('&lt;Element&gt;&lt;Sub-Element&gt;')"/>
</int:recipient-list-router>

Ideally the first router should pass the test but in this case its failing. Can anyone help me finding out what is the SpEL equivalent of \r \n etc ?

Suvasis
  • 1,451
  • 4
  • 24
  • 42

2 Answers2

0

SpEL doesn't have escapes for those, but you can use regular expressions to do the selection...

<recipient selector-expression="payload matches '&lt;Element&gt;\r\n&lt;Sub-Element&gt;.*'" channel="channel1"/>
<recipient selector-expression="!(payload matches '&lt;Element&gt;\r\n&lt;Sub-Element&gt;.*')" channel="channel2"/>

If you are not familiar with regex, the .* at the end matches anything (hence this regex is the equivalent of startsWith()).

EDIT:

While this will work, I feel I should point out that relying on specific values in insignificant white space in XML documents is brittle - if the client changes to use, say \n instead, or even no whitespace, your application will break. You should consider using something like an <int-xml:xpath-router/> instead.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Does selector-expression understand\compile "\r\n" ? – Suvasis Mar 28 '13 at 19:21
  • No; like I said, SpEL has no way to escape those characters but, when they are in the regex, they are not parsed by SpEL, they are parsed by the regular expression engine. That's why this works. – Gary Russell Mar 28 '13 at 19:42
  • I have the regular expression as "(?s)\\s*(.*)". I tried below code in selector-expression. selector-expression="payload.getBufferData() matches ('(?s)<Element>\\s*<Sub>(.*)')" which gives expection as Caused by: org.springframework.expression.spel.SpelParseException: EL1044E:(pos 61): Unexpectedly ran out of input – Suvasis Apr 02 '13 at 07:36
  • You need `\s` not `\\s`. Also you don't need captures `()`. What is `(?s)` supposed to do?. – Gary Russell Apr 02 '13 at 12:21
  • U are right Gary. In Java Class we give additional '\', its not required in selector-expression. \s works fine. – Suvasis Apr 02 '13 at 12:32
0

Thanks Gary. So the working list-recipient-router looks like

Either

<recipient selector-expression="payload matches '(?s)&lt;Element&gt;(\s*)&lt;Sub&gt;(.*)'" channel="channel1"/>
<recipient selector-expression="!(payload matches '(?s)&lt;Element&gt;(\s*)&lt;Sub&gt;(.*)')" channel="channel2"/>

Or

<recipient selector-expression="payload matches '(?s)&lt;Element&gt;(.*)&lt;Sub&gt;(.*)'" channel="channel1"/>
    <recipient selector-expression="!(payload matches '(?s)&lt;Element&gt;(.*)&lt;Sub&gt;(.*)')" channel="channel2"/>

May keep captures () or may not. Both works.

Suvasis
  • 1,451
  • 4
  • 24
  • 42