FileDocCategorySizeDatePackage
TorrentFilter.javaAPI DocAzureus 3.0.3.45880Mon Sep 26 01:09:48 BST 2005org.gudy.azureus2.ui.console.commands

TorrentFilter

public class TorrentFilter extends Object

Fields Summary
private static final Pattern
rangePattern
Constructors Summary
public TorrentFilter()

	
	  
		super();
	
Methods Summary
public java.util.ListgetTorrents(java.util.List torrentsToMatch, java.lang.String filter)
tries our two different matching algorithms using the supplied filter against the list of torrents in the ConsoleInput object

param
torrentsToMatch list of DownloadManager objects to attempt to match against
param
filter filter - eg: range or glob filter
return
list of matched DownloadManager objects

		List torrents = new ArrayList();
		torrents.addAll(matchRange(torrentsToMatch, filter) );
		torrents.addAll(matchWildcard(torrentsToMatch, filter) );
		return torrents;
	
public java.util.ListgetTorrents(java.util.List torrentsToMatch, java.util.List args)
first tries to match torrents by concatenating all of the arguments. if that doesn't work, attempts to match each argument individually.

param
torrentsToMatch list of DownloadManager objects to attempt to match against
param
args arguments to try to match
return
list of matched DownloadManager objects

		// first, try to match the whole list concatenated as a string
		StringBuffer allArgs = new StringBuffer();
		boolean first = true;
		for (Iterator iter = args.iterator(); iter.hasNext();) {
			if( ! first ) 
				allArgs.append(",");
			else
				first = false;
			allArgs.append(iter.next());
		}
		List torrents;
		torrents = matchWildcard(torrentsToMatch, allArgs.toString());
		if( torrents.size() > 0 )
			return torrents;
		torrents = matchRange(torrentsToMatch, allArgs.toString());
		if( torrents.size() > 0 )
			return torrents;
		// if no torrents then handle each argument individually
		for (Iterator iter = args.iterator(); iter.hasNext();) {
			torrents.addAll(getTorrents(torrentsToMatch, (String)iter.next()) );
		}
		return torrents;
	
private java.util.ListmatchRange(java.util.List torrents, java.lang.String filter)
matches a range of torrents. eg: 3-5 or a single torrent. eg: 3. or from 3 onwards: 3-

param
torrents torrents to match
param
filter range expression
return
list of matched DownloadManager objects

		Matcher matcher = rangePattern.matcher(filter);
		List list = new ArrayList();
		if( matcher.matches() )
		{
			int minId = Integer.parseInt(matcher.group(1));
			if( minId == 0 )
				throw new AzureusCoreException("lower range must be greater than 0");
			if( minId > torrents.size() )
				throw new AzureusCoreException("lower range specified (" + minId + ") is outside number of torrents (" + torrents.size() + ")");
			if( matcher.group(2) == null )
			{				
				// received a single number. eg: 3
				list.add(torrents.get(minId-1));
				return list;
			}
			int maxId;
			if( matcher.group(3) == null )
				// received bound range. eg: 3-5
				maxId = Integer.parseInt(matcher.group(5));
			else
				// received open ended range. eg: 3-
				maxId = torrents.size();
			
			if( minId > maxId )
				throw new AzureusCoreException("when specifying a range, the max value must be greater than or equal to the min value");
			
			for( int i = (minId-1) ; i < maxId && i < torrents.size() ; i++ )
			{
				list.add(torrents.get(i));
			}
		}
		return list;
	
private java.util.ListmatchWildcard(java.util.List torrents, java.lang.String filter)
attempst to match a wildcard against the list of torrents by checking their display name

param
torrents list of available torrents to match
param
filter wildcard (glob) filter
return
list of matched DownloadManager objects

		Pattern pattern = Pattern.compile(wildcardToPattern(filter), Pattern.CASE_INSENSITIVE);
		List list = new ArrayList();
		for (Iterator iter = torrents.iterator(); iter.hasNext();) {
			DownloadManager dm = (DownloadManager) iter.next();
			if( pattern.matcher(dm.getDisplayName()).matches() )
				list.add(dm);
		}
		return list;
	
private java.lang.StringwildcardToPattern(java.lang.String wild)
converts the wildcard (eg: tran*) into a regular expression - (tran.*)

param
wild wildcard (glob) expression
return
regular expression string

		if (wild == null)
			return null;

		StringBuffer buffer = new StringBuffer();

		char[] chars = wild.toCharArray();

		for (int i = 0; i < chars.length; ++i) {
			if (chars[i] == '*")
				buffer.append(".*");
			else if (chars[i] == '?")
				buffer.append(".");
			else if ("+()^$.{}[]|\\".indexOf(chars[i]) != -1)
				buffer.append('\\").append(chars[i]); // prefix all metacharacters
													// with backslash
			else
				buffer.append(chars[i]);
		}
		return buffer.toString().toLowerCase();