Project

General

Profile

1
package eu.dnetlib.enabling.is.sn;
2

    
3
import java.util.Collection;
4

    
5
import org.apache.oro.text.perl.Perl5Util;
6

    
7
/**
8
 * Provides common functionality for subscription registries.
9
 * 
10
 * @author marko
11
 * 
12
 */
13
public abstract class AbstractSubscriptionRegistry {
14

    
15
	/**
16
	 * prefix position in regular expression.
17
	 */
18
	private static final int PREFIX_POSITION = 1;
19
	
20
	/**
21
	 * rest of string position in regular expression.
22
	 */
23
	private static final int REST_POSITION = 3;
24
	
25
	/**
26
	 * Return true if the subscription prefix is acceptable.
27
	 * 
28
	 * Topic expressions are made of slash-delimited components. The "prefix" here is the first slash (/) delimited
29
	 * string of the topic expression.
30
	 * 
31
	 * TODO: stupid implementation.
32
	 * 
33
	 * @param subscription
34
	 *            subscription request
35
	 * @return true if accepted
36
	 */
37
	protected TopicExpressionMatchResult matchPrefix(final SubscriptionRequest subscription) {
38
		final Perl5Util matcher = new Perl5Util(); // NOPMD
39
		
40
		for (String prefix : getAcceptedPrefixes())
41
			if (matcher.match("/^(" + prefix + ")($|/)(.*)$/", subscription.getTopicExpression())) {
42
				return new TopicExpressionMatchResult(matcher.getMatch().group(PREFIX_POSITION), matcher.getMatch().group(REST_POSITION));
43
			}
44
		return null;
45
	}
46

    
47
	/**
48
	 * A collection of accepted prefixes.
49
	 * 
50
	 * @return prefix list
51
	 */
52
	protected abstract Collection<String> getAcceptedPrefixes();
53
}
(3-3/23)