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.List;
19  
20  import org.seasar.tuigwaa.cms.core.CmsRequest;
21  import org.seasar.tuigwaa.cms.core.CmsResponse;
22  import org.seasar.tuigwaa.cms.core.pdf.PdfCmsPageEvents;
23  import org.seasar.tuigwaa.cms.core.pdf.PdfUtils;
24  import org.seasar.tuigwaa.cms.core.wiki.base.VisitorUtils;
25  import org.seasar.tuigwaa.cms.core.wiki.base.WikiHelper;
26  import org.seasar.tuigwaa.cms.core.wiki.engine.Node;
27  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiAlias;
28  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiGenerateTree;
29  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiHeading;
30  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiInlinePlugin;
31  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiLetters;
32  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiLink;
33  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiPagename;
34  import org.seasar.tuigwaa.plugin.PluginException;
35  import org.seasar.tuigwaa.plugin.PluginRequest;
36  
37  import com.lowagie.text.Chunk;
38  import com.lowagie.text.Font;
39  import com.lowagie.text.Paragraph;
40  
41  /***
42   * @author someda
43   */
44  public class PdfContentsPlugin extends ContentsPlugin {
45  
46  	
47  	public Object doPDFView(CmsRequest request,CmsResponse response,PluginRequest prequest) throws PluginException{
48  		
49  		if(request.getRootNode() == null) return null;		
50  		if(!(request.getRootNode() instanceof WikiGenerateTree)) return null;
51  		
52  		List list = ((WikiGenerateTree)request.getRootNode()).toc;
53  		if(list.size() == 0) return null;		
54  				
55  		Paragraph p = new Paragraph();
56  		p.setIndentationLeft(20f);
57  		p.setIndentationRight(20f);
58  		
59  		int[] num = {0,0,0};
60  		
61  		Font cfont = new Font(PdfUtils.FONT_JA_MINCHO);
62  		cfont.setSize(10f);
63  		
64  		for(int i=0,n=list.size();i<n;i++){
65  			if(list.get(i) instanceof WikiHeading){				
66  				WikiHeading heading = (WikiHeading) list.get(i);
67  
68  				
69  				StringBuffer buf = new StringBuffer();
70  				int level = heading.level;
71  				
72  				for(int j=0;j<level;j++){
73  					buf.append("\u00a0\u00a0");					
74  				}
75  				
76  				for(int j=0;j<level-1;j++){
77  					buf.append(num[j] + ".");
78  				}
79  				buf.append(++num[level-1] + ".");
80  				
81  				accept(heading,buf);				
82  				
83  				Chunk c = new Chunk(buf.toString().trim(),cfont);
84  				String anchor = VisitorUtils.getAnchorId(heading);
85  				if(anchor != null){
86  					c.setLocalGoto(anchor.substring(1));
87  				}
88  				c.setGenericTag(PdfCmsPageEvents.GENERICTAG_TOC);
89  				p.add(c);
90  				p.add(Chunk.NEWLINE);				
91  				for(int j=level;j<num.length;j++){
92  					num[j] = 0;
93  				}				
94  			}
95  		}
96  		
97  		Chunk c = new Chunk("\u00a0");
98  		c.setGenericTag(PdfCmsPageEvents.GENERICTAG_CLOSETOC);
99  		p.add(c);
100 		return p;
101 	}
102 
103 	private void accept(Node node, StringBuffer buf){
104 		
105 		if(node instanceof WikiLetters){
106 			WikiLetters l = (WikiLetters) node;
107 			if(!l.isAnchor)buf.append(l.letter);
108 		}else if(node instanceof WikiLink){
109 			WikiLink l = (WikiLink) node;
110 			String[] s = WikiHelper.split(l.image, WikiHelper.LINK_DELIMITER);
111 			buf.append(s[0]);
112 		}else if(node instanceof WikiAlias){
113 			WikiAlias l = (WikiAlias) node;
114 			String[] s = WikiHelper.split(l.image, WikiHelper.ALIAS_DELIMITER);
115 			buf.append(s[0]);
116 		}else if(node instanceof WikiPagename){
117 			WikiPagename l = (WikiPagename) node;
118 			buf.append(l.image);			
119 		}else if(node instanceof WikiInlinePlugin){
120 			WikiInlinePlugin l = (WikiInlinePlugin) node;
121 			buf.append("PLUGIN:" + l.name);
122 		}		
123 		
124 		for(int i=0;i<node.jjtGetNumChildren();i++){
125 			accept(node.jjtGetChild(i),buf);
126 		}
127 	}
128 	
129 	
130 }