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.cms.core.wiki;
17  
18  import org.seasar.tuigwaa.cms.core.CmsConstants;
19  import org.seasar.tuigwaa.cms.core.CmsEngine;
20  import org.seasar.tuigwaa.cms.core.CmsRequest;
21  import org.seasar.tuigwaa.cms.core.CmsResponse;
22  import org.seasar.tuigwaa.cms.core.Page;
23  import org.seasar.tuigwaa.cms.core.wiki.base.CacheManager;
24  import org.seasar.tuigwaa.cms.core.wiki.base.HTMLWikiVisitor;
25  import org.seasar.tuigwaa.cms.core.wiki.base.PDFWikiVisitor;
26  import org.seasar.tuigwaa.cms.core.wiki.base.TextWikiVisitor;
27  import org.seasar.tuigwaa.cms.core.wiki.base.WikiConfiguration;
28  import org.seasar.tuigwaa.cms.core.wiki.engine.Node;
29  import org.seasar.tuigwaa.cms.core.wiki.engine.SimpleNode;
30  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiParserVisitor;
31  import org.seasar.tuigwaa.cms.core.wiki.engine.WikiParserWrapper;
32  
33  
34  public class WikiEngine implements CmsEngine{
35  
36  	//private static Log log_ = LogFactory.getLog(WikiEngine.class);	
37  
38  	private WikiParserVisitor visitor_;
39  	
40  	private CacheManager mgr_ = CacheManager.getInstance();
41  	
42  	private WikiConfiguration config;
43  	
44  	public WikiEngine(){
45  	}
46  	
47  	public void setConfiguration(WikiConfiguration cfg) {
48  		this.config = cfg;
49  	}
50  				
51  	public void service(CmsRequest request, CmsResponse response){
52  		Node rootnode = getRootNode(request,response);
53  		if(config.isDebug()) ((SimpleNode) rootnode).dump(".");
54  //		((SimpleNode) rootnode).dump(".");
55  		
56  		if(request.getType() == CmsConstants.OUTPUTTYPE_HTML){
57  			visitor_ = new HTMLWikiVisitor(request,response,config);
58  			response.setString((String)rootnode.jjtAccept(visitor_,null));
59  		}else if(request.getType() == CmsConstants.OUTPUTTYPE_PDF || request.getType() == CmsConstants.OUTPUTTYPE_RTF){
60  			visitor_ = new PDFWikiVisitor(request,response,config);
61  			response.setBytes((byte[])rootnode.jjtAccept(visitor_,null));
62  		}else if(request.getType() == CmsConstants.OUTPUTTYPE_TEXT){
63  			visitor_ = new TextWikiVisitor(request,response,config);
64  			response.setString((String)rootnode.jjtAccept(visitor_,null));
65  		}
66  	}	
67  	
68  	private Node getRootNode(CmsRequest request, CmsResponse response){
69  		Node node;
70  		Page page = request.getPage();
71  		
72  		if((node = mgr_.get(page)) == null){
73  			WikiParserWrapper parser = new WikiParserWrapper(page);
74  			parser.setDebug(config.isDebug());
75  			//parser.setDebug(true);
76  			response.setCached(false);
77  			node = parser.getTree();
78  			
79  			response.setNParseError(parser.getNumOfParseError());
80  			response.setNLexicalError(parser.getNumOfLexicalError());
81  			if(mgr_.isCachable(page,node))
82  				mgr_.cache(page,node);
83  		}
84  
85  		request.setRootNode(node);
86  		return node;
87  	}
88  }