b8f7eab0180685b5c25e6f29da3c1a8dd29349c5
[jalview.git] / src / jalview / ws / seqfetcher / DbSourceProxyImpl.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.seqfetcher;
19
20 import jalview.datamodel.Alignment;
21 import jalview.io.FormatAdapter;
22 import jalview.io.IdentifyFile;
23
24 import java.util.Hashtable;
25
26 /**
27  * common methods for implementations of the DbSourceProxy interface.
28  * 
29  * @author JimP
30  * 
31  */
32 public abstract class DbSourceProxyImpl implements DbSourceProxy
33 {
34   public DbSourceProxyImpl()
35   {
36     // default constructor - do nothing probably.
37   }
38
39   private Hashtable props = null;
40
41   /*
42    * (non-Javadoc)
43    * 
44    * @see jalview.ws.DbSourceProxy#getDbSourceProperties()
45    */
46   public Hashtable getDbSourceProperties()
47   {
48     if (props == null)
49     {
50       props = new Hashtable();
51     }
52     return props;
53   }
54
55   protected void addDbSourceProperty(Object propname)
56   {
57     addDbSourceProperty(propname, propname);
58   }
59
60   protected void addDbSourceProperty(Object propname, Object propvalue)
61   {
62     if (props == null)
63     {
64       props = new Hashtable();
65     }
66     props.put(propname, propvalue);
67   }
68
69   boolean queryInProgress = false;
70
71   protected StringBuffer results = null;
72
73   /*
74    * (non-Javadoc)
75    * 
76    * @see jalview.ws.DbSourceProxy#getRawRecords()
77    */
78   public StringBuffer getRawRecords()
79   {
80     return results;
81   }
82
83   /*
84    * (non-Javadoc)
85    * 
86    * @see jalview.ws.DbSourceProxy#queryInProgress()
87    */
88   public boolean queryInProgress()
89   {
90     return queryInProgress;
91   }
92
93   /**
94    * call to set the queryInProgress flag
95    * 
96    */
97   protected void startQuery()
98   {
99     queryInProgress = true;
100   }
101
102   /**
103    * call to clear the queryInProgress flag
104    * 
105    */
106   protected void stopQuery()
107   {
108     queryInProgress = false;
109   }
110
111   /**
112    * create an alignment from raw text file...
113    * 
114    * @param result
115    * @return null or a valid alignment
116    * @throws Exception
117    */
118   protected Alignment parseResult(String result) throws Exception
119   {
120     Alignment sequences = null;
121     String format = new IdentifyFile().Identify(result, "Paste");
122     if (FormatAdapter.isValidFormat(format))
123     {
124       sequences = new FormatAdapter().readFile(result.toString(), "Paste",
125               format);
126     }
127     return sequences;
128   }
129
130   @Override
131   public boolean isA(Object dbsourceproperty)
132   {
133     assert (dbsourceproperty != null);
134     return (props == null) ? false : props.containsKey(dbsourceproperty);
135   }
136
137 }