Skip to content

Instantly share code, notes, and snippets.

@porcelli
Created August 10, 2010 20:08
Show Gist options
  • Save porcelli/517901 to your computer and use it in GitHub Desktop.
Save porcelli/517901 to your computer and use it in GitHub Desktop.
import java.security.InvalidParameterException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class RestParser {
final char[] content;
final Map<Integer, String> positionedVariables = new TreeMap<Integer, String>();
int p = 0;
public static void main(String[] args) {
RestParser test = new RestParser("/A/V/A/AA2323/sdf23/{1}/{afff}");
System.out.println(test.parseTemplate().toString()
.equals("{6=1, 7=afff}"));
System.out.println(test
.getVariableValues("/A/V/A/AA2323/sdf23/teste/valor")
.toString().equals("{1=teste, afff=valor}"));
RestParser test2 = new RestParser("/A/V/A/{dsdfgd}/sddsd/{aaa");
System.out.println(test2.parseTemplate().toString()
.equals("{4=dsdfgd}"));
System.out.println(test2
.getVariableValues("/A/V/A/aquialguma #coisa/sddsd/aaa")
.toString().equals("{dsdfgd=aquialguma #coisa}"));
RestParser test3 = new RestParser("/A/V/A/{dsdfgd}/sddsd/{aaa}");
System.out.println(test3.parseTemplate().toString()
.equals("{4=dsdfgd, 6=aaa}"));
System.out.println(test3.getVariableValues("/A/V/A/algum%valor/sddsd")
.toString().equals("{dsdfgd=algum%valor}"));
System.out.println(test3
.getVariableValues("/A/V/A/algum%valor/sddsd/a").toString()
.equals("{dsdfgd=algum%valor, aaa=a}"));
RestParser test4 = new RestParser("/A/V/A/{dsdfgd/sddsd/aaa}");
System.out.println(test4.parseTemplate().toString().equals("{}"));
System.out.println(test4.getVariableValues("/A/V/A/dsdfgd/sddsd/aaa")
.toString().equals("{}"));
RestParser test5 = new RestParser("/A/V/A/dsdfgd/sddsd/aaa");
System.out.println(test5.parseTemplate().toString().equals("{}"));
System.out.println(test5.getVariableValues("/A/V/A/dsdfgd/sddsd/aaa")
.toString().equals("{}"));
}
public RestParser(String text) {
if (text == null) {
throw new InvalidParameterException("text cannot be null");
}
this.content = text.toCharArray();
}
public Map<Integer, String> parseTemplate() {
if (p > 0) {
return positionedVariables;
}
int slashPos = 0;
StringBuilder buf = new StringBuilder();
while (p < content.length) {
switch (content[p]) {
case '/':
slashPos++;
buf = new StringBuilder();
p++;
break;
case '{':
boolean gotEndBracket = true;
p++;
do {
buf.append(content[p]);
p++;
if (p >= content.length || content[p] == '/') {
gotEndBracket = false;
break;
}
} while (content[p] != '}');
p++;
if (gotEndBracket) {
positionedVariables.put(slashPos, buf.toString());
}
buf = new StringBuilder();
break;
default:
buf.append(content[p]);
p++;
break;
}
}
return positionedVariables;
}
public Map<String, String> getVariableValues(String input) {
Map<String, String> returnValue = new LinkedHashMap<String, String>();
if (positionedVariables.size() == 0) {
return returnValue;
}
String[] content = input.split("/");
for (Entry<Integer, String> variable : positionedVariables.entrySet()) {
int pos = variable.getKey();
if (pos < content.length) {
returnValue
.put(variable.getValue(), content[variable.getKey()]);
}
}
return returnValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment