eXistAPI 0.0.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/collection.rb CHANGED
@@ -3,12 +3,27 @@ require 'document'
3
3
 
4
4
  class Collection
5
5
 
6
+ #name of the collection
6
7
  attr_reader :name
8
+ #owner of the collection
7
9
  attr_reader :owner
10
+ #owner group of the collection
8
11
  attr_reader :group
12
+ #permissions of the collection
9
13
  attr_reader :permissions
14
+ #descendent collection of the collection
10
15
  attr_reader :childCollection
11
16
 
17
+ #Creates new collection.
18
+ #
19
+ # * *Args* :
20
+ # - +client+ -> e.g. ExistAPI.new("http://localhost:8080/exist/xmlrpc", "admin", "password")
21
+ # - +collectionName+ -> /db/data/cities/
22
+ # * *Returns* :
23
+ # -new instance of collection
24
+ # * *Raises* :
25
+ # - +nothing+ ->
26
+ #
12
27
  def initialize(client, collectionName)
13
28
  @client = client
14
29
  load(collectionName)
@@ -16,20 +31,56 @@ class Collection
16
31
  raise e
17
32
  end
18
33
 
34
+ #Returns string of collection. That inclunde permissions, owner, group and name.
35
+ #
36
+ # * *Args* :
37
+ # - +none+
38
+ # * *Returns* :
39
+ # -string of collection
40
+ # * *Raises* :
41
+ # - +nothing+
42
+ #
19
43
  def to_s()
20
44
  return "#{@permissions} #{@owner} #{@group} #{@name}"
21
45
  end
22
46
 
47
+ #Yield each document from collection.
48
+ #
49
+ # * *Args* :
50
+ # - +none+
51
+ # * *Returns* :
52
+ # -documents
53
+ # * *Raises* :
54
+ # - +nothing+
55
+ #
23
56
  def documents
24
57
  @documents.each { |d| yield d }
25
58
  end
26
59
 
27
- def docs #returns string array of all documents in collection
60
+ #Returns string array of all documents in collection
61
+ #
62
+ # * *Args* :
63
+ # - +none+
64
+ # * *Returns* :
65
+ # -array of strings
66
+ # * *Raises* :
67
+ # - +nothing+
68
+ #
69
+ def docs
28
70
  ds = []
29
71
  @documents.each{ |d| ds.push(d.name)}
30
72
  return ds
31
73
  end
32
74
 
75
+ #Returns instance of collection's child specified by name.
76
+ #
77
+ # * *Args* :
78
+ # - +key+ -> name of the child
79
+ # * *Returns* :
80
+ # -instance of collection's child (document or collection)
81
+ # * *Raises* :
82
+ # - +nothing+
83
+ #
33
84
  def [](key)
