Skip to content

Commit

Permalink
Code reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Jun 26, 2013
1 parent e899a86 commit fe5ae4c
Showing 1 changed file with 69 additions and 70 deletions.
139 changes: 69 additions & 70 deletions SolrNet/Impl/ResponseParsers/ResultsResponseParser.cs
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);
}
}
}

0 comments on commit fe5ae4c

Please sign in to comment.