fa42fe4724cbb191a8062fe62e7d94494cb9b953
[jalview.git] / src / jalview / 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 org.xml.sax.helpers.DefaultHandler;
27 import org.xml.sax.Attributes;
28
29 import java.util.ArrayList ;
30 import java.util.HashMap ;
31 import java.util.List;
32
33 /**
34  * a class to parse the response of a DAS - Feature request
35  * @author Andreas Prlic
36  *
37  */
38 public class DAS_Feature_Handler  extends DefaultHandler{
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         super();
56
57         features= new ArrayList() ;
58         first_flag = true ;
59         featurefield = "" ;
60         characterdata = "";
61         dasCommand = "" ;
62         comeBackLater = -1;
63         maxFeatures = -1;
64     }
65
66     /** specifies a maximum number of features to be downloaded. if a
67         server returns more, they will be ignored.  default is to load
68         all features
69     @param max the maximium number of features to be downloaded
70     */
71
72     public void setMaxFeatures(int max) {
73         maxFeatures = max;
74     }
75
76     public int getMaxFeatures() {
77         return maxFeatures;
78     }
79
80     public void setDASCommand(String cmd) { dasCommand = cmd ;}
81     public String getDASCommand() { return dasCommand; }
82
83     public List get_features() {
84         return features ;
85     }
86
87     public int getComBackLater(){
88         return comeBackLater;
89     }
90
91     void start_feature(String uri, String name, String qName, Attributes atts) {
92
93         if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) {
94             characterdata = "";
95             return;
96         }
97         feature = new HashMap() ;
98         String id       = atts.getValue("id");
99         feature.put("id",id);
100         feature.put("dassource",dasCommand);
101         characterdata = "";
102     }
103
104     void add_featuredata(String uri, String name, String qName) {
105         //System.out.println("featurefield "+featurefield+ " data "+characterdata);
106         // NOTE can have multiple lines ..
107
108         if (( maxFeatures > 0 ) && ( features.size() > maxFeatures ) ) {
109             return;
110         }
111
112
113         String data = (String)feature.get(featurefield);
114         if (data != null){
115             characterdata = data + " " + characterdata;
116         }
117
118         feature.put(featurefield,characterdata);
119         featurefield = "";
120         characterdata = "";
121     }
122
123     private void addLink(String uri, String name, String qName, Attributes atts) {
124         String href = atts.getValue("href");
125         feature.put("LINK",href);
126         characterdata="";
127         featurefield = "LINK-TEXT";
128
129     }
130
131     public void startElement (String uri, String name, String qName, Attributes atts){
132         //System.out.println("new element "+qName);
133
134         if (qName.equals("FEATURE"))
135             start_feature(uri,  name,  qName,  atts);
136         else if ( qName.equals("LINK"))
137             addLink(uri,name,qName, atts);
138         else if ( qName.equals("METHOD") ||
139                 qName.equals("TYPE") ||
140                 qName.equals("START") ||
141                 qName.equals("END") ||
142                 qName.equals("NOTE") ||
143                 qName.equals("SCORE")
144         ){
145             characterdata ="";
146             featurefield = qName ;
147         }
148
149     }
150
151     public void startDocument() {
152     }
153
154     public void endDocument ()  {
155     }
156     public void endElement(String uri, String name, String qName) {
157
158         if ( qName.equals("METHOD") ||
159                 qName.equals("TYPE") ||
160                 qName.equals("START") ||
161                 qName.equals("END") ||
162                 qName.equals("NOTE") ||
163                 qName.equals("LINK") ||
164                 qName.equals("SCORE")
165         ) {
166             add_featuredata(uri,name,qName);
167         }
168         else if ( qName.equals("FEATURE")) {
169
170             if ( maxFeatures > 0 ) {
171                 if ( features.size() < maxFeatures ) {
172                      features.add(feature);
173                 }
174             } else {
175                 // no restriction
176                 features.add(feature);
177             }
178         }
179     }
180
181     public void characters (char ch[], int start, int length){
182
183         for (int i = start; i < start + length; i++) {
184
185             characterdata += ch[i];
186         }
187
188     }
189
190 }