X-Git-Url: http://secure.phpeclipse.com diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/wizards/URLBuilder.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/wizards/URLBuilder.java new file mode 100644 index 0000000..a22233a --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/wizards/URLBuilder.java @@ -0,0 +1,67 @@ +package com.quantum.wizards; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; + + +/** + *

This class takes a URL pattern and creates a full URL. A URL pattern might look + * like this: jdbc:postgresql://{hostname}:{port}/{dbname}, and the + * properties should include "hostname", "port" and "dbname". + * + * @author BC Holmes + */ +public class URLBuilder { + + public static String createURL(String urlPattern, Map properties) { + StringBuffer buffer = new StringBuffer(); + boolean isVariable = false; + String variableName = null; + for (StringTokenizer tokenizer = new StringTokenizer(urlPattern, "{}", true); + tokenizer.hasMoreTokens(); ) { + String token = tokenizer.nextToken(); + + if ("{".equals(token) && !isVariable) { + isVariable = true; + } else if (isVariable && "}".equals(token) && variableName != null) { + if (!properties.containsKey(variableName)) { + buffer.append("{"); + buffer.append(variableName); + buffer.append("}"); + } else { + buffer.append(properties.get(variableName)); + } + isVariable = false; + } else if (isVariable) { + variableName = token; + } else { + buffer.append(token); + } + } + return buffer.toString(); + } + + public static String[] getVariableNames(String urlPattern) { + List list = new ArrayList(); + if (urlPattern != null) { + boolean isVariable = false; + String variableName = null; + for (StringTokenizer tokenizer = new StringTokenizer(urlPattern, "{}", true); + tokenizer.hasMoreTokens(); ) { + String token = tokenizer.nextToken(); + + if ("{".equals(token) && !isVariable) { + isVariable = true; + } else if (isVariable && "}".equals(token) && variableName != null) { + list.add(variableName); + isVariable = false; + } else if (isVariable) { + variableName = token; + } + } + } + return (String[]) list.toArray(new String[list.size()]); + } +}