34
85
  @documents.each { |d|
35
86
  return d if key == d.name
@@ -38,7 +89,16 @@ class Collection
38
89
  end
39
90
 
40
91
  protected
41
-
92
+
93
+ #Loads collection from db.
94
+ #
95
+ # * *Args* :
96
+ # - +collectionName+ -> e.g. /db/data/cities/
97
+ # * *Returns* :
98
+ # - instance of collection
99
+ # * *Raises* :
100
+ # - +ExistException+ -> Failed to load Collection
101
+ #
42
102
  def load(collectionName)
43
103
  resp = @client.call("getCollectionDesc", collectionName)
44
104
  @name = resp['name']+"/"
data/lib/document.rb CHANGED
@@ -1,11 +1,28 @@
1
1
  class Document
2
2
 
3
+ #path of the document
3
4
  attr_reader :path
5
+ #name of the document
4
6
  attr_reader :name
7
+ #owner of the document
5
8
  attr_reader :owner
9
+ #owner group of the document
6
10
  attr_reader :group
11
+ #permissions of the document
7
12
  attr_reader :permissions
8
-
13
+
14
+ #Creates new instance of document
15
+ #
16
+ # * *Args* :
17
+ # - +client+ -> e.g. ExistAPI.new("http://localhost:8080/exist/xmlrpc", "admin", "password")
18
+ # - +hash+ -> hash, name -> "name of the document"
19
+ # - +colname+ -> name of the collection
20
+ # * *Returns* :
21
+ # -instance of document
22
+ # * *Raises* :
23
+ # - +nothing+
24
+ # * *Example* :
25
+ # - Document.new(api, hash, collection_path)
9
26
  def initialize(client, hash, colname)
10
27
  @client = client
11
28
  @path = colname + hash['name']
@@ -17,11 +34,29 @@ class Document
17
34
  rescue => e
18
35
  raise e
19
36
  end
20
-
37
+
38
+ #Returns string of document. That inclunde permissions, owner, group and name.
39
+ #
40
+ # * *Args* :
41
+ # - +none+
42
+ # * *Returns* :
43
+ # -string of collection
44
+ # * *Raises* :
45
+ # - +nothing+
46
+ #
21
47
  def to_s
22
48
  return "#{@permissions} #{@owner} #{@group} #{@name}"
23
49
  end
24
50
 
51
+ #Returns content of document in db. Usually document is xml.
52
+ #Retrieves a document from the database.
53
+ # * *Args* :
54
+ # - +none+
55
+ # * *Returns* :
56
+ # - the result of call
57
+ # * *Raises* :
58
+ # - +ExistException+ -> Failed to load content of Document
59
+ #
25
60
  def content
26
61
  options = { "indent" => "yes", "encoding" => "UTF-8",
27
62
  "expand-xincludes" => "yes" }
data/lib/eXistAPI.rb CHANGED
@@ -1,18 +1,52 @@
1
1
  require "xmlrpc/client"
2
+ $:.unshift(".")
2
3
  require 'collection'
3
4
 
5
+ #ExistAPI is comunication interface to eXist-db based on XML-RPC. With eXistAPI
6
+ #you are able to create, delete or get collections or retrieve whole content of
7
+ #documents or part of it by querying.
8
+ #Also with eXistAPI you can work with documents stored in eXist-db.
9
+ #You can insert, replace, rename and delete nodes or change values.
10
+ #To query use xQuery or xPath.
11
+ #
12
+ #
13
+ # ==== Examples
14
+ #db = ExistAPI.new("http://localhost:8080/exist/xmlrpc", "admin", "admin")
15
+ #puts db.existscollection?("db")
4
16
  class ExistAPI
17
+
18
+ #Create new instance of ExistAPI.
19
+ #
5
20
  #example ExistAPI.new("http://localhost:8080/exist/xmlrpc", "admin", "password")
21
+ #
22
+ # * *Args* :
23
+ # - +uri+ -> uri of the db server
24
+ # - +username+ -> username
25
+ # - +password+ -> password to specified username
26
+ # * *Returns* :
27
+ # - new instance of ExistAPI
28
+ # * *Raises* :
29
+ # - +ExistException+ -> Database login failed
30
+ #
6
31
  def initialize(uri = nil , username = nil, password = nil)
7
32
  @uri = uri
8
33
  @username = username
9
34
  @password = password
10
35
  @parameters = { "encoding" => "UTF-8", "indent" => "yes"}
11
- #outputoptions = { "encoding" => "UTF-8", "indent" => "yes"}
12
36
  connect
13
37
  end
14
38
 
15
39
  private
40
+
41
+ #Creates collection with name in parrent collection(optionaly)#
42
+ #
43
+ # * *Args* :
44
+ # - +name+ -> the name of collection
45
+ # * *Returns* :
46
+ # - the result of call
47
+ # * *Raises* :
48
+ # - +ExistException+ -> Database login failed
49
+ #
16
50
  def connect
17
51
  @client = XMLRPC::Client.new2(@uri)
18
52
  @client.user=(@username)
@@ -24,17 +58,47 @@ class ExistAPI
24
58
  raise ExistException.new("Database login failed", 1), caller
25
59
  end
26
60
 
61
+ #Checks if path starts and ends with "/". If not, adds "/".
62
+ #
63
+ # * *Args* :
64
+ # - +path+ -> the path of collection
65
+ # * *Returns* :
66
+ # - Path with "/" at the begging and end.
67
+ # * *Raises* :
68
+ # - +ArgumentError+ -> if any value is nil or negative
69
+ #
70
+ def check_slashes(path)
71
+ if(path[0] != "/")
72
+ path = "/" + path
73
+ end
74
+ if(path[-1] != "/") #if last is not "/" then add it
75
+ path += "/"
76
+ end
77
+ return path
78
+ end
79
+
80
+
27
81
  public
28
82
 
29
- def createcollection(_name, _parent = nil)
30
- if (_parent == nil)
83
+ #Creates collection with name in parrent(optionaly)
84
+ #
85
+ # * *Args* :
86
+ # - +name+ -> the name of collection
87
+ # * *Returns* :
88
+ # - the result of call
89
+ # * *Raises* :
90
+ # - +ExistException+ -> if collection is not created.
91
+ #
92
+ def createcollection(name, parent_col = nil)
93
+ name = check_slashes(name)
94
+ if (parent_col == nil)
31
95
  begin
32
- result = @client.call("createCollection", _name)
96
+ result = @client.call("createCollection", name)
33
97
  return result
34
98
  end
35
99
  else
36
100
  begin
37
- colname = _parent.getname + _name
101
+ colname = parent_col.name + name
38
102
  result = @client.call("createCollection", colname)
39
103
  return result
40
104
  end
@@ -45,17 +109,34 @@ class ExistAPI
45
109
  raise ExistException.new("Failed to create Collection", 2), caller
46
110
  end
47
111
 
112
+ #Returns Collection at specified path.
113
+ #
114
+ # * *Args* :
115
+ # - +path+ -> Path of the collection in db.
116
+ # * *Returns* :
117
+ # -instance of class Collection.
118
+ # * *Raises* :
119
+ # - +nothing+
120
+ #
48
121
  def getcollection(path)
49
- if(path[-1] != "/") #if last is not "/" then add it
50
- path += "/"
51
- end
122
+ path = check_slashes(path)
52
123
  col = Collection.new(@client, path)
53
124
  return col
54
125
  rescue => e
55
126
  raise e
56
127
  end
57
128
 
129
+ #Checks if collection with path exists or not.
130
+ #
131
+ # * *Args* :
132
+ # - +path+ -> Path of the collection in db.
133
+ # * *Returns* :
134
+ # -boolean. true -> exists, false -> doesn't exist
135
+ # * *Raises* :
136
+ # - +nothing+
137
+ #
58
138
  def existscollection?(orig_path)
139
+ orig_path = check_slashes(orig_path)
59
140
  collections = orig_path.split("/")
60
141
  collections.delete("")
61
142
  i=0
@@ -85,9 +166,18 @@ class ExistAPI
85
166
  raise e
86
167
  end
87
168
 
88
- def remove_collection(_name)
89
- # boolean removeCollection( String collection)
90
- result = @client.call("removeCollection", _name)
169
+
170
+ #Removes collection with specified path.
171
+ #
172
+ # * *Args* :
173
+ # - +path+ -> Path of the collection in db.
174
+ # * *Returns* :
175
+ # - the result of call
176
+ # * *Raises* :
177
+ # - +ExistException+ -> if failed to remove collection.
178
+ #
179
+ def remove_collection(path)
180
+ result = @client.call("removeCollection", path)
91
181
  return result
92
182
  rescue XMLRPC::FaultException => e
93
183
  raise e
@@ -95,6 +185,18 @@ class ExistAPI
95
185
  raise ExistException.new("Failed to remove Collection", 3), caller
96
186
  end
97
187
 
188
+ #Stores resource to document in db.
189
+ #Inserts a new document into the database or replace an existing one:
190
+ #
191
+ # * *Args* :
192
+ # - +_res+ -> resource which you want to safe. XML content of this document as a UTF-8 encoded byte array.
193
+ # - +_docname+ -> name of the document where to safe. Path to the database location where the new document is to be stored.
194
+ # - +_overwrite+ -> Set this value to > 0 to automatically replace an existing document at the same location.
195
+ # * *Returns* :
196
+ # - boolean, the result of the call
197
+ # * *Raises* :
198
+ # - +ExistException+ -> Resource or document name is nil
199
+ # - +ExistException+ -> Failed to store resource"
98
200
  def storeresource(_res, _docname, _overwrite = 1)
99
201
  if ((_res == nil)||(_docname == nil))
100
202
  raise ExistException.new("Resource or document name is nil", 4), caller
@@ -110,6 +212,22 @@ class ExistAPI
110
212
  end
111
213
  end
112
214
 
215
+ #Executes an XQuery and returns a reference identifier to the generated result set. This reference can be used later to retrieve results.
216
+ #
217
+ # * *Args* :
218
+ # - +xquery+ -> String xquery. A valid XQuery expression.
219
+ # - +parameters+ -> HashMap parameters. The parameters a HashMap values.
220
+ #sort-expr :
221
+ #namespaces :
222
+ #variables :
223
+ #base-uri :
224
+ #static-documents :
225
+ #protected :
226
+ # * *Returns* :
227
+ # -int, handle of query
228
+ # * *Raises* :
229
+ # - +ExistException+ -> Failed to execute query
230
+ #
113
231
  def execute_query(xquery, parameters = @parameters)
114
232
  #puts xquery
115
233
  begin
@@ -122,6 +240,18 @@ class ExistAPI
122
240
  end
123
241
  end
124
242
 
243
+ #Retrieves a single result-fragment from the result-set referenced by resultId.
244
+ #The result-fragment is identified by its position in the result-set, which is passed in the parameter pos.
245
+ #
246
+ # * *Args* :
247
+ # - +handle+ -> Reference to a result-set as returned by a previous call to executeQuery.
248
+ # - +pos+ -> The position of the item in the result-sequence, starting at 0.
249
+ # - +parameters+ -> A struct containing key=value pairs to configure the output.
250
+ # * *Returns* :
251
+ # - the result of call
252
+ # * *Raises* :
253
+ # - +ExistException+ -> Failed to retrive resource
254
+ #
125
255
  def retrieve(handle, pos)
126
256
  begin
127
257
  res = @client.call("retrieve", handle, pos, @parameters)
@@ -133,6 +263,16 @@ class ExistAPI
133
263
  end
134
264
  end
135
265
 
266
+ #Get the number of hits in the result-set identified by resultId.
267
+ #example: gethits(handle_id)
268
+ #
269
+ # * *Args* :
270
+ # - +handle+ -> Reference to a result-set as returned by a previous call to executeQuery.
271
+ # * *Returns* :
272
+ # -the number of hits
273
+ # * *Raises* :
274
+ # - +ExistException+ -> Failed to get number of hits
275
+ #
136
276
  def get_hits(handle)
137
277
  begin
138
278
  summary = @client.call("querySummary", handle)
@@ -145,7 +285,24 @@ class ExistAPI
145
285
  end
146
286
  end
147
287
 
148
- #<email type="office">andrew@gmail.com</email>, pos = into | following | preceding, expr_single e.g. //address[fname="Andrew"]
288
+ #Inserts the content sequence specified in expr into the element node passed
289
+ #via exprSingle. exprSingle and expr should evaluate to a node set.
290
+ #If exprSingle contains more than one element node, the modification will be
291
+ #applied to each of the nodes. The position of the insertion is determined by
292
+ #the keywords "into", "following" or "preceding":
293
+ #
294
+ #
295
+ # * *Args* :
296
+ # - +expr+ -> The content is appended after the last child node of the specified elements.
297
+ # e.g.: <email type="office">andrew@gmail.com</email>,
298
+ # - +pos+ -> The content is inserted immediately after the node specified in exprSingle.
299
+ # pos = "into" | "following" | "preceding"
300
+ # - +expr_single+ -> The content is inserted before the node specified in exprSingle.
301
+ # e.g. '//address[fname="Andrew"]'
302
+ # * *Returns* :
303
+ # -int handle of query
304
+ # * *Raises* :
305
+ # - +nothing+
149
306
  def update_insert(expr, pos, expr_single)
150
307
  query = "update insert "+expr+" "+pos+" "+expr_single
151
308
  #puts "query #{query}"
@@ -154,6 +311,20 @@ class ExistAPI
154
311
  raise e
155
312
  end
156
313
 
314
+ #Replaces the nodes returned by expr with the nodes in exprSingle.
315
+ #expr may evaluate to a single element, attribute or text node.
316
+ #If it is an element, exprSingle should contain a single element node as well.
317
+ #If it is an attribute or text node, the value of the attribute or the text
318
+ #node is set to the concatenated string values of all nodes in exprSingle.
319
+ #
320
+ # * *Args* :
321
+ # - +expr+ -> e.g.: //fname[. = "Andrew"]
322
+ # - +expr_single+ -> <fname>Andy</fname>
323
+ # * *Returns* :
324
+ # -int handle of query
325
+ # * *Raises* :
326
+ # - +nothing+
327
+ #
157
328
  def update_replace(expr, expr_single)
158
329
  query = "update replace "+expr+" with "+expr_single
159
330
  execute_query(query)
@@ -161,6 +332,18 @@ class ExistAPI
161
332
  raise e
162
333
  end
163
334
 
335
+ #Updates the content of all nodes in expr with the items in exprSingle.
336
+ #If expr is an attribute or text node, its value will be set to the
337
+ #concatenated string value of all items in exprSingle.
338
+ #
339
+ # * *Args* :
340
+ # - +expr+ -> "//node()[@id="RockyRacoon"]/@id"
341
+ # - +expr_single+ -> '"RockyR"'
342
+ # * *Returns* :
343
+ # -int handle of query
344
+ # * *Raises* :
345
+ # - +nothing+
346
+ #
164
347
  def update_value(expr, expr_single)
165
348
  query = "update replace " + expr + " with " + expr_single
166
349
  execute_query(query)
@@ -168,6 +351,15 @@ class ExistAPI
168
351
  raise e
169
352
  end
170
353
 
354
+ #Removes all nodes in expr from their document.
355
+ #
356
+ # * *Args* :
357
+ # - +expr+ -> "//node()[@id="RockyRacoon"]
358
+ # * *Returns* :
359
+ # -int handle of query
360
+ # * *Raises* :
361
+ # - +nothing+
362
+ #
171
363
  def update_delete(expr)
172
364
  query = "update delete " + expr
173
365
  #puts "query #{query}"
@@ -176,6 +368,18 @@ class ExistAPI
176
368
  raise e
177
369
  end
178
370
 
371
+ #Renames the nodes in expr using the string value of the first item in
372
+ #exprSingle as the new name of the node. expr should evaluate to a set of
373
+ #elements or attributes.
374
+ #
375
+ # * *Args* :
376
+ # - +expr+ -> //city
377
+ # - +expr_single+ -> "town"
378
+ # * *Returns* :
379
+ # -int handle of query
380
+ # * *Raises* :
381
+ # - +nothing+ ->
382
+ #
179
383
  def update_rename(expr, expr_single)
180
384
  query = "update rename " + expr + " as " + expr_single
181
385
  #puts "query #{query}"
@@ -184,26 +388,3 @@ class ExistAPI
184
388
  raise e
185
389
  end
186
390
  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
@@ -29,7 +29,7 @@ class ExistException < RuntimeError
29
29
 
30
30
  end
31
31
 
32
- #Soupis jednotlivych vyjimek, v zavorce metoda tera tuto vyjimku vyhazuje
32
+ #List of all exception. In brackets is method, that raise mentioned exception.
33
33
  #1: Database login failed (DatabaseManager.connect)
34
34
  #2: Failed to create Collection (DatabaseManager.createcollection)
35
35
  #3: Failed to remove Collection (DatabaseManager.removecollection)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eXistAPI
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-02 00:00:00.000000000 Z
12
+ date: 2012-05-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! "API for eXist-db based on XML-RPC. \nAPI is using xQuery, xPath and
15
15
  xQuery Update Facility."