Michael
Michael

Reputation: 1833

ColdFusion and JSoup - The addTags method was not found error

I am trying to use JSoup with ColdFusion to clean up some HTML but am encountering the following error:

The addTags method was not found. Either there are no methods with the specified method name and argument types or the addTags method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity.

My code is as follows:

<cfset jsoup = createObject('java','org.jsoup.Jsoup')>
<cfset Whitelist = createObject("java", "org.jsoup.safety.Whitelist")>

<cfset parsedhtml = jsoup.parse(form.contentrichtext)> 
<cfset post = parsedhtml.body().html()>
<cfset post = jsoup.clean(post, Whitelist.none().addTags("span"))>

I have dumped out the Whitelist object and the add Tags method is present. If I remove the addTags() method and use one of the standard JSoup Whitelists such as basic(), none() or relaxed() then the code runs perfectly. As far as I can see from other online examples this is the correct syntax for using the addTags() method.

I am fairly new to using Java objects within ColdFusion so this has got me stumped.

Any help would be greatly appreciated.

Thanks, Michael.

Upvotes: 4

Views: 924

Answers (1)

Leigh
Leigh

Reputation: 28873

The addTags method expects an array of strings, not just a single string. Put the value into an array first:

<!--- create a CF array then cast it as type string[] --->  
<cfset tagArray = javacast("string[]", ["span"]) >
<cfset post = jsoup.clean(post, Whitelist.none().addTags( tagArray ))>

Edit:

As far as I can see from other online examples this is the correct syntax

To clarify, that is the correct syntax - for java. In java you can pass in a variable number of arguments using either an array or this syntax: addTags("tag1", "tag2", ...). However, CF only supports the array syntax. So if you cfdump the java object, you will see square brackets after the class name, which indicates the argument is an array:

     method:  addTags( java.lang.String[] )  // array of strings

Upvotes: 6

Related Questions