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 net.sf.composite.util.ObjectUtils;
19  import net.sf.morph.lang.InvalidExpressionException;
20  
21  /**
22   * A base class for expression parsers.
23   *
24   * @author Matt Sgarlata
25   * @since Nov 28, 2004
26   */
27  public abstract class BaseExpressionParser implements ExpressionParser {
28  
29  	/**
30  	 * Implement <code>parse(expression)</code>.
31  	 * @param expression to parse
32  	 * @return String[] of path elements.
33  	 * @throws Exception
34  	 */
35  	public abstract String[] parseImpl(String expression) throws Exception;
36  
37  	/**
38  	 * {@inheritDoc}
39  	 */
40  	public String[] parse(String expression) throws InvalidExpressionException {
41  		if (ObjectUtils.isEmpty(expression)) {
42  			return new String[] { };
43  		}
44  
45  		try {
46  			return parseImpl(expression);
47  		}
48  		catch (InvalidExpressionException e) {
49  			throw e;
50  		}
51  		catch (Exception e) {
52  			throw new InvalidExpressionException("The expression '" + expression + "' is invalid", e);
53  		}
54  	}
55  
56  }