OSDN Git Service

* changed feature.
[modchxj/mod_chxj.git] / src / qs_parse_tag.c
1 /*
2  * Copyright (C) 2005-2011 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   if (! doc) {
38     QX_LOGGER_FATAL("runtime exception: qs_parse_tag(): doc is null");
39     return NULL;
40   }
41
42   sv_s = sp = (char *)s;
43   ll         = len;
44   next_point = 0;
45
46   QX_LOGGER_DEBUG("start parse_tag()");
47
48
49   /* 
50    * s[0] == '<' && s[len] == '>' 
51    */
52   if (sv_s[len-1] == '/'){
53         ll -= 2;
54         tag_name = (char *)s_get_tag_name(doc, ++s, ll);
55   }
56   else{
57         tag_name = (char *)s_get_tag_name(doc, ++s, --ll);
58   }
59
60   node = (Node *)qs_new_tag(doc);
61   if (! node) {
62     QX_LOGGER_DEBUG("runtime exception: qs_parse_tag(): Out of memory.");
63     return NULL;
64   }
65   node->name = tag_name;
66   node->otext = apr_palloc(doc->pool,len+2);
67   memset(node->otext, 0, len+2);
68   memcpy(node->otext, sp, len+1);
69
70   QX_LOGGER_DEBUG(tag_name);
71
72   ll -= (strlen(tag_name));
73   QX_LOGGER_DEBUG_INT("ll",ll);
74   sp += (strlen(tag_name)+1);
75   for (;;) {
76     Attr *attr = qs_parse_attr(doc,sp, ll, &next_point);
77     if (attr == NULL) {
78       QX_LOGGER_DEBUG("End of QS_PARSE_ATTR()");
79       break;
80     }
81     QX_LOGGER_DEBUG(attr->name);
82     QX_LOGGER_DEBUG(attr->value);
83     sp += next_point;
84     ll -= next_point;
85     QX_LOGGER_DEBUG_INT(sp, ll);
86     node = (Node *)qs_add_attr(doc,node, attr);
87   }
88
89   if (sv_s[len-1] == '/') {
90     node->closed_by_itself = 1;
91   }
92   else {
93     node->closed_by_itself = 0;
94   }
95   QX_LOGGER_DEBUG("end parse_tag()");
96
97   return node;
98 }
99
100
101
102
103 static char * 
104 s_get_tag_name(Doc *doc, const char *s, int len)  
105 {
106   int ii;
107   int sp;
108   int size;
109   char *return_value = NULL;
110
111   /* ignore space. */
112   for (ii = 0; ii < len; ii++) {
113     if (is_white_space(s[ii])) 
114       continue;
115     break;
116   }
117
118   sp = ii;
119   for (;ii<len; ii++) {
120     if (is_white_space(s[ii]))
121       break;
122   }
123
124   size = ii-sp;
125
126   return_value = (char *)apr_palloc(doc->pool, size+1);
127
128   memset(return_value, 0, size+1);
129   memcpy(return_value, &s[sp], size);
130
131   QX_LOGGER_DEBUG(return_value);
132   return return_value;
133 }
134
135
136
137 Node *
138 qs_new_tag(Doc *doc) 
139 {
140   Node *node;
141
142   if (! doc) {
143     QX_LOGGER_FATAL("runtime exception: qs_new_tag(): doc is NULL");
144     return NULL;
145   }
146   if (! doc->pool) {
147     QX_LOGGER_FATAL("runtime exception: qs_new_tag(): doc->pool is NULL");
148     return NULL;
149   }
150
151   node = (Node *)apr_palloc(doc->pool, sizeof(Node));
152   node->next      = NULL;
153   node->parent    = NULL;
154   node->child     = NULL;
155   node->child_tail= NULL;
156   node->attr      = NULL;
157   node->attr_tail = NULL;
158   node->name      = NULL;
159   node->value     = NULL;
160
161   return node;
162 }
163
164
165
166
167 Node *
168 qs_add_attr(Doc *doc, Node *node, Attr *attr) 
169 {
170   if (node == NULL) {
171     QX_LOGGER_FATAL("runtime exception: qs_add_attr(): node is null");
172     return NULL;
173   }
174   if (! attr) {
175     return node;
176   }
177
178   attr->parent = node;
179   attr->next   = NULL;
180
181   if (node->attr == NULL) {
182     node->attr      = attr;
183     node->attr_tail = attr;
184
185     return node;
186   }
187
188   node->attr_tail->next = attr;
189   node->attr_tail       = attr;
190
191   return node;
192 }
193 /*
194  * vim:ts=2 et
195  */