KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > rule > VariableParser


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

20  
21 package net.innig.macker.rule;
22
23 import java.util.*;
24
25 import org.apache.regexp.RE;
26 import org.apache.regexp.RESyntaxException;
27
28 public final class VariableParser
29     {
30     public static String JavaDoc parse(EvaluationContext context, String JavaDoc inS)
31         throws UndeclaredVariableException
32         {
33         StringBuffer JavaDoc outS = new StringBuffer JavaDoc();
34         for(int pos = 0; pos >= 0; )
35             {
36             boolean hasAnotherVar = var.match(inS, pos);
37             int expEnd = hasAnotherVar ? var.getParenStart(0) : inS.length();
38             
39             if(pos < expEnd)
40                 outS.append(inS.substring(pos, expEnd));
41             if(hasAnotherVar)
42                 outS.append(context.getVariableValue(var.getParen(1)));
43             
44             pos = hasAnotherVar ? var.getParenEnd(0) : -1;
45             }
46         return outS.toString();
47         }
48     
49     static private RE var;
50     static
51         {
52         try
53             { var = new RE("\\$\\{([A-Za-z0-9_\\.\\-]+)\\}"); }
54         catch(RESyntaxException rese)
55             {
56             rese.printStackTrace(System.out);
57             throw new RuntimeException JavaDoc("Can't initialize VariableParser: " + rese);
58             }
59         }
60     
61     private VariableParser() { }
62     }
63
64
Popular Tags