OSDN Git Service

* Merge branch 'branch_0.10.0' into branch_0.11.0
[modchxj/mod_chxj.git] / src / qs_parse_tag.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 <stdio.h>
18 #include "qs_parse_string.h"
19 #include "qs_log.h"
20 #include "qs_parse_attr.h"
21 #include "qs_parse_tag.h"
22
23 static char *s_get_tag_name(Doc *doc, const char *s, int len) ; 
24
25
26
27 Node *
28 qs_parse_tag(Doc *doc, const char *s, int len) 
29 {
30   Node   *node;
31   char   *tag_name;
32   char   *sp;
33   char   *sv_s;
34   int    ll;
35   int    next_point;
36
37   sv_s = sp = (char *)s;
38   ll         = len;
39   next_point = 0;
40
41   QX_LOGGER_DEBUG("start parse_tag()");
42
43
44   /* 
45    * s[0] == '<' && s[len] == '>' 
46    */
47   tag_name = (char *)s_get_tag_name(doc, ++s, --ll);
48
49   node = (Node*)qs_new_tag(doc);
50   node->name = tag_name;
51   node->otext = apr_palloc(doc->pool,len+2);
52   memset(node->otext, 0, len+2);
53   memcpy(node->otext, sp, len+1);
54
55   QX_LOGGER_DEBUG(tag_name);
56
57   ll -= (strlen(tag_name));
58   QX_LOGGER_DEBUG_INT("ll",ll);
59   sp += (strlen(tag_name)+1);
60   for (;;) {
61     Attr *attr = qs_parse_attr(doc,sp, ll, &next_point);
62     if (attr == NULL) {
63       QX_LOGGER_DEBUG("End of QS_PARSE_ATTR()");
64       break;
65     }
66     QX_LOGGER_DEBUG(attr->name);
67     QX_LOGGER_DEBUG(attr->value);
68     sp += next_point;
69     ll -= next_point;
70     QX_LOGGER_DEBUG_INT(sp, ll);
71     node = (Node*)qs_add_attr(doc,node, attr);
72   }
73
74   if (sv_s[len-1] == '/') {
75     node->closed_by_itself = 1;
76   }
77   else {
78     node->closed_by_itself = 0;
79   }
80   QX_LOGGER_DEBUG("end parse_tag()");
81
82   return node;
83 }
84
85
86
87
88 static char* 
89 s_get_tag_name(Doc* doc, const char* s, int len)  
90 {
91   int ii;
92   int sp;
93   int size;
94   char* return_value = NULL;
95
96   /* ignore space. */
97   for (ii = 0; ii < len; ii++) {
98     if (is_white_space(s[ii])) 
99       continue;
100     break;
101   }
102
103   sp = ii;
104   for (;ii<len; ii++) {
105     if (is_white_space(s[ii])) 
106       break;
107   }
108
109   size = ii-sp;
110
111   return_value = (char*)apr_palloc(doc->pool, size+1);
112
113   memset(return_value, 0, size+1);
114   memcpy(return_value, &s[sp], size);
115
116   QX_LOGGER_DEBUG(return_value);
117   return return_value;
118 }
119
120
121
122 Node*
123 qs_new_tag(Doc* doc) 
124 {
125   Node* node      = (Node*)apr_palloc(doc->pool, sizeof(Node));
126   node->next      = NULL;
127   node->parent    = NULL;
128   node->child     = NULL;
129   node->child_tail= NULL;
130   node->attr      = NULL;
131   node->attr_tail = NULL;
132   node->name      = NULL;
133   node->value     = NULL;
134
135   return node;
136 }
137
138
139
140
141 Node*
142 qs_add_attr(Doc* doc, Node* node, Attr* attr) 
143 {
144   if (node == NULL)
145     QX_LOGGER_FATAL("qs_add_attr() node is null");
146
147   attr->parent = node;
148   attr->next   = NULL;
149
150   if (node->attr == NULL) {
151     node->attr      = attr;
152     node->attr_tail = attr;
153
154     return node;
155   }
156
157   node->attr_tail->next = attr;
158   node->attr_tail       = attr;
159
160   return node;
161 }
162 /*
163  * vim:ts=2 et
164  */