b506c513377e4902e84c49fddc13a5a197d8f465
[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  *
35  */
36 public class DAS_Feature_Handler
37     extends DefaultHandler
38 {
39
40   /**
41    *
42    */
43   List features;
44   boolean first_flag;
45   HashMap feature;
46   String featurefield;
47   String characterdata;
48   String dasCommand;
49
50   int comeBackLater;
51
52   int maxFeatures;
53
54   public DAS_Feature_Handler()
55   {
56     super();
57
58     features = new ArrayList();
59     first_flag = true;
60     featurefield = "";
61     characterdata = "";
62     dasCommand = "";
63     comeBackLater = -1;
64     maxFeatures = -1;
65   }
66
67   /** specifies a maximum number of features to be downloaded. if a
68     server returns more, they will be ignored.  default is to load
69     all features
70        @param max the maximium number of features to be downloaded
71    */
72
73   public void setMaxFeatures(int max)
74   {
75     maxFeatures = max;
76   }
77
78   public int getMaxFeatures()
79   {
80     return maxFeatures;
81   }
82
83   public void setDASCommand(String cmd)
84   {
85     dasCommand = cmd;
86   }
87
88   public String getDASCommand()
89   {
90     return dasCommand;
91   }
92
93   public List get_features()
94   {
95     return features;
96   }
97
98   public int getComBackLater()
99   {
100     return comeBackLater;
101   }
102
103   void start_feature(String uri, String name, String qName, Attributes atts)
104   {
105
106     if ( (maxFeatures > 0) && (features.size() > maxFeatures))
107     {
108       characterdata = "";
109       return;
110     }
111     feature = new HashMap();
112     String id = atts.getValue("id");
113     feature.put("id", id);
114     feature.put("dassource", dasCommand);
115     characterdata = "";
116   }
117
118   void add_featuredata(String uri, String name, String qName)
119   {
120     //System.out.println("featurefield "+featurefield+ " data "+characterdata);
121     // NOTE can have multiple lines ..
122
123     if ( (maxFeatures > 0) && (features.size() > maxFeatures))
124     {
125       return;
126     }
127
128     String data = (String) feature.get(featurefield);
129     if (data != null)
130     {
131       characterdata = data + " " + characterdata;
132     }
133
134     feature.put(featurefield, characterdata);
135     featurefield = "";
136     characterdata = "";
137   }
138
139   private void addLink(String uri, String name, String qName, Attributes atts)
140   {
141     String href = atts.getValue("href");
142     feature.put("LINK", href);
143     characterdata = "";
144     featurefield = "LINK-TEXT";
145
146   }
147
148   public void startElement(String uri, String name, String qName,
149                            Attributes atts)
150   {
151     //System.out.println("new element "+qName);
152
153     if (qName.equals("FEATURE"))
154     {
155       start_feature(uri, name, qName, atts);
156     }
157     else if (qName.equals("LINK"))
158     {
159       addLink(uri, name, qName, atts);
160     }
161     else if (qName.equals("METHOD") ||
162              qName.equals("TYPE") ||
163              qName.equals("START") ||
164              qName.equals("END") ||
165              qName.equals("NOTE") ||
166              qName.equals("SCORE")
167         )
168     {
169       characterdata = "";
170       featurefield = qName;
171     }
172
173   }
174
175   public void startDocument()
176   {
177   }
178
179   public void endDocument()
180   {
181   }
182
183   public void endElement(String uri, String name, String qName)
184   {
185
186     if (qName.equals("METHOD") ||
187         qName.equals("TYPE") ||
188         qName.equals("START") ||
189         qName.equals("END") ||
190         qName.equals("NOTE") ||
191         qName.equals("LINK") ||
192         qName.equals("SCORE")
193         )
194     {
195       add_featuredata(uri, name, qName);
196     }
197     else if (qName.equals("FEATURE"))
198     {
199
200       if (maxFeatures > 0)
201       {
202         if (features.size() < maxFeatures)
203         {
204           features.add(feature);
205         }
206       }
207       else
208       {
209         // no restriction
210         features.add(feature);
211       }
212     }
213   }
214
215   public void characters(char ch[], int start, int length)
216   {
217
218     for (int i = start; i < start + length; i++)
219     {
220
221       characterdata += ch[i];
222     }
223
224   }
225
226 }