1 package net.logAnalyzer.handlers;
2
3 import net.logAnalyzer.converters.LAConverter;
4 import net.logAnalyzer.converters.UnknownOrLiteralConverterException;
5
6 /***
7 * Instances of this class are used to contain a log messageLabel parsed from a log
8 * file.
9 * <p>
10 * Each messageLabel has references to converters in {@link #getConverters()} used to
11 * parse/format messageLabel field values.
12 * </p>
13 *
14 * @author KKI
15 * @version 0.1
16 */
17 public final class LAMessage {
18 private LALogHandler handler = null;
19
20 private Object[] values = null;
21
22 /***
23 * Constructs a messageLabel with the converters and values specified.
24 *
25 * @param handler
26 * LALogHandler creating the messageLabel.
27 * @param values
28 * Values of the messageLabel.
29 */
30 public LAMessage(LALogHandler handler, Object[] values) {
31 this.handler = handler;
32 this.values = values;
33 }
34
35 /***
36 * Adds an unparsed line to the value of the messageLabel column (converter '%m'
37 * for log4j but could be any converter with {@link LAConverter#isMessage()}
38 * returning <code>true</code>).
39 *
40 * @param unparsedLine
41 * Unparsed line to add to the messageLabel column value.
42 * @see LALogHandler#getMessageConverter()
43 */
44 public void extendsMessage(String unparsedLine) {
45 try {
46 int index = handler.getConverterIndex(handler.getMessageConverter()
47 .getLiteral());
48
49 values[index] = values[index].toString() + "\n" + unparsedLine;
50 } catch (UnknownOrLiteralConverterException uolce) {
51
52 }
53 }
54
55 /***
56 * Returns the converter at the specified index.
57 *
58 * @param index
59 * Index of the converter.
60 * @return Converter at the specified index.
61 */
62 public LAConverter getConverter(int index) {
63 return this.handler.getConverter(index);
64 }
65
66 /***
67 * Returns the converters.
68 *
69 * @return Converters.
70 */
71 public LAConverter[] getConverters() {
72 return this.handler.getConverters();
73 }
74
75 /***
76 * Returns the index of the converter associated to the literal.
77 *
78 * @param literal
79 * Literal of the converter.
80 * @return index.
81 */
82 public int getConverterIndex(String literal)
83 throws UnknownOrLiteralConverterException {
84 return handler.getConverterIndex(literal);
85 }
86
87 /***
88 * Returns the value to the specified index.
89 *
90 * @param index
91 * Index of the value.
92 * @return Value at the specified index.
93 */
94 public String getStringValue(int index) {
95 return getConverter(index).toString(this.values[index]);
96 }
97
98 /***
99 * Returns the value for the specified key.
100 *
101 * @param literal
102 * Converter key as literal.
103 * @return Value at the specified index.
104 */
105 public String getStringValue(String literal)
106 throws UnknownOrLiteralConverterException {
107 return getStringValue(handler.getConverterIndex(literal));
108 }
109
110 /***
111 * Returns the value to the specified index.
112 *
113 * @param index
114 * Index of the value.
115 * @return Value at the specified index.
116 */
117 public Object getValue(int index) {
118 return this.values[index];
119 }
120
121 /***
122 * Returns the value to the specified column.
123 *
124 * @param literal
125 * Literal of the column.
126 * @return Value for the specified column.
127 * @throws UnknownOrLiteralConverterException
128 * If the literal does not identify a non literal column.
129 */
130 public Object getValue(String literal)
131 throws UnknownOrLiteralConverterException {
132 return this.values[handler.getConverterIndex(literal)];
133 }
134
135 /***
136 * Returns the values.
137 *
138 * @return Values.
139 */
140 public Object[] getValues() {
141 return this.values;
142 }
143
144 /***
145 * Returns the String representation of a messageLabel. Equals to the original
146 * messageLabel read from the log file (plus the computed column if any).
147 *
148 * @return String representation of the messageLabel.
149 * @see java.lang.Object#toString()
150 */
151 public String toString() {
152 StringBuffer message = new StringBuffer();
153 for (int i = 0; i < values.length; i++) {
154 message.append(getConverter(i).toString(values[i]));
155 }
156 return message.toString();
157 }
158 }