Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: de37d6cb948fac78494694ba60c74ac4f70d27cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*******************************************************************************
 * Copyright (c) 2004,2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Keith Seitz <keiths@redhat.com> - initial API and implementation
 *    Kent Sebastian <ksebasti@redhat.com>
 *******************************************************************************/ 
package org.eclipse.linuxtools.internal.oprofile.core.opxml.info;

import java.util.ArrayList;

import org.eclipse.linuxtools.internal.oprofile.core.daemon.OpEvent;
import org.eclipse.linuxtools.internal.oprofile.core.daemon.OpUnitMask;
import org.eclipse.linuxtools.internal.oprofile.core.opxml.OprofileSAXHandler;
import org.eclipse.linuxtools.internal.oprofile.core.opxml.XMLProcessor;
import org.xml.sax.Attributes;


/**
 * XML handler class for opxml's "event-list".
 * @see org.eclipse.linuxtools.internal.oprofile.core.opxml.OpxmlRunner
 */
public class EventListProcessor extends XMLProcessor {
	// The current event being constructed
	private OpEvent _currentEvent;
	private int _counter;
	private ArrayList<OpEvent> _currentEventList;
	
	// An XML processor for reading the unit mask information for an event
	private UnitMaskProcessor _umProcessor;
	
	// XML elements recognized by this processor
	private static final String _EVENT_TAG = "event"; //$NON-NLS-1$
	private static final String _UNIT_MASK_TAG = "unit-mask"; //$NON-NLS-1$
	private static final String _NAME_TAG = "name"; //$NON-NLS-1$
	private static final String _VALUE_TAG = "value"; //$NON-NLS-1$
	private static final String _DESCRIPTION_TAG = "description"; //$NON-NLS-1$
	private static final String _MASK_TAG = "mask"; //$NON-NLS-1$
	private static final String _MINIMUM_COUNT_TAG = "minimum"; //$NON-NLS-1$
	private static final String _ATTR_EVENT_LIST_COUNTER = "counter"; //$NON-NLS-1$
	
	// This is a special processor which is used to deal with a single mask value
	private class MaskProcessor extends XMLProcessor {
		private OpUnitMask.MaskInfo _info;
		
		/**
		 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#reset()
		 */
		public void reset(Object callData) {
			_info = new OpUnitMask.MaskInfo();
		}
		
		/**
		 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#endElement(String)
		 */
		public void endElement(String name, Object callData) {
			if (name.equals(_VALUE_TAG)) {
				// Set mask's value
				_info.value = Integer.parseInt(_characters);
			} else if (name.equals(_DESCRIPTION_TAG)) {
				_info.description = _characters;
			} else if (name.equals(_MASK_TAG)) {
				// Pop and pass mask tag to previous processor (UnitMaskProcessor)
				OprofileSAXHandler.getInstance(callData).pop(_MASK_TAG);
			}
		}

		/**
		 * Returns the information that has been collected about a mask.
		 * @return the mask information
		 */
		public OpUnitMask.MaskInfo getResult() {
			return _info;
		}
	}
	
	// This is a special processor to handle unit mask information
	private class UnitMaskProcessor extends XMLProcessor {
		// An ArrayList to hold all the valid masks for a unit mask.
		private ArrayList<OpUnitMask.MaskInfo> _masks;
		
		// The unit mask being constructed
		private OpUnitMask _unitMask;
		
		// An XML processor for each individual mask value.
		private MaskProcessor _maskProcessor;
		
		// XML elements recognized by this processor
		private static final String _MASK_TYPE_TAG = "type"; //$NON-NLS-1$
		private static final String _MASK_DEFAULT_TAG = "default"; //$NON-NLS-1$
		private static final String _MASK_TYPE_BITMASK = "bitmask"; //$NON-NLS-1$
		private static final String _MASK_TYPE_MANDATORY = "mandatory"; //$NON-NLS-1$
		private static final String _MASK_TYPE_EXCLUSIVE = "exclusive"; //$NON-NLS-1$
		
		/**
		 * Constructor for UnitMaskProcessor. Initializes internal state.
		 */
		public UnitMaskProcessor() {
			super();
			_maskProcessor = new MaskProcessor();
			_masks = new ArrayList<OpUnitMask.MaskInfo>();
		}
		
		/**
		 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#reset()
		 */
		public void reset(Object callData) {
			_unitMask = new OpUnitMask();
			_masks.clear();
		}
		
