4

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.

1
  • Also just to mention, I am unsure how to implement the JavaCast function within context of my code and whether this will be of any help.
    – Michael
    Sep 10, 2012 at 16:03

1 Answer 1

6

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.