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
12
13
14
15
16 public abstract class AbstractPrototypeEntryParser implements PrototypeEntryParser
17 {
18
19 private Log log = LogFactory.getLog(AbstractPrototypeEntryParser.class);
20
21
22
23
24
25 private String currentLine;
26
27
28
29
30
31 public AbstractPrototypeEntryParser()
32 {
33 }
34
35
36
37
38
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
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
75
76
77
78
79
80 private final PrototypeEntry parseItems(String[] items, PrototypeEntryCommandDefault entryCommandDefault)
81 {
82 int currentIndex = 0;
83 Integer part = null;
84
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
98
99
100
101
102
103
104
105
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 }