Class: ExistAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/eXistAPI.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri = nil, username = nil, password = nil) ⇒ ExistAPI

example ExistAPI.new(“localhost:8080/exist/xmlrpc”, “admin”, “password”)



6
7
8
9
10
11
12
13
# File 'lib/eXistAPI.rb', line 6

def initialize(uri = nil , username = nil, password = nil)
  @uri = uri
  @username = username
  @password = password
  @parameters = { "encoding" => "UTF-8", "indent" => "yes"}
  #outputoptions = { "encoding" => "UTF-8", "indent" => "yes"}
  connect
end

Instance Method Details

#createcollection(_name, _parent = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/eXistAPI.rb', line 29

def createcollection(_name, _parent = nil)
  if (_parent == nil)
    begin
      result = @client.call("createCollection", _name)
      return result
    end
  else
    begin
      colname = _parent.getname + _name
      result = @client.call("createCollection", colname)
      return result
    end
  end
rescue XMLRPC::FaultException => e
  raise e    
rescue
  raise ExistException.new("Failed to create Collection", 2), caller
end

#execute_query(xquery, parameters = @parameters) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/eXistAPI.rb', line 113

def execute_query(xquery, parameters = @parameters)
  #puts xquery
  begin
    handle = @client.call("executeQuery", XMLRPC::Base64.new(xquery), parameters)
    return handle
  rescue XMLRPC::FaultException => e
    raise e    
  rescue
    raise ExistException.new("Failed to execute query", 6), caller
  end
end

#existscollection?(orig_path) ⇒ Boolean

Returns:

  • (Boolean)


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
85
86
# File 'lib/eXistAPI.rb', line 58

def existscollection?(orig_path)
  collections = orig_path.split("/")
  collections.delete("")
  i=0
  path = "/" 
  while(i < collections.length)
    path = path + collections[i] + "/"
    col = Collection.new(@client, path)
    if (!col.childCollection.include?(collections[i+1]))
      break
    end
    i = i + 1
  end
  
  if(path[-1]=="/")
    path = path[0..-2]
  end
  if(orig_path[-1]=="/")
    orig_path = orig_path[0..-2]
  end
  
  if(path == orig_path)
    return true
  else
    return false
  end
rescue  => e
  raise e  
end

#get_hits(handle) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/eXistAPI.rb', line 136

def get_hits(handle)
  begin
    summary = @client.call("querySummary", handle)
    hits = summary['hits']
    return hits
  rescue XMLRPC::FaultException => e
    raise e
  rescue
    raise ExistException.new("Failed to get number of hits", 8), caller
  end
end

#getcollection(path) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/eXistAPI.rb', line 48

def getcollection(path)
  if(path[-1] != "/")    #if last is not "/" then add it
    path += "/"
  end
  col = Collection.new(@client, path)
  return col
rescue  => e
  raise e  
end

#remove_collection(_name) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/eXistAPI.rb', line 88

def remove_collection(_name)
  # boolean removeCollection( String collection)
  result = @client.call("removeCollection", _name)
  return result
rescue XMLRPC::FaultException => e
  raise e    
rescue
  raise ExistException.new("Failed to remove Collection", 3), caller
end

#retrieve(handle, pos) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/eXistAPI.rb', line 125

def retrieve(handle, pos)
  begin
    res = @client.call("retrieve", handle, pos, @parameters)
    return res
  rescue XMLRPC::FaultException => e
    raise e   
  rescue
    raise ExistException.new("Failed to retrive resource", 7), caller
  end
end

#storeresource(_res, _docname, _overwrite = 1) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/eXistAPI.rb', line 98

def storeresource(_res, _docname, _overwrite = 1)
  if ((_res == nil)||(_docname == nil))
    raise ExistException.new("Resource or document name is nil", 4), caller
  end
  begin
    @client.call("parse",_res.to_s, _docname, _overwrite)
  rescue XMLRPC::FaultException => e
    raise e    
  rescue ExistException => e
    raise e
  rescue
    raise ExistException.new("Failed to store resource", 5), caller
  end
end

#update_delete(expr) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/eXistAPI.rb', line 171

def update_delete(expr)
  query = "update delete " + expr
  #puts "query #{query}"
  execute_query(query)
rescue  => e
  raise e  
end

#update_insert(expr, pos, expr_single) ⇒ Object

<email type=“office”>[email protected]</email>, pos = into | following | preceding, expr_single e.g. //address



149
150
151
152
153
154
155
# File 'lib/eXistAPI.rb', line 149

def update_insert(expr, pos, expr_single) 
  query = "update insert "+expr+" "+pos+" "+expr_single
  #puts "query #{query}"
  execute_query(query)
rescue  => e
  raise e    
end

#update_rename(expr, expr_single) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/eXistAPI.rb', line 179

def update_rename(expr, expr_single)
  query = "update rename " + expr + " as " + expr_single
  #puts "query #{query}"
  execute_query(query)
rescue  => e
  raise e  
end

#update_replace(expr, expr_single) ⇒ Object



157
158
159
160
161
162
# File 'lib/eXistAPI.rb', line 157

def update_replace(expr, expr_single)
  query = "update replace "+expr+" with "+expr_single
  execute_query(query)
rescue  => e
  raise e  
end

#update_value(expr, expr_single) ⇒ Object



164
165
166
167
168
169
# File 'lib/eXistAPI.rb', line 164

def update_value(expr, expr_single)
  query = "update replace " + expr + " with " + expr_single
  execute_query(query)
rescue  => e
  raise e  
end