Eclipseplugins
VariableParser.java
1 package com.proalpha.java.oea.plugins.querygenerator;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7 
8 public class VariableParser {
9 
10  private String parseText = "";
11  private String resultText = "";
12 
13  private List<Variable> variableList = null;
14 
15  public VariableParser() {
16  super();
17  }
18 
19  public VariableParser(String parseText) {
20  super();
21  this.parseText = parseText;
22  }
23 
24  public void parse() {
25 
26  variableList = new ArrayList<Variable>();
27 
28  StringBuffer buffer = new StringBuffer(parseText.length());
29 
30  Pattern pattern = Pattern.compile("\\$\\{([^\\}]+)\\}");
31  Matcher matcher = pattern.matcher(parseText);
32 
33  int lastMatchEnd = 0;
34 
35  while(matcher.find()) {
36 
37  buffer.append(parseText.substring(lastMatchEnd, matcher.start()));
38  Variable variable = new Variable(matcher.group(1), buffer.length());
39 
40  buffer.append(variable.getText());
41 
42  variableList.add(variable);
43 
44  lastMatchEnd = matcher.end();
45 
46  }
47 
48  buffer.append(parseText.substring(lastMatchEnd));
49  resultText = buffer.toString();
50 
51  }
52 
53  public String getParseText() {
54  return parseText;
55  }
56 
57  public void setParseText(String parseText) {
58  this.parseText = parseText;
59  }
60 
61  public String getResultText() {
62  return resultText;
63  }
64 
65  public Variable[] getVariables() {
66  if (variableList != null)
67  return variableList.toArray(new Variable[variableList.size()]);
68  else
69  return new Variable[0];
70  }
71 
72 }