View Javadoc

1   package net.sf.jpkgmk.prototype;
2   
3   import java.util.Arrays;
4   
5   import net.sf.jpkgmk.AbstractParser;
6   import net.sf.jpkgmk.ParseException;
7   import net.sf.jpkgmk.util.StringUtil;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  /**
13   * Utility to parse an existing prototype file
14   * @author gommma (gommma AT users.sourceforge.net)
15   * @author Last changed by: $Author: gommma $
16   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
17   * @since 1.0
18   */
19  public class PrototypeParser extends AbstractParser<Prototype>
20  {
21  
22  	/**
23  	 * Stores the last PrototypeEntry of type {@link PrototypeEntryType#COMMAND} and subtype {@link CommandType#DEFAULT}
24  	 */
25  	private PrototypeEntryCommandDefault lastEntryCommandDefault;
26  	
27  	
28  	
29  	private Log log = LogFactory.getLog(PrototypeParser.class);
30  	
31  	
32  	
33  	public PrototypeParser()
34  	{
35  		super();
36  	}
37  
38  
39  	@Override
40  	protected void parseAndAddLine(String line, Prototype prototype) {
41  		PrototypeEntry entry = parseLine(line);
42  		prototype.add(entry);
43  	}
44  
45  	@Override
46  	protected Prototype createResult() {
47  		return new Prototype();
48  	}
49  
50  
51  	public PrototypeEntry parseLine(String line)
52  	{
53  		log.debug("<entering> parseLine()");
54  		
55  		String[] items = PrototypeParser.getLineItems(line);
56  		
57  		if(items.length <= 0 || StringUtil.isNullOrEmpty(line)) {
58  			throw new IllegalArgumentException("The given line '" + line + "' is empty. Cannot parse");
59  		}
60  		
61  		PrototypeEntryType type = PrototypeParser.resolveType(items);
62  
63  		PrototypeEntryParser parser = type.getParser();
64  		PrototypeEntry result = parser.parse(line, this.lastEntryCommandDefault);
65  
66  		// If it is a command of type "default" remind it
67  		if(result instanceof PrototypeEntryCommandDefault) {
68  			this.lastEntryCommandDefault = (PrototypeEntryCommandDefault)result;
69  		}
70  
71  		return result;
72  	}
73  	
74  	
75  	
76  	public static PrototypeEntryType resolveType(String[] items) {
77  		String firstItem = items[0].trim();
78  		
79  		String typeString = null;
80  		
81  		if(StringUtil.isInteger(firstItem)) {
82  			/*Integer part = */new Integer(firstItem);
83  			typeString = items[1].trim();
84  		}
85  		else {
86  			typeString = firstItem;
87  		}
88  		
89  		PrototypeEntryType type = PrototypeEntryType.findType(typeString);
90  		if(type == null) {
91  			throw new ParseException("The type '" + typeString + "' is unknown. Line items: '" + Arrays.asList(items) + "'");
92  		}
93  		return type;
94  	}
95  
96  
97  }