updated jalview version of dasobert 1.53e client and added Das Sequence Source discov...
[jalview.git] / src / org / biojava / dasobert / das / DAS_Feature_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 java.util.*;
27
28 import org.xml.sax.*;
29 import org.xml.sax.helpers.*;
30
31 /**
32  * a class to parse the response of a DAS - Feature request
33  * @author Andreas Prlic
34  * Adapted for jalview use.
35  * @author Andrew Waterhouse 
36  * Updated to Das 1.53e feature spec.
37  * @author Jim Procter
38
39  */
40 public class DAS_Feature_Handler
41     extends DefaultHandler
42 {
43
44   /**
45    *
46    */
47   List features;
48   boolean first_flag;
49   HashMap feature;
50   String featurefield;
51   StringBuffer characterdata;
52   String dasCommand;
53
54   int comeBackLater;
55
56   int maxFeatures;
57
58         String segmentId ;
59         String version;
60         String type_id;
61         String type_category;
62         
63         
64         public DAS_Feature_Handler() {
65     super();
66
67     features = new ArrayList();
68     first_flag = true;
69     featurefield = "";
70                 characterdata = new StringBuffer();
71     dasCommand = "";
72     comeBackLater = -1;
73     maxFeatures = -1;
74                 segmentId = "";
75                 version   = "";
76                 type_id = "";
77                 type_category="";
78         }
79
80         
81         
82         /** get the id information specified int the SEGMENT field of the DAS response
83          * 
84          * @return the segmentId or an emtpy string if not available
85          */
86         public String getSegmentId() {
87                 return segmentId;
88         }
89
90         /** get the version informationspecified in the SEGMENT field of the DAS response
91          * 
92          * @return the version information of an empty string if not available 
93          */
94         public String getVersion() {
95                 return version;
96   }
97         
98         public boolean isMD5Checksum(){
99
100                 if ((version != null) && (version.length() == 32))
101                         return true;
102                 return false;
103         }
104
105
106   /** specifies a maximum number of features to be downloaded. if a
107     server returns more, they will be ignored.  default is to load
108     all features
109        @param max the maximium number of features to be downloaded
110    */
111
112   public void setMaxFeatures(int max)
113   {
114     maxFeatures = max;
115   }
116
117   public int getMaxFeatures()
118   {
119     return maxFeatures;
120   }
121
122   public void setDASCommand(String cmd)
123   {
124     dasCommand = cmd;
125   }
126
127   public String getDASCommand()
128   {
129     return dasCommand;
130   }
131
132   public List get_features()
133   {
134     return features;
135   }
136
137   public int getComBackLater()
138   {
139     return comeBackLater;
140   }
141
142   void start_feature(String uri, String name, String qName, Attributes atts)
143   {
144
145                 if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) {
146                         characterdata = new StringBuffer();
147       return;
148     }
149     feature = new HashMap();
150     String id = atts.getValue("id");
151     feature.put("id", id);
152     feature.put("dassource", dasCommand);
153                 characterdata = new StringBuffer();
154   }
155
156   void add_featuredata(String uri, String name, String qName)
157   {
158     //System.out.println("featurefield "+featurefield+ " data "+characterdata);
159     // NOTE can have multiple lines ..
160
161     if ( (maxFeatures > 0) && (features.size() > maxFeatures))
162     {
163       return;
164     }
165
166     String data = (String) feature.get(featurefield);
167                 String featureText = characterdata.toString();
168                 if (data != null){
169                         featureText = data + " " + featureText;
170     }
171
172                 if ( qName.equals("TYPE")){
173                         if ( featureText.length() < 1)
174                                 featureText = type_id;
175                 
176                         feature.put("TYPE_ID",type_id);
177                         feature.put("TYPE_CATEGORY", type_category);
178                         type_id = "";
179                         type_category="";
180                 }
181                 
182                 
183                 feature.put(featurefield,featureText);
184     featurefield = "";
185                 characterdata = new StringBuffer();
186   }
187
188   private void addLink(String uri, String name, String qName, Attributes atts)
189   {
190     String href = atts.getValue("href");
191     feature.put("LINK", href);
192                 characterdata= new StringBuffer();
193     featurefield = "LINK-TEXT";
194
195   }
196
197         private void addGroup(String uri, String name, String qName, Attributes atts) {
198                 String id = atts.getValue("id");
199                 feature.put("GROUP",id);
200                 characterdata= new StringBuffer();
201                 featurefield = "GROUP";
202         }
203         
204         public void startElement (String uri, String name, String qName, Attributes atts){
205     //System.out.println("new element "+qName);
206
207     if (qName.equals("FEATURE"))
208     {
209       start_feature(uri, name, qName, atts);
210     }
211     else if (qName.equals("LINK"))
212     {
213       addLink(uri, name, qName, atts);
214                  } else if ( qName.equals("GROUP"))
215                         { addGroup(uri,name,qName, atts); }
216     else if (qName.equals("METHOD") ||
217              qName.equals("TYPE") ||
218              qName.equals("START") ||
219              qName.equals("END") ||
220              qName.equals("NOTE") ||
221                                 qName.equals("SCORE") ||
222                                 qName.equals("ORIENTATION")                             
223                 ){
224       characterdata = new StringBuffer();
225       featurefield = qName;
226                 } else if (qName.equals("SEGMENT")){
227                         String id = atts.getValue("id");
228                         if (id != null)
229                                 segmentId = id;
230         String v = atts.getValue("version");
231                         if ( v != null)
232                                 version = v;
233                         
234                         
235                 }
236                 if ( qName.equals("TYPE")){
237                         type_id      = atts.getValue("id");
238                         type_category= atts.getValue("category");
239                 }
240
241   }
242
243   public void startDocument()
244   {
245   }
246
247   public void endDocument()
248   {
249   }
250
251   public void endElement(String uri, String name, String qName)
252   {
253
254     if (qName.equals("METHOD") ||
255         qName.equals("TYPE") ||
256         qName.equals("START") ||
257         qName.equals("END") ||
258         qName.equals("NOTE") ||
259         qName.equals("LINK") ||
260                                 qName.equals("SCORE") ||
261                                 qName.equals("ORIENTATION") ||
262                                 qName.equals("GROUP")
263                 ) {
264       add_featuredata(uri, name, qName);
265     }
266     else if (qName.equals("FEATURE"))
267     {
268
269       if (maxFeatures > 0)
270       {
271         if (features.size() < maxFeatures)
272         {
273           features.add(feature);
274         }
275       }
276       else
277       {
278         // no restriction
279         features.add(feature);
280       }
281     }
282   }
283
284         public void characters (char ch[], int start, int length){
285                 if ( maxFeatures > 0)
286                         if ( features.size() > maxFeatures )
287                                 return;
288
289                 for (int i = start; i < start + length; i++) {
290
291                         characterdata.append(ch[i]);
292                 }
293
294         }
295
296 }