001/* 002 003 * BioJava development code 004 005 * 006 007 * This code may be freely distributed and modified under the 008 009 * terms of the GNU Lesser General Public Licence. This should 010 011 * be distributed with the code. If you do not have a copy, 012 013 * see: 014 015 * 016 017 * http://www.gnu.org/copyleft/lesser.html 018 019 * 020 021 * Copyright for this code is held jointly by the individual 022 023 * authors. These should be listed in @author doc comments. 024 025 * 026 027 * For more information on the BioJava project and its aims, 028 029 * or to join the biojava-l mailing list, visit the home page 030 031 * at: 032 033 * 034 035 * http://www.biojava.org/ 036 037 * 038 039 */ 040 041package org.biojava.bio.seq.io.agave; 042 043import java.util.ListIterator; 044 045import org.xml.sax.SAXException; 046 047 048 049 050 051/** 052 053 * Deals with sequence code 054 055 * 056 057 * @author Hanning Ni Doubletwist Inc 058 * @author Greg Cox 059 060 */ 061 062public class AGAVESeqPropHandler 063 064 extends StAXPropertyHandler 065 066{ 067 068 // set up factory method 069 070 public static final StAXHandlerFactory AGAVE_SEQ_PROP_HANDLER_FACTORY 071 072 = new StAXHandlerFactory() { 073 074 public StAXContentHandler getHandler(StAXFeatureHandler staxenv) { 075 076 return new AGAVESeqPropHandler(staxenv); 077 078 } 079 080 }; 081 082 private StringBuffer dnaTokens ; 083 084 085 086 AGAVESeqPropHandler(StAXFeatureHandler staxenv) { 087 088 // execute superclass method to setup environment 089 090 super(staxenv); 091 092 setHandlerCharacteristics("sequence", true); 093 094 } 095 096 097 098 public void characters(char[] ch, int start, int length) 099 100 throws SAXException 101 102 { 103 104 dnaTokens = new StringBuffer() ; 105 106 for( int i = start ; i < start + length; i++) 107 108 { 109 110 char c = ch[i] ; 111 112 if( c != ' ' && c != '\n' && c!= '\t') 113 114 dnaTokens.append( c ); 115 116 } 117 118 } 119 120 121 122 public void endElementHandler( 123 124 String nsURI, 125 126 String localName, 127 128 String qName, 129 130 StAXContentHandler handler) 131 132 throws SAXException 133 134 { 135 136 int currLevel = staxenv.getLevel(); 137 138 if (currLevel >=1) 139 140 { 141 142 ListIterator li = staxenv.getHandlerStackIterator(currLevel); 143 144 while(li.hasPrevious()) 145 146 { 147 148 Object ob = li.previous(); 149 150 if (ob instanceof AGAVEBioSeqCallbackItf) 151 152 { 153 154 ((AGAVEBioSeqCallbackItf) ob).reportDna( dnaTokens.substring(0) ); 155 156 return; 157 158 } 159 160 } 161 162 li = staxenv.getHandlerStackIterator(currLevel); 163 164 while (li.hasPrevious()) 165 166 { 167 168 Object ob = li.previous(); 169 170 if (ob instanceof AGAVEContigCallbackItf) 171 172 { 173 174 ((AGAVEContigCallbackItf) ob).reportDna( dnaTokens.substring(0) ); 175 176 return; 177 178 } 179 180 } 181 182 } 183 184 } 185 186} 187