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.ssbind; 023 024import org.biojava.bio.program.xff.ElementRecognizer; 025import org.biojava.bio.search.SearchContentHandler; 026import org.biojava.utils.stax.DelegationManager; 027import org.biojava.utils.stax.StAXContentHandler; 028import org.biojava.utils.stax.StAXContentHandlerBase; 029import org.biojava.utils.stax.StringElementHandlerBase; 030import org.xml.sax.Attributes; 031import org.xml.sax.SAXException; 032 033/** 034 * <code>AlignmentStAXHandler</code> handles the Hit element of 035 * BioJava BlastLike XML. 036 * 037 * @author Keith James 038 * @since 1.3 039 */ 040public class HitStAXHandler extends SeqSimilarityStAXHandler 041{ 042 public static final StAXHandlerFactory HIT_HANDLER_FACTORY = 043 new StAXHandlerFactory() 044 { 045 public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext) 046 { 047 return new HitStAXHandler(ssContext); 048 } 049 }; 050 051 /** 052 * Creates a new instance which sends callbacks to the specified 053 * <code>SeqSimilarityStAXAdapter</code>. 054 * 055 * @param ssContext a <code>SeqSimilarityStAXAdapter</code>. 056 */ 057 HitStAXHandler(SeqSimilarityStAXAdapter ssContext) 058 { 059 super(ssContext); 060 addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE, 061 "HitId"), 062 new StAXHandlerFactory() 063 { 064 public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext) 065 { 066 return new HitIDStAXHandler(); 067 } 068 }); 069 070 addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE, 071 "QueryId"), 072 new StAXHandlerFactory() 073 { 074 public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext) 075 { 076 return new QueryIDStAXHandler(); 077 } 078 }); 079 080 addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE, 081 "HitDescription"), 082 new StAXHandlerFactory() 083 { 084 public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext) 085 { 086 return new HitDescriptionStAXHandler(); 087 } 088 }); 089 090 addHandler(new ElementRecognizer.ByNSName(SeqSimilarityStAXAdapter.NAMESPACE, 091 "HSPCollection"), 092 new StAXHandlerFactory() 093 { 094 public StAXContentHandler getHandler(SeqSimilarityStAXAdapter ssContext) 095 { 096 return new HSPCollectionStAXHandler(); 097 } 098 }); 099 } 100 101 protected void handleStartElement(String nsURI, 102 String localName, 103 String qName, 104 Attributes attrs) 105 throws SAXException 106 { 107 SearchContentHandler sch = ssContext.getSearchContentHandler(); 108 109 sch.startHit(); 110 if (attrs.getValue("sequenceLength") != null) 111 { 112 sch.addHitProperty("subjectSequenceLength", 113 attrs.getValue("sequenceLength")); 114 } 115 } 116 117 protected void handleEndElement(String nsURI, 118 String localName, 119 String qName) 120 throws SAXException 121 { 122 ssContext.getSearchContentHandler().endHit(); 123 } 124 125 /** 126 * <code>HitIDStAXHandler</code> handles the hit ID. 127 */ 128 private class HitIDStAXHandler extends StAXContentHandlerBase 129 { 130 public void startElement(String uri, 131 String localName, 132 String qName, 133 Attributes attr, 134 DelegationManager dm) 135 throws SAXException 136 { 137 ssContext.getSearchContentHandler().addHitProperty("subjectId", attr.getValue("id")); 138 } 139 } 140 141 /** 142 * <code>QueryIDStAXHandler</code> handles the query ID. 143 */ 144 private class QueryIDStAXHandler extends StAXContentHandlerBase 145 { 146 public void startElement(String uri, 147 String localName, 148 String qName, 149 Attributes attr, 150 DelegationManager dm) 151 throws SAXException 152 { 153 ssContext.getSearchContentHandler().setQueryID(attr.getValue("id")); 154 } 155 } 156 157 /** 158 * <code>HitDescriptionStAXHandler</code> handles the hit 159 * description. 160 */ 161 private class HitDescriptionStAXHandler extends StringElementHandlerBase 162 { 163 protected void setStringValue(String s) 164 { 165 ssContext.getSearchContentHandler().addHitProperty("subjectDescription", s); 166 } 167 } 168 169 /** 170 * <code>HSPCollectionStAXHandler</code> handles the HSPCollection 171 * element. 172 */ 173 private class HSPCollectionStAXHandler extends StAXContentHandlerBase 174 { 175 public void startElement(String uri, 176 String localName, 177 String qName, 178 Attributes attr, 179 DelegationManager dm) 180 throws SAXException 181 { 182 if (localName.equals("HSP")) 183 dm.delegate(HSPStAXHandler.HSP_HANDLER_FACTORY.getHandler(ssContext)); 184 } 185 } 186}