xmlhash-1.3.6/ 0000755 0000041 0000041 00000000000 12576040722 013222 5 ustar www-data www-data xmlhash-1.3.6/Rakefile 0000644 0000041 0000041 00000001023 12576040722 014663 0 ustar www-data www-data # -*- ruby -*-
require 'rubygems'
require 'hoe'
require 'rake/extensiontask'
Hoe.spec 'xmlhash' do
developer('Stephan Kulow', 'coolo@suse.com')
self.readme_file = 'README.txt'
self.license('MIT')
self.spec_extras = { :extensions => ["ext/xmlhash/extconf.rb"] }
self.extra_dev_deps << ['rake-compiler', '>= 0']
self.extra_deps << ['pkg-config']
Rake::ExtensionTask.new('xmlhash', spec) do |ext|
ext.lib_dir = File.join('lib', 'xmlhash')
end
end
Rake::Task[:test].prerequisites << :compile
# vim: syntax=ruby
xmlhash-1.3.6/.gemtest 0000644 0000041 0000041 00000000000 12576040722 014661 0 ustar www-data www-data xmlhash-1.3.6/Manifest.txt 0000644 0000041 0000041 00000000234 12576040722 015530 0 ustar www-data www-data .autotest
.travis.yml
Gemfile
History.txt
Manifest.txt
README.txt
Rakefile
ext/xmlhash/extconf.rb
ext/xmlhash/xmlhash.c
lib/xmlhash.rb
test/test_xmlhash.rb
xmlhash-1.3.6/Gemfile 0000644 0000041 0000041 00000000130 12576040722 014507 0 ustar www-data www-data source 'http://rubygems.org'
gem 'hoe'
gem 'rake-compiler'
gem 'pkg-config'
gem 'json'
xmlhash-1.3.6/.autotest 0000644 0000041 0000041 00000000357 12576040722 015100 0 ustar www-data www-data # -*- ruby -*-
require 'autotest/restart'
Autotest.add_hook :initialize do |at|
at.add_mapping(/.*\.c/) do |f, _|
at.files_matching(/test_.*rb$/)
end
end
Autotest.add_hook :run_command do |at|
system "rake clean compile"
end
xmlhash-1.3.6/README.txt 0000644 0000041 0000041 00000003134 12576040722 014721 0 ustar www-data www-data = xmlhash
* https://github.com/coolo/xmlhash
== DESCRIPTION:
A small C module that wraps libxml2's xmlreader to parse a XML
string into a ruby hash
== FEATURES/PROBLEMS:
* only one function: parse(xml) -> hash
== SYNOPSIS:
ret = parse("")
assert_equal ret, { 'who' => 'world' }
== REQUIREMENTS:
* libxml2 >= 2.6
== INSTALL:
* sudo gem install
== DEVELOPERS:
After checking out the source, run:
$ rake newb
This task will install any missing dependencies, run the tests/specs,
and generate the RDoc.
== LICENSE:
(The MIT License)
Copyright (c) 2012 Stephan Kulow
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
xmlhash-1.3.6/History.txt 0000644 0000041 0000041 00000002406 12576040722 015426 0 ustar www-data www-data === 1.3.6 / 2013-09-11
* Wrap libxml2 parsing in a synchronize block - otherwise the stacking
will not work and it's too much work to make it reentrant
=== 1.3.5 / 2012-12-21 ("final edition")
* Initialize result so we don't return something random in case of parse error
=== 1.3.4 / 2012-12-04
* "upgrade" latin1 and us-ascii to utf-8 automatically
=== 1.3.3 / 2012-11-06
* should return [] not [{}]
=== 1.3.2 / 2012-11-06
* elements that have both attributes and content, are now parsed
as { "attr" => "value", "_content" => "CONTENT" }
=== 1.3.1 / 2012-10-08
* fix manifest
* add constructor to Xmlhash
=== 1.3.0 / 2012-10-01
* parse will no longer return a plain ruby Hash, but a XMLHash,
which is a subclass with some convenience functions added to it
=== 1.2.4 / 2012-09-27
* set the encoding of the result to the encoding of the input
=== 1.2.3 / 2012-06-22
* (pulled)
=== 1.2.2 / 2012-06-20
* use pkg-config for libxml
=== 1.2.1 / 2012-03-24
* mark even more variables for ruby
=== 1.2 / 2012-03-21
* be careful with ruby variables to avoid GC stress killing us
=== 1.1 / 2012-03-19
* make it C code to avoid problems on sle11
=== 1.0.1 / 2012-03-09
* Compile with ruby 1.9
=== 1.0.0 / 2012-02-28
* 1 major enhancement
* Birthday!
xmlhash-1.3.6/.travis.yml 0000644 0000041 0000041 00000000317 12576040722 015334 0 ustar www-data www-data language: ruby
rvm:
- 1.9.3
- 1.8.7
- 1.9.2
- 1.9.3
- rbx-18mode
- rbx-19mode
- ruby-head
- ree
before_script:
- sudo apt-get update
- sudo apt-get install libxml2-dev
- bundle install
xmlhash-1.3.6/lib/ 0000755 0000041 0000041 00000000000 12576040722 013770 5 ustar www-data www-data xmlhash-1.3.6/lib/xmlhash.rb 0000644 0000041 0000041 00000003266 12576040722 015770 0 ustar www-data www-data require 'xmlhash/xmlhash'
module Xmlhash
VERSION = '1.3.6'
class XMLHash < Hash
# Return an array of elements or []. It requires a plain string as argument
#
# This makes it easy to write code that assumes an array.
# If there is just a single child in the XML, it will be wrapped
# in a single-elemnt array and if there are no children, an empty
# array is returned.
#
# You can also pass a block to iterate over all childrens.
def elements(name)
unless name.kind_of? String
raise ArgumentError, "expected string"
end
sub = self[name]
return [] if !sub || sub.empty?
unless sub.kind_of? Array
if block_given?
yield sub
return
else
return [sub]
end
end
return sub unless block_given?
sub.each do |n|
yield n
end
end
# Return the element by the given name or an empty hash
#
# This makes it easy to write code that assumes a child to be present.
# obj["a"]["b"] will give you a "[] not defined for nil".
# obj.get("a")["b"] will give you nil
def get(name)
sub = self[name]
return sub if sub
return XMLHash.new
end
# Return the value of the name or nil if nothing is there
#
def value(name)
sub = self[name.to_s]
return nil unless sub
return '' if sub.empty? # avoid {}
return sub
end
# Initialize with a hash
def initialize(opts = nil)
self.replace(opts) if opts
end
def inspect
"X(#{super})"
end
end
def self.parse(str)
@@mutex ||= Mutex.new
@@mutex.synchronize { parse_int(str) }
end
end
xmlhash-1.3.6/metadata.yml 0000644 0000041 0000041 00000005317 12576040722 015533 0 ustar www-data www-data --- !ruby/object:Gem::Specification
name: xmlhash
version: !ruby/object:Gem::Version
version: 1.3.6
platform: ruby
authors:
- Stephan Kulow
autorequire:
bindir: bin
cert_chain: []
date: 2013-09-11 00:00:00.000000000 Z
dependencies:
- !ruby/object:Gem::Dependency
name: pkg-config
requirement: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
type: :runtime
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
- !ruby/object:Gem::Dependency
name: rdoc
requirement: !ruby/object:Gem::Requirement
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '4.0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '4.0'
- !ruby/object:Gem::Dependency
name: rake-compiler
requirement: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
- !ruby/object:Gem::Dependency
name: hoe
requirement: !ruby/object:Gem::Requirement
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.7'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '3.7'
description: |-
A small C module that wraps libxml2's xmlreader to parse a XML
string into a ruby hash
email:
- coolo@suse.com
executables: []
extensions:
- ext/xmlhash/extconf.rb
extra_rdoc_files:
- History.txt
- Manifest.txt
- README.txt
files:
- .autotest
- .travis.yml
- Gemfile
- History.txt
- Manifest.txt
- README.txt
- Rakefile
- ext/xmlhash/extconf.rb
- ext/xmlhash/xmlhash.c
- lib/xmlhash.rb
- test/test_xmlhash.rb
- .gemtest
homepage: https://github.com/coolo/xmlhash
licenses:
- MIT
metadata: {}
post_install_message:
rdoc_options:
- --main
- README.txt
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
required_rubygems_version: !ruby/object:Gem::Requirement
requirements:
- - '>='
- !ruby/object:Gem::Version
version: '0'
requirements: []
rubyforge_project: xmlhash
rubygems_version: 2.0.3
signing_key:
specification_version: 4
summary: A small C module that wraps libxml2's xmlreader to parse a XML string into
a ruby hash
test_files:
- test/test_xmlhash.rb
xmlhash-1.3.6/test/ 0000755 0000041 0000041 00000000000 12576040722 014201 5 ustar www-data www-data xmlhash-1.3.6/test/test_xmlhash.rb 0000644 0000041 0000041 00000013345 12576040722 017237 0 ustar www-data www-data # encoding: UTF-8
require "test/unit"
require "xmlhash"
require 'json'
Xml = <Big commentplease make sure to wait before these depencencies are in openSUSE:Factory: libopendbx-devel, libopendbx1, libopendbxplus1, opendbx-backend-pgsqlupdate and factory fix (forwarded request 86230 from -miska-)
eos
Output = {"history" =>
[{"name" => "review", "when" => "2011-11-25T15:02:53", "who" => "coolo"},
{"comment" => "please make sure to wait before these depencencies are in openSUSE:Factory: libopendbx-devel, libopendbx1, libopendbxplus1, opendbx-backend-pgsql",
"name" => "declined", "when" => "2011-11-25T16:17:30", "who" => "coolo"}
],
"review" =>
[
{"comment" => "Big comment",
"by_group" => "legal-auto",
"when" => "2011-11-25T15:09:55",
"who" => "licensedigger",
"state" => "accepted"
},
{"by_group" => "factory-auto",
"state" => "new"}
], "action" => {"type" => "submit", "target" => {"project" => "openSUSE:Factory", "package" => "pdns"}, "source" => {"rev" => "65", "project" => "server:dns", "package" => "pdns"}}, "id" => "93651", "description" => "update and factory fix (forwarded request 86230 from -miska-)", "state" => {"comment" => {}, "name" => "revoked", "when" => "2011-12-19T13:20:50", "who" => "coolo"}}
class TestXmlhash < Test::Unit::TestCase
def test_xml
1000.times {
ret = Xmlhash.parse(Xml)
GC.start
assert_equal ret, Output
}
10000.times {
ret = Xmlhash.parse(Xml)
assert_equal ret, Output
}
end
def test_threading
10.times do
Thread.new do
100.times do
ret = Xmlhash.parse(Xml)
assert_equal ret, Output
end
end
end
end
def test_entry
xml = <
eos
rubyoutput = {"count" => "4",
"entry" =>
[{"name" => "Apache"},
{"name" => "Apache:APR_Pool_Debug"},
{"name" => "Apache:MirrorBrain"},
{"name" => "Apache:Modules"}]}
ret = Xmlhash.parse(xml)
assert_equal ret, rubyoutput
assert_equal ret.elements("entry").first.value("name"), "Apache"
end
def test_encoding
xml = "Adrian Schröter"
ret = Xmlhash.parse(xml)
assert_equal ret, "Adrian Schröter"
xml = ""
ret = Xmlhash.parse(xml)
assert_equal ret, {"value" => "Adrian Schröter"}
assert_equal ret.get("value"), "Adrian Schröter"
end
def test_cdata
xml = <DummyContent
eos
ret = Xmlhash.parse(xml)
assert_equal ret['diff'], {"lines" => "1", "_content" => "DummyContent"}
end
def test_empty
xml = ""
ret = Xmlhash.parse(xml)
assert_equal ret.elements('files'), []
end
def test_garbage
# unfortunately it's rather challening testing nothing is printed to stderr
ret = Xmlhash.parse("asdasdaskdladka")
assert_equal nil, ret
end
def test_utf8
xml = 'libconfig – C/C++ Configuration File LibraryLibconfig is a simple library for processing structured configuration files, like this one: test.cfg. This file format is more compact and more readable than XML. And unlike XML, it is type-aware, so it is not necessary to do string parsing in application code.
Libconfig is very compact — just 38K for the stripped C shared library (less than one-fourth the size of the expat XML parser library) and 66K for the stripped C++ shared library. This makes it well-suited for memory-constrained systems like handheld devices.
The library includes bindings for both the C and C++ languages. It works on POSIX-compliant UNIX systems (GNU/Linux, Mac OS X, Solaris, FreeBSD) and Windows (2000, XP and later).'
xh = Xmlhash.parse(xml)
assert_equal "UTF-8", xh['title'].encoding.to_s
# now try with different input encoding
xml.encode!('US-ASCII')
xh = Xmlhash.parse(xml)
assert_equal "UTF-8", xh['title'].encoding.to_s
xml = '
Äöß'
xml.encode!('ISO-8859-1')
xh = Xmlhash.parse(xml)
assert_equal "ISO-8859-1", xh['title'].encoding.to_s
xml = '
äÓþ'
xml.encode!('US-ASCII')
xh = Xmlhash.parse(xml)
assert_equal "UTF-8", xh['title'].encoding.to_s
end
end
xmlhash-1.3.6/ext/ 0000755 0000041 0000041 00000000000 12576040722 014022 5 ustar www-data www-data xmlhash-1.3.6/ext/xmlhash/ 0000755 0000041 0000041 00000000000 12576040722 015466 5 ustar www-data www-data xmlhash-1.3.6/ext/xmlhash/extconf.rb 0000644 0000041 0000041 00000000336 12576040722 017463 0 ustar www-data www-data require 'mkmf'
require 'pkg-config'
unless find_library('xml2', 'xmlAddID')
abort "xml2 is missing. please install libxml2"
end
$CFLAGS << ' ' + PackageConfig.new('libxml-2.0').cflags
create_makefile('xmlhash/xmlhash')
xmlhash-1.3.6/ext/xmlhash/xmlhash.c 0000644 0000041 0000041 00000014305 12576040722 017301 0 ustar www-data www-data #include
/* libxml headers first - see https://github.com/coolo/xmlhash/issues/1 */
#include
#include
#include
#ifdef HAVE_RUBY_ST_H
# include
#else
# include
#endif
/* API_VERSION_CODE is only defined in those we want */
#ifdef HAVE_RUBY_ENCODING_H
# include
#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_NOENT | 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);
}