OSDN Git Service

e0c78547fd7f192dc619125f3ec77f5fe3ad0c6f
[modchxj/mod_chxj.git] / src / chxj_tag_util.c
1 /*
2  * Copyright (C) 2005-2008 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 "chxj_tag_util.h"
18
19
20 /**
21  * The value of the VALUE attribute that the object tag node maintains is
22  * acquired.
23  *
24  * @param doc  [i] The pointer to the Doc structure to be scanned is
25  *                 specified.
26  * @param node [i] The tag node to be scanned is specified.
27  * @param pool [i] To use POOL.
28  * @return The value of the VALUE attribute that the object tag node maintains
29  *         is returned. NULL is returned when not found.
30  */
31 char *
32 qs_get_value_attr(Doc *doc, Node *node, apr_pool_t *pool)
33 {
34   Attr *attr;
35
36   /*--------------------------------------------------------------------------*/
37   /* The object tag node is scanned.                                          */
38   /*--------------------------------------------------------------------------*/
39   for (attr = qs_get_attr(doc,node);
40        attr;
41        attr = qs_get_next_attr(doc,attr)) {
42     char *name  = qs_get_attr_name(doc,attr);
43     char *value = qs_get_attr_value(doc,attr);
44     if (STRCASEEQ('v','V',"value",name)) {
45       /*----------------------------------------------------------------------*/
46       /* The VALUE attribute was found.                                       */
47       /*----------------------------------------------------------------------*/
48       return apr_pstrdup(pool, value);
49     }
50   }
51   /*--------------------------------------------------------------------------*/
52   /* not found                                                                */
53   /*--------------------------------------------------------------------------*/
54   return NULL;
55 }
56
57
58 /**
59  * The value of the checked tag is acquired.
60  *
61  * @param doc  [i] The pointer to the Doc structure to be scanned is
62  *                 specified.
63  * @param tag  [i] The tag node to be scanned is specified.
64  * @param pool [i] To use POOL(unused).
65  * @return The value of the checked tag is returned. NULL is returned when
66  *         not found.
67  */
68 char *
69 qs_get_checked_attr(Doc *doc, Node *tag, apr_pool_t *UNUSED(pool))
70 {
71   Attr *attr;
72   /*--------------------------------------------------------------------------*/
73   /* The object tag node is scanned.                                          */
74   /*--------------------------------------------------------------------------*/
75   for (attr = qs_get_attr(doc,tag);
76        attr != NULL;
77        attr = qs_get_next_attr(doc,attr)) {
78     char *name  = qs_get_attr_name(doc,attr);
79     if (STRCASEEQ('c','C',"checked",name)) {
80       /*----------------------------------------------------------------------*/
81       /* The VALUE attribute was found.                                       */
82       /*----------------------------------------------------------------------*/
83       return name;
84     }
85   }
86   /*------------------------------------------------------------------------*/
87   /* not found                                                              */
88   /*------------------------------------------------------------------------*/
89   return NULL;
90 }
91
92
93 /**
94  * The value of the type attribute is acquired.
95  *
96  * @param doc  [i] The pointer to the Doc structure to be scanned is
97  *                 specified.
98  * @param tag  [i] The tag node to be scanned is specified.
99  * @param r    [i] To use POOL, the pointer to request_rec is specified.
100  * @return The value of the type attribute is returned. NULL is returned when
101  *         not found.
102  */
103 char *
104 qs_get_type_attr(Doc *doc, Node *tag, apr_pool_t *pool)
105 {
106   Attr *attr;
107   /*--------------------------------------------------------------------------*/
108   /* The object tag node is scanned.                                          */
109   /*--------------------------------------------------------------------------*/
110   for (attr = qs_get_attr(doc,tag);
111        attr != NULL;
112        attr = qs_get_next_attr(doc,attr)) {
113     char *name  = qs_get_attr_name(doc,attr);
114     char *value = qs_get_attr_value(doc,attr);
115     if (STRCASEEQ('t','T',"type",name)) {
116       /*----------------------------------------------------------------------*/
117       /* The VALUE attribute was found.                                       */
118       /*----------------------------------------------------------------------*/
119       return apr_pstrdup(pool, value);
120     }
121   }
122   /*--------------------------------------------------------------------------*/
123   /* not found                                                                */
124   /*--------------------------------------------------------------------------*/
125   return NULL;
126 }
127
128
129 /**
130  * The character string area in 0 bytes is allocated.
131  *
132  * @param pool    [i]   To use POOL.
133  * @return The allocated 0 byte character string is returned.
134  */
135 char *
136 qs_alloc_zero_byte_string(apr_pool_t *pool)
137 {
138   char *tgt;
139
140   if (! pool) {
141     return NULL;
142   }
143   tgt = apr_palloc(pool, 1);
144   tgt[0] = '\0';
145
146   return tgt;
147 }
148
149
150 /**
151  * A consecutive head and the last WHITESPACE are removed.
152  *
153  * @param p    [i]   To use POOL
154  * @param s    [i]   The character string that should be removed is specified.
155  * @return The character string that has been removed is returned.
156  */
157 char*
158 qs_trim_string(apr_pool_t *p, char* s)
159 {
160   char *ss = apr_pstrdup(p, s);
161   int len = strlen(s);
162   int ii;
163
164   ii = 0;
165   for (ii = 0;is_white_space(*ss) && ii < len; ss++, ii++);
166
167   ii = strlen(ss);
168   for(;is_white_space(ss[ii-1]) && ii > 0; ii--);
169
170   ss[ii] = '\0';
171
172   return ss;
173 }
174
175
176 /**
177  * The value of child TEXT of tag that maintains the SELECTED attribute is 
178  * returned. 
179  *
180  * @param Doc  [i] The pointer to the Doc structure to be scanned is 
181  *                 specified. 
182  * @param node [i] The tag node to be scanned is specified.
183  * @param pool [i] To use POOL.
184  * @reutrn  The value of child TEXT of tag that maintains the SELECTED 
185  *          attribute is returned. NULL is returned when not found. 
186  */
187 char *
188 qs_get_selected_value_text(Doc *doc, Node *node, apr_pool_t *pool)
189 {
190   Node *child;
191   Node *selchild;
192   char *result   = NULL;
193
194   for (child = qs_get_child_node(doc,node);
195        child != NULL; 
196        child = qs_get_next_node(doc,child)) {
197     char *name = qs_get_node_name(doc,child);
198     /*------------------------------------------------------------------------*/
199     /* <OPTION> tag                                                           */
200     /*------------------------------------------------------------------------*/
201     if (STRCASEEQ('o','O',"option",name)) {
202       Attr *attr;
203       for (attr =  qs_get_attr(doc,child); 
204            attr != NULL; 
205            attr = qs_get_next_attr(doc,attr)) {
206 <<<<<<< HEAD:src/chxj_tag_util.c
207         char *name  = qs_get_attr_name(doc,attr);
208         DBG(r, "qs_get_selected_value name::[%s]" , name);
209
210         if ((*name == 's'|| *name == 'S') && strcasecmp(name, "selected") == 0) {
211 =======
212         char *name2  = qs_get_attr_name(doc,attr);
213         if (STRCASEEQ('s','S',"selected",name2)) {
214 >>>>>>>   * updated new trunk.:src/chxj_tag_util.c
215           /*------------------------------------------------------------------*/
216           /* SELECTED Value Found                                             */
217           /*------------------------------------------------------------------*/
218           selchild = qs_get_child_node(doc, child);
219           if (! selchild) {
220             /* void value */
221             return apr_pstrdup(pool, "");
222           }
223           return qs_get_node_value(doc, selchild);
224         }
225       }
226     }
227
228     if ((result = qs_get_selected_value_text(doc, child, pool)) != NULL) {
229       return result;
230     }
231   }
232
233   /*--------------------------------------------------------------------------*/
234   /* not found                                                                */
235   /*--------------------------------------------------------------------------*/
236   return NULL;
237 }
238
239
240 /**
241  * The value of tag that maintains the SELECTED attribute is acquired. 
242  *
243  * @param doc    [i]  The pointer to the Doc structure to be scanned is 
244  *                    specified. 
245  * @param node   [i]  The SELECT tag node is specified.
246  * @param pool   [i] To use POOL.
247  * @return The value of tag that maintains the SELECTED attribute is 
248  *         returned. NULL is returned when not found. 
249  */
250 char *
251 qs_get_selected_value(Doc *doc, Node *node, apr_pool_t *pool)
252 {
253   Node *child;
254   char *result    = NULL;
255
256   for (child = qs_get_child_node(doc,node); 
257        child != NULL; 
258        child = qs_get_next_node(doc,child)) {
259     char *name = qs_get_node_name(doc,child);
260     /*------------------------------------------------------------------------*/
261     /* <OPTION> tag                                                           */
262     /*------------------------------------------------------------------------*/
263     if (STRCASEEQ('o','O',"option",name)) {
264       Attr *attr;
265       for (attr = qs_get_attr(doc,child); 
266            attr; 
267            attr = qs_get_next_attr(doc,attr)) {
268         char *name2  = qs_get_attr_name(doc,attr);
269         if (STRCASEEQ('s','S',"selected",name2)) {
270           /*------------------------------------------------------------------*/
271           /* SELECTED Value Found                                             */
272           /*------------------------------------------------------------------*/
273           return qs_get_value_attr(doc, child, doc->buf.pool);
274         }
275       }
276     }
277
278     if ((result = qs_get_selected_value(doc, child, pool)) != NULL) {
279       return result;
280     }
281   }
282
283   /*--------------------------------------------------------------------------*/
284   /* not found                                                                */
285   /*--------------------------------------------------------------------------*/
286   return NULL;
287 }
288
289
290 /**
291  * The value of the NAME attribute is acquired.
292  *
293  * @param doc  [i] The pointer to the Doc structure at the output
294  *                 destination is specified.
295  * @param tag  [i] The tag node to want to acquire the SIZE attribute
296  *                 is specified.
297  * @param pool [i] To use POOL.
298  * @return The value of the NAME attribute is returned. NULL is
299  *         returned when not is.
300  */
301 char *
302 qs_get_name_attr(Doc *doc, Node *tag, apr_pool_t *pool)
303 {
304   Attr *attr;
305   for (attr = qs_get_attr(doc,tag); 
306        attr; 
307        attr = qs_get_next_attr(doc,attr)) {
308     char *name  = qs_get_attr_name(doc,attr);
309     char *value = qs_get_attr_value(doc,attr);
310     if (STRCASEEQ('n','N',"name",name)) {
311       return apr_pstrdup(pool, (value ? value : ""));
312     }
313   }
314   return NULL;
315 }
316
317
318 /**
319  * The value of the SIZE attribute is acquired.
320  *
321  * @param doc  [i] The pointer to the Doc structure at the output
322  *                 destination is specified.
323  * @param tag  [i] The tag node to want to acquire the SIZE attribute
324  *                 is specified.
325  * @param pool [i] To use POOL.
326  * @return The value of the SIZE attribute is returned. NULL is
327  *         returned when not is.
328  */
329 char *
330 qs_get_size_attr(Doc *doc, Node *tag, apr_pool_t *pool)
331 {
332   Attr *attr;
333   for (attr = qs_get_attr(doc,tag); 
334        attr; 
335        attr = qs_get_next_attr(doc,attr)) {
336     char *name  = qs_get_attr_name(doc,attr);
337     char *value = qs_get_attr_value(doc,attr);
338     if (STRCASEEQ('s','S',"size",name)) {
339       return apr_pstrdup(pool, (value ? value : ""));
340     }
341   }
342   return NULL;
343 }
344
345
346 /**
347  * The value of the ACCESSKEY attribute is acquired.
348  *
349  * @param doc  [i] The pointer to the Doc structure at the output
350  *                 destination is specified.
351  * @param tag  [i] The tag node to want to acquire the ACCESSKEY attribute
352  *                 is specified.
353  * @param pool [i] To use POOL.
354  * @return The value of the ACCESSKEY attribute is returned. NULL is
355  *         returned when not is.
356  */
357 char *
358 qs_get_accesskey_attr(Doc *doc, Node *tag, apr_pool_t *pool)
359 {
360   Attr *attr;
361   for (attr = qs_get_attr(doc,tag); 
362        attr; 
363        attr = qs_get_next_attr(doc,attr)) {
364     char *name  = qs_get_attr_name(doc,attr);
365     char *value = qs_get_attr_value(doc,attr);
366     if (STRCASEEQ('a','A',"accesskey",name)) {
367       return apr_pstrdup(pool, value);
368     }
369   }
370   return NULL;
371 }
372
373
374 /**
375  * The value of the ISTYLE attribute is acquired.
376  *
377  * @param doc  [i] The pointer to the Doc structure at the output
378  *                 destination is specified.
379  * @param tag  [i] The tag node to want to acquire the ISTYLE attribute
380  *                 is specified.
381  * @param pool [i] To use POOL.
382  * @return The value of the ISTYLE attribute is returned. NULL is
383  *         returned when not is.
384  */
385 char *
386 qs_get_istyle_attr(Doc *doc, Node *tag, apr_pool_t *pool)
387 {
388   Attr *attr;
389   for (attr = qs_get_attr(doc,tag); 
390        attr != NULL; 
391        attr = qs_get_next_attr(doc,attr)) {
392     char *name  = qs_get_attr_name(doc,attr);
393     char *value = qs_get_attr_value(doc,attr);
394     if (STRCASEEQ('i','I',"istyle",name)) {
395       return apr_pstrdup(pool, value);
396     }
397   }
398   return NULL;
399 }
400
401
402 /**
403  * The value of the MAXLENGTH attribute is acquired from the tag node of the
404  * object.
405  *
406  * @param doc  [i] The pointer to the Doc structure at the output
407  *                 destination is specified.
408  * @param tag  [i] The tag node to want to acquire the MAXLENGTH attribute
409  *                 is specified.
410  * @param pool [i] To use POOL.
411  * @return The value of the MAXLENGTH attribute is returned. NULL is
412  *         returned when not is.
413  */
414 char *
415 qs_get_maxlength_attr(Doc *doc, Node *tag, apr_pool_t *pool)
416 {
417   Attr *attr;
418   for (attr = qs_get_attr(doc,tag);
419        attr; 
420        attr = qs_get_next_attr(doc,attr)) {
421     char *name  = qs_get_attr_name(doc,attr);
422     char *value = qs_get_attr_value(doc,attr);
423     if (STRCASEEQ('m','M',"maxlength",name)) {
424       return apr_pstrdup(pool, value);
425     }
426   }
427   return NULL;
428 }
429
430
431 /**
432  * It is scanned whether the CHECKBOX tag of the object is CHECKED. 
433  *
434  * @param doc  [i] The pointer to the Doc structure at the output
435  *                 destination is specified.
436  * @param tag  [i] The tag node to want to acquire the CHECKBOX attribute
437  *                 is specified.
438  * @param pool [i] To use POOL.
439  * @return 1 is returned when it is CHECKED and, additionally, 0 is returned. 
440  */
441 int
442 <<<<<<< HEAD:src/chxj_tag_util.c
443 qs_is_checked_checkbox_attr(Doc* doc, Node* tag, request_rec* UNUSED(r))
444 =======
445 qs_is_checked_checkbox_attr(Doc *doc, Node *tag, apr_pool_t *UNUSED(pool))
446 >>>>>>>   * updated new trunk.:src/chxj_tag_util.c
447 {
448   Attr *attr;
449   for (attr = qs_get_attr(doc,tag);
450        attr; 
451        attr = qs_get_next_attr(doc,attr)) {
452     char *name  = qs_get_attr_name(doc,attr);
453     if (STRCASEEQ('c','C',"checked",name)) {
454       return 1;
455     }
456   }
457   return 0;
458 }
459
460
461 int
462 chxj_chxjif_is_mine(device_table *spec, Doc *doc, Node *tag)
463 {
464   request_rec *r = doc->r;
465   Attr        *attr;
466
467   for (attr = qs_get_attr(doc,tag);
468        attr; 
469        attr = qs_get_next_attr(doc,attr)) {
470     char *name  = qs_get_attr_name(doc,attr);
471     char *value = qs_get_attr_value(doc,attr);
472     if ((*name == 'l' || *name == 'L') && strcasecmp(name, "lang") == 0) {
473
474       DBG(r, "lang found [%s] spec [%d]", value, spec->html_spec_type);
475
476       if (STRCASEEQ('x','X',"xhtml",value)) {
477         if (spec->html_spec_type == CHXJ_SPEC_XHtml_Mobile_1_0) {
478           /* Yes , it is mine */
479           return 1;
480         }
481       }
482       else if (STRCASEEQ('h','H',"hdml",value)) {
483         if (spec->html_spec_type == CHXJ_SPEC_Hdml) {
484           /* Yes , it is mine */
485           return 1;
486         }
487       }
488       else if (STRCASEEQ('j','J',"jhtml",value)) {
489         if (spec->html_spec_type == CHXJ_SPEC_Jhtml) {
490           /* Yes , it is mine */
491           return 1;
492         }
493       }
494       else if (STRCASEEQ('j','J',"jxhtml",value)) {
495         if (spec->html_spec_type == CHXJ_SPEC_Jxhtml) {
496           /* Yes , it is mine */
497           return 1;
498         }
499       }
500       else if (STRCASEEQ('c','C',"chtml",value)) {
501         switch (spec->html_spec_type) {
502         case CHXJ_SPEC_Chtml_1_0:
503         case CHXJ_SPEC_Chtml_2_0:
504         case CHXJ_SPEC_Chtml_3_0:
505         case CHXJ_SPEC_Chtml_4_0:
506         case CHXJ_SPEC_Chtml_5_0:
507           return 1;
508         default:
509           break;
510         }
511       }
512       else if (STRCASEEQ('c','C',"cxhtml",value)) {
513         switch (spec->html_spec_type) {
514         case CHXJ_SPEC_Chtml_6_0:
515         case CHXJ_SPEC_Chtml_7_0:
516           return 1;
517         default:
518           break;
519         }
520       }
521     }
522   }
523
524   /* No, it is not mine. */
525   return 0;
526 }
527
528
529 /**
530  * The value of the DESTLANG attribute is acquired from the tag node of the
531  * object.
532  *
533  * @param doc  [i] The pointer to the Doc structure at the output
534  *                 destination is specified.
535  * @param tag  [i] The tag node to want to acquire the DESTLANG attribute
536  *                 is specified.
537  * @param pool [i] To use POOL.
538  * @return The value of the DESTLANG attribute is returned. NULL is
539  *         returned when not is.
540  */
541 char *
542 qs_get_destlang_attr(Doc *doc, Node *tag, apr_pool_t *pool)
543 {
544   Attr  *attr;
545   for (attr = qs_get_attr(doc,tag);
546        attr; 
547        attr = qs_get_next_attr(doc,attr)) {
548     char *name  = qs_get_attr_name(doc,attr);
549     char *value = qs_get_attr_value(doc,attr);
550     if (STRCASEEQ('d','D',"destlang",name)) {
551       return apr_pstrdup(pool, value);
552     }
553   }
554
555   return NULL;
556 }
557
558
559 /**
560  * The value of the PARSE attribute is acquired.
561  *
562  * @param doc  [i] The pointer to the Doc structure to be scanned is
563  *                 specified.
564  * @param tag  [i] The tag node to be scanned is specified.
565  * @param pool [i] To use POOL.
566  * @return The value of the PARSE attribute is returned. NULL is returned when
567  *         not found.
568  */
569 char *
570 qs_get_parse_attr(Doc *doc, Node *tag, apr_pool_t *pool)
571 {
572   Attr *attr;
573   /*--------------------------------------------------------------------------*/
574   /* The object tag node is scanned.                                          */
575   /*--------------------------------------------------------------------------*/
576   for (attr = qs_get_attr(doc,tag);
577        attr;
578        attr = qs_get_next_attr(doc,attr)) {
579     char *name  = qs_get_attr_name(doc,attr);
580     char *value = qs_get_attr_value(doc,attr);
581     if (STRCASEEQ('p','P',"parse",name)) {
582       /*----------------------------------------------------------------------*/
583       /* The VALUE attribute was found.                                       */
584       /*----------------------------------------------------------------------*/
585       return apr_pstrdup(pool, value);
586     }
587   }
588
589   /*--------------------------------------------------------------------------*/
590   /* not found                                                                */
591   /*--------------------------------------------------------------------------*/
592   return NULL;
593 }
594
595 /*
596  * vim:ts=2 et
597  */