dreamcat4-libxml-bindings 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 dreamcat4
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = libxml-bindings
2
+
3
+ This introduction text is taken from http://thebogles.com/blog/an-hpricot-style-interface-to-libxml#,
4
+ credit to Phil Bogle.
5
+
6
+ == Convenience functions
7
+
8
+ You can call to_xml_doc on any string to convert it into an XML::Document:
9
+
10
+ >> s = '<foo id="1"><author>p. bogle</author><bar>content</bar><bar>cont2</bar></foo>'
11
+ >> root = s.to_xml_doc.root
12
+
13
+ The at() method returns the first Node matching the given xpath:
14
+
15
+ >> root.at("author")
16
+ => <author>p. bogle</author>
17
+
18
+ The search() method returns a list of Nodes matching the given xpath:
19
+
20
+ >> root.search("bar")
21
+ => [<bar>content</bar>, <bar>content2</bar>]
22
+
23
+ search() can also be called with a block to iterate through each of the matching nodes:
24
+
25
+ >> root.search("bar") do |bar| puts bar.xpath; end
26
+ /foo/bar[1]
27
+ /foo/bar[2]
28
+
29
+ == Namespace helpers
30
+
31
+ The handling of default namespaces in libxml-ruby is awkward because you have to remember to pass along an array of namespace strings to every find() method call, and because you have to repeat yourself about the href of the default namespace.
32
+
33
+ The helpers add a register_default_namespace function that makes this simpler.
34
+
35
+ Suppose you had XML like the following
36
+
37
+ <?xml version="1.0" encoding="UTF-8"?>
38
+ <feed xmlns="http://www.w3.org/2005/Atom"
39
+ xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
40
+ xmlns:gContact="http://schemas.google.com/contact/2008\"
41
+ xmlns:gd="http://schemas.google.com/g/2005">
42
+ <title type=\"text\">Phil Bogle's Contacts</title>
43
+ ...
44
+ </feed>
45
+
46
+ Then you could say the following and have it work as expected:
47
+
48
+ root.register_default_namespace("atom")
49
+ root.search("atom:title")
50
+
51
+ == Copyright
52
+
53
+ Copyright (c) 2009 dreamcat4. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "libxml-bindings"
8
+ gem.summary = %Q{Dreamcat4's bindings for libxml-ruby. In development. This gem will provide convenience functions for both reading and writing XML in ruby. Implemented as wrapper for underlying libxml2 / SAX. Aims are to be faster (very much) and simpler (little bit) than hpricot. This project is a continuation / extension of libxml_helper.rb by Phil Bogle.}
9
+ gem.email = "dreamcat4@gmail.com"
10
+ gem.homepage = "http://github.com/dreamcat4/libxml-bindings"
11
+ gem.authors = ["dreamcat4"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "libxml-bindings #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 0
@@ -0,0 +1,90 @@
1
+ require "xml/libxml"
2
+
3
+ class XML::Node
4
+ ##
5
+ # Open up XML::Node from libxml and add convenience methods inspired
6
+ # by hpricot.
7
+ # (http://code.whytheluckystiff.net/hpricot/wiki/HpricotBasics)
8
+ # Also:
9
+ # * provide better handling of default namespaces
10
+ # an array of default namespaces to past into
11
+ attr_accessor :default_namespaces
12
+
13
+ # find the child node with the given xpath
14
+ def at(xpath)
15
+ self.find_first(xpath)
16
+ end
17
+
18
+ # find the array of child nodes matching the given xpath
19
+ def search(xpath)
20
+ results = self.find(xpath).to_a
21
+ if block_given?
22
+ results.each do |result|
23
+ yield result
24
+ end
25
+ end
26
+ return results
27
+ end
28
+
29
+ # alias for search
30
+ def /(xpath)
31
+ search(xpath)
32
+ end
33
+ # return the inner contents of this node as a string
34
+ def inner_xml
35
+ child.to_s
36
+ end
37
+
38
+ # alias for inner_xml
39
+ def inner_html
40
+ inner_xml
41
+ end
42
+
43
+ # return this node and its contents as an xml string
44
+ def to_xml
45
+ self.to_s
46
+ end
47
+
48
+ # alias for path
49
+ def xpath
50
+ self.path
51
+ end
52
+
53
+ # provide a name for the default namespace
54
+ def register_default_namespace(name)
55
+ self.namespace.each do |n|
56
+ if n.to_s == nil
57
+ register_namespace("#{name}:#{n.href}")
58
+ return
59
+ end
60
+ end
61
+ raise "No default namespace found"
62
+ end
63
+
64
+ # register a namespace, of the form "foo:http://example.com/ns"
65
+ def register_namespace(name_and_href)
66
+ (@default_namespaces ||= []) <<name_and_href
67
+ end
68
+
69
+ def find_with_default_ns(xpath_expr, namespace=nil)
70
+ find_base(xpath_expr, namespace || default_namespaces)
71
+ end
72
+
73
+ def find_first_with_default_ns(xpath_expr, namespace=nil)
74
+ find_first_base(xpath_expr, namespace || default_namespaces)
75
+ end
76
+
77
+ alias_method :find_base, :find unless method_defined?(:find_base)
78
+ alias_method :find, :find_with_default_ns
79
+ alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
80
+ alias_method :find_first, :find_first_with_default_ns
81
+
82
+ end
83
+
84
+ class String
85
+ def to_libxml_doc
86
+ xp = XML::Parser.new
87
+ xp.string = self
88
+ return xp.parse
89
+ end
90
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class LibxmlBindingsTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'libxml_bindings'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dreamcat4-libxml-bindings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - dreamcat4
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dreamcat4@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/libxml_bindings.rb
31
+ - test/libxml_bindings_test.rb
32
+ - test/test_helper.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/dreamcat4/libxml-bindings
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Dreamcat4's bindings for libxml-ruby. In development. This gem will provide convenience functions for both reading and writing XML in ruby. Implemented as wrapper for underlying libxml2 / SAX. Aims are to be faster (very much) and simpler (little bit) than hpricot. This project is a continuation / extension of libxml_helper.rb by Phil Bogle.
59
+ test_files:
60
+ - test/libxml_bindings_test.rb
61
+ - test/test_helper.rb