1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.plugin.basic;
17
18 import java.util.Collections;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.seasar.tuigwaa.cms.core.CmsConstants;
23 import org.seasar.tuigwaa.cms.core.CmsRequest;
24 import org.seasar.tuigwaa.cms.core.CmsResponse;
25 import org.seasar.tuigwaa.cms.core.Resource;
26 import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
27 import org.seasar.tuigwaa.plugin.AbstractPlugin;
28 import org.seasar.tuigwaa.plugin.PluginException;
29 import org.seasar.tuigwaa.plugin.PluginRequest;
30 import org.seasar.tuigwaa.plugin.PluginUtils;
31 import org.seasar.tuigwaa.system.TgwSecurityException;
32 import org.seasar.tuigwaa.util.TgwUtils;
33
34 import com.isenshi.util.CharUtil;
35 import com.isenshi.util.HtmlBuffer;
36
37 /***
38 * @author nishioka
39 */
40 public class FolderlistPlugin extends AbstractPlugin {
41
42 public String doHTMLView(CmsRequest request, CmsResponse response,
43 PluginRequest prequest) throws PluginException {
44
45 List folderlist;
46 WikiContext context = getConfiguration().getWikiContext();
47
48 HtmlBuffer buf = new HtmlBuffer();
49 buf.appendStartTag("h2");
50 buf.appendBody(getMessage("folderlist.list"));
51 buf.endTag();
52
53 try {
54 folderlist = context.getResourceList(request, "");
55 Collections.sort(folderlist, TgwUtils.COMPARATOR_FILEFOLDER);
56 } catch (Exception e) {
57 throw new PluginException(e);
58 }
59
60 buf.appendStartTag("ul");
61 if (folderlist != null && folderlist.size() > 0) {
62 printFolderList(buf, folderlist, request);
63 } else {
64 buf.appendBody(getMessage("folderlist.notfound"));
65 }
66 buf.endTag();
67 return buf.toString();
68 }
69
70 private void printFolderList(HtmlBuffer buf, List folderlist,
71 CmsRequest request) throws PluginException {
72
73 String curParentPath = request.getMainPageParentPath();
74 boolean isTopFolder = false;
75
76 if (curParentPath == null || curParentPath.equals("")) {
77 isTopFolder = true;
78 } else {
79 curParentPath = CharUtil.chartrim(curParentPath, '/');
80 curParentPath += "/";
81 }
82
83 for (Iterator i = folderlist.iterator(); i.hasNext();) {
84
85 Resource resource = (Resource) i.next();
86 String pagePath = resource.getPath();
87 pagePath = CharUtil.chartrim(pagePath, '/');
88
89 boolean isAttach = PluginUtils.isAttachment(resource);
90 String hidden = resource.getProperty(CmsConstants.PROPERTY_HIDDEN);
91
92 if ((hidden == null || !"true".equalsIgnoreCase(hidden)) && !isAttach) {
93
94 int startPoint = buf.nextIndex();
95
96 try{
97 buf.appendStartTag("li");
98 if (resource.isFolder()) {
99 if (!isTopFolder
100 && curParentPath.startsWith(pagePath + "/")) {
101 buf.appendAttribute("class", "folderopen");
102 printLink(buf, request, pagePath + "/", resource);
103 printChildren(buf, request, pagePath);
104 } else {
105 buf.appendAttribute("class", "folder");
106 printLink(buf, request, pagePath + "/", resource);
107 }
108 } else {
109 buf.appendAttribute("class", "file");
110 printLink(buf, request, pagePath, resource);
111 }
112 buf.endTag();
113 }catch(TgwSecurityException tse){
114 int currentPoint = buf.nextIndex();
115 if(startPoint != currentPoint){
116 buf.cut(startPoint,currentPoint);
117 }
118 }
119 }
120
121 }
122 }
123
124 private void printLink(HtmlBuffer buf, CmsRequest request, String path,
125 Resource resource) throws TgwSecurityException{
126
127 WikiContext context = getConfiguration().getWikiContext();
128 String url = context.getURLByName(path,request).toString();
129 buf.appendAnchor(url, resource.getPageName());
130
131 }
132
133 private void printChildren(HtmlBuffer buf, CmsRequest request, String parent)
134 throws PluginException {
135 List folderList = null;
136 WikiContext context = getConfiguration().getWikiContext();
137 try {
138 parent = CharUtil.chartrim(parent, '/');
139 folderList = context.getResourceList(request, parent);
140 Collections.sort(folderList, TgwUtils.COMPARATOR_FILEFOLDER);
141 } catch (Exception e) {
142 throw new PluginException(e);
143 }
144 if (folderList != null && folderList.size() > 0) {
145 buf.appendStartTag("ul");
146 printFolderList(buf, folderList, request);
147 buf.endTag();
148 }
149 }
150 }