View Javadoc

1   package net.sf.jpkgmk.prototype;
2   
3   import net.sf.jpkgmk.ParseException;
4   import net.sf.jpkgmk.util.ArrayUtil;
5   import net.sf.jpkgmk.util.StringUtil;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  /**
11   * @author gommma (gommma AT users.sourceforge.net)
12   * @author Last changed by: $Author: gommma $
13   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
14   * @since 1.0
15   */
16  public abstract class AbstractPrototypeEntryParser implements PrototypeEntryParser
17  {
18  	
19  	private Log log = LogFactory.getLog(AbstractPrototypeEntryParser.class);
20  	
21  	/**
22  	 * The current line being parsed. Is set when the method {@link #parse(String, PrototypeEntryCommandDefault)}
23  	 * is entered and reset when the method is leaved.	 
24  	 */
25  	private String currentLine;
26  	
27  	
28  	/**
29  	 * Default constructor
30  	 */
31  	public AbstractPrototypeEntryParser()
32  	{
33  	}
34  	
35  	
36  	/**
37  	 * @return Returns the current line being parsed. Is set when the method {@link #parse(String, PrototypeEntryCommandDefault)}
38  	 * is entered and reset when the method is leaved.
39  	 */
40  	public String getCurrentLine() {
41  		return currentLine;
42  	}
43  
44  
45  	public final PrototypeEntry parse(String line, PrototypeEntryCommandDefault entryCommandDefault) {
46  		
47  		log.debug("Parsing line '" + line + "' using the entryDefault='" + entryCommandDefault + "'");
48  		
49  		this.currentLine = line;
50  		String[] items = PrototypeParser.getLineItems(line);
51  
52  		if(items.length <= 0 || StringUtil.isNullOrEmpty(line)) {
53  			throw new IllegalArgumentException("The given line '" + line + "' is empty. Cannot parse");
54  		}
55  		
56  		try {
57  			PrototypeEntry entry = parseItems(items, entryCommandDefault);
58  			return entry;
59  		}
60  		catch(Exception e) {
61  			throw new ParseException("Exception while parsing line '" + line + "'. ", e);
62  		}
63  		finally {
64  			// Reset current line
65  			this.currentLine = null;
66  		}
67  	}
68  
69  	protected final String getArrayValue(String[] items, int i) {
70  		return ArrayUtil.getArrayValue(items, i);
71  	}
72  
73  	/**
74  	 * Invoked by the {@link #parse(String, PrototypeEntryCommandDefault)} method to parse
75  	 * the items of a line. The given array of items represents the whole line split up into strings.
76  	 * @param items
77  	 * @param entryCommandDefault
78  	 * @return
79  	 */
80  	private final PrototypeEntry parseItems(String[] items, PrototypeEntryCommandDefault entryCommandDefault) 
81  	{
82  		int currentIndex = 0;
83  		Integer part = null;
84  		// Check if the "part" field is defined at all - it is an optional field
85  		if(ArrayUtil.isArrayValueInt(items, currentIndex)) {
86  			part = ArrayUtil.getArrayValueAsInt(items, currentIndex);
87  			currentIndex++;
88  		}
89  
90  		String typeString = items[currentIndex++];
91  		PrototypeEntryType type = PrototypeEntryType.findType(typeString);
92  
93  		return parseItems(items, part, type, currentIndex, entryCommandDefault);
94  	}
95  	
96  	/**
97  	 * Invoked by the {@link #parse(String, PrototypeEntryCommandDefault)} method to parse
98  	 * the items of a line. The given array of items represents the whole line split up into strings.
99  	 * @param items All items of this line
100 	 * @param part The pre-parsed value for the optional "part" field
101 	 * @param type The pre-parsed value for the mandatory "type" character field
102 	 * @param currentIndex The current index located after the "type" field already parsed. This is the index where
103 	 * the parsing should be continued by the concrete implementations.
104 	 * @param entryCommandDefault
105 	 * @return
106 	 */
107 	protected abstract PrototypeEntry parseItems(String[] items, Integer part, PrototypeEntryType type, int currentIndex, PrototypeEntryCommandDefault entryCommandDefault);
108 
109 	
110 	
111 	@Override
112 	public String toString()
113 	{
114 		StringBuffer sb = new StringBuffer();
115 		sb.append(this.getClass().getName()).append("[");
116 		sb.append("currentLine=").append(this.currentLine);
117 		sb.append("]");
118 		return sb.toString();
119 	}
120 }