File: xmlhash.c

package info (click to toggle)
ruby-xmlhash 1.3.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 140 kB
  • sloc: ansic: 205; ruby: 186; makefile: 6
file content (248 lines) | stat: -rw-r--r-- 6,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <assert.h>

/* libxml headers first - see https://github.com/coolo/xmlhash/issues/1 */
#include <libxml/parser.h>
#include <libxml/xmlreader.h>

#include <ruby.h>
#ifdef HAVE_RUBY_ST_H
# include <ruby/st.h>
#else
# include <st.h>
#endif

/* API_VERSION_CODE is only defined in those we want */
#ifdef HAVE_RUBY_ENCODING_H
# include <ruby/encoding.h>
#endif

static VALUE m_current = Qnil;
static VALUE m_stack = Qnil;
static VALUE m_cstring = Qnil;
static VALUE m_result = Qnil;
static VALUE m_xmlClass = Qnil;
#ifdef HAVE_RUBY_ENCODING_H
static rb_encoding *m_current_encoding = NULL;
#endif

void init_XmlhashParserData()
{
  m_current = Qnil;
  rb_ary_clear(m_stack);
  rb_ary_clear(m_cstring);
}

void xml_hash_start_element(const xmlChar *name)
{
  VALUE pair;
  /* needed for further attributes */
  m_current = rb_class_new_instance(0, 0, m_xmlClass);
  pair = rb_ary_new();
  rb_ary_push(pair, rb_str_new2((const char*)name));
  rb_ary_push(pair, m_current);
  rb_ary_push(m_stack, pair);
  rb_ary_clear(m_cstring);
}

void xml_hash_end_element(const xmlChar *name)
{
  VALUE pair, cname, chash, phash, obj;

  assert(m_stack != Qnil);
  pair = rb_ary_pop(m_stack);
  assert(pair != Qnil);
  cname = rb_ary_entry(pair, 0);
  chash = rb_ary_entry(pair, 1);
  assert(!strcmp((const char*)name, RSTRING_PTR(cname)));

  if (rb_obj_is_kind_of(chash, rb_cHash)) {
    VALUE string;
    const char *string_ptr;
    long string_len;

    /* now check if the cstring array contains non-empty string */
    string = rb_ary_join(m_cstring, Qnil);
    string_ptr = RSTRING_PTR(string);
    string_len = RSTRING_LEN(string);
    while (string_len > 0 && (string_ptr[0] == ' ' || string_ptr[0] == '\t' || string_ptr[0] == '\n')) {
      string_ptr++;
      string_len--;
    }
    while (string_len > 0 && (string_ptr[string_len-1] == ' ' || string_ptr[string_len-1] == '\t' || string_ptr[string_len-1] == '\n')) {
      string_len--;
    }
    /* avoid overwriting empty hash with empty string */
    if (string_len > 0) {
      if (RHASH_SIZE(chash) == 0)
	chash = string;
      else {
	rb_hash_aset(chash, rb_str_new2("_content"), string);
      }
    }
  }
  rb_ary_clear(m_cstring);
  if (RARRAY_LEN(m_stack) == 0) {
    m_result = chash;
    return;
  }

  pair = rb_ary_entry(m_stack, RARRAY_LEN(m_stack)-1);
  phash = rb_ary_entry(pair, 1);

  obj = rb_hash_aref(phash, cname);
  if (obj != Qnil) {
    if (rb_obj_is_kind_of(obj, rb_cArray)) {
      rb_ary_push(obj, chash);
    } else {
      VALUE nobj = rb_ary_new();
      rb_ary_push(nobj, obj);
      rb_ary_push(nobj, chash);
      rb_hash_aset(phash, cname, nobj);
    }
  } else {
    /* implement force_array */
    rb_hash_aset(phash, cname, chash);
  }
}

void xml_hash_add_attribute(const xmlChar *name, const xmlChar *value)
{
#ifdef HAVE_RUBY_ENCODING_H
  VALUE v_name, v_value;
#endif

  assert(m_current != Qnil);
#ifdef HAVE_RUBY_ENCODING_H
  v_name = rb_external_str_new_with_enc((const char*)name, xmlStrlen(name), m_current_encoding);
  v_value = rb_external_str_new_with_enc((const char*)value, xmlStrlen(value), m_current_encoding);
  rb_hash_aset(m_current, v_name, v_value);
#else
  rb_hash_aset(m_current, rb_str_new2((const char*)name), rb_str_new2((const char*)value));
#endif
}

