1 package com.quantum.wizards;
3 import java.util.ArrayList;
6 import java.util.StringTokenizer;
10 * <p>This class takes a URL pattern and creates a full URL. A URL pattern might look
11 * like this: <code>jdbc:postgresql://{hostname}:{port}/{dbname}</code>, and the
12 * properties should include "hostname", "port" and "dbname".
16 public class URLBuilder {
18 public static String createURL(String urlPattern, Map properties) {
19 StringBuffer buffer = new StringBuffer();
20 boolean isVariable = false;
21 String variableName = null;
22 for (StringTokenizer tokenizer = new StringTokenizer(urlPattern, "{}", true);
23 tokenizer.hasMoreTokens(); ) {
24 String token = tokenizer.nextToken();
26 if ("{".equals(token) && !isVariable) {
28 } else if (isVariable && "}".equals(token) && variableName != null) {
29 if (!properties.containsKey(variableName)) {
31 buffer.append(variableName);
34 buffer.append(properties.get(variableName));
37 } else if (isVariable) {
43 return buffer.toString();
46 public static String[] getVariableNames(String urlPattern) {
47 List list = new ArrayList();
48 if (urlPattern != null) {
49 boolean isVariable = false;
50 String variableName = null;
51 for (StringTokenizer tokenizer = new StringTokenizer(urlPattern, "{}", true);
52 tokenizer.hasMoreTokens(); ) {
53 String token = tokenizer.nextToken();
55 if ("{".equals(token) && !isVariable) {
57 } else if (isVariable && "}".equals(token) && variableName != null) {
58 list.add(variableName);
60 } else if (isVariable) {
65 return (String[]) list.toArray(new String[list.size()]);