001
002
003
004package org.biojava.utils.regex;
005
006import org.biojava.bio.symbol.FiniteAlphabet;
007import org.biojava.bio.symbol.IllegalAlphabetException;
008import org.biojava.bio.symbol.SymbolList;
009
010/**
011 * A class analogous to java.util.regex.Pattern but for SymbolLists.
012 * @author David Huen
013 * @since 1.4
014 */
015public class Pattern
016{
017    private FiniteAlphabet alfa;
018    private java.util.regex.Pattern pattern;
019    private String label;
020    private String patternTxt;
021
022    Pattern(String patternTxt, PatternChecker checker, FiniteAlphabet alfa)
023        throws IllegalAlphabetException, RegexException
024    {
025        this.pattern = java.util.regex.Pattern.compile(checker.parse(patternTxt));
026        this.patternTxt = patternTxt;
027        this.alfa = alfa;
028        label = patternTxt;
029    }
030
031    Pattern(String patternTxt, PatternChecker checker, FiniteAlphabet alfa, String label)
032        throws IllegalAlphabetException, RegexException
033    {
034        this.pattern = java.util.regex.Pattern.compile(checker.parse(patternTxt));
035        this.patternTxt = patternTxt;
036        this.alfa = alfa;
037        this.label = label;
038    }
039
040    /**
041     * return the String label associated with this pattern.
042     */
043    public String getName() { return label; }
044
045    /**
046     * Creates a matcher that will match the given input against this pattern.
047     * @param sl SymbolList against which match is to be made.
048     * @return A new matcher for this pattern.
049     */
050    public org.biojava.utils.regex.Matcher matcher(SymbolList sl)
051    {
052        return new org.biojava.utils.regex.Matcher(this, sl);
053    }
054
055    /**
056     * returns the Pattern to be matched as a String.
057     */
058    public String patternAsString()
059    {
060        return patternTxt;
061    }
062
063    /**
064     * returns the actual String used to construct the regex with all
065     * ambiguities expanded.
066    //FIXME: do something about unicode strings and conversion back to something sensible.
067     */
068    public String patternExpanded()
069    {
070        return pattern.pattern();
071    }
072
073    /**
074     * returns the java.util.regex.Pattern object that underlies this instance.
075     */
076    java.util.regex.Pattern getPattern() { return pattern; }
077
078    public FiniteAlphabet getAlphabet() { return alfa; }
079}
080