forked from SolrNet/SolrNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
69 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,70 @@ | ||
#region license | ||
// Copyright (c) 2007-2010 Mauricio Scheffer | ||
// | ||
// Licensed 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. | ||
#endregion | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Xml.Linq; | ||
using System.Linq; | ||
|
||
namespace SolrNet.Impl.ResponseParsers { | ||
/// <summary> | ||
/// Parses documents from a query response | ||
/// </summary> | ||
/// <typeparam name="T">Document type</typeparam> | ||
public class ResultsResponseParser<T> : ISolrAbstractResponseParser<T> | ||
{ | ||
private readonly ISolrDocumentResponseParser<T> docParser; | ||
|
||
public ResultsResponseParser(ISolrDocumentResponseParser<T> docParser) { | ||
this.docParser = docParser; | ||
} | ||
|
||
public void Parse(XDocument xml, AbstractSolrQueryResults<T> results) { | ||
// IsNullOrEmpty part is needed to pass tests -- ptasz3k | ||
var resultNode = xml.Element("response").Elements("result").FirstOrDefault(e => String.IsNullOrEmpty((string)e.Attribute("name")) || (string)e.Attribute("name") == "response"); | ||
|
||
//FIX BY klaas | ||
//If resultNode == null exit func | ||
// This can occur when grouped results are returned | ||
//if (resultNode == null) | ||
//{ | ||
// return; | ||
//} | ||
|
||
//FIX by kanda | ||
// result (if present) to be parsed even if grouped results are present | ||
if (resultNode == null) | ||
{ | ||
var groupElement = xml.Element("response").Elements("lst").FirstOrDefault(e => (string)e.Attribute("name") == "grouped"); | ||
if (groupElement != null) | ||
{ | ||
resultNode = groupElement.Descendants().FirstOrDefault(e => e.Name == "result"); | ||
if (resultNode == null) return; | ||
} | ||
else | ||
return; | ||
} | ||
|
||
results.NumFound = Convert.ToInt32(resultNode.Attribute("numFound").Value); | ||
var maxScore = resultNode.Attribute("maxScore"); | ||
if (maxScore != null) { | ||
results.MaxScore = double.Parse(maxScore.Value, CultureInfo.InvariantCulture.NumberFormat); | ||
} | ||
|
||
foreach (var result in docParser.ParseResults(resultNode)) | ||
results.Add(result); | ||
} | ||
} | ||
#region license | ||
|
||
// Copyright (c) 2007-2010 Mauricio Scheffer | ||
// | ||
// Licensed 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. | ||
|
||
#endregion | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
|
||
namespace SolrNet.Impl.ResponseParsers { | ||
/// <summary> | ||
/// Parses documents from a query response | ||
/// </summary> | ||
/// <typeparam name="T">Document type</typeparam> | ||
public class ResultsResponseParser<T> : ISolrAbstractResponseParser<T> { | ||
private readonly ISolrDocumentResponseParser<T> docParser; | ||
|
||
public ResultsResponseParser(ISolrDocumentResponseParser<T> docParser) { | ||
this.docParser = docParser; | ||
} | ||
|
||
public void Parse(XDocument xml, AbstractSolrQueryResults<T> results) { | ||
// IsNullOrEmpty part is needed to pass tests -- ptasz3k | ||
var resultNode = xml.Element("response").Elements("result").FirstOrDefault(e => String.IsNullOrEmpty((string) e.Attribute("name")) || (string) e.Attribute("name") == "response"); | ||
|
||
//FIX BY klaas | ||
//If resultNode == null exit func | ||
// This can occur when grouped results are returned | ||
//if (resultNode == null) | ||
//{ | ||
// return; | ||
//} | ||
|
||
//FIX by kanda | ||
// result (if present) to be parsed even if grouped results are present | ||
if (resultNode == null) { | ||
var groupElement = xml.Element("response").Elements("lst").FirstOrDefault(e => (string) e.Attribute("name") == "grouped"); | ||
if (groupElement != null) { | ||
resultNode = groupElement.Descendants().FirstOrDefault(e => e.Name == "result"); | ||
if (resultNode == null) | ||
return; | ||
} else | ||
return; | ||
} | ||
|
||
results.NumFound = Convert.ToInt32(resultNode.Attribute("numFound").Value); | ||
var maxScore = resultNode.Attribute("maxScore"); | ||
if (maxScore != null) { | ||
results.MaxScore = double.Parse(maxScore.Value, CultureInfo.InvariantCulture.NumberFormat); | ||
} | ||
|
||
foreach (var result in docParser.ParseResults(resultNode)) | ||
results.Add(result); | ||
} | ||
} | ||
} |