Skip to content

Instantly share code, notes, and snippets.

@dizz
Created February 9, 2011 12:30
Show Gist options
  • Save dizz/818393 to your computer and use it in GitHub Desktop.
Save dizz/818393 to your computer and use it in GitHub Desktop.
package be.edmonds.occi;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.json.JSONException;
import org.json.JSONObject;
public class OcciRequest {
private static final String ACTIONS = "actions";
private static final String ATTRIBUTES = "attributes";
private static final String LOCATION = "location";
private static final String REL = "rel";
private static final String TITLE = "title";
private static final String CLASS = "class";
private static final String SCHEME = "scheme";
private static final String TERM = "term";
private static final String CATEGORY = "category";
public static void main(String[] args) throws RecognitionException{
String occiCatHeader = "Category: " +
"myterm; " +
"scheme=\"http://myscheme\"; " +
"class=\"mixin\"; " +
"title=\"Mytitle\"; " +
"rel=\"itisrel\"; " +
"location=\"backyard\"; " +
"attributes=\"attr.one attr.two attr.three\"; " +
"actions=\"http://action http://action\", "+
"myterm; " +
"scheme=\"http://myscheme\"; " +
"class=\"mixin\"; " +
"title=\"Mytitle\"; " +
"rel=\"itisrel\"; " +
"location=\"backyard\"; " +
"attributes=\"attr.one attr.two attr.three\"; " +
"actions=\"http://action http://action\"\n";
String occiLinkHeader = "Link: " +
"</home/andy>; " +
"rel=\"tosomething\"; " +
"self=\"andy\"; " +
"category=\"yo\"; " +
"int.attr=12; " +
"str.attr=\"weeeee\", " +
"</home/andy>; " +
"rel=\"tosomething\"; " +
"self=\"andy\"; " +
"category=\"yo\"; " +
"int.attr=12; " +
"str.attr=\"weeeee\"\n";
String occiAttributeHeader = "X-OCCI-Attribute: occi.computearchitechture=\"test\", another.me=12\n";
String occiLocationHeader = "X-OCCI-Location: http://hey, http://way\n";
String occiRequest = occiCatHeader + occiLinkHeader + occiAttributeHeader + occiLocationHeader;
System.out.println("Will parse:\n" + occiRequest + "\n");
occi_http_textParser parser = getParser(occiRequest);
String json = parser.occi_request();
if(parser.getNumberOfSyntaxErrors() == 0){
System.out.println("Generated intermediatry JSON representation:");
System.out.println("\t"+json+"\n");
System.out.println("Now parsing and extracting values with JSON parser:");
try {
JSONObject o = new JSONObject(json);
System.out.println("\tterm: \t\t" + o.getJSONObject(CATEGORY).getString(TERM));
System.out.println("\tscheme: \t" + o.getJSONObject(CATEGORY).getString(SCHEME));
System.out.println("\tclass: \t\t" + o.getJSONObject(CATEGORY).getString(CLASS));
System.out.println("\ttitle: \t\t" + o.getJSONObject(CATEGORY).getString(TITLE));
System.out.println("\trel: \t\t" + o.getJSONObject(CATEGORY).getString(REL));
System.out.println("\tlocation: \t" + o.getJSONObject(CATEGORY).getString(LOCATION));
System.out.println("\tattributes: \t" + o.getJSONObject(CATEGORY).getString(ATTRIBUTES));
System.out.println("\tactions: \t" + o.getJSONObject(CATEGORY).getString(ACTIONS));
} catch (JSONException e) {
e.printStackTrace();
}
}
else{
System.out.println("Failed to parse the text representation.");
}
}
private static occi_http_textParser getParser(String occiCatHeader) {
CharStream stream = new ANTLRStringStream(occiCatHeader);
occi_http_textLexer lexer = new occi_http_textLexer(stream);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
occi_http_textParser parser = new occi_http_textParser(tokenStream);
return parser;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment