OSDN Git Service

a@id/name for XHTML(ez,y!)
[modchxj/mod_chxj.git] / src / chxj_load_device_data.c
1 /*
2  * Copyright (C) 2005-2009 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include "mod_chxj.h"
18 #include <qs_ignore_sp.h>
19 #include <qs_log.h>
20 #include <qs_malloc.h>
21 #include <qs_parse_attr.h>
22 #include <qs_parse_file.h>
23 #include <qs_parse_string.h>
24 #include <qs_parse_tag.h>
25 #include "chxj_load_device_data.h"
26 #include "chxj_str_util.h"
27
28
29 static void s_set_devices_data(
30   Doc              *doc, 
31   apr_pool_t       *p, 
32   mod_chxj_config  *conf, 
33   Node             *node) ;
34
35 static void s_set_user_agent_data(
36   Doc              *doc, 
37   apr_pool_t       *p, 
38   mod_chxj_config  *conf, 
39   Node             *node);
40
41 static void s_set_device_data(
42   Doc               *doc, 
43   apr_pool_t        *p, 
44   device_table_list *dtl, 
45   Node              *node) ;
46
47
48 /**
49  * load device_data.xml
50  */
51 void
52 chxj_load_device_data(Doc *doc, apr_pool_t *p, mod_chxj_config *conf) 
53 {
54   conf->devices = NULL;
55   s_set_devices_data(doc, p, conf,qs_get_root(doc));
56 }
57
58
59 /**
60  * <devices>
61  */
62 static void
63 s_set_devices_data(Doc *doc, apr_pool_t *p, mod_chxj_config *conf, Node *node) 
64 {
65   Node *child;
66
67   for (child = qs_get_child_node(doc,node); 
68        child ; 
69        child = qs_get_next_node(doc,child)) {
70     char *name = qs_get_node_name(doc,child);
71     if (STRCASEEQ('d','D',"devices",name)) {
72       s_set_user_agent_data(doc, p, conf, child);
73     }
74   }
75 }
76
77 /**
78  * <user_agent>
79  */
80 static void
81 s_set_user_agent_data(Doc *doc, apr_pool_t *p, mod_chxj_config *conf, Node *node) 
82 {
83   Node              *child;
84   device_table_list *t;
85
86   for (child = qs_get_child_node(doc,node);
87        child ;
88        child = qs_get_next_node(doc,child)) {
89     char *name = qs_get_node_name(doc,child);
90     if (STRCASEEQ('u','U',"user_agent",name)) {
91       Attr *attr;
92       device_table_list *dtl;
93
94       if (! conf->devices) {
95         conf->devices = apr_pcalloc(p, sizeof(device_table_list));
96         conf->devices->next    = NULL;
97         conf->devices->pattern = NULL;
98         conf->devices->table   = NULL;
99         conf->devices->tail    = NULL;
100         dtl = conf->devices;
101       }
102       else {
103         for (t = conf->devices; t ; t = t->next) {
104           if (! t->next)
105             break;
106         }
107         t->next = apr_pcalloc(p, sizeof(device_table_list));
108         t->next->next    = NULL;
109         t->next->pattern = NULL;
110         t->next->table   = NULL;
111         t->next->tail    = NULL;
112         dtl = t->next;
113       }
114
115       for (attr = qs_get_attr(doc,child); 
116            attr ; 
117            attr = qs_get_next_attr(doc,attr)) {
118         char *attr_name = qs_get_attr_name(doc,attr);
119
120         if (STRCASEEQ('p','P',"pattern",attr_name)) {
121             dtl->pattern = apr_pstrdup(p, qs_get_attr_value(doc,attr));
122             dtl->regexp = ap_pregcomp(p, (const char *)dtl->pattern, AP_REG_EXTENDED|AP_REG_ICASE);
123         }
124       }
125       s_set_device_data(doc, p, dtl, child);
126     }
127   }
128 }
129
130
131 static void
132 s_set_device_data(Doc *doc, apr_pool_t *p, device_table_list *dtl, Node *node) 
133 {
134   Node         *child;
135   device_table *dt;
136
137   dt = apr_pcalloc(p, sizeof(device_table));
138   dt->next           = NULL;
139   dt->device_id      = NULL;
140   dt->device_name    = NULL;
141   dt->html_spec_type = CHXJ_SPEC_Chtml_3_0;
142   dt->width          = 0;
143   dt->heigh          = 0;
144   dt->wp_width       = 0;
145   dt->wp_heigh       = 0;
146   dt->cache          = 5;
147   dt->emoji_type     = NULL;
148   dt->color          = 256;
149   dt->dpi_width      = 96;
150   dt->dpi_heigh      = 96;
151
152   for (child = qs_get_child_node(doc,node); 
153        child ;
154        child = qs_get_next_node(doc,child)) {
155     char *name = qs_get_node_name(doc,child);
156     switch (*name) {
157     case 'd':
158     case 'D':
159       if (strcasecmp(name, "device") == 0) {
160         s_set_device_data(doc,p, dtl, child);
161       }
162       else
163       if (strcasecmp(name, "device_id") == 0) {
164         Node* ch = qs_get_child_node(doc, child);
165         if (ch &&  strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
166           dt->device_id = apr_pstrdup(p, qs_get_node_value(doc, ch));
167         }
168       }
169       else
170       if (strcasecmp(name, "device_name") == 0) {
171         Node* ch = qs_get_child_node(doc, child);
172         if (ch &&  strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
173           dt->device_name = apr_pstrdup(p, qs_get_node_value(doc, ch));
174         }
175       }
176       else
177       if (strcasecmp(name, "dpi_width") == 0) {
178         Node* ch = qs_get_child_node(doc, child);
179         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
180           char *vv = qs_get_node_value(doc,ch);
181           unsigned int ii;
182           for (ii=0; ii<strlen(vv); ii++) {
183             if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
184               continue;
185             break;
186           }
187           if (ii == strlen(vv)) 
188             dt->dpi_width = atoi(qs_get_node_value(doc,ch));
189           else 
190             dt->dpi_width = 0;
191         }
192       }
193       else
194       if (strcasecmp(name, "dpi_heigh") == 0) {
195         Node* ch = qs_get_child_node(doc, child);
196         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
197           char *vv = qs_get_node_value(doc,ch);
198           unsigned int ii;
199           for (ii=0; ii<strlen(vv); ii++) {
200             if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
201               continue;
202             break;
203           }
204   
205           if (ii == strlen(vv)) 
206             dt->dpi_heigh = atoi(qs_get_node_value(doc,ch));
207           else 
208             dt->dpi_heigh = 0;
209         }
210       }
211       break;
212
213     case 'h':
214     case 'H':
215       if (strcasecmp(name, "html_spec_type") == 0) {
216         Node *ch = qs_get_child_node(doc, child);
217         if (ch &&  strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
218           char *vv = qs_get_node_value(doc, ch);
219           if (STRCASEEQ('x','X',"xhtml_mobile_1_0",vv)) {
220             dt->html_spec_type = CHXJ_SPEC_XHtml_Mobile_1_0;
221           }
222           else if (STRCASEEQ('c','C',"chtml_1_0",vv)) {
223             dt->html_spec_type = CHXJ_SPEC_Chtml_1_0;
224           }
225           else if (STRCASEEQ('c','C',"chtml_2_0",vv)) {
226             dt->html_spec_type = CHXJ_SPEC_Chtml_2_0;
227           }
228           else if (STRCASEEQ('c','C',"chtml_3_0",vv)) {
229             dt->html_spec_type = CHXJ_SPEC_Chtml_3_0;
230           }
231           else if (STRCASEEQ('c','C',"chtml_4_0",vv)) {
232             dt->html_spec_type = CHXJ_SPEC_Chtml_4_0;
233           }
234           else if (STRCASEEQ('c','C',"chtml_5_0",vv)) {
235             dt->html_spec_type = CHXJ_SPEC_Chtml_5_0;
236           }
237           else if (STRCASEEQ('c','C',"chtml_6_0",vv)) {
238             dt->html_spec_type = CHXJ_SPEC_Chtml_6_0;
239           }
240           else if (STRCASEEQ('c','C',"chtml_7_0",vv)) {
241             dt->html_spec_type = CHXJ_SPEC_Chtml_7_0;
242           }
243           else if (STRCASEEQ('h','H',"hdml",vv)) {
244             dt->html_spec_type = CHXJ_SPEC_Hdml;
245           }
246           else if (STRCASEEQ('j','J',"jhtml",vv)) {
247             dt->html_spec_type = CHXJ_SPEC_Jhtml;
248           }
249           else if (STRCASEEQ('j','J',"jxhtml",vv)) {
250             dt->html_spec_type = CHXJ_SPEC_Jxhtml;
251           }
252         }
253       }
254       else 
255       if (strcasecmp(name, "heigh") == 0) {
256         Node* ch = qs_get_child_node(doc, child);
257         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
258           char *vv = qs_get_node_value(doc,ch);
259           unsigned int ii;
260           for (ii=0; ii<strlen(vv); ii++) {
261             if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
262               continue;
263             break;
264           }
265   
266           if (ii == strlen(vv)) 
267             dt->heigh = atoi(qs_get_node_value(doc,ch));
268           else 
269             dt->heigh = 0;
270         }
271       }
272       break;
273
274     case 'w':
275     case 'W':
276       if (strcasecmp(name, "width") == 0) {
277         Node *ch = qs_get_child_node(doc, child);
278         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
279           char *vv = qs_get_node_value(doc,ch);
280           unsigned int ii;
281           for (ii=0; ii<strlen(vv); ii++) {
282             if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0')
283               continue;
284             break;
285           }
286           if (ii == strlen(vv))
287             dt->width = atoi(qs_get_node_value(doc,ch));
288           else 
289             dt->width = 0;
290         }
291       }
292       else
293       if (strcasecmp(name, "wp_width") == 0) {
294         Node *ch = qs_get_child_node(doc, child);
295         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
296           char *vv = qs_get_node_value(doc,ch);
297           unsigned int ii;
298           for (ii=0; ii<strlen(vv); ii++) {
299             if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
300               continue;
301             break;
302           }
303   
304           if (ii == strlen(vv)) 
305             dt->wp_width = atoi(qs_get_node_value(doc,ch));
306           else 
307             dt->wp_width = 0;
308         }
309       }
310       else
311       if (strcasecmp(name, "wp_heigh") == 0) {
312         Node *ch = qs_get_child_node(doc, child);
313         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
314           char *vv = qs_get_node_value(doc,ch);
315           unsigned int ii;
316           for (ii=0; ii<strlen(vv); ii++) {
317             if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
318               continue;
319             break;
320           }
321   
322           if (ii == strlen(vv)) 
323             dt->wp_heigh = atoi(qs_get_node_value(doc,ch));
324           else 
325             dt->wp_heigh = 0;
326         }
327       }
328       break;
329
330     case 'g':
331     case 'G':
332       if (strcasecmp(name, "gif") == 0) {
333         Node *ch = qs_get_child_node(doc, child);
334         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
335           char *vv = qs_get_node_value(doc,ch);
336   
337           if (strcasecmp(vv, "true") == 0)
338             dt->available_gif = 1;
339           else
340             dt->available_gif = 0;
341         }
342       }
343       break;
344
345     case 'j':
346     case 'J':
347       if (strcasecmp(name, "jpeg") == 0 || strcasecmp(name, "jpg") == 0) {
348         Node *ch = qs_get_child_node(doc, child);
349         if (ch != NULL && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
350           char *vv = qs_get_node_value(doc,ch);
351   
352           if (strcasecmp(vv, "true") == 0) 
353             dt->available_jpeg = 1;
354           else 
355             dt->available_jpeg = 0;
356         }
357       }
358       break;
359   
360     case 'p':
361     case 'P':
362       if (strcasecmp(name, "png") == 0) {
363         Node *ch = qs_get_child_node(doc, child);
364         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
365           char *vv = qs_get_node_value(doc,ch);
366   
367           if (strcasecmp(vv, "true") == 0) 
368             dt->available_png = 1;
369           else
370             dt->available_png = 0;
371         }
372       }
373       break;
374
375     case 'b':
376     case 'B':
377       if (strcasecmp(name, "bmp2") == 0) {
378         Node *ch = qs_get_child_node(doc, child);
379         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
380           char *vv = qs_get_node_value(doc,ch);
381   
382           if (strcasecmp(vv, "true") == 0) 
383             dt->available_bmp2 = 1;
384           else
385             dt->available_bmp2 = 0;
386         }
387       }
388       else
389       if (strcasecmp(name, "bmp4") == 0) {
390         Node *ch = qs_get_child_node(doc, child);
391         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
392           char *vv = qs_get_node_value(doc,ch);
393   
394           if (strcasecmp(vv, "true") == 0) 
395             dt->available_bmp4 = 1;
396           else
397             dt->available_bmp4 = 0;
398         }
399       }
400       break;
401
402     case 'c':
403     case 'C':
404       if (strcasecmp(name, "color") == 0) {
405         Node *ch = qs_get_child_node(doc, child);
406         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
407           char *vv = qs_get_node_value(doc,ch);
408   
409           if (chxj_chk_numeric(vv) != 0)
410             dt->color = 0;
411           else 
412             dt->color = chxj_atoi(vv);
413         }
414       }
415       else
416       if (strcasecmp(name, "cache") == 0) {
417         Node *ch = qs_get_child_node(doc, child);
418         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) {
419           char *vv = qs_get_node_value(doc,ch);
420           unsigned int ii;
421           for (ii=0; ii<strlen(vv); ii++) {
422             if ((vv[ii] >= '1' && vv[ii] <= '9') || vv[ii] == '0') 
423               continue;
424             break;
425           }
426   
427           if (ii == strlen(vv)) 
428             dt->cache = atoi(qs_get_node_value(doc,ch));
429           else 
430             dt->cache = 0;
431         }
432       }
433       break;
434
435     case 'e':
436     case 'E':
437       if (strcasecmp(name, "emoji_type") == 0) {
438         Node *ch = qs_get_child_node(doc, child);
439         if (ch && strcasecmp(qs_get_node_name(doc,ch), "text") == 0) 
440           dt->emoji_type = apr_pstrdup(p, qs_get_node_value(doc, ch));
441       }
442       break;
443     default:
444       break;
445     }
446   }
447
448   if (dt->device_id) {
449     if (! dtl->table) {
450       dtl->table = dt;
451       dtl->tail = dt;
452     }
453     else {
454       dtl->tail->next = dt;
455       dtl->tail = dt;
456     }
457   }
458 }
459 /*
460  * vim:ts=2 et
461  */