view test/org/tmatesoft/hg/test/StatusOutputParser.java @ 94:af1f3b78b918

*StatusCollector renamed to Hg*StatusCollector
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 27 Jan 2011 21:18:47 +0100
parents d55d4eedfc57
children a3a2e5deb320
line wrap: on
line source
/*
 * Copyright (c) 2011 TMate Software Ltd
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact TMate Software at support@svnkit.com
 */
package org.tmatesoft.hg.test;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.tmatesoft.hg.core.Path;
import org.tmatesoft.hg.repo.HgStatusCollector;
import org.tmatesoft.hg.util.PathPool;
import org.tmatesoft.hg.util.PathRewrite;

/**
 *
 * @author Artem Tikhomirov
 * @author TMate Software Ltd.
 */
public class StatusOutputParser implements OutputParser {

	private final Pattern pattern;
	// although using StatusCollector.Record is not really quite honest for testing,
	// it's deemed acceptable as long as that class is primitive 'collect all results'
	private HgStatusCollector.Record result = new HgStatusCollector.Record();
	private final PathPool pathHelper;

	public StatusOutputParser() {
//		pattern = Pattern.compile("^([MAR?IC! ]) ([\\w \\.-/\\\\]+)$", Pattern.MULTILINE);
		pattern = Pattern.compile("^([MAR?IC! ]) (.+)$", Pattern.MULTILINE);
		pathHelper = new PathPool(new PathRewrite() {
			
			private final boolean winPathSeparator = File.separatorChar == '\\';

			public String rewrite(String s) {
				if (winPathSeparator) {
					// Java impl always give slashed path, while Hg uses local, os-specific convention
					s = s.replace('\\', '/'); 
				}
				return s;
			}
		});
	}

	public void reset() {
		result = new HgStatusCollector.Record();
	}

	public void parse(CharSequence seq) {
		Matcher m = pattern.matcher(seq);
		Path lastAdded = null;
		while (m.find()) {
			String fname = m.group(2);
			switch ((int) m.group(1).charAt(0)) {
			case (int) 'M' : {
				result.modified(pathHelper.path(fname));
				break;
			}
			case (int) 'A' : {
				result.added(lastAdded = pathHelper.path(fname));
				break;
			}
			case (int) 'R' : {
				result.removed(pathHelper.path(fname));
				break;
			}
			case (int) '?' : {
				result.unknown(pathHelper.path(fname));
				break;
			}
			case (int) 'I' : {
				result.ignored(pathHelper.path(fname));
				break;
			}
			case (int) 'C' : {
				result.clean(pathHelper.path(fname));
				break;
			}
			case (int) '!' : {
				result.missing(pathHelper.path(fname));
				break;
			}
			case (int) ' ' : {
				// last added is copy destination
				// to get or to remove it - depends on what StatusCollector does in this case
				result.copied(pathHelper.path(fname), lastAdded);
				break;
			}
			}
		}
	}

	// 
	public List<Path> getModified() {
		return result.getModified();
	}

	public List<Path> getAdded() {
		List<Path> rv = new LinkedList<Path>(result.getAdded());
		for (Path p : result.getCopied().keySet()) {
			rv.remove(p); // remove only one duplicate
		}
		return rv;
	}

	public List<Path> getRemoved() {
		return result.getRemoved();
	}

	public Map<Path,Path> getCopied() {
		return result.getCopied();
	}

	public List<Path> getClean() {
		return result.getClean();
	}

	public List<Path> getMissing() {
		return result.getMissing();
	}

	public List<Path> getUnknown() {
		return result.getUnknown();
	}

	public List<Path> getIgnored() {
		return result.getIgnored();
	}
}