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