		/**
		 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#startElement(String, Attributes)
		 */
		public void startElement(String name, Attributes attrs, Object callData) {
			if (name.equals(_MASK_TAG)) {
				// Tell SAX handler to use the mask processor
				OprofileSAXHandler.getInstance(callData).push(_maskProcessor);
			} else {
				super.startElement(name, attrs, callData);
			}
		}
		
		/**
		 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#endElement(String)
		 */
		public void endElement(String name, Object callData) {
			if (name.equals(_MASK_TYPE_TAG)) {
				// Set the mask type
				_unitMask._setType(_getTypeFromString(_characters));
			} else if (name.equals(_MASK_DEFAULT_TAG)) {
				// Set the default mask
				_unitMask._setDefault(Integer.parseInt(_characters));
			} else if (name.equals(_MASK_TAG)) {
				// Add this mask description to the list of all masks
				_masks.add(_maskProcessor.getResult());
			} else if (name.equals(_UNIT_MASK_TAG)) {
				// All done. Add the known masks to the unit mask
				OpUnitMask.MaskInfo[] descs = new OpUnitMask.MaskInfo[_masks.size()];
				_masks.toArray(descs);
				_unitMask._setMaskDescriptions(descs);
				
				// Pop this processor and pass _UNIT_MASK_TAG to previoius processor
				OprofileSAXHandler.getInstance(callData).pop(_UNIT_MASK_TAG);
			}
		}
				
		/**
		 * Returns the constructed unit mask.
		 * @return the unit mask
		 */
		public OpUnitMask getResult() {
			return _unitMask;
		}
		
		// Converts a string representing a mask type into an integer
		private int _getTypeFromString(String string) {
			if (string.equals(_MASK_TYPE_MANDATORY)) {
				return OpUnitMask.MANDATORY;
			} else if (string.equals(_MASK_TYPE_BITMASK)) {
				return OpUnitMask.BITMASK;
			} else if (string.equals(_MASK_TYPE_EXCLUSIVE)) {
				return OpUnitMask.EXCLUSIVE;
			}

			return -1;
		}
	};
	
	/**
	 * Constructor for EventListProcessor. Initializes internal state.
	 */
	public EventListProcessor() {
		super();
		_umProcessor = new UnitMaskProcessor();
	}
	
	@Override
	public void reset(Object callData) {
		_currentEventList = new ArrayList<OpEvent>();
	}
	
	/**
	 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#startElement(String, Attributes)
	 */
	public void startElement(String name, Attributes attrs, Object callData) {
		if (name.equals(_EVENT_TAG)) {
			// new event
			_currentEvent = new OpEvent();
		} else if (name.equals(_UNIT_MASK_TAG)) {
			// Tell the SAX handler to use the unit mask processor
			OprofileSAXHandler.getInstance(callData).push(_umProcessor);
		} else if (name.equals(OpInfoProcessor.EVENT_LIST_TAG)) {
			// Our start tag: grab the counter number from the attributes
			_counter = Integer.parseInt(attrs.getValue(_ATTR_EVENT_LIST_COUNTER));
		} else {
			super.startElement(name, attrs, callData);
		}
	}
	
	/**
	 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#endElement(String)
	 */
	public void endElement(String name, Object callData) {
		if (name.equals(_EVENT_TAG)) {
			// Finished constructing an event. Add it to the list.
			_currentEventList.add(_currentEvent);
		} else if (name.equals(_UNIT_MASK_TAG)) {
			// Set the event's unit mask
			_currentEvent.setUnitMask(_umProcessor.getResult());
		} else if (name.equals(_NAME_TAG)) {
			// Set event's name
			_currentEvent.setText(_characters);
		} else if (name.equals(_DESCRIPTION_TAG)) {
			// Set event's description
			_currentEvent.setTextDescription(_characters);
		} else if (name.equals(_MINIMUM_COUNT_TAG)) {
			// Set event's minimum count
			_currentEvent.setMinCount(Integer.parseInt(_characters));
		} else if (name.equals(OpInfoProcessor.EVENT_LIST_TAG)) {
			OprofileSAXHandler.getInstance(callData).pop(name);
		}
	}
	
	public int getCounterNum() {
		return _counter;
	}
	
	public OpEvent[] getEvents() {
		OpEvent[] events = new OpEvent[_currentEventList.size()];
		_currentEventList.toArray(events);
		return events;
	}
}

Back to the top