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
12
13
14
15
16 public abstract class AbstractPkgMapEntryParser implements PkgMapEntryParser
17 {
18
19 private Log log = LogFactory.getLog(AbstractPkgMapEntryParser.class);
20
21
22
23
24
25 private String currentLine;
26
27
28
29
30
31 public AbstractPkgMapEntryParser()
32 {
33 }
34
35
36
37
38
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
65 this.currentLine = null;
66 }
67 }
68
69
70
71
72
73
74
75
76 private final PkgMapEntry parseItems(String[] items)
77 {
78 int currentIndex = 0;
79 Integer part = null;
80
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
94
95
96
97
98
99
100
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 }