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.wiki.base.VisitorUtils;
23  import org.seasar.tuigwaa.cms.core.wiki.base.WikiHelper;
24  import org.seasar.tuigwaa.cms.core.wiki.engine.Node;
25  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiAlias;
26  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiGenerateTree;
27  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiHeading;
28  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiInlinePlugin;
29  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiLetters;
30  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiLink;
31  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiPagename;
32  import org.seasar.tuigwaa.plugin.AbstractPlugin;
33  import org.seasar.tuigwaa.plugin.PluginException;
34  import org.seasar.tuigwaa.plugin.PluginRequest;
35  
36  import com.isenshi.util.XMLStringBuffer;
37  
38  /***
39   * @author someda
40   */
41  public class ContentsPlugin extends AbstractPlugin {
42  
43  	public String doHTMLView(CmsRequest request, CmsResponse response,
44  			PluginRequest prequest) throws PluginException {
45  		
46  		if(request.getRootNode() == null) return null;		
47  		if(!(request.getRootNode() instanceof WikiGenerateTree)) return null;
48  		
49  		List list = ((WikiGenerateTree)request.getRootNode()).toc;
50  		if(list.size() == 0) return null;
51  		
52  		int curlevel = 0;
53  		int prelevel = 0;
54  		int[] level = {0,0,0};
55  		
56  		//StringBuffer buf = new StringBuffer("<div class=\"contents\">");
57  		XMLStringBuffer buf = new XMLStringBuffer();
58  		buf.setNewline(false);
59  		buf.setTab(false);
60  		buf.appendStartTag("div");
61  		buf.appendAttribute("class","contents");		
62  		
63  		for(int i=0,n=list.size();i<n;i++){
64  			if(list.get(i) instanceof WikiHeading){
65  				
66  				WikiHeading heading = (WikiHeading) list.get(i);
67  				curlevel = heading.level;
68  
69  				if(curlevel > prelevel) buf.appendStartTag("ul");
70  				
71  				if(curlevel < prelevel){
72  					for(int j=curlevel;j<prelevel;j++){
73  						if(level[j] != 0){
74  							buf.endTag();
75  							level[j] = 0;							
76  						}
77  					}
78  				}
79  				
80  				if(curlevel != prelevel){
81  					prelevel = curlevel;
82  					level[curlevel-1] = 1;
83  				}
84  				
85  				buf.appendStartTag("li");
86  				buf.appendStartTag("a");
87  				buf.appendAttribute("href",VisitorUtils.getAnchorId(heading));
88  				accept(heading,buf);
89  				buf.endTag();
90  				buf.endTag();
91  			}
92  		}
93  		
94  		for(int i=0;i<level.length;i++)
95  			if(level[i] != 0) buf.endTag();
96  
97  		if(!buf.isTagEmpty())buf.endTag();
98  		return buf.toString();
99  	}
100 	
101 	protected void accept(Node node, XMLStringBuffer buf){
102 							
103 		if(node instanceof WikiLetters){
104 			WikiLetters l = (WikiLetters) node;
105 			if(!l.isAnchor)buf.appendBody(l.letter);
106 		}else if(node instanceof WikiLink){
107 			WikiLink l = (WikiLink) node;
108 			String[] s = WikiHelper.split(l.image, WikiHelper.LINK_DELIMITER);
109 			buf.appendBody(s[0]);
110 		}else if(node instanceof WikiAlias){
111 			WikiAlias l = (WikiAlias) node;
112 			String[] s = WikiHelper.split(l.image, WikiHelper.ALIAS_DELIMITER);
113 			buf.appendBody(s[0]);
114 		}else if(node instanceof WikiPagename){
115 			WikiPagename l = (WikiPagename) node;
116 			buf.appendBody(l.image);			
117 		}else if(node instanceof WikiInlinePlugin){
118 			WikiInlinePlugin l = (WikiInlinePlugin) node;
119 			buf.appendBody("PLUGIN:" + l.name);
120 		}
121 		for(int i=0;i<node.jjtGetNumChildren();i++){
122 			accept(node.jjtGetChild(i),buf);
123 		}
124 	}
125 		
126 	
127 }