Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8f9554b5be1574c214d4096874a2ec3a9141b17d (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
/*******************************************************************************
 * Copyright (c) 2013 Red Hat.
 * 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:
 *     Red Hat - Andrew Ferrazzutti
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.graphingapi.core.datasets.row;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.linuxtools.systemtap.graphingapi.core.datasets.IDataEntry;
import org.eclipse.linuxtools.systemtap.graphingapi.core.datasets.IDataSetParser;

/**
 * A DataSetParser for parsing a string, line-by-line, with a preconstructed
 * single-line regex string.
 * 
 * @author aferrazz
 * @since 1.1
 *
 */
public class LineParser implements IDataSetParser {
	public LineParser(String regEx) {
		wholePattern = Pattern.compile(regEx, Pattern.MULTILINE);
	}

	@Override
	public IDataEntry parse(StringBuilder s) {
		if(null == s) {
			return null;
		}

		RowEntry e = null;
		Matcher wholeMatcher = wholePattern.matcher(s);

		if(wholeMatcher.find()) {
			e = new RowEntry();
			int groupCount = wholeMatcher.groupCount();
			Object[] data = new Object[groupCount];

			for(int i = 0; i < groupCount; i++) {
				data[i] = wholeMatcher.group(i+1);
			}
			e.putRow(0, data);
			s.delete(0, wholeMatcher.end());
		}

		return e;
	}

	private Pattern wholePattern;
}

Back to the top