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.search.SearchContentHandler; 025import org.biojava.utils.stax.DelegationManager; 026import org.biojava.utils.stax.StAXContentHandler; 027import org.biojava.utils.stax.StAXContentHandlerBase; 028import org.xml.sax.Attributes; 029import org.xml.sax.SAXException; 030 031/** 032 * <code>SeqSimilarityStAXAdapter</code> is a handler for XML 033 * conforming to the BioJava BlastLike DTD. Together with its modular 034 * delegate handlers it converts XML into calls on a 035 * <code>SearchContentHandler</code> interface. Implementations of the 036 * at interface create various types of Java object from the XML data. 037 * 038 * @author Keith James 039 * @since 1.3 040 */ 041public class SeqSimilarityStAXAdapter extends StAXContentHandlerBase 042{ 043 public static final String NAMESPACE = "http://www.biojava.org"; 044 045 // Incremented on startElement, decremented on endElement. Used to 046 // identify which method calls to handle here and which to 047 // delegate. 048 private int depth; 049 050 // The target handler 051 private SearchContentHandler scHandler; 052 // The name of the program which generated the results 053 private String program = "unknown"; 054 055 public void startElement(String nsURI, 056 String localName, 057 String qName, 058 Attributes attrs, 059 DelegationManager dm) 060 throws SAXException 061 { 062 depth++; 063 064 if (! nsURI.equals(NAMESPACE)) 065 throw new SAXException("Failed to handle Element " + localName 066 + " in namespace " + nsURI); 067 068 if (depth == 1) 069 { 070 if (localName.equals("BlastLikeDataSetCollection")) 071 { 072 scHandler.setMoreSearches(true); 073 } 074 } 075 else 076 { 077 if (localName.equals("BlastLikeDataSet")) 078 { 079 scHandler.startSearch(); 080 program = attrs.getValue("program"); 081 scHandler.addSearchProperty("program", program); 082 scHandler.addSearchProperty("version", attrs.getValue("version")); 083 return; 084 } 085 else if (localName.equals("Header")) 086 { 087 // Capture Header element and subtree 088 dm.delegate(HeaderStAXHandler.HEADER_HANDLER_FACTORY.getHandler(this)); 089 return; 090 } 091 else if (localName.equals("Hit")) 092 { 093 // Drop through the Detail element to the Hit elements 094 // and their subtrees 095 dm.delegate(HitStAXHandler.HIT_HANDLER_FACTORY.getHandler(this)); 096 } 097 } 098 } 099 100 public void endElement(String nsURI, 101 String localName, 102 String qName, 103 StAXContentHandler handler) 104 throws SAXException 105 { 106 depth--; 107 108 if (depth == 0) 109 { 110 if (localName.equals("BlastLikeDataSetCollection")) 111 { 112 scHandler.setMoreSearches(false); 113 } 114 } 115 else 116 { 117 if (localName.equals("BlastLikeDataSet")) 118 { 119 scHandler.endSearch(); 120 } 121 } 122 } 123 124 /** 125 * <code>getSearchContentHandler</code> gets the handler which 126 * will receive the method calls generated by the adapter. 127 * 128 * @return a <code>SearchContentHandler</code>. 129 */ 130 public SearchContentHandler getSearchContentHandler() 131 { 132 return scHandler; 133 } 134 135 /** 136 * <code>setSearchContentHandler</code> sets the handler which 137 * will recieve the method calls generated by the adapter. 138 * 139 * @param scHandler a <code>SearchContentHandler</code>. 140 */ 141 public void setSearchContentHandler(SearchContentHandler scHandler) 142 { 143 this.scHandler = scHandler; 144 } 145 146 /** 147 * <code>getProgram</code> returns the program type which 148 * generated the results. 149 * 150 * @return a <code>String</code> indicating the progam 151 * name. 152 */ 153 String getProgram() 154 { 155 return program; 156 } 157 158 /** 159 * <code>setProgram</code> informs the adapter which program type 160 * it is working on. 161 * 162 * @param program a <code>String</code> indicating the progam 163 * name. 164 */ 165 void setProgram(String program) 166 { 167 this.program = program; 168 } 169}