eXistAPI 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/collection.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "xmlrpc/client"
2
+ require 'document'
3
+
4
+ class Collection
5
+
6
+ attr_reader :name
7
+ attr_reader :owner
8
+ attr_reader :group
9
+ attr_reader :permissions
10
+ attr_reader :childCollection
11
+
12
+ def initialize(client, collectionName)
13
+ @client = client
14
+ load(collectionName)
15
+ rescue => e
16
+ raise e
17
+ end
18
+
19
+ def to_s()
20
+ return "#{@permissions} #{@owner} #{@group} #{@name}"
21
+ end
22
+
23
+ def documents
24
+ @documents.each { |d| yield d }
25
+ end
26
+
27
+ def docs #returns string array of all documents in collection
28
+ ds = []
29
+ @documents.each{ |d| ds.push(d.name)}
30
+ return ds
31
+ end
32
+
33
+ def [](key)
34
+ @documents.each { |d|
35
+ return d if key == d.name
36
+ }
37
+ return nil
38
+ end
39
+
40
+ protected
41
+
42
+ def load(collectionName)
43
+ resp = @client.call("getCollectionDesc", collectionName)
44
+ @name = resp['name']+"/"
45
+ @owner = resp['owner']
46
+ @group = resp['group']
47
+ @permissions = resp['permissions']
48
+ @childCollection = resp['collections']
49
+
50
+ @documents = Array.new
51
+ docs = resp['documents']
52
+ #puts "docs #{docs}"
53
+ docs.each { |d| @documents.push(Document.new(@client, d, @name)) }
54
+ rescue XMLRPC::FaultException => e
55
+ raise e
56
+ rescue
57
+ raise ExistException.new("Failed to load Collection", 10), callers
58
+ end #end def load
59
+ end #end class Collection
data/lib/document.rb ADDED
@@ -0,0 +1,34 @@
1
+ class Document
2
+
3
+ attr_reader :path
4
+ attr_reader :name
5
+ attr_reader :owner
6
+ attr_reader :group
7
+ attr_reader :permissions
8
+
9
+ def initialize(client, hash, colname)
10
+ @client = client
11
+ @path = colname + hash['name']
12
+ @name = @path[/[^\/]+$/]
13
+ #puts "filename #{@name}"
14
+ @owner = hash['owner']
15
+ @group = hash['group']
16
+ @permissions = hash['permissions']
17
+ rescue => e
18
+ raise e
19
+ end
20
+
21
+ def to_s
22
+ return "#{@permissions} #{@owner} #{@group} #{@name}"
23
+ end
24
+
25
+ def content
26
+ options = { "indent" => "yes", "encoding" => "UTF-8",
27
+ "expand-xincludes" => "yes" }
28
+ return @client.call("getDocument", @path, options)
29
+ rescue XMLRPC::FaultException => e
30
+ raise e
31
+ rescue
32
+ raise ExistException.new("Failed to load content of Document", 11), callers
33
+ end
34
+ end
data/lib/eXistAPI.rb ADDED
@@ -0,0 +1,209 @@
1
+ require "xmlrpc/client"
2
+ require 'collection'
3
+
4
+ class ExistAPI
5
+ #example ExistAPI.new("http://localhost:8080/exist/xmlrpc", "admin", "password")
6
+ def initialize(uri = nil , username = nil, password = nil)
7
+ @uri = uri
8
+ @username = username
9
+ @password = password
10
+ @parameters = { "encoding" => "UTF-8", "indent" => "yes"}
11
+ #outputoptions = { "encoding" => "UTF-8", "indent" => "yes"}
12
+ connect
13
+ end
14
+
15
+ private
16
+ def connect
17
+ @client = XMLRPC::Client.new2(@uri)
18
+ @client.user=(@username)
19
+ @client.password=(@password)
20
+ @client.call("isXACMLEnabled") #test if the connection is established.
21
+ rescue XMLRPC::FaultException => e
22
+ raise e
23
+ rescue
24
+ raise ExistException.new("Database login failed", 1), caller
25
+ end
26
+
27
+ public
28
+
29
+ def createcollection(_name, _parent = nil)
30
+ if (_parent == nil)
31
+ begin
32
+ result = @client.call("createCollection", _name)
33
+ return result
34
+ end
35
+ else
36
+ begin
37
+ colname = _parent.getname + _name
38
+ result = @client.call("createCollection", colname)
39
+ return result
40
+ end
41
+ end
42
+ rescue XMLRPC::FaultException => e
43
+ raise e
44
+ rescue
45
+ raise ExistException.new("Failed to create Collection", 2), caller
46
+ end
47
+
48
+ def getcollection(path)
49
+ if(path[-1] != "/") #if last is not "/" then add it
50
+ path += "/"
51
+ end
52
+ col = Collection.new(@client, path)
53
+ return col
54
+ rescue => e
55
+ raise e
56
+ end
57
+
58
+ def existscollection?(orig_path)
59
+ collections = orig_path.split("/")
60
+ collections.delete("")
61
+ i=0
62
+ path = "/"
63
+ while(i < collections.length)
64
+ path = path + collections[i] + "/"
65
+ col = Collection.new(@client, path)
66
+ if (!col.childCollection.include?(collections[i+1]))
67
+ break
68
+ end
69
+ i = i + 1
70
+ end
71
+
72
+ if(path[-1]=="/")
73
+ path = path[0..-2]
74
+ end
75
+ if(orig_path[-1]=="/")
76
+ orig_path = orig_path[0..-2]
77
+ end
78
+
79
+ if(path == orig_path)
80
+ return true
81
+ else
82
+ return false
83
+ end
84
+ rescue => e
85
+ raise e
86
+ end
87
+
88
+ def remove_collection(_name)
89
+ # boolean removeCollection( String collection)
90
+ result = @client.call("removeCollection", _name)
91
+ return result
92
+ rescue XMLRPC::FaultException => e
93
+ raise e
94
+ rescue
95
+ raise ExistException.new("Failed to remove Collection", 3), caller
96
+ end
97
+
98
+ def storeresource(_res, _docname, _overwrite = 1)
99
+ if ((_res == nil)||(_docname == nil))
100
+ raise ExistException.new("Resource or document name is nil", 4), caller
101
+ end
102
+ begin
103
+ @client.call("parse",_res.to_s, _docname, _overwrite)
104
+ rescue XMLRPC::FaultException => e
105
+ raise e
106
+ rescue ExistException => e
107
+ raise e
108
+ rescue
109
+ raise ExistException.new("Failed to store resource", 5), caller
110
+ end
111
+ end
112
+
113
+ def execute_query(xquery, parameters = @parameters)
114
+ #puts xquery
115
+ begin
116
+ handle = @client.call("executeQuery", XMLRPC::Base64.new(xquery), parameters)
117
+ return handle
118
+ rescue XMLRPC::FaultException => e
119
+ raise e
120
+ rescue
121
+ raise ExistException.new("Failed to execute query", 6), caller
122
+ end
123
+ end
124
+
125
+ def retrieve(handle, pos)
126
+ begin
127
+ res = @client.call("retrieve", handle, pos, @parameters)
128
+ return res
129
+ rescue XMLRPC::FaultException => e
130
+ raise e
131
+ rescue
132
+ raise ExistException.new("Failed to retrive resource", 7), caller
133
+ end
134
+ end
135
+
136
+ def get_hits(handle)
137
+ begin
138
+ summary = @client.call("querySummary", handle)
139
+ hits = summary['hits']
140
+ return hits
141
+ rescue XMLRPC::FaultException => e
142
+ raise e
143
+ rescue
144
+ raise ExistException.new("Failed to get number of hits", 8), caller
145
+ end
146
+ end
147
+
148
+ #<email type="office">andrew@gmail.com</email>, pos = into | following | preceding, expr_single e.g. //address[fname="Andrew"]
149
+ def update_insert(expr, pos, expr_single)
150
+ query = "update insert "+expr+" "+pos+" "+expr_single
151
+ #puts "query #{query}"
152
+ execute_query(query)
153
+ rescue => e
154
+ raise e
155
+ end
156
+
157
+ def update_replace(expr, expr_single)
158
+ query = "update replace "+expr+" with "+expr_single
159
+ execute_query(query)
160
+ rescue => e
161
+ raise e
162
+ end
163
+
164
+ def update_value(expr, expr_single)
165
+ query = "update replace " + expr + " with " + expr_single
166
+ execute_query(query)
167
+ rescue => e
168
+ raise e
169
+ end
170
+
171
+ def update_delete(expr)
172
+ query = "update delete " + expr
173
+ #puts "query #{query}"
174
+ execute_query(query)
175
+ rescue => e
176
+ raise e
177
+ end
178
+
179
+ def update_rename(expr, expr_single)
180
+ query = "update rename " + expr + " as " + expr_single
181
+ #puts "query #{query}"
182
+ execute_query(query)
183
+ rescue => e
184
+ raise e
185
+ end
186
+ end
187
+
188
+
189
+ #db = ExistAPI.new("http://localhost:8080/exist/xmlrpc", "admin", "admin")
190
+ #puts db.existscollection?("db")
191
+
192
+ #client = XMLRPC::Client.new("localhost", "/exist/xmlrpc", 8080)
193
+
194
+ #-----------------
195
+ #if $*.length < 1 then
196
+ # puts "usage: collections.rb collection-path"
197
+ # exit(0)
198
+ #end
199
+ #
200
+ #path = $*[0]
201
+ #collection = Collection.new(client, "/db/pokus/")
202
+ #puts collection.to_s
203
+ #collection.documents { |d| puts d.to_s }
204
+ #
205
+ #doc = collection['books.xml']
206
+ #if doc == nil
207
+ # error("document not found")
208
+ #end
209
+ #puts doc.content
@@ -0,0 +1,52 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ class ExistException < RuntimeError
5
+
6
+ @description
7
+ @number
8
+
9
+ def initialize(_description = "Epic fail \n", _number = nil)
10
+ @description = _description + "\n"
11
+ @number = _number
12
+ end
13
+
14
+ def inspect
15
+ self.tostring
16
+ end
17
+
18
+ def to_s
19
+ self.tostring
20
+ end
21
+
22
+ def tostring
23
+ @number.to_s + ": " +@description
24
+ end
25
+
26
+ def code
27
+ @number
28
+ end
29
+
30
+ end
31
+
32
+ #Soupis jednotlivych vyjimek, v zavorce metoda tera tuto vyjimku vyhazuje
33
+ #1: Database login failed (DatabaseManager.connect)
34
+ #2: Failed to create Collection (DatabaseManager.createcollection)
35
+ #3: Failed to remove Collection (DatabaseManager.removecollection)
36
+ #
37
+ #10: Failed to initialize Collection (Collection.initialize)
38
+ #11: Failed to close Collection (Collection.close)
39
+ #12: No such child Collection (Collection.getchildcollection)
40
+ #13: Failed to get parent Collection (Collection.getparentcollection)
41
+ #14: Unknown resource (Collection.getresource)
42
+ #15: Failed to list child Collections (Collection.listchildcollection)
43
+ #16: Failed to list resources (Collection.listresources)
44
+ #17: Failed to remove resource (Collection.removeresource)
45
+ #18: Failed to store resource (Collection.storeresource)
46
+ #
47
+ #20: Failed to execute query (XPathQueryService.query)
48
+ #21: Failed to query Resource (XPathQueryService.queryresource)
49
+ #
50
+ #30: Failed to update Collection (XUpdateService.update)
51
+ #31: Failed to update Resource (XUpdateService.updateResource)
52
+ #
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eXistAPI
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jenda Sirl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! "API for eXist-db based on XML-RPC. \nAPI is using xQuery, xPath and
15
+ xQuery Update Facility."
16
+ email: jan.sirl@seznam.cz
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/eXistAPI.rb
22
+ - lib/collection.rb
23
+ - lib/document.rb
24
+ - lib/exist_exception.rb
25
+ homepage: http://rubygems.org/gems/existapi
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.16
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: API for eXist-db based on XML-RPC.
49
+ test_files: []