View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.plugin.basic;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.seasar.tuigwaa.cms.core.CmsConstants;
22  import org.seasar.tuigwaa.cms.core.CmsRequest;
23  import org.seasar.tuigwaa.cms.core.CmsResponse;
24  import org.seasar.tuigwaa.cms.core.Resource;
25  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
26  import org.seasar.tuigwaa.plugin.AbstractPlugin;
27  import org.seasar.tuigwaa.plugin.PluginException;
28  import org.seasar.tuigwaa.plugin.PluginRequest;
29  import org.seasar.tuigwaa.plugin.PluginUtils;
30  import org.seasar.tuigwaa.system.TgwSecurityException;
31  
32  import com.isenshi.util.CharUtil;
33  import com.isenshi.util.HtmlBuffer;
34  
35  /***
36   * @author someda
37   */
38  public class ListPlugin extends AbstractPlugin {
39  
40  	public String doHTMLView(CmsRequest request, CmsResponse response,
41  			PluginRequest prequest) throws PluginException {
42  
43  		WikiContext context = getConfiguration().getWikiContext();
44  
45  		String path = request.getPage().getResource().getParentPath();
46  		String[] args = prequest.getArgs();
47  		if (args != null && args.length > 0) {
48  			path = args[0];
49  			path = CharUtil.chartrim(path, '/');
50  		}
51  
52  		List list;
53  		try {
54  			list = context.getResourceList(request, path);
55  		} catch (Exception e) {
56  			throw new PluginException(e);
57  		}
58  
59  		String name = prequest.getName();
60  		HtmlBuffer buf = new HtmlBuffer();
61  		if ("list".equals(name)) {
62  			renderList(list, buf, request, context);
63  		} else if ("navi".equals(name)) {
64  			renderNavi(list, buf, request, context);
65  		}
66  
67  		return buf.toString();
68  	}
69  
70  	private void renderList(List list, HtmlBuffer buf, CmsRequest request,
71  			WikiContext context) {
72  
73  		if (list == null) {
74  			buf.appendBody(getMessage("list.notfound"));
75  			return;
76  		}
77  
78  		buf.appendStartTag("ul");
79  
80  		for (Iterator i = list.iterator(); i.hasNext();) {
81  			Resource resource = (Resource) i.next();
82  			String hidden = resource.getProperty(CmsConstants.PROPERTY_HIDDEN);
83  			boolean isAttach = PluginUtils.isAttachment(resource);
84  
85  			if ((hidden == null || !hidden.equalsIgnoreCase("true")) && !isAttach) {
86  				appendLinkItem(buf, null, resource.getPageName(),context,request,resource.getPath());
87  			}
88  		}
89  		buf.endTag();
90  	}
91  
92  	private void renderNavi(List list, HtmlBuffer buf, CmsRequest request,
93  			WikiContext context) {
94  
95  		if (list == null)
96  			return;
97  
98  		buf.appendStartTag("ul");
99  		buf.appendAttribute("class", "navi");
100 		String currentPagePath = request.getMainPagePath();
101 		// FIXME
102 		currentPagePath = currentPagePath.replaceAll("//", "/"); 
103 		int size = list.size();
104 		int prevIndex = -1;
105 		int nextIndex = -1;
106 
107 		for (int i = 0; i < size; i++) {
108 			Resource resource = (Resource) list.get(i);
109 			if (currentPagePath.equals(resource.getPath())) {
110 				prevIndex = (i != 0) ? i - 1 : size - 1;
111 				nextIndex = (i != size - 1) ? i + 1 : 0;
112 				break;
113 			}
114 		}
115 
116 		if (prevIndex >= 0) {
117 			Resource resource = (Resource) list.get(prevIndex);
118 				appendLinkItem(buf, "navi_left", "Prev",context,request,resource.getPath());
119 		}
120 
121 		if (nextIndex >= 0) {
122 			Resource resource = (Resource) list.get(nextIndex);
123 			appendLinkItem(buf, "navi_right", "Next",context,request,resource.getPath());
124 		}
125 
126 		Resource resource = request.getPage().getResource();
127 		appendLinkItem(buf, "navi_none",resource.getParentPath(),context,request,resource.getParentPath() + "/");
128 
129 		buf.endTag();
130 	}
131 
132 	private void appendLinkItem(HtmlBuffer buf, String styleClass,
133 			String href, WikiContext context, CmsRequest request, String path) {
134 		
135 		try{
136 			String uri = context.getURLByName(path,request).toString();
137 			buf.appendStartTag("li");
138 			if (styleClass != null)
139 				buf.appendAttribute("class", styleClass);
140 			buf.appendAnchor(uri, href);
141 			buf.endTag();
142 		}catch(TgwSecurityException tse){
143 			// do-nothing, security link
144 		}						
145 
146 	}
147 
148 }