Quantum version 2.4.1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / wizards / URLBuilder.java
1 package com.quantum.wizards;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.StringTokenizer;
7
8
9 /**
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". 
13  * 
14  * @author BC Holmes
15  */
16 public class URLBuilder {
17
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();
25                         
26                         if ("{".equals(token) && !isVariable) {
27                                 isVariable = true;
28                         } else if (isVariable && "}".equals(token) && variableName != null) {
29                                 if (!properties.containsKey(variableName)) {
30                                         buffer.append("{");
31                                         buffer.append(variableName);
32                                         buffer.append("}");
33                                 } else {
34                                         buffer.append(properties.get(variableName));
35                                 }
36                                 isVariable = false;
37                         } else if (isVariable) {
38                                 variableName = token;
39                         } else {
40                                 buffer.append(token);
41                         }
42                 }
43                 return buffer.toString();
44         }
45
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();
54                                 
55                                 if ("{".equals(token) && !isVariable) {
56                                         isVariable = true;
57                                 } else if (isVariable && "}".equals(token) && variableName != null) {
58                                         list.add(variableName);
59                                         isVariable = false;
60                                 } else if (isVariable) {
61                                         variableName = token;
62                                 }
63                         }
64                 }
65                 return (String[]) list.toArray(new String[list.size()]);
66         }
67 }