0

Pretty new to sling and Java so I apologize in advance. But does anybody have any idea why when I'm at the root it's outputting my path twice? It's odd that it only happens at the absolute root.

public static String generateTest(Page page, Page rootPage, String bc) {

    Page parent = page.getParent();

    String bread = ""; 
    bread += (parent != null) ? "<li><a href=" + parent.getPath() + ">" + parent.getTitle() + "</a>" : "";
    bread += "<li>" + "<a href=" + page.getPath() + ">" + page.getTitle() + "</a></li>" + bc;
    return (ifAtRoot(parent , rootPage)) ? breadcrumb : generateTest(parent, rootPage, bread);

}

public static boolean ifAtRoot(Page page, Page root) {
    return (page == null || root.getPath() == page.getPath());
}

Any help is greatly appreciated!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Delmon Young
  • 2,013
  • 5
  • 39
  • 51

1 Answers1

1

First, ifAtRoot() will return true only if page is null because you cannot compare objects (including strings) using ==. You should use .equals() instead:

public static boolean ifAtRoot(Page page, Page root) {
    return (page == null || root.getPath().equals(page.getPath()));
}

In your case first call of ifAtRoot() returned false, so you called it second time recursively passing brend that just has been created. The second call creates brend again and appends bc (that contains previously created brend) to it. The second call of ifAtRoot() for your luck returns true. Otherwise you'd enter infinite recursion and finish with StackOverflowError.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Thanks for replying @AlexR. Although I'm a bit confused using .equals instead of == didn't seem to change anything. Also if I was using == instead of .equals() wouldn't that fail altogether. Thanks for the help! – Delmon Young Apr 09 '13 at 20:50