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.io.IOException;
19  import java.net.MalformedURLException;
20  import java.net.URL;
21  
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.pdf.PdfUtils;
26  import org.seasar.tuigwaa.cms.core.wiki.base.WikiHelper;
27  import org.seasar.tuigwaa.plugin.PluginException;
28  import org.seasar.tuigwaa.plugin.PluginRequest;
29  import org.seasar.tuigwaa.util.TgwContext;
30  
31  import com.lowagie.text.Anchor;
32  import com.lowagie.text.BadElementException;
33  import com.lowagie.text.Element;
34  import com.lowagie.text.Image;
35  
36  /***
37   * @author someda
38   */
39  public class PdfRefPlugin extends RefPlugin {	
40  	
41  	// FIXME: very very very temporal
42  	private static final String LOCAL_WEBDAV_PATH = "WEB-INF/classes/webdav/content/webfile"; 
43  	
44  	public Object doPDFView(CmsRequest request,CmsResponse response,PluginRequest prequest) throws PluginException{
45  		
46  		String[] args = prequest.getArgs();
47  		if(args == null)
48  			throw new PluginException("ref plugin needs argument");
49  				
50  		String href = getHref(args,request);
51  		String label = getLabel(args,href);
52  		
53  		Element e = null;
54  		if(label.indexOf("img") != -1){
55  			try{								
56  				URL url = new URL(href);
57  
58  				Image img = null;
59  				if(WikiHelper.isURL(args[0])){
60  					img = Image.getInstance(url);
61  				}else{
62  					// in attachement case, need to authentication, thus temporal fix
63  					Resource resource = request.getPage().getResource();					
64  					String file = request.getSiteName() + "/" + resource.getPath() + ".attach/" + args[0];
65  					String prefix = LOCAL_WEBDAV_PATH;
66  					String realpath = TgwContext.getRealPath(prefix + "/" + file + "_1.0");
67  					img = Image.getInstance(realpath);					
68  				}
69  				img.setAlignment(Image.ALIGN_MIDDLE | Image.TEXTWRAP);
70  				e = (Element) img;				
71  			}catch(MalformedURLException mue){
72  				throw new PluginException(mue);
73  			}catch(BadElementException bee){
74  				throw new PluginException(bee);
75  			}catch(IOException ioe){
76  				throw new PluginException(ioe);
77  			}			
78  		}else{
79  			Anchor a = new Anchor(label,PdfUtils.FONT_JA_MINCHO);
80  			a.setReference(href);
81  			e = (Element)a;
82  		}
83  		return e;
84  
85  	}
86  
87  }