Change link text in XDOM with velocity or groovy

Hi,

I have to update links in bulk on my wiki because the underlying URL changed. So I have a ton of links that display http://foo.com/something and point to http://foo.com/something. After the update they should display and point to http://bar.com/something.

I already have a script in velocity that loops over all links and updates the URL but I can’t find a way to update the display.

Is there a way to update displayed value also? It looks like it is composed of some Wordblocks and special character blocks that are children of the link. Is there an easy way to generate a new display value that is the URL?

Thanks.

So you used a regex to replace http://foo.com/ into http://bar.com/but what’s in your content was somehting like [[http~://foo.com>>http://foo.com]] ?

Yes in source mode my links look like this. I looped over all the Linkblocks in the XDOM and replaced the Reference of the ResourceReference, so the URL changed.

LinkBlock like many Blocks is more or less immutable so not so easy to manipulate with Velocity (since you don’t have any syntax to create new java objects). Would be much simpler using Groovy.

I can switch to groovy. Currently I’m cloning the Linkblocks, update the Reference and replace the old Linkblock with the new one (replaceChild). The only thing missing is to update the display text.

Edit: Updated the title, to show that velocity or groovy is possible.

OK so the question ist how ist this done in groovy?

What I would require is the following:
Loop over every link in page
Get full description text
Replace text if it matches my old address.
Set/Update new description

Currently if I work with LinkBlocks I don’t get the full description, only Text/SpecialCharacterBlocks.

Any suggestions?

This is the label. It’s just parsed as any plain text which mean separating special symbols from the rest, basically you should match something like:

  • WordBlock(“http”)
  • SpecialSymbolBlock(‘:’)
  • SpecialSymbolBlock(‘/’)
  • SpecialSymbolBlock(‘/’)
  • WordBlock(“foo”)
  • SpecialSymbolBlock(‘.’)
  • WordBlock(“com”)

That being said I really don’t think I would have used XDOM to do this search & replace. In this specific use case good old regexp based search&replace on the document String content field would be much simpler and there is no much risk to have false positive when you are searching for a URL (unless you actually want to keep the old domain in some cases in which case indeed regex is starting to be dangerous).

If you need info and some example on XDOM, please see http://rendering.xwiki.org/xwiki/bin/view/Main/GettingStarted#HExamples