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.net.URL;
19  
20  import org.seasar.tuigwaa.cms.core.CmsRequest;
21  import org.seasar.tuigwaa.cms.core.CmsResponse;
22  import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
23  import org.seasar.tuigwaa.cms.core.wiki.base.WikiHelper;
24  import org.seasar.tuigwaa.plugin.AbstractPlugin;
25  import org.seasar.tuigwaa.plugin.PluginException;
26  import org.seasar.tuigwaa.plugin.PluginRequest;
27  
28  import com.isenshi.util.HtmlBuffer;
29  
30  /***
31   * @author someda
32   */
33  public class RefPlugin extends AbstractPlugin {
34  
35  	public String doHTMLView(CmsRequest request, CmsResponse response,
36  			PluginRequest prequest) throws PluginException {
37  		
38  		String[] args = prequest.getArgs();
39  		if(args == null)
40  			throw new PluginException("ref plugin needs argument");
41  		
42  		HtmlBuffer buf = new HtmlBuffer();
43  		buf.setNewline(false);
44  		buf.setTab(false);
45  		
46  		String href = getHref(args,request);		
47  		String label = getLabel(args,href);
48  							
49  		buf.appendAnchor(href,label);				
50  		return buf.toString();
51  	}
52  	
53  	protected String getHref(String[] args, CmsRequest request){
54  		String file = args[0];
55  		String href = args[0];
56  		if(!WikiHelper.isURL(file)){
57  			URL url = null;
58  			WikiContext ctx = getConfiguration().getWikiContext();
59  			if(args.length >= 3 && "true".equalsIgnoreCase(args[2])){
60  				url = ctx.getCompleteURL(file,request);
61  			}else{			
62  				url = ctx.getAttachedFileURL(request.getPage().getResource().getPath(),file,request);
63  			}			
64  			if(url != null)
65  				href = url.toString();
66  		}
67  		return href;		
68  	}
69  	
70  	protected String getLabel(String[] args, String href){
71  
72  		String file = args[0];
73  		if(WikiHelper.isURL(file)){
74  			int idx = file.lastIndexOf('/');
75  			if(idx != -1)
76  				file = file.substring(idx+1);
77  		}		
78  		
79  		String label = file;		
80  		if(args.length >= 2){
81  			label = args[1];
82  		}
83  							
84  		if(WikiHelper.isImage(file)){
85  			label = "<img src=\"" + href + "\" border=0/>";
86  		}		
87  		return label;		
88  	}
89  }