1 /*
2 Copyright (c) 2008-2013 uim Project https://github.com/uim/uim
3
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name of authors nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 SUCH DAMAGE.
30
31 */
32
33 #include <config.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <expat.h>
38
39 #include "uim.h"
40 #include "uim-scm.h"
41 #include "uim-scm-abbrev.h"
42 #include "uim-helper.h"
43 #include "uim-notify.h"
44 #include "dynlib.h"
45
46 typedef struct {
47 uim_lisp start_;
48 uim_lisp end_;
49 uim_lisp characterdata_;
50 } uim_xml_userdata;
51
52 typedef struct {
53 XML_Parser parser;
54 uim_xml_userdata *data;
55 } uim_xml_ctx;
56
57 static uim_lisp
xml_start_element_handler_internal(const XML_Char * atts[])58 xml_start_element_handler_internal(const XML_Char *atts[])
59 {
60 uim_lisp atts_ = uim_scm_null();
61
62 while (*atts != '\0') {
63 atts_ = CONS(CONS(MAKE_STR(*atts), MAKE_STR(*(atts + 1))), atts_);
64 atts += 2;
65 }
66 return atts_;
67 }
68
69 static void
xml_start_element_handler(void * userData,const XML_Char * name,const XML_Char * atts[])70 xml_start_element_handler(void *userData, const XML_Char *name, const XML_Char *atts[])
71 {
72 uim_xml_userdata *data = (uim_xml_userdata *)userData;
73
74 if (data && data->start_) {
75 uim_lisp atts_;
76
77 atts_ = (uim_lisp)uim_scm_call_with_gc_ready_stack((uim_gc_gate_func_ptr)xml_start_element_handler_internal,
78 (void *)atts);
79
80 atts_ = uim_scm_callf("reverse", "o", atts_);
81
82 uim_scm_call(data->start_, LIST2(MAKE_STR(name), atts_));
83 }
84 }
85
86 static void
xml_end_element_handler(void * userData,const XML_Char * name)87 xml_end_element_handler(void *userData, const XML_Char *name)
88 {
89 uim_xml_userdata *data = (uim_xml_userdata *)userData;
90
91 if (data && data->end_) {
92 uim_scm_call(data->end_, LIST1(MAKE_STR(name)));
93 }
94 }
95
96 static void
xml_characterdata_handler(void * userData,const XML_Char * s,int len)97 xml_characterdata_handler(void *userData, const XML_Char *s, int len)
98 {
99 uim_xml_userdata *data = (uim_xml_userdata *)userData;
100
101 char *str = uim_malloc(len + 1);
102
103 memcpy(str, s, len);
104 str[len] = '\0';
105
106 if (data && data->characterdata_) {
107 uim_scm_call(data->characterdata_, LIST1(MAKE_STR(str)));
108 }
109
110 free(str);
111 }
112
113 static uim_lisp
uim_xml_parser_create(uim_lisp encoding_)114 uim_xml_parser_create(uim_lisp encoding_)
115 {
116 uim_xml_ctx *ctx;
117 const XML_Char *encoding = REFER_C_STR(encoding_);
118 XML_Parser parser;
119
120 parser = XML_ParserCreate(encoding);
121
122 if (parser) {
123 XML_SetElementHandler(parser, xml_start_element_handler, xml_end_element_handler);
124 XML_SetCharacterDataHandler(parser, xml_characterdata_handler);
125 }
126
127 ctx = uim_calloc(1, sizeof(uim_xml_ctx *));
128 ctx->parser = parser;
129
130 ctx->data = uim_malloc(sizeof(uim_xml_userdata *));
131 ctx->data->start_ = NULL;
132 ctx->data->end_ = NULL;
133 ctx->data->characterdata_ = NULL;
134
135 return MAKE_PTR(ctx);
136 }
137
138 static uim_lisp
uim_xml_parser_free(uim_lisp ctx_)139 uim_xml_parser_free(uim_lisp ctx_)
140 {
141 uim_xml_ctx *ctx = C_PTR(ctx_);
142
143 free(ctx->data);
144 XML_ParserFree(ctx->parser);
145 free(ctx);
146
147 return uim_scm_t();
148 }
149
150 static uim_lisp
uim_xml_set_element_handler(uim_lisp ctx_,uim_lisp element_start_,uim_lisp element_end_)151 uim_xml_set_element_handler(uim_lisp ctx_, uim_lisp element_start_, uim_lisp element_end_)
152 {
153 uim_xml_ctx *ctx = C_PTR(ctx_);
154
155 ctx->data->start_ = element_start_;
156 ctx->data->end_ = element_end_;
157
158 return uim_scm_t();
159 }
160
161 static uim_lisp
uim_xml_set_characterdata_handler(uim_lisp ctx_,uim_lisp element_characterdata_)162 uim_xml_set_characterdata_handler(uim_lisp ctx_, uim_lisp element_characterdata_)
163 {
164 uim_xml_ctx *ctx = C_PTR(ctx_);
165
166 ctx->data->characterdata_ = element_characterdata_;
167
168 return uim_scm_t();
169 }
170
171 struct uim_xml_parse_args {
172 uim_lisp ctx_;
173 uim_lisp s_;
174 uim_lisp isFinal_;
175 };
176
177 static void *uim_xml_parse_internal(struct uim_xml_parse_args *);
178
179 static uim_lisp
uim_xml_parse(uim_lisp ctx_,uim_lisp s_,uim_lisp isFinal_)180 uim_xml_parse(uim_lisp ctx_, uim_lisp s_, uim_lisp isFinal_)
181 {
182 struct uim_xml_parse_args args;
183
184 args.ctx_ = ctx_;
185 args.s_ = s_;
186 args.isFinal_ = isFinal_;
187
188 return uim_scm_call_with_gc_ready_stack((uim_gc_gate_func_ptr)uim_xml_parse_internal, (void *)&args);
189 }
190
191 static void *
uim_xml_parse_internal(struct uim_xml_parse_args * args)192 uim_xml_parse_internal(struct uim_xml_parse_args *args)
193 {
194 uim_xml_ctx *ctx = C_PTR(args->ctx_);
195 const XML_Char *s = REFER_C_STR(args->s_);
196 int isFinal = C_INT(args->isFinal_);
197
198 XML_SetUserData(ctx->parser, ctx->data);
199 return MAKE_INT(XML_Parse(ctx->parser, s, strlen(s), isFinal));
200 }
201
202 void
uim_plugin_instance_init(void)203 uim_plugin_instance_init(void)
204 {
205 uim_scm_init_proc1("xml-parser-create", uim_xml_parser_create);
206 uim_scm_init_proc1("xml-parser-free", uim_xml_parser_free);
207 uim_scm_init_proc3("xml-element-handler-set!", uim_xml_set_element_handler);
208 uim_scm_init_proc2("xml-characterdata-handler-set!", uim_xml_set_characterdata_handler);
209 uim_scm_init_proc3("xml-parse", uim_xml_parse);
210 }
211
212 void
uim_plugin_instance_quit(void)213 uim_plugin_instance_quit(void)
214 {
215 }
216