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 
26 /**
27 *
28 *  Resolves variable references.
29 *
30 */
31 
32 public class AcdVariableResolve
33 {
34 
35   /** expression to resolve */
36   private String exp;
37 
38   /**
39   *
40   * Given an expression this is searched for occurences of
41   * a variable name for which the value is known. The value
42   * is subsituted into the expression.
43   * @param exp		expression to resolve
44   * @param varName	variable name
45   * @param varValue	variable value
46   *
47   */
AcdVariableResolve(String exp, String varName, String varValue)48   public AcdVariableResolve(String exp, String varName, String varValue)
49   {
50 
51     RECompiler rec = new RECompiler();
52 
53     try
54     {
55       exp = exp.replace('\n',' ');
56       REProgram varexp = rec.compile("^(.*)\\$\\((" + varName + ")\\)");
57 
58 // resolve variable references
59       RE regvarexp = new RE(varexp);
60       if(regvarexp.match(exp))
61       {
62         /*String var =*/ regvarexp.getParen(2);    // variable name
63         int iend   = regvarexp.getParenEnd(1);
64         String newvar = exp.substring(0,iend);
65         iend   = regvarexp.getParenEnd(2);
66         newvar = newvar.concat(varValue);
67         newvar = newvar.concat(exp.substring(iend+1));
68 //      System.out.println("VARIABLE  " + var  + " -->  " + varValue);
69 //      System.out.println("EXPRESSION" + exp + " -->  " +  newvar);
70         exp = new String(newvar);
71       }
72     }
73     catch (RESyntaxException rese)
74     {
75       System.out.println("RESyntaxException ");
76     }
77     this.exp = exp;
78   }
79 
80   /**
81   *
82   * Get the resolved expression
83   * @return	result value of the expression
84   *
85   */
getResult()86   public String getResult()
87   {
88     return exp;
89   }
90 
91 }
92 
93