aboutsummaryrefslogtreecommitdiffstats
blob: 22665b0ef1fd32f883378f4a4a2fc6382be1e7e4 (plain)
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
/*******************************************************************************
 * Copyright (c) 2008 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:
 *    Kent Sebastian <ksebasti@redhat.com> - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.linuxtools.internal.oprofile.core.opxml.modeldata;

import java.util.ArrayList;

import org.eclipse.linuxtools.internal.oprofile.core.model.OpModelSymbol;
import org.eclipse.linuxtools.internal.oprofile.core.opxml.OprofileSAXHandler;
import org.eclipse.linuxtools.internal.oprofile.core.opxml.XMLProcessor;
import org.xml.sax.Attributes;

/**
 * A processor for the <symbols> tag from `opxml samples ..`
 */
public class SymbolsProcessor extends XMLProcessor {
	//XML tags parsed by this processor
	private static final String SYMBOLS_TAG = "symbols"; //$NON-NLS-1$
	private static final String SYMBOL_TAG = "symbol";  //$NON-NLS-1$
	private static final String SAMPLE_TAG = "sample"; //$NON-NLS-1$

	//attribute tags
	private static final String ATTR_NAME = "name"; //$NON-NLS-1$
	private static final String ATTR_FILE = "file"; //$NON-NLS-1$
	private static final String ATTR_COUNT = "count";	 //$NON-NLS-1$
	private static final String ATTR_LINE = "line";	 //$NON-NLS-1$
	
	//the current symbol being constructed
	private OpModelSymbol _symbol;
	//all the symbols in this <symbols> tag, to be returned to the calling ModelDataProcessor
	private ArrayList<OpModelSymbol> _symbols;
	
	//the processor used for individual samples
	private SamplesProcessor _samplesProcessor = new SamplesProcessor();
	
	
	public void reset(Object callData) {
		_symbol = new OpModelSymbol();
		_symbols = new ArrayList<OpModelSymbol>();
	}

	/**
	 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#startElement(String, Attributes)
	 */
	public void startElement(String name, Attributes attrs, Object callData) {
		if (name.equals(SYMBOL_TAG)) {
			_symbol._setName(valid_string(attrs.getValue(ATTR_NAME)));
			_symbol._setCount(Integer.parseInt(attrs.getValue(ATTR_COUNT)));
			_symbol._setFilePath(valid_string(attrs.getValue(ATTR_FILE)));
			_symbol.setLine(Integer.parseInt(attrs.getValue(ATTR_LINE)));
		} else if (name.equals(SAMPLE_TAG)) {
			OprofileSAXHandler.getInstance(callData).push(_samplesProcessor);
		}
	}
	
	/**
	 * @see org.eclipse.linuxtools.internal.oprofile.core.XMLProcessor#endElement(String)
	 */
	public void endElement(String name, Object callData) {
		if (name.equals(SYMBOL_TAG)) {
			_symbol._setSamples(_samplesProcessor.getSamples());
			_symbols.add(_symbol);
			_symbol = new OpModelSymbol();
		} else if (name.equals(SYMBOLS_TAG)) {
			OprofileSAXHandler.getInstance(callData).pop(SYMBOLS_TAG);
		}
	}
	
	//return all parsed samples
	public OpModelSymbol[] getSymbols() {
		OpModelSymbol [] symbols = new OpModelSymbol[_symbols.size()];
		_symbols.toArray(symbols);
		return symbols;
	}
}