Skip to content

Instantly share code, notes, and snippets.

@manzke
Created December 6, 2010 09:37
Show Gist options
  • Save manzke/730068 to your computer and use it in GitHub Desktop.
Save manzke/730068 to your computer and use it in GitHub Desktop.
Proof-of-Concept for a Guice-based Parser
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.name.Names;
public class Parser {
public static Annotations parse(String annotationAsString) throws IOException{
Annotations annotations = new Annotations();
BufferedReader reader = new BufferedReader(new StringReader(annotationAsString));
String s;
Annotation annotation = null;
Custom custom = null;
Injector injector;
ColorConverter colorConverter = new ColorConverter();
List<Module> modules = new ArrayList<Module>();
while((s = reader.readLine()) != null){
if(s.startsWith("[") && (annotation != null || custom != null)){
modules.add(colorConverter);
injector = Guice.createInjector(modules.toArray(new Module[modules.size()]));
if(annotation != null){
injector.injectMembers(annotation);
}else{
injector.injectMembers(custom);
}
modules.clear();
annotation = null;
injector = null;
}
if(s.startsWith("[TEXT]")){
annotation = new TextAnnotation();
annotations.annotations.add(annotation);
}else if(s.startsWith("[CUSTOM]")){
custom = new Custom();
annotations.custom = custom;
}else{ //parse Line
int index = s.indexOf("=");
if(index < 1){
continue;
}
final String key = s.substring(0, index).trim();
final String value = s.substring(index+1).trim();
modules.add(new AbstractModule() {
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named(key.toUpperCase())).to(value);
}
});
}
}
if(annotation != null || custom != null){
modules.add(colorConverter);
injector = Guice.createInjector(modules.toArray(new Module[modules.size()]));
if(annotation != null){
injector.injectMembers(annotation);
}else{
injector.injectMembers(custom);
}
modules.clear();
annotation = null;
injector = null;
}
return annotations;
}
public static void main(String[] args) throws IOException {
StringBuilder builder = new StringBuilder();
builder.append("[TEXT]\r\n");
builder.append("CUSTOMPROPERTY = 1\r\n");
builder.append("X = 177\r\n");
builder.append("Y = 149\r\n");
builder.append("WIDTH = 145\r\n");
builder.append("HEIGHT = 151\r\n");
builder.append("PAGE = 1\r\n");
builder.append("EDIT = 1\r\n");
builder.append("TEXT = Checker\r\n");
builder.append("FONTHEIGHT = 44790\r\n");
builder.append("COLOR = 0, 0, 0\r\n");
builder.append("FILLCOLOR = 0, 255, 0\r\n");
builder.append("TRANSPARENT = 0\r\n");
builder.append("[TEXT]\r\n");
builder.append("CUSTOMPROPERTY = 2\r\n");
builder.append("X = 35\r\n");
builder.append("Y = 354\r\n");
builder.append("WIDTH = 315\r\n");
builder.append("HEIGHT = 151\r\n");
builder.append("PAGE = 1\r\n");
builder.append("EDIT = 1\r\n");
builder.append("TEXT = Mehrzeilige Notiz&lt;N&gt;und das nach dem Umbruch&lt;N&gt;Oder doch nicht?\r\n");
builder.append("FONTHEIGHT = 44790\r\n");
builder.append("COLOR = 128, 128, 128\r\n");
builder.append("FILLCOLOR = 255, 255, 0\r\n");
builder.append("TRANSPARENT = 0\r\n");
builder.append("[CUSTOM]\r\n");
builder.append("FRAMES = |1|2|3|\r\n");
Annotations annotations = parse(builder.toString());
System.out.println(annotations.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment