updated jalview version of dasobert 1.53e client and added Das Sequence Source discov...
[jalview.git] / src / org / biojava / dasobert / das / DAS_Sequence_Handler.java
1 /*
2  *                    BioJava development code
3  *
4  * This code may be freely distributed and modified under the
5  * terms of the GNU Lesser General Public Licence.  This should
6  * be distributed with the code.  If you do not have a copy,
7  * see:
8  *
9  *      http://www.gnu.org/copyleft/lesser.html
10  *
11  * Copyright for this code is held jointly by the individual
12  * authors.  These should be listed in @author doc comments.
13  *
14  * For more information on the BioJava project and its aims,
15  * or to join the biojava-l mailing list, visit the home page
16  * at:
17  *
18  *      http://www.biojava.org/
19  *
20  * Created on 19.03.2004
21  * @author Andreas Prlic
22  *
23  */
24 package org.biojava.dasobert.das;
25
26
27 import org.xml.sax.helpers.DefaultHandler;
28 import org.xml.sax.Attributes            ;
29 import java.util.logging.*                ;
30
31 /**
32  * a class that parses the XML response of a DAS - sequence command.
33  * @author Andreas Prlic
34  *
35  */
36 public class DAS_Sequence_Handler extends DefaultHandler {
37
38         StringBuffer sequence ;
39         int length ;
40
41         int maxLength;
42         String version;
43
44         boolean dna_flag; 
45         /**
46          * 
47          */
48         public DAS_Sequence_Handler() {
49                 super();
50
51                 sequence = new StringBuffer() ;
52                 length = 0;
53                 dna_flag = false ;
54                 maxLength = -1;
55                 version = "";
56         }
57
58
59
60         /** set a maximum length of sequence that should be loaded
61          * default: -1. if -1 no length restriction is being supplied
62          * @return the maximum length or -1 if no restriction
63          */
64         public int getMaxLength() {
65                 return maxLength;
66         }
67
68
69
70         /** set a maximum length of sequence that should be loaded
71          * default: -1. if -1 no length restriction is being supplied
72          * @param maxLength the maximum length or -1 if unrestricted
73          */
74         public void setMaxLength(int maxLength) {
75                 this.maxLength = maxLength;
76         }
77
78
79
80
81         public void startElement (String uri, String name, String qName, Attributes atts){
82
83                 if ( qName.equals("SEQUENCE")){
84                         version = atts.getValue("version");
85                         String lenstr   = atts.getValue("stop");
86                         length = Integer.parseInt(lenstr);
87                         dna_flag = true ;
88                 }
89
90         }
91
92         public void characters (char ch[], int start, int length){              
93
94                 if (maxLength > 0)
95                         if ( sequence.length() > maxLength)
96                                 return;
97
98                 if (dna_flag) 
99                         for (int i = start; i < start + length; i++) {
100
101                                 // all sorts of characters can be found in "seqeunces" ... ignore them...
102                                 switch (ch[i]) {
103                                 case '\\':
104                                         //System.out.print("\\\\");
105                                         break;
106                                 case '"':
107                                         //System.out.print("\\\"");
108                                         break;
109                                 case '\n':
110                                         //System.out.print("\\n");
111                                         break;
112                                 case '\r':
113                                         //System.out.print("\\r");
114                                         break;
115                                 case '\t':
116                                         //System.out.print("\\t");
117                                         break;
118                                 case ' ':
119                                         break;
120                                 default:
121                                         sequence = sequence.append(ch[i]);
122
123                                 break;
124                                 }
125                         }
126
127         }
128
129         public String get_sequence() {
130
131                 if ( maxLength < 0) {           
132                         if ( length != sequence.length()) {     
133                                 Logger logger  = Logger.getLogger("org.biojava.spice");
134                                 logger.warning("Sequence does not match specified length!");
135                         }
136                 }
137
138                 return sequence.toString();
139         }
140
141         public String getVersion() {
142                 return version;
143         }
144
145 }