2 * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3 * Copyright (C) 2005 QSDN,Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "chxj_apache.h"
22 #include "qs_parse_string.h"
23 #include "qs_parse_tag.h"
30 #define NL_COUNT_MAX (10)
32 typedef struct node_stack_element {
34 struct node_stack_element *next;
35 struct node_stack_element **ref;
38 typedef struct node_stack {
39 NodeStackElement head;
40 NodeStackElement tail;
43 static int s_cut_tag (const char* s, int len);
44 static int s_cut_text(const char* s, int len, int script);
45 static void qs_dump_node(Doc* doc, Node* node, int indent);
46 static void qs_push_node(Doc* doc, Node *node, NodeStack stack);
47 static Node *qs_pop_node(Doc* doc, NodeStack stack);
48 #ifdef DUMP_NODE_STACK
49 static void qs_dump_node_stack(Doc *doc, NodeStack stack);
51 static void qs_free_node_stack(Doc *doc, NodeStack stack);
52 static void s_error_check(Doc *doc, Node *node, NodeStack node_stack, NodeStack err_stack);
56 qs_parse_string(Doc *doc, const char *src, int srclen)
73 memset(encoding, 0, 256);
75 doc->now_parent_node = qs_init_root_node(doc);
76 if (! src || srclen <= 0) {
77 return doc->root_node;
80 node_stack = apr_palloc(doc->r->pool, sizeof(struct node_stack));
81 memset(node_stack, 0, sizeof(struct node_stack));
82 err_stack = apr_palloc(doc->r->pool, sizeof(struct node_stack));
83 memset(err_stack, 0, sizeof(struct node_stack));
86 node_stack = calloc(sizeof(struct node_stack), 1);
87 err_stack = calloc(sizeof(struct node_stack), 1);
91 * It is the pre reading.
92 * Because I want to specify encoding.
94 for (ii=0; ii<srclen; ii++) {
95 if (src[ii] == '\n') nl_cnt++;
96 if (nl_cnt >= NL_COUNT_MAX) break; /* not found <?xml ...> */
98 if (is_white_space(src[ii]))
101 if ((unsigned char)'<' == src[ii]) {
102 int endpoint = s_cut_tag(&src[ii], srclen - ii);
104 node = qs_parse_tag(doc, &src[ii], endpoint);
107 if (node->name[0] != '?') break;
109 if (strcasecmp(node->name, "?xml") == 0) {
111 for(parse_attr = node->attr;
112 parse_attr && *encoding == '\0';
113 parse_attr = parse_attr->next) {
114 if ((*parse_attr->name == 'e' || *parse_attr->name == 'E')
115 && strcasecmp(parse_attr->name, "encoding") == 0) {
116 switch (*parse_attr->value) {
119 if (strcasecmp(parse_attr->value, "x-sjis" ) == 0) {
120 strcpy((char*)encoding, (char*)"NONE");
126 if ((strcasecmp(parse_attr->value, "Shift_JIS") == 0)
127 || (strcasecmp(parse_attr->value, "SJIS" ) == 0)
128 || (strcasecmp(parse_attr->value, "Shift-JIS") == 0)) {
129 strcpy((char*)encoding, (char*)"NONE");
135 if ((strcasecmp(parse_attr->value, "EUC_JP") == 0)
136 || (strcasecmp(parse_attr->value, "EUC-JP") == 0)
137 || (strcasecmp(parse_attr->value, "EUCJP" ) == 0)) {
138 strcpy((char*)encoding, "EUC-JP");
144 if ((strcasecmp(parse_attr->value, "UTF-8") == 0)
145 || (strcasecmp(parse_attr->value, "UTF8") == 0)) {
146 strcpy((char*)encoding, "UTF-8");
151 strcpy((char*)encoding, "NONE");
162 if (strcasecmp(encoding, "NONE") != 0 && strlen(encoding) != 0) {
164 olen = srclen * 4 + 1;
165 sv_osrc = osrc =(char*)apr_palloc(doc->pool, olen);
166 memset((char*)osrc, 0, olen);
167 if ((cd = iconv_open("CP932", encoding)) != (iconv_t) -1) {
169 ibuf = apr_palloc(doc->pool, ilen+1);
170 memset(ibuf, 0, ilen+1);
171 memcpy(ibuf, src, ilen);
173 size_t result = iconv(cd, &ibuf, &ilen, &osrc, &olen);
174 if (result == (size_t)(-1)) {
185 * Now, true parsing is done here.
188 for (ii=0; ii<srclen; ii++) {
189 if (src[ii] == '\n') nl_cnt++;
190 if (doc->parse_mode != PARSE_MODE_NO_PARSE
191 && is_white_space(src[ii])
192 && (doc->now_parent_node == NULL || !STRCASEEQ('p','P',"pre",doc->now_parent_node->name))) {
195 if ((unsigned char)'<' == src[ii]) {
196 int endpoint = s_cut_tag(&src[ii], srclen - ii);
198 node = qs_parse_tag(doc, &src[ii], endpoint);
202 if (node->name[0] == '/' ) {
203 if (doc->parse_mode == PARSE_MODE_CHTML) {
204 if (has_child(&(node->name[1]))) {
205 if (doc->now_parent_node->parent != NULL) {
206 doc->now_parent_node = doc->now_parent_node->parent;
207 doc->parse_mode = PARSE_MODE_CHTML;
209 if (STRCASEEQ('s','S',"script",&node->name[1])) {
212 s_error_check(doc, node, node_stack, err_stack);
219 if (doc->parse_mode == PARSE_MODE_NO_PARSE) {
220 if (STRCASEEQ('c','C',"chxj:if",&node->name[1]) || STRCASEEQ('p','P',"plaintext",&node->name[1])) {
221 if (doc->now_parent_node->parent != NULL) {
222 doc->now_parent_node = doc->now_parent_node->parent;
223 doc->parse_mode = PARSE_MODE_CHTML;
224 s_error_check(doc, node, node_stack, err_stack);
229 if (doc->parse_mode != PARSE_MODE_NO_PARSE)
233 if (*node->name == '!' && strncmp(node->name, "!--", 3) == 0) {
237 qs_add_child_node(doc,node);
238 if ((has_child(node->name) && doc->parse_mode != PARSE_MODE_NO_PARSE) || STRCASEEQ('p','P',"plaintext",node->name)) {
239 qs_push_node(doc, node, node_stack);
242 if (doc->parse_mode == PARSE_MODE_NO_PARSE) {
243 if (node->name[0] == '/')
247 if (doc->parse_mode == PARSE_MODE_CHTML && STRCASEEQ('c','C',"chxj:if", node->name)) {
249 doc->parse_mode = PARSE_MODE_NO_PARSE;
250 doc->now_parent_node = node;
251 for(parse_attr = node->attr;
253 parse_attr = parse_attr->next) {
254 if (STRCASEEQ('p','P',"parse",parse_attr->name)) {
255 if (STRCASEEQ('t','T',"true",parse_attr->value)) {
256 doc->parse_mode = PARSE_MODE_CHTML;
261 else if (doc->parse_mode == PARSE_MODE_CHTML && STRCASEEQ('p','P',"plaintext",node->name)) {
262 doc->parse_mode = PARSE_MODE_NO_PARSE;
263 doc->now_parent_node = node;
266 if (doc->parse_mode == PARSE_MODE_CHTML && has_child(node->name)) {
267 doc->now_parent_node = node;
269 if (STRCASEEQ('s','S',"script", node->name)) {
275 int endpoint = s_cut_text(&src[ii], srclen - ii, script_flag);
276 Node* node = qs_new_tag(doc);
277 node->value = (char*)apr_palloc(doc->pool,endpoint+1);
278 node->name = (char*)apr_palloc(doc->pool,4+1);
279 node->otext = (char*)apr_palloc(doc->pool,endpoint+1);
280 node->size = endpoint;
282 memset(node->value, 0, endpoint+1);
283 memset(node->otext, 0, endpoint+1);
284 memset(node->name, 0, 4+1 );
285 memcpy(node->value, &src[ii], endpoint);
286 memcpy(node->name, "text", 4);
287 memcpy(node->otext,node->value, endpoint);
289 qs_add_child_node(doc,node);
290 ii += (endpoint - 1);
294 QX_LOGGER_DEBUG("parse_string end");
297 if (doc->r != NULL) {
298 qs_dump_node(doc, doc->root_node, 0);
301 #ifdef DUMP_NODE_STACK
302 qs_dump_node_stack(doc, node_stack);
306 for (prevNode = qs_pop_node(doc,node_stack);
308 prevNode = qs_pop_node(doc, node_stack)) {
309 if (has_child(prevNode->name)) {
311 ERR(doc->r, "tag parse error (perhaps, not close). tag_name:[%s] line:[%d]", prevNode->name, prevNode->line);
313 fprintf(stderr, "error :tag parse error (perhaps, not close). tag_name:[%s] line:[%d]\n", prevNode->name, prevNode->line);
317 qs_free_node_stack(doc, node_stack); node_stack = NULL;
318 qs_free_node_stack(doc, err_stack); err_stack = NULL;
319 return doc->root_node;
324 s_error_check(Doc *doc, Node *node, NodeStack node_stack, NodeStack err_stack)
328 for (prevNode = qs_pop_node(doc,node_stack);
330 prevNode = qs_pop_node(doc, node_stack)) {
331 if (prevNode && strcasecmp(prevNode->name, &node->name[1]) != 0) {
332 qs_push_node(doc, prevNode, err_stack);
339 Node *tmpNode = qs_pop_node(doc,node_stack);
340 if (tmpNode == NULL && err != 1) {
342 ERR(doc->r, "tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]", &node->name[1], node->line);
344 fprintf(stderr, "error :tag parse error (perhaps, miss spell). tag_name:[%s] line:[%d]\n", &node->name[1], node->line);
345 for (prevNode = qs_pop_node(doc,err_stack);
347 prevNode = qs_pop_node(doc, err_stack)) {
348 qs_push_node(doc, prevNode, node_stack);
352 for (prevNode = qs_pop_node(doc,err_stack);
354 prevNode = qs_pop_node(doc, err_stack)) {
356 ERR(doc->r, "tag parse error (perhaps, not close). tag_name:[%s] line:[%d]", prevNode->name, prevNode->line);
358 fprintf(stderr, "error :tag parse error (perhaps, not close). tag_name:[%s] line:[%d]\n", prevNode->name, prevNode->line);
360 qs_push_node(doc, tmpNode, node_stack);
368 qs_dump_node(Doc* doc, Node* node, int indent)
370 Node* p = (Node*)qs_get_child_node(doc,node);
372 for (;p;p = (Node*)qs_get_next_node(doc,p)) {
374 if ((char*)qs_get_node_value(doc,p) != NULL) {
375 DBG(doc->r,"%*.*sNode:[%s][%s]\n", indent,indent," ",
376 (char*)qs_get_node_name(doc,p),
377 (char*)qs_get_node_value(doc,p));
380 DBG(doc->r,"%*.*sNode:[%s]\n", indent,indent," ", qs_get_node_name(doc,p));
382 for (attr = (Attr*)qs_get_attr(doc,p); attr; attr = (Attr*)qs_get_next_attr(doc,attr)) {
383 DBG(doc->r,"%*.*s ATTR:[%s]\n", indent,indent," ", (char *)qs_get_attr_name(doc,attr));
384 DBG(doc->r,"%*.*s VAL :[%s]\n", indent,indent," ", (char *)qs_get_attr_value(doc,attr));
386 qs_dump_node(doc,p, indent+4);
393 s_cut_tag(const char* s, int len)
400 if (strncmp("<!--", s, 4) == 0) {
403 else if (strncasecmp("<![CDATA[", s, sizeof("<![CDATA[")-1) == 0) {
407 for (ii=0;ii<len; ii++) {
408 if (is_sjis_kanji(s[ii])) {
412 if (is_sjis_kana(s[ii]))
415 if (is_white_space(s[ii]))
422 if (comment && s[ii] == '-') {
423 if (strncmp(&s[ii], "-->", 3) == 0) {
428 if (cdata && s[ii] == ']') {
429 if (strncmp(&s[ii], "]]>", 3) == 0) {
435 if (!cdata && !comment && s[ii] == '>') {
446 s_cut_text(const char* s, int len, int script)
452 for (ii=0;ii<len; ii++) {
453 if (is_sjis_kanji(s[ii])) {
457 if (is_sjis_kana(s[ii]))
460 if (is_white_space(s[ii]))
469 if (s[ii] == '\'' && !dq) {
474 if (s[ii] == '"' && !sq) {
480 if (!sq && !dq && s[ii] == '<')
490 qs_init_root_node(Doc* doc)
492 doc->root_node = (Node*)apr_palloc(doc->pool,sizeof(struct Node));
493 if (doc->root_node == NULL)
494 QX_LOGGER_FATAL("Out Of Memory");
496 doc->root_node->next = NULL;
497 doc->root_node->parent = NULL;
498 doc->root_node->child = NULL;
499 doc->root_node->attr = NULL;
501 doc->root_node->name = (char*)apr_palloc(doc->pool,5);
502 if (doc->root_node->name == NULL) {
503 QX_LOGGER_FATAL("Out Of Memory");
505 memset(doc->root_node->name, 0, 5);
506 strcpy(doc->root_node->name, "ROOT");
508 return doc->root_node;
514 qs_add_child_node(Doc *doc,Node *node)
518 node->child_tail = NULL;
519 node->parent = doc->now_parent_node;
520 if (doc->now_parent_node->child == NULL) {
521 doc->now_parent_node->child = node;
522 doc->now_parent_node->child_tail = node;
526 QX_LOGGER_DEBUG("search child free node");
528 doc->now_parent_node->child_tail->next = node;
529 doc->now_parent_node->child_tail = node;
537 qs_get_root(Doc *doc) {
538 return doc->root_node;
545 qs_get_node_value(Doc *UNUSED(doc), Node *node) {
553 qs_get_node_name(Doc *UNUSED(doc), Node *node) {
560 qs_get_child_node(Doc *UNUSED(doc), Node *node) {
568 qs_get_next_node(Doc* UNUSED(doc), Node *node) {
575 qs_get_attr(Doc *UNUSED(doc), Node *node) {
583 qs_get_next_attr(Doc *UNUSED(doc), Attr *attr) {
590 qs_get_attr_name(Doc *UNUSED(doc), Attr *attr) {
597 qs_get_attr_value(Doc *UNUSED(doc), Attr *attr) {
602 qs_get_node_size(Doc *UNUSED(doc), Node *node) {
607 #define list_insert(node, point) do { \
608 node->ref = point->ref; \
610 node->next = point; \
611 point->ref = &node->next; \
614 #define list_remove(node) do { \
615 *node->ref = node->next; \
616 node->next->ref = node->ref; \
621 qs_push_node(Doc* doc, Node *node, NodeStack stack)
623 NodeStackElement elem;
624 if (doc->r != NULL) {
625 elem = apr_palloc(doc->r->pool, sizeof(struct node_stack_element));
626 memset(elem, 0, sizeof(struct node_stack_element));
629 elem = malloc(sizeof(struct node_stack_element));
630 memset(elem, 0, sizeof(struct node_stack_element));
633 if (stack->head == NULL) {
635 if (doc->r != NULL) {
636 stack->head = apr_palloc(doc->r->pool, sizeof(struct node_stack_element));
637 memset(stack->head, 0, sizeof(struct node_stack_element));
640 stack->head = malloc(sizeof(struct node_stack_element));
641 memset(stack->head, 0, sizeof(struct node_stack_element));
643 stack->head->next = stack->head;
644 stack->head->ref = &stack->head->next;
646 list_insert(elem, stack->head);
650 #include "apr_ring.h"
653 qs_pop_node(Doc *doc, NodeStack stack)
655 NodeStackElement tail = stack->tail;
658 if (tail == NULL) return NULL;
659 if (tail == stack->head) return NULL;
664 stack->tail = (NodeStackElement)((apr_size_t)stack->head->ref - (apr_size_t)APR_OFFSETOF(struct node_stack_element, next));
671 #ifdef DUMP_NODE_STACK
673 qs_dump_node_stack(Doc *doc, NodeStack stack)
675 NodeStackElement elm;
676 for (elm = stack->head->next;elm != stack->head; elm = elm->next) {
677 if (doc->r) DBG(doc->r, "name:[%s]", elm->node->name);
678 else fprintf(stderr, "[%x] name:[%s] next:[%x]\n", (apr_size_t)elm, elm->node->name, (apr_size_t)elm->next);
684 qs_free_node_stack(Doc *doc, NodeStack stack)
686 if (doc->r == NULL && stack != NULL) {
688 for (elm = qs_pop_node(doc, stack);elm; elm = qs_pop_node(doc,stack))