0

I have a simple groovy script where I want to escape a String for XML...

 @Grapes(@Grab(group='org.apache.commons', module='commons-lang3',version='3.9'))
import org.apache.commons.lang3.StringEscapeUtils

 def value = "[Apple MacBook Pro with Touch Bar - 15.4 & Core i7 - 16 GB RAM - 512 GB SSD]"
 StringEscapeUtils.escapeXml11​(value.toString())

As per the docs, it looks like this method just needs a String, so why is it giving this exception...

 No signature of method: static org.apache.commons.lang3.StringEscapeUtils.escapeXml11​() is applicable for argument types: (String) values: [[Apple MacBook Pro with Touch Bar - 15.4 & Core i7 - 16 GB RAM - 512 GB SSD]]
AbuMariam
  • 3,282
  • 13
  • 49
  • 82
  • correction: `escapeXml10​` introduced in version 3.3, so probably you have older version of library. in this case use the deprecated version of method: `escapeXml` – daggett Jun 27 '19 at 15:18
  • That's `escapeXml` that's deprecated, not `escapeXml10`. And you're discussing different libraries; commons.lang and commons.text are not the same packages. They may run the same (or similar; text explicitly states it was "adapted" from lang 3.5) code (haven't checked), but they're not the same thing. – Dave Newton Jun 27 '19 at 15:46
  • @DaveNewton, and Victor, I updated my code to use commons.lang but I am still getting the error. – AbuMariam Jun 27 '19 at 16:03

2 Answers2

1

I have tested and received the same error message, but I changed the method to escapeXml11 and it works now:

import org.apache.commons.lang3.StringEscapeUtils

class Program {
    static void main(String[] args) {
        testStringUtils()
    }

    private static void testStringUtils() {
        def value = "[Apple MacBook Pro with Touch Bar - 15.4 & Core i7 - 16 GB RAM - 512 GB SSD]"
        def sample = StringEscapeUtils.escapeXml11(value)
        println(sample)
    }
}

I am using version 3.9:

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'

This is the output:

> Task :Program.main()
[Apple MacBook Pro with Touch Bar - 15.4 & Core i7 - 16 GB RAM - 512 GB SSD]

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
17:24:42: Task execution finished 'Program.main()'.
Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
1

You have a non-printable character in your code:

  Offset: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F   
00000000: 53 74 72 69 6E 67 45 73 63 61 70 65 55 74 69 6C    StringEscapeUtil
00000010: 73 2E 65 73 63 61 70 65 58 6D 6C 31 31 E2 80 8B    s.escapeXml11b..
00000020: 28 76 61 6C 75 65 2E 74 6F 53 74 72 69 6E 67 28    (value.toString(
00000030: 29 29 0A                                           )).

See after the Xml11?

It works fine with normal characters.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • how the heck did that get in there? Maybe from a copy and paste from the docs? – AbuMariam Jun 27 '19 at 17:55
  • @AbuMariam Most likely--cutting and pasting from web/visual editors often introduces utter madness for no apparent reason. It was one of those things where it's obvious it *should* work, so I generally check assumptions immediately! – Dave Newton Jun 27 '19 at 17:57