OSDN Git Service

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