void xml_hash_add_text(const xmlChar *text)
{
#ifdef HAVE_RUBY_ENCODING_H
  VALUE str;
  str = rb_external_str_new_with_enc((const char*)text, xmlStrlen(text), m_current_encoding);
  rb_ary_push(m_cstring, str);
#else
  rb_ary_push(m_cstring, rb_str_new2((const char*)text));
#endif
}

void processAttribute(xmlTextReaderPtr reader) 
{
  const xmlChar *name = xmlTextReaderConstName(reader);
  assert(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ATTRIBUTE);
  xml_hash_add_attribute(name, xmlTextReaderConstValue(reader));
}

void processNode(xmlTextReaderPtr reader) 
{
  const xmlChar *name;
  const xmlChar *value;
  int nodetype;

  name = xmlTextReaderConstName(reader);
  value = xmlTextReaderConstValue(reader);

  nodetype = xmlTextReaderNodeType(reader);
  
  if (nodetype == XML_READER_TYPE_END_ELEMENT) {
    xml_hash_end_element(name);
    assert(value == NULL);
    return;
  }

  if (nodetype == XML_READER_TYPE_ELEMENT) {
    xml_hash_start_element(name);
    assert(value == NULL);

    if (xmlTextReaderMoveToFirstAttribute(reader) == 1)
      {
	processAttribute(reader);
	while (xmlTextReaderMoveToNextAttribute(reader) == 1)
	  processAttribute(reader);

	xmlTextReaderMoveToElement(reader);
      }

    if (xmlTextReaderIsEmptyElement(reader) == 1) {
      xml_hash_end_element(name);
    }
    return;
  }

  if (nodetype == XML_READER_TYPE_TEXT || 
      nodetype == XML_READER_TYPE_WHITESPACE || 
      nodetype == XML_READER_TYPE_SIGNIFICANT_WHITESPACE)
    {
      xml_hash_add_text(value);
      return;
    }

  printf("%d %s\n",
	 nodetype,
	 name
	 );

}

static VALUE parse_xml_hash(VALUE self, VALUE rb_xml)
{
  char *data;
  xmlTextReaderPtr reader;
  int ret;

  Check_Type(rb_xml, T_STRING);
#ifdef HAVE_RUBY_ENCODING_H
  m_current_encoding = rb_enc_get(rb_xml);
  if (m_current_encoding == rb_ascii8bit_encoding() || m_current_encoding == rb_usascii_encoding())
      m_current_encoding = rb_utf8_encoding();
#endif

  m_result = Qnil;

  data = (char*)calloc(RSTRING_LEN(rb_xml), sizeof(char));
  memcpy(data, StringValuePtr(rb_xml), RSTRING_LEN(rb_xml));

  reader = xmlReaderForMemory(data, RSTRING_LEN(rb_xml), 
			      NULL, NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING );
  init_XmlhashParserData();

  if (reader != NULL) {
    ret = xmlTextReaderRead(reader);
    while (ret == 1) {
      processNode(reader);
      ret = xmlTextReaderRead(reader);
    }
    xmlFreeTextReader(reader);
    if (ret != 0) {
      /* printf("%s : failed to parse\n", data); */
    }
  }

  free(data);
#ifdef HAVE_RUBY_ENCODING_H
  m_current_encoding = 0;
#endif
  return m_result;
}

void Init_xmlhash()
{
  VALUE mXmlhash;

  LIBXML_TEST_VERSION
  mXmlhash = rb_define_module("Xmlhash");
  m_xmlClass = rb_define_class_under(mXmlhash, "XMLHash", rb_cHash);
  rb_define_singleton_method(mXmlhash, "parse_int", &parse_xml_hash, 1);
  m_stack = rb_ary_new();
  rb_global_variable(&m_stack);
  m_cstring = rb_ary_new();
  rb_global_variable(&m_cstring);
  rb_global_variable(&m_result);
  rb_global_variable(&m_current);
}