CS 330 Lecture 18 – XML Pull Parsing and GUI Abstraction
Agenda
- what ?s
- think about this
- languages for data (https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=)
- a GUI description language using XML
- XML pull parsing
TODO
- Read http://msdn.microsoft.com/en-us/magazine/cc500572.aspx. Read http://stackoverflow.com/questions/3536893/what-are-the-pros-and-cons-of-xml-and-json. Bonus 1/4 sheet.
Think About This
What makes a language good? What makes it bad?
Code
gui.xml
<?xml version="1.0" encoding="UTF-8"?>
<panel orientation="vertical">
<panel orientation="horizontal">
<label text="To:" />
<textbox name="toBox" />
</panel>
<panel orientation="horizontal">
<label text="Subject:" />
<textbox name="subjectBox" />
</panel>
<textarea name="bodyBox" />
<button text="Send" />
</panel>
gui_madness.c
#include <stdio.h>
#include <stdlib.h>
#include <expat.h>
#include <string.h>
void open_handler(void *data,
const char *tag,
const char **attributes) {
/* printf("open tag: %s\n", tag); */
if (strcmp(tag, "panel") == 0) {
int is_vertical = 1;
for (int i = 0; attributes[i] != NULL; i += 2) {
if (strcmp(attributes[i], "orientation") == 0) {
if (strcmp(attributes[i + 1], "horizontal") == 0) {
is_vertical = 0;
}
}
}
printf("JPanel panel = new JPanel(new BoxLayout(BoxLayout.%s));\n", is_vertical ? "PAGE_AXIS" : "LINE_AXIS");
} else if (strcmp(tag, "button") == 0) {
printf("new JButton(%s);\n", attributes[1]);
}
}
void close_handler(void *data,
const char *tag) {
/* printf("close tag: %s\n", tag); */
}
int main(int argc, char **argv) {
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetElementHandler(parser, open_handler, close_handler);
const int BUFFER_SIZE = 8192;
char buffer[BUFFER_SIZE];
FILE *xml_file = fopen(argv[1], "r");
int is_eof = 0;
while (!is_eof) {
int length = fread(buffer, sizeof(char), BUFFER_SIZE, xml_file);
is_eof = feof(xml_file);
XML_Parse(parser, buffer, length, is_eof);
}
fclose(xml_file);
XML_ParserFree(parser);
return 0;
}
Haiku
I keep a door stack
Push! Pop! It makes life a game
I win when I’m home
Push! Pop! It makes life a game
I win when I’m home