mirror of https://github.com/apache/lucene.git
SOLR-122: Add optional support for libxml2, with fall back to REXML if libxml is not installed. (Contributed by Coda Hale)
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@504077 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
60766a8813
commit
8b194f6d2a
|
@ -15,3 +15,4 @@ require 'solr/exception'
|
||||||
require 'solr/request'
|
require 'solr/request'
|
||||||
require 'solr/connection'
|
require 'solr/connection'
|
||||||
require 'solr/response'
|
require 'solr/response'
|
||||||
|
require 'solr/xml'
|
|
@ -10,7 +10,7 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
require 'rexml/document'
|
require 'solr/xml'
|
||||||
require 'solr/field'
|
require 'solr/field'
|
||||||
|
|
||||||
class Solr::Document
|
class Solr::Document
|
||||||
|
@ -64,7 +64,7 @@ class Solr::Document
|
||||||
|
|
||||||
# convert the Document to a REXML::Element
|
# convert the Document to a REXML::Element
|
||||||
def to_xml
|
def to_xml
|
||||||
e = REXML::Element.new 'doc'
|
e = Solr::XML::Element.new 'doc'
|
||||||
@fields.each {|f| e.add_element(f.to_xml)}
|
@fields.each {|f| e.add_element(f.to_xml)}
|
||||||
return e
|
return e
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
require 'rexml/document'
|
require 'solr/xml'
|
||||||
require 'time'
|
require 'time'
|
||||||
|
|
||||||
class Solr::Field
|
class Solr::Field
|
||||||
|
@ -26,7 +26,7 @@ class Solr::Field
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_xml
|
def to_xml
|
||||||
e = REXML::Element.new 'field'
|
e = Solr::XML::Element.new 'field'
|
||||||
e.attributes['name'] = @name
|
e.attributes['name'] = @name
|
||||||
e.text = @value
|
e.text = @value
|
||||||
return e
|
return e
|
||||||
|
|
|
@ -10,10 +10,10 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
require 'solr/xml'
|
||||||
require 'solr/request/base'
|
require 'solr/request/base'
|
||||||
require 'solr/document'
|
require 'solr/document'
|
||||||
require 'solr/request/update'
|
require 'solr/request/update'
|
||||||
require 'rexml/document'
|
|
||||||
|
|
||||||
class Solr::Request::AddDocument < Solr::Request::Update
|
class Solr::Request::AddDocument < Solr::Request::Update
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ class Solr::Request::AddDocument < Solr::Request::Update
|
||||||
# returns the request as a string suitable for posting
|
# returns the request as a string suitable for posting
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
e = REXML::Element.new 'add'
|
e = Solr::XML::Element.new 'add'
|
||||||
for doc in @docs
|
for doc in @docs
|
||||||
e.add_element doc.to_xml
|
e.add_element doc.to_xml
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,12 +10,12 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
require 'rexml/document'
|
require 'solr/xml'
|
||||||
|
|
||||||
class Solr::Request::Commit < Solr::Request::Update
|
class Solr::Request::Commit < Solr::Request::Update
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
REXML::Element.new('commit').to_s
|
Solr::XML::Element.new('commit').to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
require 'rexml/document'
|
require 'solr/xml'
|
||||||
|
|
||||||
class Solr::Request::Delete < Solr::Request::Update
|
class Solr::Request::Delete < Solr::Request::Update
|
||||||
|
|
||||||
|
@ -34,13 +34,13 @@ class Solr::Request::Delete < Solr::Request::Update
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
delete_element = REXML::Element.new('delete')
|
delete_element = Solr::XML::Element.new('delete')
|
||||||
if @document_id
|
if @document_id
|
||||||
id_element = REXML::Element.new('id')
|
id_element = Solr::XML::Element.new('id')
|
||||||
id_element.text = @document_id
|
id_element.text = @document_id
|
||||||
delete_element.add_element(id_element)
|
delete_element.add_element(id_element)
|
||||||
elsif @query
|
elsif @query
|
||||||
query = REXML::Element.new('query')
|
query = Solr::XML::Element.new('query')
|
||||||
query.text = @query
|
query.text = @query
|
||||||
delete_element.add_element(query)
|
delete_element.add_element(query)
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
# (the "License"); you may not use this file except in compliance with
|
||||||
|
# the License. You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
module Solr::XML
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
|
||||||
|
# If we can load rubygems and libxml-ruby...
|
||||||
|
require 'rubygems'
|
||||||
|
require 'xml/libxml'
|
||||||
|
|
||||||
|
# then make a few modifications to XML::Node so it can stand in for REXML::Element
|
||||||
|
class XML::Node
|
||||||
|
# element.add_element(another_element) should work
|
||||||
|
alias_method :add_element, :<<
|
||||||
|
|
||||||
|
# element.attributes['blah'] should work
|
||||||
|
def attributes
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
# element.text = "blah" should work
|
||||||
|
def text=(x)
|
||||||
|
self.content = x.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# And use XML::Node for our XML generation
|
||||||
|
Solr::XML::Element = XML::Node
|
||||||
|
|
||||||
|
rescue LoadError => e # If we can't load either rubygems or libxml-ruby
|
||||||
|
|
||||||
|
# Just use REXML.
|
||||||
|
require 'rexml/document'
|
||||||
|
Solr::XML::Element = REXML::Element
|
||||||
|
|
||||||
|
end
|
|
@ -16,12 +16,12 @@ class DeleteTest < SolrMockBaseTestCase
|
||||||
|
|
||||||
def test_delete_request
|
def test_delete_request
|
||||||
request = Solr::Request::Delete.new(:id => '123')
|
request = Solr::Request::Delete.new(:id => '123')
|
||||||
assert_equal "<delete><id>123</id></delete>", request.to_s
|
assert_match(/<delete>[\s]*<id>123<\/id>[\s]*<\/delete>/m, request.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_delete_by_query_request
|
def test_delete_by_query_request
|
||||||
request = Solr::Request::Delete.new(:query => 'name:summers')
|
request = Solr::Request::Delete.new(:query => 'name:summers')
|
||||||
assert_equal "<delete><query>name:summers</query></delete>", request.to_s
|
assert_match(/<delete>[\s]*<query>name:summers<\/query>[\s]*<\/delete>/m, request.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_delete_response
|
def test_delete_response
|
||||||
|
@ -50,7 +50,7 @@ class DeleteTest < SolrMockBaseTestCase
|
||||||
|
|
||||||
def test_delete_by_i18n_query_request
|
def test_delete_by_i18n_query_request
|
||||||
request = Solr::Request::Delete.new(:query => 'ëäïöü')
|
request = Solr::Request::Delete.new(:query => 'ëäïöü')
|
||||||
assert_equal "<delete><query>ëäïöü</query></delete>", request.to_s
|
assert_match(/<delete>[\s]*<query>ëäïöü<\/query>[\s]*<\/delete>/m, request.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,21 +18,21 @@ class DocumentTest < Test::Unit::TestCase
|
||||||
def test_xml
|
def test_xml
|
||||||
doc = Solr::Document.new
|
doc = Solr::Document.new
|
||||||
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
||||||
assert_kind_of REXML::Element, doc.to_xml
|
assert_kind_of Solr::XML::Element, doc.to_xml
|
||||||
assert_equal "<doc><field name='creator'>Erik Hatcher</field></doc>",
|
assert_match(/<doc>[\s]*<field name=['"]creator['"]>Erik Hatcher<\/field>[\s]*<\/doc>/m, doc.to_xml.to_s)
|
||||||
doc.to_xml.to_s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_repeatable
|
def test_repeatable
|
||||||
doc = Solr::Document.new
|
doc = Solr::Document.new
|
||||||
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
doc << Solr::Field.new(:creator => 'Erik Hatcher')
|
||||||
doc << Solr::Field.new(:creator => 'Otis Gospodnetic')
|
doc << Solr::Field.new(:creator => 'Otis Gospodnetic')
|
||||||
assert_equal "<doc><field name='creator'>Erik Hatcher</field><field name='creator'>Otis Gospodnetic</field></doc>", doc.to_xml.to_s
|
assert_kind_of Solr::XML::Element, doc.to_xml
|
||||||
|
assert_match(/<doc>[\s]*<field name=['"]creator['"]>Erik Hatcher<\/field>[\s]*<field name=['"]creator['"]>Otis Gospodnetic<\/field>[\s]*<\/doc>/m, doc.to_xml.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_repeatable_in_hash
|
def test_repeatable_in_hash
|
||||||
doc = Solr::Document.new({:creator => ['Erik Hatcher', 'Otis Gospodnetic']})
|
doc = Solr::Document.new({:creator => ['Erik Hatcher', 'Otis Gospodnetic']})
|
||||||
assert_equal "<doc><field name='creator'>Erik Hatcher</field><field name='creator'>Otis Gospodnetic</field></doc>", doc.to_xml.to_s
|
assert_match(/<doc>[\s]*<field name=['"]creator['"]>Erik Hatcher<\/field>[\s]*<field name=['"]creator['"]>Otis Gospodnetic<\/field>[\s]*<\/doc>/m, doc.to_xml.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_bad_doc
|
def test_bad_doc
|
||||||
|
|
|
@ -17,20 +17,20 @@ class FieldTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_xml
|
def test_xml
|
||||||
field = Solr::Field.new :creator => 'Erik Hatcher'
|
field = Solr::Field.new :creator => 'Erik Hatcher'
|
||||||
assert_kind_of REXML::Element, field.to_xml
|
assert_kind_of Solr::XML::Element, field.to_xml
|
||||||
assert_equal "<field name='creator'>Erik Hatcher</field>", field.to_xml.to_s
|
assert_match(/<field name=["']creator["']>Erik Hatcher<\/field>/, field.to_xml.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_xml_date
|
def test_xml_date
|
||||||
field = Solr::Field.new :time => Time.now
|
field = Solr::Field.new :time => Time.now
|
||||||
assert_kind_of REXML::Element, field.to_xml
|
assert_kind_of Solr::XML::Element, field.to_xml
|
||||||
assert_match(/<field name='time'>[\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}Z<\/field>/, field.to_xml.to_s)
|
assert_match(/<field name=["']time["']>[\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}Z<\/field>/, field.to_xml.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_i18n_xml
|
def test_i18n_xml
|
||||||
field = Solr::Field.new :i18nstring => 'Äêâîôû Öëäïöü'
|
field = Solr::Field.new :i18nstring => 'Äêâîôû Öëäïöü'
|
||||||
assert_kind_of REXML::Element, field.to_xml
|
assert_kind_of Solr::XML::Element, field.to_xml
|
||||||
assert_equal "<field name='i18nstring'>Äêâîôû Öëäïöü</field>", field.to_xml.to_s
|
assert_match(/<field name=["']i18nstring["']>Äêâîôû Öëäïöü<\/field>/m, field.to_xml.to_s)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,7 +27,7 @@ class RequestTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_add_doc_request
|
def test_add_doc_request
|
||||||
request = Solr::Request::AddDocument.new(:title => "title")
|
request = Solr::Request::AddDocument.new(:title => "title")
|
||||||
assert_equal "<add><doc><field name='title'>title</field></doc></add>", request.to_s
|
assert_match(/<add>[\s]*<doc>[\s]*<field name=["']title["']>title<\/field>[\s]*<\/doc>[\s]*<\/add>/m, request.to_s)
|
||||||
assert_equal :xml, request.response_format
|
assert_equal :xml, request.response_format
|
||||||
assert_equal 'update', request.handler
|
assert_equal 'update', request.handler
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ class RequestTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_add_multidoc_request
|
def test_add_multidoc_request
|
||||||
request = Solr::Request::AddDocument.new([{:title => "title1"}, {:title => "title2"}])
|
request = Solr::Request::AddDocument.new([{:title => "title1"}, {:title => "title2"}])
|
||||||
assert_equal "<add><doc><field name='title'>title1</field></doc><doc><field name='title'>title2</field></doc></add>", request.to_s
|
assert_match(/<add>[\s]*<doc>[\s]*<field name=["']title["']>title1<\/field>[\s]*<\/doc>[\s]*<doc>[\s]*<field name=["']title["']>title2<\/field>[\s]*<\/doc>[\s]*<\/add>/m, request.to_s)
|
||||||
assert_equal :xml, request.response_format
|
assert_equal :xml, request.response_format
|
||||||
assert_equal 'update', request.handler
|
assert_equal 'update', request.handler
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue