[webp] Added start element function
[moonshot-ui.git] / src / moonshot-webp-parser.vala
1 namespace WebProvisioning
2
3   IdCard card;
4   IdCard[] cards;
5
6   bool
7   check_stack (SList<string> stack, string[] reference)
8   {
9     if (stack.length () < reference.length)
10       return false;
11     
12     for (int i = 0; i<reference.length; i++)
13     {
14       if (stack.nth_data(i) != reference[i])
15         return false;
16     }
17
18     return true;
19   }
20
21   bool
22   server_cert_handler (SList<string> stack)
23   {
24     string[] server_cert_path = {"server-cert", "trust-anchor", "identity", "identities"};
25     
26     return check_stack (stack, server_cert_path);
27   }
28
29   bool
30   subject_alt_handler (SList<string> stack)
31   {
32     string[] subject_alt_path = {"subject-alt", "trust-anchor", "identity", "identities"};
33     
34     return check_stack (stack, subject_alt_path);
35   }
36
37   bool
38   subject_handler (SList<string> stack)
39   {
40     string[] subject_path = {"subject", "trust-anchor", "identity", "identities"};
41     
42     return check_stack (stack, subject_path);
43   }
44   
45   bool
46   ca_cert_handler (SList<string> stack)
47   {
48     string[] ca_path = {"ca-cert", "trust-anchor", "identity", "identities"};
49     
50     return check_stack (stack, ca_path);
51   }
52
53   bool
54   realm_handler (SList<string> stack)
55   {
56     string[] realm_path = {"realm", "identity", "identities"};
57     
58     return check_stack (stack, realm_path);
59   }
60
61   bool
62   password_handler (SList<string> stack)
63   {
64     string[] password_path = {"password", "identity", "identities"};
65     
66     return check_stack (stack, password_path);
67   }
68
69   bool
70   user_handler (SList<string> stack)
71   {
72     string[] user_path = {"user", "identity", "identities"};
73     
74     return check_stack (stack, user_path);
75   }
76
77   bool
78   display_name_handler (SList<string> stack)
79   {
80     string[] display_name_path = {"display-name", "identity", "identities"};
81     
82     return check_stack (stack, display_name_path);
83   }
84   
85   public void
86   start_element_func (MarkupParseContext context,
87                       string element_name,
88                       string[] attribute_names,
89                       string[] attribute_values) throws MarkupError
90   {
91     debug ("START %s", element_name); 
92   }
93
94   public void
95   text_element_func (MarkupParseContext context,
96                      string             text,
97                      size_t             text_len) throws MarkupError
98   {
99     unowned SList<string> stack = context.get_element_stack ();
100     
101     if (text_len < 1)
102       return;
103     
104     if (stack.nth_data(0) == "display-name" && display_name_handler (stack))
105     {
106       card.display_name = text;
107     }
108     else if (stack.nth_data(0) == "user" && user_handler (stack))
109     {
110       card.username = text;
111     }
112     else if (stack.nth_data(0) == "password" && password_handler (stack))
113     {
114       card.password = text;
115     }
116     else if (stack.nth_data(0) == "realm" && realm_handler (stack))
117     {
118       card.issuer = text;
119     }
120     else if (stack.nth_data(0) == "service")
121     {
122       string[] services = card.services;
123       card.services = new string[services.length + 1];
124       for (int i = 0; i<services.length; i++)
125       {
126         card.services[i] = services[i];
127       }
128       card.services[services.length] = text;
129     }
130     /* Rules */
131     else if (stack.nth_data(0) == "pattern")
132     {
133     }
134     else if (stack.nth_data(0) == "always_confirm")
135     {
136     }
137     
138     /*Trust anchor*/
139     else if (stack.nth_data(0) == "ca-cert" && ca_cert_handler (stack))
140     {
141       card.trust_anchor.ca_cert = text;
142     }
143     else if (stack.nth_data(0) == "subject" && subject_handler (stack))
144     {
145       card.trust_anchor.subject = text;
146     }
147     else if (stack.nth_data(0) == "subject-alt" && subject_alt_handler (stack))
148     {
149       card.trust_anchor.subject_alt = text;
150     }
151     else if (stack.nth_data(0) == "server-cert" && server_cert_handler (stack))
152     {
153       card.trust_anchor.server_cert = text;
154     }
155   }
156
157   class WebProvisionParser
158   {
159     public WebProvisionParser (string path)
160     {
161       string text = "";
162       var file = File.new_for_path (path);
163     
164       try
165       {
166         var dis = new DataInputStream (file.read ());
167         string line;
168         while ((line = dis.read_line (null)) != null)
169           text += line;
170       }
171       catch (Error e)
172       {
173         error ("Could not retreive file size");
174       }
175       
176       card = new IdCard();
177       
178       MarkupParser parser = {null, null, text_element_func, null, null};
179       
180       var ctx = new MarkupParseContext(parser, 0, null, null);
181       
182       try
183       {
184         ctx.parse (text, text.length);
185       }
186       catch (Error e)
187       {
188         error ("Could not parse %s, invalid content", path);
189       } 
190     }
191   }
192
193   public static int main (string[] args)
194   {
195     if (args.length < 2)
196     {
197       error ("Usage %s [-a] WEB_PROVISIONING_FILE", args[0]);
198     }
199     
200     if (!FileUtils.test (args[1], FileTest.EXISTS | FileTest.IS_REGULAR))
201     {
202       error ("%s does not exist", args[1]);
203     }
204     
205     var webp = new WebProvisionParser (args[1]);
206     
207     debug ("'%s' '%s' '%s' '%s'", card.display_name, card.username, card.password, card.issuer);
208     
209     foreach (string srv in card.services)
210     {
211       debug ("service: %s", srv);
212     }
213     
214     return 0;
215   }
216 }