merge from 2_4_Release branch
[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 import org.xml.sax.helpers.DefaultHandler;
27 import org.xml.sax.Attributes;
28 import java.util.logging.*;
29
30 /**
31  * a class that parses the XML response of a DAS - sequence command.
32  * 
33  * @author Andreas Prlic
34  * 
35  */
36 public class DAS_Sequence_Handler extends DefaultHandler
37 {
38
39   StringBuffer sequence;
40
41   int length;
42
43   int maxLength;
44
45   String version;
46
47   boolean dna_flag;
48
49   /**
50    * 
51    */
52   public DAS_Sequence_Handler()
53   {
54     super();
55
56     sequence = new StringBuffer();
57     length = 0;
58     dna_flag = false;
59     maxLength = -1;
60     version = "";
61   }
62
63   /**
64    * set a maximum length of sequence that should be loaded default: -1. if -1
65    * no length restriction is being supplied
66    * 
67    * @return the maximum length or -1 if no restriction
68    */
69   public int getMaxLength()
70   {
71     return maxLength;
72   }
73
74   /**
75    * set a maximum length of sequence that should be loaded default: -1. if -1
76    * no length restriction is being supplied
77    * 
78    * @param maxLength
79    *                the maximum length or -1 if unrestricted
80    */
81   public void setMaxLength(int maxLength)
82   {
83     this.maxLength = maxLength;
84   }
85
86   public void startElement(String uri, String name, String qName,
87           Attributes atts)
88   {
89
90     if (qName.equals("SEQUENCE"))
91     {
92       version = atts.getValue("version");
93       String lenstr = atts.getValue("stop");
94       length = Integer.parseInt(lenstr);
95       dna_flag = true;
96     }
97
98   }
99
100   public void characters(char ch[], int start, int length)
101   {
102
103     if (maxLength > 0)
104       if (sequence.length() > maxLength)
105         return;
106
107     if (dna_flag)
108       for (int i = start; i < start + length; i++)
109       {
110
111         // all sorts of characters can be found in "seqeunces" ... ignore
112         // them...
113         switch (ch[i])
114         {
115         case '\\':
116           // System.out.print("\\\\");
117           break;
118         case '"':
119           // System.out.print("\\\"");
120           break;
121         case '\n':
122           // System.out.print("\\n");
123           break;
124         case '\r':
125           // System.out.print("\\r");
126           break;
127         case '\t':
128           // System.out.print("\\t");
129           break;
130         case ' ':
131           break;
132         default:
133           sequence = sequence.append(ch[i]);
134
135           break;
136         }
137       }
138
139   }
140
141   public String get_sequence()
142   {
143
144     if (maxLength < 0)
145     {
146       if (length != sequence.length())
147       {
148         Logger logger = Logger.getLogger("org.biojava.spice");
149         logger.warning("Sequence does not match specified length!");
150       }
151     }
152
153     return sequence.toString();
154   }
155
156   public String getVersion()
157   {
158     return version;
159   }
160
161 }