summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/ProcessStreamBuffer.java
blob: 111f6071f8fc018ef42bdc2081f0bc0e1526cd03 (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
83
84
85
86
87
/*******************************************************************************
 * Copyright (c) 2013 Intel Corporation.
 * 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:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.remote.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class ProcessStreamBuffer {
	private static final String WHITESPACES = "\\s+";
	List<String> errorLines;
	List<String> outputLines;
	boolean isTerminal;

	ProcessStreamBuffer(boolean isTerminal){
		this.isTerminal = isTerminal;
		errorLines = new ArrayList<String>();
		outputLines = new ArrayList<String>();
	}

	public void addErrorLine(String line){
		errorLines.add(line);
	}
	public void addOutputLine(String line){
		outputLines.add(line);
	}

	public List<String> getOutputLines(){
		return outputLines;
	}

	public List<String> getErrorLines(){
		return errorLines;
	}

	public String getMergedOutputLines(){
		String returnVal = "";
		for (int i = 0; i < outputLines.size(); i++) {
			String line = outputLines.get(i);
			returnVal += line;
			if (outputLines.size() > 1 && i != outputLines.size() - 1)
				returnVal += "\n";
		}
		return returnVal;
	}

	public boolean hasErrors() {
		return errorLines.size() != 0;
	}

	public String getLastOutputLineContaining(String str) {
		if (!errorLines.isEmpty())
			return null;
		for (int i = outputLines.size() - 1; i >= 0; i--){
			String line = outputLines.get(i);
			if (line.replaceAll(WHITESPACES, "").contains(str.replaceAll(WHITESPACES, "")))
				return line;
		}
		return null;
	}

	public String getOutputLineContaining(String arg, String pattern) {
		List<String> lines = null;
		if (isTerminal)
			lines = errorLines;
		else
			lines = outputLines;
		for (int i = lines.size() - 1; i >= 0; i--){
			String line = lines.get(i);
			if (line.contains(arg)) {
				String[] tokens = line.split("\\s+");
				if (Pattern.matches(pattern,  tokens[0])) {
					return tokens[0];
				}
			}
		}
		return "";
	}
}