View Javadoc

1   package net.sf.jpkgmk.pkgmap;
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 AbstractPkgMapEntryParser implements PkgMapEntryParser
17  {
18  	
19  	private Log log = LogFactory.getLog(AbstractPkgMapEntryParser.class);
20  	
21  	/**
22  	 * The current line being parsed. Is set when the method {@link #parse(String)}
23  	 * is entered and reset when the method is leaved.	 
24  	 */
25  	private String currentLine;
26  	
27  	
28  	/**
29  	 * Default constructor
30  	 */
31  	public AbstractPkgMapEntryParser()
32  	{
33  	}
34  	
35  	
36  	/**
37  	 * @return Returns the current line being parsed. Is set when the method {@link #parse(String)}
38  	 * is entered and reset when the method is leaved.
39  	 */
40  	public String getCurrentLine() {
41  		return currentLine;
42  	}
43  
44  
45  	public final PkgMapEntry parse(String line) {
46  		
47  		log.debug("Parsing line '" + line + "'");
48  		
49  		this.currentLine = line;
50  		String[] items = PkgMapParser.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  			PkgMapEntry entry = parseItems(items);
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  	
70  	/**
71  	 * Invoked by the {@link #parse(String)} method to parse
72  	 * the items of a line. The given array of items represents the whole line split up into strings.
73  	 * @param items
74  	 * @return
75  	 */
76  	private final PkgMapEntry parseItems(String[] items) 
77  	{
78  		int currentIndex = 0;
79  		Integer part = null;
80  		// Check if the "part" field is defined at all - it is an optional field
81  		if(ArrayUtil.isArrayValueInt(items, currentIndex)) {
82  			part = ArrayUtil.getArrayValueAsInt(items, currentIndex);
83  			currentIndex++;
84  		}
85  
86  		String typeString = items[currentIndex++];
87  		PkgMapEntryType type = PkgMapEntryType.findType(typeString);
88  
89  		return parseItems(items, part, type, currentIndex);
90  	}
91  	
92  	/**
93  	 * Invoked by the {@link #parse(String)} method to parse
94  	 * the items of a line. The given array of items represents the whole line split up into strings.
95  	 * @param items All items of this line
96  	 * @param part The pre-parsed value for the optional "part" field
97  	 * @param type The pre-parsed value for the mandatory "type" character field
98  	 * @param currentIndex The current index located after the "type" field already parsed. This is the index where
99  	 * the parsing should be continued by the concrete implementations.
100 	 * @return
101 	 */
102 	protected abstract PkgMapEntry parseItems(String[] items, Integer part, PkgMapEntryType type, int currentIndex);
103 	
104 	
105 	@Override
106 	public String toString()
107 	{
108 		StringBuffer sb = new StringBuffer();
109 		sb.append(this.getClass().getName()).append("[");
110 		sb.append("currentLine=").append(this.currentLine);
111 		sb.append("]");
112 		return sb.toString();
113 	}
114 }