lib/nmap/xml.rb in ruby-nmap-0.10.0 vs lib/nmap/xml.rb in ruby-nmap-1.0.0
- old
+ new
@@ -1,12 +1,12 @@
-require 'nmap/scanner'
-require 'nmap/scan_task'
-require 'nmap/scan'
-require 'nmap/host'
-require 'nmap/run_stat'
-require 'nmap/prescript'
-require 'nmap/postscript'
+require 'nmap/xml/scanner'
+require 'nmap/xml/scan_task'
+require 'nmap/xml/scan'
+require 'nmap/xml/host'
+require 'nmap/xml/run_stat'
+require 'nmap/xml/prescript'
+require 'nmap/xml/postscript'
require 'nokogiri'
module Nmap
#
@@ -14,35 +14,40 @@
#
class XML
include Enumerable
+ # The parsed XML document.
+ #
+ # @return [Nokogiri::XML]
+ #
+ # @api private
+ attr_reader :doc
+
# Path of the Nmap XML scan file
+ #
+ # @return [String, nil]
attr_reader :path
#
# Creates a new XML object.
#
- # @param [Nokogiri::XML::Document, IO, String] document
+ # @param [Nokogiri::XML::Document] doc
# The path to the Nmap XML scan file or Nokogiri::XML::Document.
#
+ # @param [String, nil] path
+ # The optional path the XML was loaded from.
+ #
# @yield [xml]
# If a block is given, it will be passed the new XML object.
#
# @yieldparam [XML] xml
# The newly created XML object.
#
- def initialize(document)
- case document
- when Nokogiri::XML::Document
- @doc = document
- when IO, StringIO
- @doc = Nokogiri::XML(document)
- else
- @path = File.expand_path(document)
- @doc = File.open(@path) { |file| Nokogiri::XML(file) }
- end
+ def initialize(doc, path: nil)
+ @doc = doc
+ @path = File.expand_path(path) if path
yield self if block_given?
end
#
@@ -62,19 +67,10 @@
def self.parse(text,&block)
new(Nokogiri::XML(text),&block)
end
#
- # @deprecated Use {parse} instead.
- #
- # @since 0.7.0
- #
- def self.load(text,&block)
- parse(text,&block)
- end
-
- #
# Creates a new XML object from the file.
#
# @param [String] path
# The path to the XML file.
#
@@ -85,11 +81,14 @@
# The newly created XML object.
#
# @since 0.7.0
#
def self.open(path,&block)
- new(path,&block)
+ path = File.expand_path(path)
+ doc = Nokogiri::XML(File.open(path))
+
+ new(doc, path: path, &block)
end
#
# Parses the scanner information.
#
@@ -268,12 +267,10 @@
@prescript ||= if (prescript = @doc.at('prescript'))
Prescript.new(prescript)
end
end
- alias prescripts prescript
-
#
# The NSE scripts ran after the scan.
#
# @return [Postscript]
# Contains the script output and data.
@@ -284,12 +281,10 @@
@postscript ||= if (postscript = @doc.at('postscript'))
Postscript.new(postscript)
end
end
- alias postscripts postscript
-
#
# Parses the hosts in the scan.
#
# @yield [host]
# Each host will be passed to a given block.
@@ -435,23 +430,15 @@
#
# Converts the XML parser to a String.
#
# @return [String]
- # The path of the XML scan file.
+ # The path of the XML file or the raw XML.
#
def to_s
- @path.to_s
- end
-
- #
- # Inspects the XML file.
- #
- # @return [String]
- # The inspected XML file.
- #
- def inspect
- "#<#{self.class}: #{self}>"
+ if @path then @path.to_s
+ else @doc.to_s
+ end
end
end
end