Sha256: f3bd962161c8c347333503ce7b1015eee911921b7faceca1df0752b455d56373

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'libxml'

module ROXML
  module XML # :nodoc:all
    Document = LibXML::XML::Document
    Node = LibXML::XML::Node
    Parser = LibXML::XML::Parser
    Error = LibXML::XML::Error

    module NamespacedSearch
      def search(xpath)
        begin
          if namespaces.default && !xpath.include?(':')
            find(namespaced(xpath),
                 in_default_namespace(namespaces.default.href))
          else
            find(xpath)
          end
        rescue Exception => ex
          raise ex, xpath
        end
      end

    private
      def namespaced(xpath)
        xpath.between('/') do |component|
          if component =~ /\w+/ && !component.include?(':') && !component.starts_with?('@')
            in_default_namespace(component)
          else
            component
          end
        end
      end

      def in_default_namespace(name)
        "roxmldefaultnamespace:#{name}"
      end
    end

    class Document
      include NamespacedSearch

    private
      delegate :namespaces, :to => :root
    end

    class Node
      include NamespacedSearch

      class << self
        def new_with_entity_escaping(name, content = nil, namespace = nil)
          new_without_entity_escaping(name, content && CGI.escapeHTML(content), namespace)
        end
        alias_method_chain :new, :entity_escaping
      end
    end

    class Parser
      class << self
        def parse(str_data)
          string(str_data).parse
        end

        def parse_file(path)
          file(path).parse
        end

        def parse_io(stream)
          io(stream).parse
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roxml-2.5.0 lib/roxml/xml/parsers/libxml.rb