JAL-1432 updated copyright notices
[jalview.git] / src / jalview / ws / seqfetcher / DbSourceProxyImpl.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
18  */
19 package jalview.ws.seqfetcher;
20
21 import jalview.datamodel.Alignment;
22 import jalview.io.FormatAdapter;
23 import jalview.io.IdentifyFile;
24
25 import java.util.Hashtable;
26
27 /**
28  * common methods for implementations of the DbSourceProxy interface.
29  * 
30  * @author JimP
31  * 
32  */
33 public abstract class DbSourceProxyImpl implements DbSourceProxy
34 {
35   public DbSourceProxyImpl()
36   {
37     // default constructor - do nothing probably.
38   }
39
40   private Hashtable props = null;
41
42   /*
43    * (non-Javadoc)
44    * 
45    * @see jalview.ws.DbSourceProxy#getDbSourceProperties()
46    */
47   public Hashtable getDbSourceProperties()
48   {
49     if (props == null)
50     {
51       props = new Hashtable();
52     }
53     return props;
54   }
55
56   protected void addDbSourceProperty(Object propname)
57   {
58     addDbSourceProperty(propname, propname);
59   }
60
61   protected void addDbSourceProperty(Object propname, Object propvalue)
62   {
63     if (props == null)
64     {
65       props = new Hashtable();
66     }
67     props.put(propname, propvalue);
68   }
69
70   boolean queryInProgress = false;
71
72   protected StringBuffer results = null;
73
74   /*
75    * (non-Javadoc)
76    * 
77    * @see jalview.ws.DbSourceProxy#getRawRecords()
78    */
79   public StringBuffer getRawRecords()
80   {
81     return results;
82   }
83
84   /*
85    * (non-Javadoc)
86    * 
87    * @see jalview.ws.DbSourceProxy#queryInProgress()
88    */
89   public boolean queryInProgress()
90   {
91     return queryInProgress;
92   }
93
94   /**
95    * call to set the queryInProgress flag
96    * 
97    */
98   protected void startQuery()
99   {
100     queryInProgress = true;
101   }
102
103   /**
104    * call to clear the queryInProgress flag
105    * 
106    */
107   protected void stopQuery()
108   {
109     queryInProgress = false;
110   }
111
112   /**
113    * create an alignment from raw text file...
114    * 
115    * @param result
116    * @return null or a valid alignment
117    * @throws Exception
118    */
119   protected Alignment parseResult(String result) throws Exception
120   {
121     Alignment sequences = null;
122     String format = new IdentifyFile().Identify(result, "Paste");
123     if (FormatAdapter.isValidFormat(format))
124     {
125       sequences = new FormatAdapter().readFile(result.toString(), "Paste",
126               format);
127     }
128     return sequences;
129   }
130
131   @Override
132   public boolean isA(Object dbsourceproperty)
133   {
134     assert (dbsourceproperty != null);
135     return (props == null) ? false : props.containsKey(dbsourceproperty);
136   }
137
138 }