1 /********************************************************************
2 *
3 *  This library is free software; you can redistribute it and/or
4 *  modify it under the terms of the GNU Library General Public
5 *  License as published by the Free Software Foundation; either
6 *  version 2 of the License, or (at your option) any later version.
7 *
8 *  This library is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 *  Library General Public License for more details.
12 *
13 *  You should have received a copy of the GNU Library General Public
14 *  License along with this library; if not, write to the
15 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 *  Boston, MA  02111-1307, USA.
17 *
18 *  @author: Copyright (C) Tim Carver
19 *
20 ********************************************************************/
21 
22 package org.emboss.jemboss.parser;
23 
24 import org.apache.regexp.*;
25 import javax.swing.*;
26 
27 import org.emboss.jemboss.gui.form.*;
28 
29 /**
30 *
31 *  Resolves any variable reference in a string.
32 *
33 */
34 
35 public class AcdVarResolve
36 {
37 
38   /** expression */
39   private String exp;
40 
41   /**
42   *
43   * @param exp 		expression to resolve
44   * @param varName	variable name
45   * @param parseAcd	ACD parser for the application
46   * @param nof		number of fields
47   * @param textf	text fields
48   * @param textInt	int fields
49   * @param textFloat	float fields
50   * @param fieldOption	combo popup fields
51   * @param checkBox	boolean fields
52   *
53   */
AcdVarResolve(String exp, String val, String varName, ParseAcd parseAcd, int nof, TextFieldSink textf[], TextFieldInt textInt[], TextFieldFloat textFloat[], JembossComboPopup fieldOption[], JCheckBox checkBox[])54   public AcdVarResolve(String exp, String val, String varName,
55                        ParseAcd parseAcd, int nof,
56                        TextFieldSink textf[], TextFieldInt textInt[],
57                        TextFieldFloat textFloat[], JembossComboPopup fieldOption[],
58                        JCheckBox  checkBox[])
59   {
60 
61     RECompiler rec = new RECompiler();
62 
63     try
64     {
65       exp = exp.replace('\n',' ');
66       REProgram varexp = rec.compile("^(.*)\\$\\(([a-zA-Z0-9_.]+)\\)");
67 
68 // resolve variable references first
69       RE regvarexp = new RE(varexp);
70       while (regvarexp.match(exp))
71       {
72         String var = regvarexp.getParen(2);    // variable name
73         String res = "";
74         if(var.endsWith("acdprotein"))
75           res = (new Boolean(SectionPanel.ajaxProtein)).toString();
76         else if (var.endsWith(".length") || var.endsWith(".end"))
77           res = (new Integer(SectionPanel.ajaxLength)).toString();
78         else if (var.endsWith(".begin"))
79           res = "0";
80         else if (var.endsWith(".totweight"))
81           res = (new Float(SectionPanel.ajaxWeight)).toString();
82         else if(var.equals(varName))
83           res = val;
84         else
85         {
86           res = new String("UnresolvedToken"+var);
87           for(int i =0;i<nof;i++)
88           {
89             if(parseAcd.getParamValueStr(i,0).equals(var))
90             {
91               //get the value
92               String att = parseAcd.getParameterAttribute(i,0).toLowerCase();
93               int h = parseAcd.getGuiHandleNumber(i);
94               if(att.startsWith("datafile")|| att.startsWith("featout")  ||
95                 att.startsWith("string")   || att.startsWith("seqout") ||
96                 att.startsWith("outfile")  || att.startsWith("matrix") ||
97                 att.startsWith("infile")   || att.startsWith("regexp") ||
98                 att.startsWith("codon") || att.startsWith("features"))
99               {
100                 if(!(textf[h].getText()).equals(""))
101                   res = textf[h].getText();
102               }
103               else if ( att.startsWith("int") )
104               {
105                 if(textInt[h].getText() != null)
106                   res = Integer.toString(textInt[h].getValue());
107               }
108               else if ( att.startsWith("float") )
109               {
110                 if(textFloat[h].getText() != null)
111                   res = Double.toString(textFloat[h].getValue());
112               }
113               else if ( att.startsWith("select") )   //defined by a number
114                 res = (new Integer(fieldOption[h].getSelectedIndex()+1)).toString();
115               else if ( att.startsWith("list") )     //defined by a label
116               {
117 
118                 int index = fieldOption[h].getSelectedIndex();
119                 res = parseAcd.getListLabel(i,index);
120               }
121               else if ( att.startsWith("bool") )
122               {
123                 if(checkBox[h].isSelected())
124                   res = new String("true");
125                 else
126                   res = new String("false");
127               }
128               break;
129             }
130           }
131         }
132 
133         int iend   = regvarexp.getParenEnd(1);
134         String newvar = exp.substring(0,iend);
135         iend   = regvarexp.getParenEnd(2);
136         newvar = newvar.concat(res);
137         newvar = newvar.concat(exp.substring(iend+1));
138         exp = new String(newvar);
139 
140       }
141     }
142     catch (RESyntaxException rese)
143     {
144       System.out.println("RESyntaxException ");
145     }
146 
147     this.exp = exp;
148   }
149 
150   /**
151   *
152   * Get the resolved expression
153   * @return 	result
154   *
155   */
getResult()156   public String getResult()
157   {
158     return exp;
159   }
160 
161 }
162 
163