OSDN Git Service

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