6 | Copyright (c) 2008-2013 Charlie Savage and contributors
7 | Copyright (c) 2002-2007 Sean Chittenden and contributors
8 | Copyright (c) 2001 Wai-Sun "Squidster" Chia
9 |
10 | Permission is hereby granted, free of charge, to any person obtaining a copy of
11 | this software and associated documentation files (the "Software"), to deal in
12 | the Software without restriction, including without limitation the rights to
13 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14 | of the Software, and to permit persons to whom the Software is furnished to do
15 | so, subject to the following conditions:
16 |
17 | The above copyright notice and this permission notice shall be included in all
18 | copies or substantial portions of the Software.
19 |
20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 | SOFTWARE.
27 |
--------------------------------------------------------------------------------
/lib/libxml/namespaces.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | module LibXML
4 | module XML
5 | class Namespaces
6 | # call-seq:
7 | # namespace.default -> XML::Namespace
8 | #
9 | # Returns the default namespace for this node or nil.
10 | #
11 | # Usage:
12 | # doc = XML::Document.string('')
13 | # ns = doc.root.namespaces.default_namespace
14 | # assert_equal(ns.href, 'http://schemas.xmlsoap.org/soap/envelope/')
15 | def default
16 | find_by_prefix(nil)
17 | end
18 |
19 | # call-seq:
20 | # namespace.default_prefix = "string"
21 | #
22 | # Assigns a name (prefix) to the default namespace.
23 | # This makes it much easier to perform XML::XPath
24 | # searches.
25 | #
26 | # Usage:
27 | # doc = XML::Document.string('')
28 | # doc.root.namespaces.default_prefix = 'soap'
29 | # node = doc.root.find_first('soap:Envelope')
30 | def default_prefix=(prefix)
31 | # Find default prefix
32 | ns = find_by_prefix(nil)
33 | raise(ArgumentError, "No default namespace was found") unless ns
34 | Namespace.new(self.node, prefix, ns.href)
35 | end
36 | end
37 | end
38 | end
--------------------------------------------------------------------------------
/test/test_suite.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | # Change to current directory so relative
4 | # requires work.
5 | dir = File.dirname(__FILE__)
6 | Dir.chdir(dir)
7 |
8 | require './tc_attr'
9 | require './tc_attr_decl'
10 | require './tc_attributes'
11 | require './tc_canonicalize'
12 | require './tc_document'
13 | require './tc_document_write'
14 | require './tc_dtd'
15 | require './tc_error'
16 | require './tc_html_parser'
17 | require './tc_html_parser_context'
18 | require './tc_namespace'
19 | require './tc_namespaces'
20 | require './tc_node'
21 | require './tc_node_cdata'
22 | require './tc_node_comment'
23 | require './tc_node_copy'
24 | require './tc_node_edit'
25 | require './tc_node_pi'
26 | require './tc_node_text'
27 | require './tc_node_write'
28 | require './tc_node_xlink'
29 | require './tc_parser'
30 | require './tc_parser_context'
31 | require './tc_reader'
32 | require './tc_relaxng'
33 | require './tc_sax_parser'
34 | require './tc_schema'
35 | require './tc_traversal'
36 | require './tc_writer'
37 | require './tc_xinclude'
38 | require './tc_xpath'
39 | require './tc_xpath_context'
40 | require './tc_xpath_expression'
41 | require './tc_xpointer'
42 |
43 |
44 | if defined?(Encoding)
45 | require './tc_encoding'
46 | require './tc_encoding_sax'
47 | end
48 | # Compatibility
49 | require './tc_properties'
50 | require './tc_deprecated_require'
--------------------------------------------------------------------------------
/ext/libxml/ruby_xml_schema_facet.c:
--------------------------------------------------------------------------------
1 | #include "ruby_libxml.h"
2 | #include "ruby_xml_schema_facet.h"
3 |
4 | VALUE cXMLSchemaFacet;
5 |
6 | static void rxml_schema_facet_free(xmlSchemaFacetPtr xschema_type)
7 | {
8 | xschema_type = NULL;
9 | xmlFree(xschema_type);
10 | }
11 |
12 | /* START FACET*/
13 |
14 | static VALUE rxml_schema_facet_node(VALUE self)
15 | {
16 | xmlSchemaFacetPtr facet;
17 |
18 | Data_Get_Struct(self, xmlSchemaFacet, facet);
19 |
20 | return rxml_node_wrap(facet->node);
21 | }
22 |
23 | static VALUE rxml_schema_facet_value(VALUE self)
24 | {
25 | xmlSchemaFacetPtr facet;
26 |
27 | Data_Get_Struct(self, xmlSchemaFacet, facet);
28 |
29 | QNIL_OR_STRING(facet->value)
30 | }
31 |
32 | static VALUE rxml_schema_facet_kind(VALUE self)
33 | {
34 | xmlSchemaFacetPtr facet;
35 |
36 | Data_Get_Struct(self, xmlSchemaFacet, facet);
37 |
38 | return INT2NUM(facet->type);
39 | }
40 |
41 | VALUE rxml_wrap_schema_facet(xmlSchemaFacetPtr facet)
42 | {
43 | return Data_Wrap_Struct(cXMLSchemaFacet, NULL, rxml_schema_facet_free, facet);
44 | }
45 |
46 | void rxml_init_schema_facet(void)
47 | {
48 | cXMLSchemaFacet = rb_define_class_under(cXMLSchema, "Facet", rb_cObject);
49 | rb_define_method(cXMLSchemaFacet, "value", rxml_schema_facet_value, 0);
50 | rb_define_method(cXMLSchemaFacet, "node", rxml_schema_facet_node, 0);
51 | rb_define_method(cXMLSchemaFacet, "kind", rxml_schema_facet_kind, 0);
52 | }
53 |
--------------------------------------------------------------------------------
/ext/libxml/ruby_xml_io.c:
--------------------------------------------------------------------------------
1 | /* Please see the LICENSE file for copyright and distribution information */
2 |
3 | #include "extconf.h"
4 | #include "ruby_libxml.h"
5 |
6 | static ID READ_METHOD;
7 | #ifndef HAVE_RB_IO_BUFWRITE
8 | static ID WRITE_METHOD;
9 | #endif /* !HAVE_RB_IO_BUFWRITE */
10 |
11 | /* This method is called by libxml when it wants to read
12 | more data from a stream. We go with the duck typing
13 | solution to support StringIO objects. */
14 | int rxml_read_callback(void *context, char *buffer, int len)
15 | {
16 | VALUE io = (VALUE) context;
17 | VALUE string = rb_funcall(io, READ_METHOD, 1, INT2NUM(len));
18 | int size;
19 |
20 | if (string == Qnil)
21 | return 0;
22 |
23 | size = RSTRING_LEN(string);
24 | memcpy(buffer, StringValuePtr(string), size);
25 |
26 | return size;
27 | }
28 |
29 | int rxml_write_callback(void *context, const char *buffer, int len)
30 | {
31 | #ifndef HAVE_RB_IO_BUFWRITE
32 | VALUE io, written, string;
33 |
34 | io = (VALUE) context;
35 | string = rb_str_new(buffer, len);
36 | written = rb_funcall(io, WRITE_METHOD, 1, string);
37 |
38 | return NUM2INT(written);
39 | #else
40 | return rb_io_bufwrite((VALUE) context, buffer, (size_t) len);
41 | #endif /* !HAVE_RB_IO_BUFWRITE */
42 | }
43 |
44 | void rxml_init_io(void)
45 | {
46 | READ_METHOD = rb_intern("read");
47 | #ifndef HAVE_RB_IO_BUFWRITE
48 | WRITE_METHOD = rb_intern("write");
49 | #endif /* !HAVE_RB_IO_BUFWRITE */
50 | }
51 |
--------------------------------------------------------------------------------
/ext/vc/libxml_ruby.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby", "libxml_ruby_18\libxml_ruby.vcxproj", "{0B65CD1D-EEB9-41AE-93BB-75496E504152}"
5 | EndProject
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libxml_ruby_19", "libxml_ruby_19\libxml_ruby_19.vcxproj", "{0E73156F-E619-4FD9-8327-113FE3CC942E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Win32 = Debug|Win32
11 | Release|Win32 = Release|Win32
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {0B65CD1D-EEB9-41AE-93BB-75496E504152}.Debug|Win32.ActiveCfg = Debug|Win32
15 | {0B65CD1D-EEB9-41AE-93BB-75496E504152}.Debug|Win32.Build.0 = Debug|Win32
16 | {0B65CD1D-EEB9-41AE-93BB-75496E504152}.Release|Win32.ActiveCfg = Release|Win32
17 | {0B65CD1D-EEB9-41AE-93BB-75496E504152}.Release|Win32.Build.0 = Release|Win32
18 | {0E73156F-E619-4FD9-8327-113FE3CC942E}.Debug|Win32.ActiveCfg = Debug|Win32
19 | {0E73156F-E619-4FD9-8327-113FE3CC942E}.Debug|Win32.Build.0 = Debug|Win32
20 | {0E73156F-E619-4FD9-8327-113FE3CC942E}.Release|Win32.ActiveCfg = Release|Win32
21 | {0E73156F-E619-4FD9-8327-113FE3CC942E}.Release|Win32.Build.0 = Release|Win32
22 | EndGlobalSection
23 | GlobalSection(SolutionProperties) = preSolution
24 | HideSolutionNode = FALSE
25 | EndGlobalSection
26 | EndGlobal
27 |
--------------------------------------------------------------------------------
/test/tc_node_cdata.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | require './test_helper'
4 | require 'test/unit'
5 |
6 | class CDataCommentTest < Test::Unit::TestCase
7 | def setup
8 | xp = XML::Parser.string('')
9 | @doc = xp.parse
10 | assert_instance_of(XML::Document, @doc)
11 | @root = @doc.root
12 | end
13 |
14 | def test_node_type
15 | cnode = XML::Node.new_cdata('test cdata')
16 | assert_equal(XML::Node::CDATA_SECTION_NODE, cnode.node_type)
17 | end
18 |
19 | def test_add_cdata
20 | @root << XML::Node.new_cdata('mycdata')
21 | assert_equal '',
22 | @root.to_s.gsub(/\n\s*/,'')
23 | end
24 |
25 | def test_add_cdata_2
26 | @root << XML::Node.new_cdata('mycdata')
27 | assert_equal 'cdata',
28 | @root.child.node_type_name
29 | end
30 |
31 | def test_add_cdata_3
32 | @root << el = XML::Node.new_cdata('mycdata')
33 | el << "_this_is_added"
34 | assert_equal '',
35 | @root.to_s.gsub(/\n\s*/,'')
36 | end
37 |
38 | def test_attributes
39 | cnode = XML::Node.new_cdata('test cdata')
40 | assert_equal(0, cnode.attributes.length)
41 | end
42 |
43 | def test_set_cdata_attribute
44 | cnode = XML::Node.new_cdata('test cdata')
45 |
46 | # Can't create attributes on non-element nodes
47 | assert_raise(ArgumentError) do
48 | cnode['attr'] = '123'
49 | end
50 | end
51 | end
52 |
--------------------------------------------------------------------------------
/lib/libxml/namespace.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | module LibXML
4 | module XML
5 | class Namespace
6 | include Comparable
7 | include Enumerable
8 |
9 | # call-seq:
10 | # namespace1 <=> namespace2
11 | #
12 | # Compares two namespace objects. Namespace objects are
13 | # considered equal if their prefixes and hrefs are the same.
14 | def <=>(other)
15 | if self.prefix.nil? and other.prefix.nil?
16 | self.href <=> other.href
17 | elsif self.prefix.nil?
18 | -1
19 | elsif other.prefix.nil?
20 | 1
21 | else
22 | self.prefix <=> other.prefix
23 | end
24 | end
25 |
26 | # call-seq:
27 | # namespace.each {|ns| .. }
28 | #
29 | # libxml stores namespaces in memory as a linked list.
30 | # Use the each method to iterate over the list. Note
31 | # the first namespace in the loop is the current namespace.
32 | #
33 | # Usage:
34 | # namespace.each do |ns|
35 | # ..
36 | # end
37 | def each
38 | ns = self
39 |
40 | while ns
41 | yield ns
42 | ns = ns.next
43 | end
44 | end
45 |
46 | # call-seq:
47 | # namespace.to_s -> "string"
48 | #
49 | # Returns the string represenation of a namespace.
50 | #
51 | # Usage:
52 | # namespace.to_s
53 | def to_s
54 | if self.prefix
55 | "#{self.prefix}:#{self.href}"
56 | else
57 | self.href
58 | end
59 | end
60 | end
61 | end
62 | end
--------------------------------------------------------------------------------
/test/tc_relaxng.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | require './test_helper'
4 |
5 | require 'test/unit'
6 |
7 | class TestRelaxNG < Test::Unit::TestCase
8 | def setup
9 | file = File.join(File.dirname(__FILE__), 'model/shiporder.xml')
10 | @doc = XML::Document.file(file)
11 | end
12 |
13 | def teardown
14 | @doc = nil
15 | end
16 |
17 | def relaxng
18 | document = XML::Document.file(File.join(File.dirname(__FILE__), 'model/shiporder.rng'))
19 | relaxng = XML::RelaxNG.document(document)
20 | end
21 |
22 | def test_from_doc
23 | assert_instance_of(XML::RelaxNG, relaxng)
24 | end
25 |
26 | def test_valid
27 | assert(@doc.validate_relaxng(relaxng))
28 | end
29 |
30 | def test_invalid
31 | new_node = XML::Node.new('invalid', 'this will mess up validation')
32 | @doc.root << new_node
33 |
34 | error = assert_raise(XML::Error) do
35 | @doc.validate_relaxng(relaxng)
36 | end
37 |
38 | assert_not_nil(error)
39 | assert_kind_of(XML::Error, error)
40 | assert(error.message.match(/Error: Did not expect element invalid there/))
41 | assert_equal(XML::Error::RELAXNGV, error.domain)
42 | assert_equal(XML::Error::LT_IN_ATTRIBUTE, error.code)
43 | assert_equal(XML::Error::ERROR, error.level)
44 | assert(error.file.match(/shiporder\.xml/))
45 | assert_nil(error.line)
46 | assert_equal('invalid', error.str1)
47 | assert_nil(error.str2)
48 | assert_nil(error.str3)
49 | assert_equal(0, error.int1)
50 | assert_equal(0, error.int2)
51 | assert_not_nil(error.node)
52 | assert_equal('invalid', error.node.name)
53 | end
54 | end
55 |
--------------------------------------------------------------------------------
/test/model/shiporder.xsd:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Shiporder type documentation
8 |
9 |
10 |
11 |
12 | orderperson element documentation
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 |
--------------------------------------------------------------------------------
/test/model/merge_bug_data.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Some Preset Data Here
6 | Some Preset Data Here
7 | name: quack!
8 | Some Preset Data Here
9 | Some Preset Data Here
10 | Some Preset Data Here
11 | Some Preset Data Here
12 | city: quack!
13 | Some Preset Data Here
14 | Some Preset Data Here
15 | Some Preset Data Here
16 | Some Preset Data Here
17 | state: quack!
18 | Some Preset Data Here
19 | Some Preset Data Here
20 | Some Preset Data Here
21 | Some Preset Data Here
22 |
23 | year: Vroom!
24 | make: Vroom!
25 | model: Vroom!
26 |
27 |
28 | year: Vroom!
29 | make: Vroom!
30 | model: Vroom!
31 |
32 |
33 | year: Vroom!
34 | make: Vroom!
35 | model: Vroom!
36 |
37 | Some Preset Data Here
38 | Some Preset Data Here
39 |
40 | year: Vroom!
41 | make: Vroom!
42 | model: Vroom!
43 |
44 |
45 | year: Vroom!
46 | make: Vroom!
47 | model: Vroom!
48 |
49 |
50 | year: Vroom!
51 | make: Vroom!
52 | model: Vroom!
53 |
54 | Some Preset Data Here
55 | Some Preset Data Here
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/libxml-ruby.gemspec:
--------------------------------------------------------------------------------
1 | # encoding: utf-8
2 |
3 | # Determine the current version of the software
4 | version = File.read('ext/libxml/ruby_xml_version.h').match(/\s*RUBY_LIBXML_VERSION\s*['"](\d.+)['"]/)[1]
5 |
6 | Gem::Specification.new do |spec|
7 | spec.name = 'libxml-ruby'
8 | spec.version = version
9 | spec.homepage = 'http://xml4r.github.com/libxml-ruby'
10 | spec.summary = 'Ruby Bindings for LibXML2'
11 | spec.description = <<-EOS
12 | The Libxml-Ruby project provides Ruby language bindings for the GNOME
13 | Libxml2 XML toolkit. It is free software, released under the MIT License.
14 | Libxml-ruby's primary advantage over REXML is performance - if speed
15 | is your need, these are good libraries to consider, as demonstrated
16 | by the informal benchmark below.
17 | EOS
18 | spec.authors = ['Ross Bamform', 'Wai-Sun Chia', 'Sean Chittenden',
19 | 'Dan Janwoski', 'Anurag Priyam', 'Charlie Savage',
20 | 'Ryan Johnson']
21 | spec.platform = Gem::Platform::RUBY
22 | spec.bindir = "bin"
23 | spec.extensions = ["ext/libxml/extconf.rb"]
24 | spec.files = Dir.glob(['HISTORY',
25 | 'LICENSE',
26 | 'libxml-ruby.gemspec',
27 | 'MANIFEST',
28 | 'Rakefile',
29 | 'README.rdoc',
30 | 'setup.rb',
31 | 'ext/libxml/*.def',
32 | 'ext/libxml/*.h',
33 | 'ext/libxml/*.c',
34 | 'ext/libxml/*.rb',
35 | 'ext/vc/*.sln',
36 | 'ext/vc/*.vcprojx',
37 | 'lib/**/*.rb',
38 | 'script/**/*',
39 | 'test/**/*'])
40 | spec.test_files = Dir.glob("test/tc_*.rb")
41 | spec.required_ruby_version = '>= 1.8.6'
42 | spec.date = DateTime.now
43 | spec.add_development_dependency('hanna_guado')
44 | end
45 |
--------------------------------------------------------------------------------
/lib/libxml/sax_parser.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | module LibXML
4 | module XML
5 | class SaxParser
6 | # call-seq:
7 | # XML::SaxParser.file(path) -> XML::SaxParser
8 | #
9 | # Creates a new parser by parsing the specified file or uri.
10 | def self.file(path)
11 | context = XML::Parser::Context.file(path)
12 | self.new(context)
13 | end
14 |
15 | # call-seq:
16 | # XML::SaxParser.io(io) -> XML::SaxParser
17 | # XML::SaxParser.io(io, :encoding => XML::Encoding::UTF_8) -> XML::SaxParser
18 | #
19 | # Creates a new reader by parsing the specified io object.
20 | #
21 | # Parameters:
22 | #
23 | # encoding - The document encoding, defaults to nil. Valid values
24 | # are the encoding constants defined on XML::Encoding.
25 | def self.io(io, options = {})
26 | context = XML::Parser::Context.io(io)
27 | context.encoding = options[:encoding] if options[:encoding]
28 | self.new(context)
29 | end
30 |
31 | # call-seq:
32 | # XML::SaxParser.string(string)
33 | #
34 | # Creates a new parser by parsing the specified string.
35 | def self.string(string)
36 | context = XML::Parser::Context.string(string)
37 | self.new(context)
38 | end
39 |
40 | # :enddoc:
41 |
42 | def file=(value)
43 | warn("XML::SaxParser#file is deprecated. Use XML::SaxParser.file instead")
44 | @context = XML::Parser::Context.file(value)
45 | end
46 |
47 | def io=(value)
48 | warn("XML::SaxParser#io is deprecated. Use XML::SaxParser.io instead")
49 | @context = XML::Parser::Context.io(value)
50 | end
51 |
52 | def string=(value)
53 | warn("XML::SaxParser#string is deprecated. Use XML::SaxParser.string instead")
54 | @context = XML::Parser::Context.string(value)
55 | end
56 | end
57 | end
58 | end
--------------------------------------------------------------------------------
/ext/libxml/ruby_xml_html_parser_options.c:
--------------------------------------------------------------------------------
1 | /* Please see the LICENSE file for copyright and distribution information */
2 |
3 | #include "ruby_libxml.h"
4 |
5 | /* Document-class: LibXML::XML::HTMLParser::Options
6 | *
7 | * Options to control the operation of the HTMLParser. The easiest
8 | * way to set a parser's options is via the methods
9 | * XML::HTMLParser.file, XML::HTMLParser.io or XML::HTMLParser.string.
10 | * For additional control, see XML::HTMLParser::Context#options=.
11 | */
12 |
13 | VALUE mXMLHtmlParserOptions;
14 |
15 | void rxml_init_html_parser_options(void)
16 | {
17 | mXMLHtmlParserOptions = rb_define_module_under(cXMLHtmlParser, "Options");
18 |
19 |
20 | #if LIBXML_VERSION >= 20621
21 | /* 1: Relax parsing. */
22 | rb_define_const(mXMLHtmlParserOptions, "RECOVER", INT2NUM(HTML_PARSE_RECOVER));
23 | #endif
24 | #if LIBXML_VERSION >= 20708
25 | /* 2: Do not default a doctype if not found */
26 | rb_define_const(mXMLHtmlParserOptions, "NODEFDTD", INT2NUM(HTML_PARSE_NODEFDTD));
27 | #endif
28 | /* 32: Suppress error reports. */
29 | rb_define_const(mXMLHtmlParserOptions, "NOERROR", INT2NUM(HTML_PARSE_NOERROR));
30 | /* 64: Suppress warning reports. */
31 | rb_define_const(mXMLHtmlParserOptions, "NOWARNING", INT2NUM(HTML_PARSE_NOWARNING));
32 | /* 128: Enable pedantic error reporting. */
33 | rb_define_const(mXMLHtmlParserOptions, "PEDANTIC", INT2NUM(HTML_PARSE_PEDANTIC));
34 | /* 256: Remove blank nodes. */
35 | rb_define_const(mXMLHtmlParserOptions, "NOBLANKS", INT2NUM(HTML_PARSE_NOBLANKS));
36 | #if LIBXML_VERSION >= 20621
37 | /* 2048: Forbid network access. */
38 | rb_define_const(mXMLHtmlParserOptions, "NONET", INT2NUM(HTML_PARSE_NONET));
39 | /* 65536: Compact small text nodes. */
40 | rb_define_const(mXMLHtmlParserOptions, "COMPACT", INT2NUM(HTML_PARSE_COMPACT));
41 | #endif
42 | #if LIBXML_VERSION >= 20707
43 | /* 8192: Do not add implied html/body... elements */
44 | rb_define_const(mXMLHtmlParserOptions, "NOIMPLIED", INT2NUM(HTML_PARSE_NOIMPLIED));
45 | #endif
46 | }
47 |
--------------------------------------------------------------------------------
/test/tc_namespace.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | require './test_helper'
4 |
5 | require 'test/unit'
6 |
7 | class TestNS < Test::Unit::TestCase
8 | def setup
9 | file = File.join(File.dirname(__FILE__), 'model/soap.xml')
10 | @doc = XML::Document.file(file)
11 | end
12 |
13 | def teardown
14 | @doc = nil
15 | end
16 |
17 | def test_create_ns
18 | node = XML::Node.new('foo')
19 | ns = XML::Namespace.new(node, 'my_namepace', 'http://www.mynamespace.com')
20 | assert_equal(ns.prefix, 'my_namepace')
21 | assert_equal(ns.href, 'http://www.mynamespace.com')
22 | end
23 |
24 | def test_create_default_ns
25 | node = XML::Node.new('foo')
26 | ns = XML::Namespace.new(node, nil, 'http://www.mynamespace.com')
27 | assert_equal(ns.prefix, nil)
28 | assert_equal(ns.href, 'http://www.mynamespace.com')
29 | end
30 |
31 | def test_create_unbound_ns
32 | error = assert_raise(TypeError) do
33 | XML::Namespace.new(nil, 'my_namepace', 'http://www.mynamespace.com')
34 | end
35 | assert_equal('wrong argument type nil (expected Data)', error.to_s)
36 | end
37 |
38 | def test_duplicate_ns
39 | node = XML::Node.new('foo')
40 | XML::Namespace.new(node, 'myname', 'http://www.mynamespace.com')
41 | assert_raises(XML::Error) do
42 | XML::Namespace.new(node, 'myname', 'http://www.mynamespace.com')
43 | end
44 | end
45 |
46 | def test_eql
47 | node = XML::Node.new('Envelope')
48 | ns = XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
49 |
50 | assert(node.namespaces.namespace.eql?(node.namespaces.namespace))
51 | end
52 |
53 | def test_equal
54 | node1 = XML::Node.new('Envelope')
55 | ns1 = XML::Namespace.new(node1, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
56 |
57 | node2 = XML::Node.new('Envelope')
58 | ns2 = XML::Namespace.new(node2, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
59 |
60 | assert(ns1 == ns2)
61 | end
62 | end
--------------------------------------------------------------------------------
/ext/libxml/libxml.c:
--------------------------------------------------------------------------------
1 | #include "ruby_libxml.h"
2 |
3 | #if RUBY_INTERN_H
4 | #include
5 | #else
6 | #include
7 | #endif
8 |
9 |
10 | VALUE mLibXML;
11 |
12 | static void rxml_init_memory(void)
13 | {
14 | /* Disable for now - broke attributes.
15 | xmlGcMemSetup(
16 | (xmlFreeFunc)ruby_xfree,
17 | (xmlMallocFunc)ruby_xmalloc,
18 | (xmlMallocFunc)ruby_xmalloc,
19 | (xmlReallocFunc)ruby_xrealloc,
20 | (xmlStrdupFunc)ruby_strdup
21 | );*/
22 | }
23 |
24 | void Init_libxml_ruby(void)
25 | {
26 | /* The libxml gem provides Ruby language bindings for GNOME's Libxml2
27 | * XML toolkit. To get started you may:
28 | *
29 | * require 'test_helper'
30 | * document = XML::Document.new
31 | *
32 | * However, when creating an application or library you plan to
33 | * redistribute, it is best to not add the LibXML module to the global
34 | * namespace, in which case you can either write your code like this:
35 | *
36 | * require 'libxml'
37 | * document = LibXML::XML::Document.new
38 | *
39 | * Refer to the README file to get started and the LICENSE file for
40 | * copyright and distribution information.
41 | */
42 | mLibXML = rb_define_module("LibXML");
43 |
44 | rxml_init_memory();
45 | rxml_init_xml();
46 | rxml_init_io();
47 | rxml_init_error();
48 | rxml_init_encoding();
49 | rxml_init_parser();
50 | rxml_init_parser_context();
51 | rxml_init_parser_options();
52 | rxml_init_node();
53 | rxml_init_attributes();
54 | rxml_init_attr();
55 | rxml_init_attr_decl();
56 | rxml_init_document();
57 | rxml_init_namespaces();
58 | rxml_init_namespace();
59 | rxml_init_sax_parser();
60 | rxml_init_sax2_handler();
61 | rxml_init_xinclude();
62 | rxml_init_xpath();
63 | rxml_init_xpath_object();
64 | rxml_init_xpath_context();
65 | rxml_init_xpath_expression();
66 | rxml_init_xpointer();
67 | rxml_init_html_parser();
68 | rxml_init_html_parser_options();
69 | rxml_init_html_parser_context();
70 | rxml_init_input_callbacks();
71 | rxml_init_dtd();
72 | rxml_init_schema();
73 | rxml_init_relaxng();
74 | rxml_init_reader();
75 | rxml_init_writer();
76 | }
77 |
--------------------------------------------------------------------------------
/ext/libxml/ruby_libxml.h:
--------------------------------------------------------------------------------
1 | /* Please see the LICENSE file for copyright and distribution information */
2 |
3 | #ifndef __RUBY_LIBXML_H__
4 | #define __RUBY_LIBXML_H__
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 |
19 | /* Needed prior to Ruby 1.9.1 */
20 | #ifndef RHASH_TBL
21 | #define RHASH_TBL(s) (RHASH(s)->tbl)
22 | #endif
23 |
24 | // Encoding support added in Ruby 1.9.*
25 | #ifdef HAVE_RUBY_ENCODING_H
26 | #include
27 | #endif
28 |
29 | #ifdef LIBXML_DEBUG_ENABLED
30 | #include
31 | #endif
32 | #ifdef LIBXML_XINCLUDE_ENABLED
33 | #include
34 | #endif
35 | #ifdef LIBXML_XPTR_ENABLED
36 | #include
37 | #endif
38 |
39 | #include "ruby_xml_version.h"
40 | #include "ruby_xml.h"
41 | #include "ruby_xml_io.h"
42 | #include "ruby_xml_error.h"
43 | #include "ruby_xml_encoding.h"
44 | #include "ruby_xml_attributes.h"
45 | #include "ruby_xml_attr.h"
46 | #include "ruby_xml_attr_decl.h"
47 | #include "ruby_xml_document.h"
48 | #include "ruby_xml_node.h"
49 | #include "ruby_xml_namespace.h"
50 | #include "ruby_xml_namespaces.h"
51 | #include "ruby_xml_parser.h"
52 | #include "ruby_xml_parser_options.h"
53 | #include "ruby_xml_parser_context.h"
54 | #include "ruby_xml_html_parser.h"
55 | #include "ruby_xml_html_parser_options.h"
56 | #include "ruby_xml_html_parser_context.h"
57 | #include "ruby_xml_reader.h"
58 | #include "ruby_xml_writer.h"
59 | #include "ruby_xml_sax2_handler.h"
60 | #include "ruby_xml_sax_parser.h"
61 | #include "ruby_xml_writer.h"
62 | #include "ruby_xml_xinclude.h"
63 | #include "ruby_xml_xpath.h"
64 | #include "ruby_xml_xpath_expression.h"
65 | #include "ruby_xml_xpath_context.h"
66 | #include "ruby_xml_xpath_object.h"
67 | #include "ruby_xml_xpointer.h"
68 | #include "ruby_xml_input_cbg.h"
69 | #include "ruby_xml_dtd.h"
70 | #include "ruby_xml_schema.h"
71 | #include "ruby_xml_relaxng.h"
72 |
73 | extern VALUE mLibXML;
74 |
75 | #endif
76 |
--------------------------------------------------------------------------------
/lib/libxml/attr_decl.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | module LibXML
4 | module XML
5 | class AttrDecl
6 | include Enumerable
7 |
8 | # call-seq:
9 | # attr_decl.child -> nil
10 | #
11 | # Obtain this attribute declaration's child attribute(s).
12 | # It will always be nil.
13 | def child
14 | nil
15 | end
16 |
17 | # call-seq:
18 | # attr_decl.child? -> (true|false)
19 | #
20 | # Returns whether this attribute declaration has child attributes.
21 | #
22 | def child?
23 | not self.children.nil?
24 | end
25 |
26 | # call-seq:
27 | # attr_decl.doc? -> (true|false)
28 | #
29 | # Determine whether this attribute declaration is associated with an
30 | # XML::Document.
31 | def doc?
32 | not self.doc.nil?
33 | end
34 |
35 | # call-seq:
36 | # attr_decl.next? -> (true|false)
37 | #
38 | # Determine whether there is a next attribute declaration.
39 | def next?
40 | not self.next.nil?
41 | end
42 |
43 | # call-seq:
44 | # attr_decl.parent? -> (true|false)
45 | #
46 | # Determine whether this attribute declaration has a parent .
47 | def parent?
48 | not self.parent.nil?
49 | end
50 |
51 | # call-seq:
52 | # attr_decl.prev? -> (true|false)
53 | #
54 | # Determine whether there is a previous attribute declaration.
55 | def prev?
56 | not self.prev.nil?
57 | end
58 |
59 | # call-seq:
60 | # attr_decl.node_type_name -> 'attribute declaration'
61 | #
62 | # Returns this attribute declaration's node type name.
63 | def node_type_name
64 | if node_type == Node::ATTRIBUTE_DECL
65 | 'attribute declaration'
66 | else
67 | raise(UnknownType, "Unknown node type: %n", node.node_type);
68 | end
69 | end
70 |
71 | # call-seq:
72 | # attr_decl.to_s -> string
73 | #
74 | # Returns a string representation of this attribute declaration.
75 | def to_s
76 | "#{name} = #{value}"
77 | end
78 | end
79 | end
80 | end
81 |
--------------------------------------------------------------------------------
/lib/libxml/hpricot.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | ## Provide hpricot API for libxml. Provided by Michael Guterl,
4 | ## inspired by http://thebogles.com/blog/an-hpricot-style-interface-to-libxml
5 | #
6 | #class String
7 | # def to_libxml_doc
8 | # xp = XML::Parser.new
9 | # xp.string = self
10 | # xp.parse
11 | # end
12 | #end
13 | #
14 | #module LibXML
15 | # module XML
16 | # class Document
17 | # alias :search :find
18 | # end
19 | #
20 | # class Node
21 | # # find the child node with the given xpath
22 | # def at(xpath)
23 | # self.find_first(xpath)
24 | # end
25 | #
26 | # # find the array of child nodes matching the given xpath
27 | # def search(xpath)
28 | # results = self.find(xpath).to_a
29 | # if block_given?
30 | # results.each do |result|
31 | # yield result
32 | # end
33 | # end
34 | # return results
35 | # end
36 | #
37 | # def /(xpath)
38 | # search(xpath)
39 | # end
40 | #
41 | # # return the inner contents of this node as a string
42 | # def inner_xml
43 | # child.to_s
44 | # end
45 | #
46 | # # alias for inner_xml
47 | # def inner_html
48 | # inner_xml
49 | # end
50 | #
51 | # # return this node and its contents as an xml string
52 | # def to_xml
53 | # self.to_s
54 | # end
55 | #
56 | # # alias for path
57 | # def xpath
58 | # self.path
59 | # end
60 | #
61 | # def find_with_default_ns(xpath_expr, namespace=nil)
62 | # find_base(xpath_expr, namespace || default_namespaces)
63 | # end
64 | #
65 | # def find_first_with_default_ns(xpath_expr, namespace=nil)
66 | # find_first_base(xpath_expr, namespace || default_namespaces)
67 | # end
68 | #
69 | ## alias_method :find_base, :find unless method_defined?(:find_base)
70 | ## alias_method :find, :find_with_default_ns
71 | ## alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
72 | ## alias_method :find_first, :find_first_with_default_ns
73 | ## alias :child? :first?
74 | ## alias :children? :first?
75 | ## alias :child :first
76 | # end
77 | # end
78 | #end
--------------------------------------------------------------------------------
/test/model/shiporder.rng:
--------------------------------------------------------------------------------
1 |
2 |
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 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 |
3 | require "rubygems"
4 | require "rake/extensiontask"
5 | require "rake/testtask"
6 | require "rubygems/package_task"
7 | require "rdoc/task"
8 | require "yaml"
9 |
10 | GEM_NAME = "libxml-ruby"
11 | SO_NAME = "libxml_ruby"
12 |
13 | # Read the spec file
14 | spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
15 |
16 | # Setup compile tasks
17 | Rake::ExtensionTask.new do |ext|
18 | ext.gem_spec = spec
19 | ext.name = SO_NAME
20 | ext.ext_dir = "ext/libxml"
21 | ext.lib_dir = "lib/#{RUBY_VERSION.sub(/\.\d$/, '')}"
22 | ext.config_options << "--with-xml2-include=C:/MinGW/local/include/libxml2"
23 | ext.config_options << "--with-zlib-dir=C:/MinGW/local"
24 | end
25 |
26 | # Setup generic gem
27 | Gem::PackageTask.new(spec) do |pkg|
28 | pkg.package_dir = 'pkg'
29 | pkg.need_tar = false
30 | end
31 |
32 | # Setup Windows Gem
33 | if RUBY_PLATFORM.match(/win32|mingw32/)
34 | binaries = (FileList['lib/**/*.so',
35 | 'lib/**/*dll'])
36 |
37 | # Windows specification
38 | win_spec = spec.clone
39 | win_spec.platform = Gem::Platform::CURRENT
40 | win_spec.files += binaries.to_a
41 | win_spec.instance_variable_set(:@cache_file, nil)
42 |
43 | # Unset extensions
44 | win_spec.extensions = nil
45 |
46 | # Rake task to build the windows package
47 | Gem::PackageTask.new(win_spec) do |pkg|
48 | pkg.package_dir = 'pkg'
49 | pkg.need_tar = false
50 | end
51 | end
52 |
53 | # RDoc Task
54 | desc "Generate rdoc documentation"
55 | RDoc::Task.new("rdoc") do |rdoc|
56 | rdoc.rdoc_dir = 'website/_site/rdoc'
57 | rdoc.title = "LibXML"
58 | # Show source inline with line numbers
59 | rdoc.options << "--line-numbers"
60 | rdoc.options << "--charset=utf-8"
61 | rdoc.options << "--format=hanna"
62 | # Make the readme file the start page for the generated html
63 | rdoc.main = 'README.rdoc'
64 | rdoc.rdoc_files.include('doc/*.rdoc',
65 | 'ext/**/libxml.c',
66 | 'ext/**/ruby_xml.c',
67 | 'ext/**/*.c',
68 | 'lib/**/*.rb',
69 | 'README.rdoc',
70 | 'HISTORY',
71 | 'LICENSE')
72 | end
73 |
74 | # Test Task
75 | Rake::TestTask.new do |t|
76 | t.libs << "test"
77 | t.verbose = true
78 | end
--------------------------------------------------------------------------------
/test/tc_node_text.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 |
3 | require './test_helper'
4 | require 'test/unit'
5 |
6 | class TestTextNode < Test::Unit::TestCase
7 | def test_content
8 | node = XML::Node.new_text('testdata')
9 | assert_instance_of(XML::Node, node)
10 | assert_equal('testdata', node.content)
11 | end
12 |
13 | def test_invalid_content
14 | error = assert_raise(TypeError) do
15 | node = XML::Node.new_text(nil)
16 | end
17 | assert_equal('wrong argument type nil (expected String)', error.to_s)
18 | end
19 |
20 | # We use the same facility that libXSLT does here to disable output escaping.
21 | # This lets you specify that the node's content should be rendered unaltered
22 | # whenever it is being output. This is useful for things like