module T2Server::XML::Methods

Public Instance Methods

get_uris_from_doc(doc, keys) click to toggle source

Given a list of xpath keys, extract the href URIs from those elements.

   # File lib/t2-server/xml/methods.rb
83 def get_uris_from_doc(doc, keys)
84   cache = XPathCache.instance
85   uris = {}
86 
87   keys.each do |key|
88     uri = xpath_attr(doc, cache[key], "href")
89     uris[key] = uri.nil? ? nil : URI.parse(uri)
90   end
91 
92   uris
93 end
xml_children(doc) { |node| ... } click to toggle source
   # File lib/t2-server/xml/methods.rb
49 def xml_children(doc, &block)
50   doc.each { |node| yield node }
51 end
xml_document(string) click to toggle source
   # File lib/t2-server/xml/methods.rb
41 def xml_document(string)
42   LibXML::XML::Document.string(string)
43 end
xml_first_child(node) click to toggle source
   # File lib/t2-server/xml/methods.rb
45 def xml_first_child(node)
46   node.first
47 end
xml_input_fragment(input, type = :value) click to toggle source
   # File lib/t2-server/xml/methods.rb
95 def xml_input_fragment(input, type = :value)
96   node = create_node("nsr:runInput", ["nsr:#{type}", input.to_s])
97   create_document(node).to_s
98 end
xml_keypair_cred_fragment(uri, name, key, type, password) click to toggle source
    # File lib/t2-server/xml/methods.rb
128 def xml_keypair_cred_fragment(uri, name, key, type, password)
129   node = create_node("nsr:credential",
130     ["nss:keypair",
131       ["nss:serviceURI", uri],
132       ["nss:credentialName", name],
133       ["nss:credentialBytes", key],
134       ["nss:fileType", type],
135       ["nss:unlockPassword", password]
136     ]
137   )
138 
139   create_document(node).to_s
140 end
xml_mkdir_fragment(name) click to toggle source
    # File lib/t2-server/xml/methods.rb
100 def xml_mkdir_fragment(name)
101   node = create_node("nsr:mkdir", { "nsr:name" => name })
102   create_document(node).to_s
103 end
xml_node_attribute(node, attribute) click to toggle source
   # File lib/t2-server/xml/methods.rb
61 def xml_node_attribute(node, attribute)
62   node.attributes[attribute]
63 end
xml_node_content(node) click to toggle source
   # File lib/t2-server/xml/methods.rb
57 def xml_node_content(node)
58   node.content
59 end
xml_node_name(node) click to toggle source
   # File lib/t2-server/xml/methods.rb
53 def xml_node_name(node)
54   node.name
55 end
xml_password_cred_fragment(uri, username, password) click to toggle source
    # File lib/t2-server/xml/methods.rb
116 def xml_password_cred_fragment(uri, username, password)
117   node = create_node("nsr:credential",
118     ["nss:userpass",
119       ["nss:serviceURI", uri],
120       ["nss:username", username],
121       ["nss:password", password]
122     ]
123   )
124 
125   create_document(node).to_s
126 end
xml_permissions_fragment(username, permission) click to toggle source
    # File lib/t2-server/xml/methods.rb
110 def xml_permissions_fragment(username, permission)
111   node = create_node("nsr:permissionUpdate",
112     ["nsr:userName", username], ["nsr:permission", permission])
113   create_document(node).to_s
114 end
xml_trust_fragment(contents, type) click to toggle source
    # File lib/t2-server/xml/methods.rb
142 def xml_trust_fragment(contents, type)
143   node = create_node("nss:trustedIdentity",
144     ["nss:certificateBytes", contents], ["nss:fileType", type])
145   create_document(node).to_s
146 end
xml_upload_fragment(name, data) click to toggle source
    # File lib/t2-server/xml/methods.rb
105 def xml_upload_fragment(name, data)
106   node = create_node("nsr:upload", { "nsr:name" => name }, data)
107   create_document(node).to_s
108 end
xpath_attr(doc, expr, attribute) click to toggle source
   # File lib/t2-server/xml/methods.rb
77 def xpath_attr(doc, expr, attribute)
78   node = xpath_first(doc, expr)
79   node.nil? ? nil : node.attributes[attribute]
80 end
xpath_compile(xpath) click to toggle source
   # File lib/t2-server/xml/methods.rb
65 def xpath_compile(xpath)
66   LibXML::XML::XPath::Expression.new(xpath)
67 end
xpath_find(doc, expr) click to toggle source
   # File lib/t2-server/xml/methods.rb
69 def xpath_find(doc, expr)
70   doc.find(expr, Namespaces::MAP)
71 end
xpath_first(doc, expr) click to toggle source
   # File lib/t2-server/xml/methods.rb
73 def xpath_first(doc, expr)
74   doc.find_first(expr, Namespaces::MAP)
75 end

Private Instance Methods

create_document(root) click to toggle source
    # File lib/t2-server/xml/methods.rb
150 def create_document(root)
151   doc = LibXML::XML::Document.new
152   doc.root = root
153 
154   Namespaces::MAP.each do |prefix, uri|
155     LibXML::XML::Namespace.new(root, prefix, uri)
156   end
157 
158   doc
159 end
create_node(name, *rest) click to toggle source
    # File lib/t2-server/xml/methods.rb
161 def create_node(name, *rest)
162   contents = nil
163   attributes = {}
164   children = []
165 
166   rest.each do |param|
167     case param
168     when Hash
169       attributes = param
170     when Array
171       children.push(param)
172     else
173       contents = param
174     end
175   end
176 
177   node = LibXML::XML::Node.new(name, contents)
178 
179   attributes.each do |attr, value|
180     LibXML::XML::Attr.new(node, attr, value)
181   end
182 
183   children.each do |c|
184     n = create_node(c.shift, *c)
185     node << n
186   end
187 
188   node
189 end