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.program.tagvalue;
023
024import java.util.Map;
025import java.util.Set;
026
027import org.biojava.utils.ParserException;
028import org.biojava.utils.SmallMap;
029
030/**
031 * <p>
032 * Pushes a new parser and listener, or delegate to a listener depending on the
033 * tag.
034 * </p>
035 *
036 * <p>
037 * setParserListener() is used to associate a tag with a TagValueParser and
038 * TagValueListener. When this tag is encountered, the pair will be pushed onto
039 * the parser processing stack and will gain control of the stream until that
040 * tag has ended. setListener() is used to associate a listener with a tag that
041 * will be used to handle those values without pushing a sub-context.
042 * The delegator is constructed with a default TagValueListener that will be
043 * informed of all events for which there are no explicit delegate pairs
044 * registered.
045 * </p>
046 *
047 * @author Matthew Pocock
048 * @since 1.2
049 */
050public class TagDelegator
051  extends
052    SimpleTagValueWrapper
053{
054  private Map parsers;
055  private Map listeners;
056  private TagValueParser delegateParser;
057  
058  private TagValueParser parser;
059  private TagValueListener listener;
060
061  {
062    parsers = new SmallMap();
063    listeners = new SmallMap();
064  }
065
066  public TagDelegator() {
067    super();
068  }
069  
070  public TagDelegator(TagValueListener delegate) {
071    super(delegate);
072    parsers = new SmallMap();
073    listeners = new SmallMap();
074  }
075
076  public void setDelegateParser(TagValueParser delegateParser) {
077    this.delegateParser = delegateParser;
078  }
079
080  public TagValueParser getDelegateParser() {
081    return delegateParser;
082  }
083  
084  public void startTag(Object tag)
085  throws ParserException {
086    parser = (TagValueParser) parsers.get(tag);
087    listener = (TagValueListener) listeners.get(tag);
088
089    if(parser == null && listener != null) {
090      listener.startTag(tag);
091    } else {
092      super.startTag(tag);
093    }
094  }
095
096  public void endTag()
097  throws ParserException {
098    if(parser == null && listener != null) {
099      listener.endTag();
100    } else {
101      super.endTag();
102    }
103  }
104
105  public void value(TagValueContext tvc, Object value)
106  throws ParserException {
107    if(parser != null) {
108      tvc.pushParser(parser, listener);
109    } else if(listener != null) {
110      listener.value(tvc, value);
111    } else if(delegateParser != null) {
112      tvc.pushParser(delegateParser, getDelegate());
113    } else {
114      super.value(tvc, value);
115    }
116  }
117  
118  public void setParserListener(
119    Object tag,
120    TagValueParser parser,
121    TagValueListener listener
122  ) {
123    parsers.put(tag, parser);
124    listeners.put(tag, listener);
125  }
126  
127  public void setListener(
128    Object tag,
129    TagValueListener listener
130  ) {
131    listeners.put(tag, listener);
132  }
133
134  public TagValueParser getParser(Object tag) {
135    return (TagValueParser) parsers.get(tag);
136  }
137
138  public TagValueListener getListener(Object tag) {
139    return (TagValueListener) listeners.get(tag);
140  }
141
142  public Set getTags() {
143    return listeners.keySet();
144  }
145}