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.Set; 025 026import org.biojava.utils.ParserException; 027import org.biojava.utils.SmallSet; 028 029/** 030 * Silently drop all tags except those specified, and pass the rest onto a 031 * delegate. 032 * 033 * @author Matthew Pocock 034 * @since 1.2 035 */ 036public class TagDropper 037extends SimpleTagValueWrapper { 038 private final Set tags; 039 private boolean retain; 040 private boolean propagate; 041 042 { 043 tags = new SmallSet(); 044 retain = true; 045 } 046 047 public TagDropper() { 048 super(); 049 } 050 051 /** 052 * Create a new TagDropper that will pass on all retained tags and values to 053 * tvl. Initially, no tags will be retained. 054 * 055 * @param tvl the TagValueListener to inform of all surviving events 056 */ 057 public TagDropper(TagValueListener tvl) { 058 super(tvl); 059 } 060 061 /** 062 * Add a tag to retain. 063 * 064 * @param tag a tag that will be forwarded to the delegate 065 */ 066 public void addTag(Object tag) { 067 tags.add(tag); 068 } 069 070 /** 071 * Remove a tag so that it will not be retained. 072 * 073 * @param tag a tag that will not be forwarded to the delegate 074 */ 075 public void removeTag(Object tag) { 076 tags.remove(tag); 077 } 078 079 /** 080 * Get the complete set of tags that are currently recognized. 081 * 082 * @return the Set of known tags 083 */ 084 public Set getTags() { 085 return tags; 086 } 087 088 /** 089 * Set wether known tags are to be retained or dropped. 090 * 091 * <p> 092 * If retain is true, then all known tags will be passed on and all other tags 093 * will be discarded. If retain is true, then all known tags will be dropped 094 * and all others passed on. 095 * </p> 096 * 097 * @param retain true if the tags are to be retained 098 */ 099 public void setRetain(boolean retain) { 100 this.retain = retain; 101 } 102 103 /** 104 * Find out if known tags are retained or dropped. 105 * 106 * @return true if values are retained 107 */ 108 public boolean doRetain() { 109 return retain; 110 } 111 112 public void startTag(Object tag) 113 throws ParserException { 114 propagate = retain == tags.contains(tag); 115 116 if(propagate) { 117 super.startTag(tag); 118 } 119 } 120 121 public void endTag() 122 throws ParserException { 123 if(propagate) { 124 super.endTag(); 125 } 126 } 127 128 public void value(TagValueContext ctxt, Object value) 129 throws ParserException { 130 if(propagate) { 131 super.value(ctxt, value); 132 } 133 } 134} 135