0

I am trying to get a substring but I get a Failed to execute the [velocity] macro. I have no idea why this is happening. Here is my code:

#if ($text != '' )
   #set ($textwo = "${text.substring(0,4)}")
   if($textwo != "Tags" )
      #getSearchResults()
      (% class="search-results-container" %)(((
         #displaySearchFacets($searchResponse)
         (% class="search-results-left" %)(((
            #displaySearchResultsSort()

            #displaySearchResults()
            )))
          )))
  #end
  #if ($textwo == 'Tags')
    #getTaggedResult
  #end
#end

If I remove the line #set ($textwo = "${text.substring(0,4)}"), I dont get the error anymore and ofcourse it does not work.

nupac
  • 2,459
  • 7
  • 31
  • 56

1 Answers1

0

If you click on the //Failed to...// error message, it will expand to show the full stack trace. You'll probably find that the real cause of the error is:

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 4

Which means that the input string is shorter than 4 characters.

What is $text supposed to be? Why are you keeping only the first four characters? Would #if ($text.startsWith('Tags')) be an equivalent check?

In XWiki you can also use $stringtool, which is actually StringUtils from Apache Commons Lang 3, and which has error-preventing methods for extracting a piece of text, or extracting a prefix, or checking for a prefix, among others.

Sergiu Dumitriu
  • 11,455
  • 3
  • 39
  • 62
  • Thank you for your reply and Sorry for the late response. To answer your questions, $text = the search text entered in the box, to answer your second question I will direct you to a [link](http://stackoverflow.com/questions/18328871/pass-button-value-using-javascript-and-make-it-usable-in-xwiki-macro), because I cannot directly send data from javascript to velocity(as you said there, Thanks), I figured I use javascript to manipulate `$text`, as a solution to my problem and for your third, Yes, that would work but I did'nt use that because I did'nt know it existed. – nupac Aug 22 '13 at 04:28
  • (I had to continue in this second comment, no characters left) So my initial problem was [link](http://stackoverflow.com/questions/18199962/xwiki-search-documents-using-tags), I used javascript to manipulate the search text and then check for the manipulation, If it exists, then show tagged documents, if not the search for the keyword. It works for now but if you have a better idea please share. Thanks – nupac Aug 22 '13 at 04:36