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.engine;
17  
18  import java.io.StringReader;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.seasar.tuigwaa.cms.core.CmsConstants;
23  import org.seasar.tuigwaa.cms.core.Page;
24  
25  
26  public class WikiParserWrapper {
27  	
28  	private org.seasar.tuigwaa.cms.core.Page page_;
29  	private boolean debug_ = false;
30  	//private static final String LINESEP = System.getProperty("line.separator");	
31  	private static final String LINESEP = CmsConstants.LINEBREAK_CODE;
32  	private Log log_ = LogFactory.getLog(this.getClass());
33  	private WikiParser parser_;
34  	
35  	public WikiParserWrapper(Page page){
36  		this.page_ = page;
37  		String processed = preprocess(page_.getContent().toString());
38  		this.parser_ = new WikiParser(new StringReader(processed));
39  	}
40  	
41  	public SimpleNode getTree(){
42  		
43  		long start = System.currentTimeMillis();
44  		try{
45  			if(debug_){
46  				parser_.enable_tracing();
47  			}else{
48  				parser_.disable_tracing();
49  			}
50  			parser_.GenerateTree();
51  		}catch(ParseException pe){
52  			log_.error(pe.getMessage());
53  		}			
54  		long end = System.currentTimeMillis();
55  		long elapsed = end - start;
56  		if(debug_)
57  			log_.info("parse time: " + elapsed + "[ms]");
58  		return (SimpleNode) parser_.jjtree.rootNode();	
59  	}
60  	
61  	public int getNumOfParseError(){
62  		return parser_.getNParseErrors();
63  	}
64  	
65  	public int getNumOfLexicalError(){
66  		return parser_.getNLexicalErrors();
67  	}
68  	
69  	private String preprocess(String data){
70  		StringBuffer buf = new StringBuffer(LINESEP);
71  		buf.append(data);
72  		buf.append(LINESEP);
73  		return buf.toString();
74  	}
75  	
76  	public void setDebug(boolean debug){
77  		this.debug_ = debug;
78  	}
79  	
80  
81  }