View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.lang.support;
17  
18  import java.util.StringTokenizer;
19  
20  import net.sf.morph.util.MorphStringTokenizer;
21  import net.sf.morph.util.StringUtils;
22  
23  /**
24   * An expression parser for use with the simple language.
25   *
26   * @author Matt Sgarlata
27   * @since Nov 28, 2004
28   */
29  public class SimpleExpressionParser extends BaseExpressionParser implements ExpressionParser {
30  
31  	private static final String DELIMITERS = "[]()\"'.";
32  
33  	/**
34  	 * {@inheritDoc}
35  	 */
36  	public String[] parseImpl(String expression) throws Exception {
37  		StringTokenizer tokenizer = new MorphStringTokenizer(
38  			StringUtils.removeWhitespace(expression), DELIMITERS);
39  		// Don't use a converter to do this conversion because that makes
40  		// testing of morph difficult and it isn't as fast as doing the
41  		// conversion directly. Using a converter would make testing more
42  		// difficult because the parser breaks if the containercopier
43  		// breaks, which happens a lot for whatever reason, and a lot of things
44  		// depend on the parser). The speed issue is very important here,
45  		// because the simple expression parser is used very heavily by other
46  		// parts of the framework (e.g. contexts)
47  		String[] tokens = new String[tokenizer.countTokens()];
48  		for (int i=0; i<tokens.length; i++) {
49  			tokens[i] = tokenizer.nextToken();
50  		}
51  		return tokens;
52  	}
53  
54  }