001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.symbol;
023
024import java.io.IOException;
025import java.io.PrintWriter;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.biojava.bio.BioException;
030import org.biojava.utils.xml.PrettyXMLWriter;
031
032
033public interface CodonPrefFilter
034{
035    /**
036     * indicates if the current CodonPref is to be accepted
037     */
038    public boolean isRequired(String name);
039
040    /**
041     * handles storage of a CodonPref object
042     */
043    public void put(CodonPref codonPref) throws BioException;
044
045
046    public class AcceptAll implements CodonPrefFilter {
047      List filteredCodonPrefs;
048
049      public AcceptAll(){
050        filteredCodonPrefs = new ArrayList();
051      }
052
053      public boolean isRequired(String name){
054        return true;
055      }
056
057      public void put(CodonPref codonPref){
058        filteredCodonPrefs.add(codonPref);
059      }
060
061      public List getCodonPrefs(){
062        return filteredCodonPrefs;
063      }
064    }
065
066    public class ByName implements CodonPrefFilter
067    {
068        String name;
069        CodonPref filteredCodonPref;
070
071        public ByName(String name)
072        {
073            this.name = name;
074        }
075
076        public boolean isRequired(String name)
077        {
078            return name.equals(this.name);
079        }
080
081        public void put(CodonPref codonPref)
082            throws BioException
083        {
084            filteredCodonPref = codonPref;
085        }
086
087        public CodonPref getCodonPref()
088        {
089            return filteredCodonPref;
090        }
091    }
092
093    public class EverythingToXML implements CodonPrefFilter
094    {
095        PrintWriter pw;
096        PrettyXMLWriter xw;
097        boolean writtenWrapper = false;
098
099        public EverythingToXML(PrintWriter pw)
100        {
101            this.pw = pw;
102            xw = new PrettyXMLWriter(pw);
103        }
104
105        public boolean isRequired(String name)
106        {
107            return true;
108        }
109
110        public void put(CodonPref codonPref)
111            throws BioException
112        {
113            try {
114            if (!writtenWrapper) {
115                xw.openTag("CodonPrefs");
116                writtenWrapper = true;
117            }
118
119            CodonPrefTools.dumpToXML(codonPref, xw, false);
120            }
121            catch (IOException ioe) {
122                throw new BioException(ioe);
123            }
124        }
125
126        public void close()
127            throws IOException
128        {
129            xw.closeTag("CodonPrefs");
130            pw.flush();
131        }
132    }
133}
134