Commit | Line | Data |
---|---|---|
146a887c FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * William Bourque (wbourque@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.lttng.event; | |
14 | ||
15 | import java.util.HashMap; | |
16 | import java.util.Iterator; | |
17 | ||
18 | import org.eclipse.linuxtools.tmf.event.TmfEventFormat; | |
19 | ||
20 | /** | |
07d9e2ee FC |
21 | * <b><u>LttngEventFormat</u></b><p> |
22 | * | |
23 | * Lttng specific implementation of the TmfEventFormat.<p> | |
146a887c FC |
24 | */ |
25 | public class LttngEventFormat extends TmfEventFormat { | |
26 | ||
27 | /** | |
07d9e2ee FC |
28 | * Constructor with parameters.<p> |
29 | * | |
30 | * @param labels The fields labels of the content. | |
146a887c | 31 | */ |
3fbd810a FC |
32 | public LttngEventFormat( String[] labels) { |
33 | super(labels); | |
146a887c | 34 | } |
3fbd810a | 35 | |
146a887c | 36 | /** |
07d9e2ee FC |
37 | * Copy constructor.<p> |
38 | * | |
39 | * @param oldFormat The format to copy from | |
146a887c | 40 | */ |
3fbd810a FC |
41 | public LttngEventFormat( LttngEventFormat oldFormat ) { |
42 | this(oldFormat.getLabels()); | |
146a887c FC |
43 | } |
44 | ||
45 | /** | |
07d9e2ee | 46 | * Parse the given LttngEvent.<p> |
146a887c | 47 | * |
07d9e2ee FC |
48 | * @param thisEvent The LttngEvent to parse |
49 | * | |
50 | * @return An array of LttngEventField that contain the parsed data | |
146a887c FC |
51 | */ |
52 | public LttngEventField[] parse(LttngEvent thisEvent) { | |
07d9e2ee | 53 | // Obtain the parsed content from the JNI |
146a887c FC |
54 | HashMap<String, Object> parsedMap = thisEvent.convertEventTmfToJni().parseAllFields(); |
55 | LttngEventField[] returnedField = new LttngEventField[parsedMap.size()]; | |
56 | ||
57 | String fieldName = null; | |
58 | int position = 0; | |
59 | Iterator<String> iterator = parsedMap.keySet().iterator(); | |
07d9e2ee | 60 | // Loop to create the LttngEventField from the parsedContent |
146a887c FC |
61 | while (iterator.hasNext()) { |
62 | fieldName = iterator.next(); | |
63 | returnedField[position] = new LttngEventField( fieldName, parsedMap.get(fieldName) ); | |
64 | position++; | |
65 | } | |
66 | ||
67 | return returnedField; | |
68 | } | |
69 | ||
70 | ||
07d9e2ee FC |
71 | /** |
72 | * <b>*FIXME*</b><br>Parse from a HashMap of content.<p> | |
73 | * | |
74 | * This function will hopefully disapears soon!<br> | |
75 | * We need to parse WHILE CREATING LttngEvent, so we cannot always use parse(LttngEvent).<br> | |
76 | * This function is ugly but should be 100% safe to use.<p> | |
77 | * | |
78 | * @param parsedEvents HashMap of parsed content, as returned by JniParser.parseAllFields() | |
79 | * | |
80 | * @return An array of TmfEventFields | |
146a887c FC |
81 | */ |
82 | public LttngEventField[] parse(HashMap<String, Object> parsedEvents) { | |
83 | LttngEventField[] returnedField = new LttngEventField[parsedEvents.size()]; | |
84 | ||
85 | String fieldName = null; | |
86 | int position = 0; | |
87 | Iterator<String> iterator = parsedEvents.keySet().iterator(); | |
07d9e2ee | 88 | // Loop to create the LttngEventField from the parsedContent in the map |
146a887c FC |
89 | while (iterator.hasNext()) { |
90 | fieldName = iterator.next(); | |
91 | returnedField[position] = new LttngEventField( fieldName, parsedEvents.get(fieldName) ); | |
92 | position++; | |
93 | } | |
146a887c FC |
94 | return returnedField; |
95 | } | |
96 | ||
07d9e2ee FC |
97 | /** |
98 | * <b>*DEPRECATED*</b><br>Parse from the content string.<p> | |
99 | * | |
100 | * <b><u>DO NOT USE!</b></u> <br> | |
101 | * This function will disapears soon! Use parse(LttngEvent) instead.<br> | |
102 | * It is evil to parse content string directly as we <u>do not control the content</u>.<br> | |
103 | * So this would theorically works but it could break at any moment if kernel developpers changes their usual format.<p> | |
104 | * | |
105 | * This is provided because we need to implement this function since we inherit TmfFormat | |
106 | * | |
107 | * @param uselessContent Content to parse as String | |
108 | * | |
109 | * @return An array of TmfEventFields | |
110 | */ | |
146a887c | 111 | public LttngEventField[] parse(String uselessContent) { |
07d9e2ee FC |
112 | |
113 | // This function is _under commented_ because you should not use it | |
114 | // (this imply you shouldn't read it as well). | |
115 | ||
146a887c FC |
116 | // *** Begining of the evil "parse String" function |
117 | // | |
118 | // - 1st : Find the number of ":" in the String. This will be the number of fields in the String. | |
119 | // Highly unreliable, as we depend on String content that we don't control! | |
120 | int nbFields = 0; | |
121 | for ( int pos = 0; pos < uselessContent.length(); pos++ ) { | |
122 | if ( uselessContent.substring(pos, pos+1).equals(":") ) { | |
123 | nbFields++; | |
124 | } | |
125 | } | |
126 | ||
127 | // - 2nd : Create the fields array | |
128 | LttngEventField[] tmpFields = new LttngEventField[nbFields]; | |
129 | ||
130 | // - 3rd : Fill the array | |
07d9e2ee FC |
131 | // Content is the NAME:VALUE format with space separators |
132 | // | |
133 | // We search for 3 possible case : | |
134 | // - Field name | |
135 | // - Value (AKA payload or parsed content) | |
136 | // - "Out of range" (EOF found) | |
137 | // | |
138 | // | |
146a887c | 139 | int fieldPosition = 0; |
3fbd810a | 140 | int lastFieldnamePos = 0; |
146a887c FC |
141 | int lastDoubleDottedPos = 0; |
142 | int lastSpacePos = 0; | |
3fbd810a FC |
143 | boolean isSearchingFieldname = true; |
144 | ||
145 | String fieldName = ""; | |
146 | String fieldValue = ""; | |
147 | ||
148 | int pos = 0; | |
149 | while ( (pos < uselessContent.length()) && (fieldPosition<nbFields) ) | |
150 | { | |
07d9e2ee | 151 | // : symbol mean we found the end of "NAME" |
146a887c | 152 | if ( uselessContent.substring(pos, pos+1).equals(":") ) { |
3fbd810a FC |
153 | |
154 | if ( isSearchingFieldname==false ) { | |
155 | fieldValue = uselessContent.substring(lastDoubleDottedPos+1, lastSpacePos); | |
156 | tmpFields[fieldPosition] = new LttngEventField( fieldName, fieldValue ); | |
157 | fieldPosition++; | |
158 | isSearchingFieldname = true; | |
159 | } | |
160 | ||
161 | lastDoubleDottedPos = pos; | |
146a887c | 162 | } |
07d9e2ee | 163 | // space mean we found the end of VALUE |
146a887c | 164 | else if ( uselessContent.substring(pos, pos+1).equals(" ") ) { |
146a887c | 165 | |
3fbd810a FC |
166 | if ( isSearchingFieldname==true ) { |
167 | fieldName = uselessContent.substring(lastFieldnamePos, lastDoubleDottedPos); | |
168 | lastFieldnamePos = pos+1; | |
169 | isSearchingFieldname = false; | |
146a887c | 170 | } |
3fbd810a FC |
171 | |
172 | lastSpacePos = pos; | |
173 | } | |
07d9e2ee | 174 | // pos+2 > length mean EOF |
3fbd810a FC |
175 | else if ( pos+2 >= uselessContent.length() ) { |
176 | fieldName = uselessContent.substring(lastSpacePos+1, lastDoubleDottedPos); | |
177 | fieldValue = uselessContent.substring(lastDoubleDottedPos+1, pos+1); | |
178 | ||
179 | tmpFields[fieldPosition] = new LttngEventField( fieldName, fieldValue ); | |
180 | fieldPosition++; | |
146a887c | 181 | } |
3fbd810a FC |
182 | |
183 | pos++; | |
146a887c FC |
184 | } |
185 | ||
186 | return tmpFields; | |
187 | } | |
07d9e2ee | 188 | |
146a887c | 189 | } |