// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.responders;

import fitnesse.wiki.WikiPage;
import fitnesse.authentication.SecureOperation;
import fitnesse.authentication.SecureReadOperation;
import org.json.JSONArray;
import util.StringUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NameWikiPageResponder extends BasicWikiPageResponder {
  protected String contentFrom(WikiPage requestedPage) {
    List<String> lines = addLines(requestedPage);

    String format = (String) request.getInput("format");
    if ("json".equalsIgnoreCase(format)) {
      JSONArray jsonPages = new JSONArray(lines);
      return jsonPages.toString();
    }
    return StringUtil.join(lines, System.getProperty("line.separator"));
  }

  private List<String> addLines(WikiPage requestedPage) {
    List<String> lines = new ArrayList<String>();
    for (Iterator<?> iterator = requestedPage.getChildren().iterator(); iterator.hasNext();) {
      WikiPage child = (WikiPage) iterator.next();
      lines.add(makeLine(child));
    }
    return lines;
  }

  private String makeLine(WikiPage child) {
    String line;
    if (request.hasInput("ShowChildCount")) {
      int numberOfChildren = child.getChildren().size();
      line = child.getName() + " " + numberOfChildren;
    } else
      line = child.getName();
    return line;
  }

  protected String getContentType() {
    return "text/plain";
  }

  @Override
  public SecureOperation getSecureOperation() {
    return new SecureReadOperation();
  }
}

@@ -199,16 +199,47 @@
 kiPage;%0A
+import fitnesse.wiki.PageData;%0A
 import f
@@ -382,16 +382,41 @@
 gUtil;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -484,16 +484,64 @@
 il.List;
+%0Aimport java.util.Set;%0Aimport java.util.TreeSet;
 %0A%0Apublic
@@ -706,16 +706,20 @@
 stedPage
+, %22%22
 );%0A%0A    
@@ -1029,32 +1029,47 @@
 ge requestedPage
+, String prefix
 ) %7B%0A    List%3CStr
@@ -1102,24 +1102,26 @@
 %3CString%3E();%0A
+%09%0A
     for (Ite
@@ -1121,30 +1121,24 @@
 or (
-Iterator%3C?%3E iterator =
+WikiPage child :
  req
@@ -1165,40 +1165,80 @@
 en()
-.iterator(); iterator.hasNext
+) %7B%0A%09  if(!request.hasInput(%22LeafOnly%22) %7C%7C child.getChildren().isEmpty
 ()
-;
 ) %7B%0A
@@ -1247,55 +1247,99 @@
     
-WikiPage child = (WikiPage) iterator.next();%0A  
+  lines.add(makeLine(child, prefix));%0A      %7D%0A%09  %0A%09  if (request.hasInput(%22Recursive%22)) %7B%0A%09
     
@@ -1351,33 +1351,75 @@
 .add
-(make
+All(add
 Line
+s
 (child
-));
+, prefix + child.getName() + %22.%22));%0A%09  %7D
 %0A    %7D%0A
+%09%0A
     
@@ -1481,28 +1481,129 @@
 hild
-) %7B%0A    String line;
+, String prefix) %7B%0A    int numberOfChildren = child.getChildren().size();%0A%09%0A    String line = prefix + child.getName();%0A%09
 %0A   
@@ -1647,27 +1647,37 @@
 %22)) %7B%0A      
+l
 in
-t
+e += %22 %22 +
  numberOfChi
@@ -1685,150 +1685,683 @@
 dren
- = child.getChildren().size();%0A      line = child.getName() + %22 %22 + numberOfChildren;%0A    %7D else%0A      line = child.getName();%0A    
+;%0A    %7D%0A%09%0A    if (request.hasInput(%22ShowTags%22)) %7B%0A%09  Set%3CString%3E tags = getTags(child);%0A%09  if(!tags.isEmpty()) %7B%0A%09    line += %22 %22;%0A%09%09for(String tag : tags) %7B%0A          line += %22%5B%22 + tag + %22%5D%22;%0A%09%09%7D%0A      %7D%0A%09%7D%0A%09%0A    return line;%0A  %7D%0A  %0A  private Set%3CString%3E getTags(WikiPage page) %7B%0A    Set%3CString%3E result = new TreeSet%3CString%3E();%0A%09%0A%09// get the tags of the given page%0A    String tags = page.getData().getAttribute(PageData.PropertySUITES);%0A    if((tags != null) && !tags.isEmpty()) %7B%0A%09  result.addAll(Arrays.asList(tags.split(%22, %22)));%0A    %7D%0A%09%0A%09// recusrively collect all tags up to the root%0A%09if(!page.isRoot()) %7B%0A%09  result.addAll(getTags(page.getParent()));%0A%09%7D%0A%09%0A%09
 return 
-line
+result
 ;%0A  

enhance "names" request with optional "Recursive", "LeafOnly", and "ShowTags" parameters