Method: Xqruby::Document#initialize

Defined in:
lib/xqruby/xqruby.rb

#initialize(options) ⇒ Document

Create an Xqruby Document The method expects a hash that contain the possible elements :root => value : The name of the root or top-most element of the document :xml => value : A string containing an XML document :url => value : A URL pointing to an XML document :file => filename : A filename pointing to an XML file :validate => true|false : whether to validate the document being read from

an XML string, URL or file

:resolve_includes => true|false replaces xi:include elements by the content they refer to :namespace => provides a namespace prefix to the elements



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
# File 'lib/xqruby/xqruby.rb', line 45

def initialize(options)
  # extract options
  validate = options[:validate] ? options[:validate] : false
  root = options[:root]
  xml = options[:xml]
  url = options[:url]
  file = options[:file]
  namespace = options[:namespace]
  resolve_includes = options[:resolve_includes] ? options[:resolve_includes] : false
  
  if root
    unless namespace
      @root = XOM::Element.new "#{root}"
    else
      @root = XOM::Element.new "#{root}", namespace
      prefix = root.to_s.split(":").first if root.include? ":"
      (@namespaces ||= {})[prefix] = namespace if prefix
    end
    
    @doc = XOM::Document.new @root
  else
    builder = XOM::Builder.new validate 
  end
  
  if xml
    @doc = builder.build(xml, nil)
  elsif url
    @doc = builder.build url
  elsif file
    java_file = Lang::File.new file        
    @doc = builder.build java_file       
  end
  
  if resolve_includes
    @doc = XOM::XIncluder.resolve(@doc)
  end
  
  @root = @doc.get_root_element unless @root
  @target = @root
end