com.jaeksoft.searchlib.render.RenderOpenSearch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensearchserver Show documentation
Show all versions of opensearchserver Show documentation
OpenSearchServer is a powerful, enterprise-class, search engine program. Using the web user interface, the crawlers (web, file, database, ...) and the REST/RESTFul API you will be able to integrate quickly and easily advanced full-text search capabilities in your application. OpenSearchServer runs on Windows and Linux/Unix/BSD.
The newest version!
/**
* License Agreement for OpenSearchServer
*
* Copyright (C)2011-2012 Emmanuel Keller / Jaeksoft
*
* http://www.open-search-server.com
*
* This file is part of OpenSearchServer.
*
* OpenSearchServer is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSearchServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSearchServer.
* If not, see .
**/
package com.jaeksoft.searchlib.render;
/**
* @author naveen
*
*/
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.xml.sax.SAXException;
import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.facet.Facet;
import com.jaeksoft.searchlib.facet.FacetCounter;
import com.jaeksoft.searchlib.facet.FacetField;
import com.jaeksoft.searchlib.facet.FacetList;
import com.jaeksoft.searchlib.function.expression.SyntaxError;
import com.jaeksoft.searchlib.query.ParseException;
import com.jaeksoft.searchlib.request.AbstractSearchRequest;
import com.jaeksoft.searchlib.request.ReturnField;
import com.jaeksoft.searchlib.result.AbstractResultSearch;
import com.jaeksoft.searchlib.result.ResultDocument;
import com.jaeksoft.searchlib.schema.FieldValueItem;
import com.jaeksoft.searchlib.snippet.SnippetField;
import com.jaeksoft.searchlib.web.ServletTransaction;
public class RenderOpenSearch implements Render {
private PrintWriter writer;
private AbstractResultSearch> result;
private AbstractSearchRequest searchRequest;
private Matcher controlMatcher;
private String serverURL;
private String outputEncoding;
public RenderOpenSearch(AbstractResultSearch> result, String serverURL, String outputEncoding) {
this.result = result;
this.searchRequest = result.getRequest();
this.serverURL = serverURL;
Pattern p = Pattern.compile("\\p{Cntrl}");
controlMatcher = p.matcher("");
this.outputEncoding = outputEncoding;
}
private String xmlTextRender(String text) {
controlMatcher.reset(text);
return StringEscapeUtils.escapeXml(controlMatcher.replaceAll(""));
}
private void renderPrefix() throws ParseException, SyntaxError, SearchLibException, IOException {
String encoding = null;
if (outputEncoding != null && !outputEncoding.equals(""))
encoding = outputEncoding;
else
encoding = "UTF-8";
writer.println("");
writer.println("");
writer.println("");
writer.println("\t");
writer.print("OpenSearchServer: ");
writer.print(StringEscapeUtils.escapeXml(searchRequest.getQueryString()));
writer.print("\t ");
writer.println("\t");
writer.print("Search results for ");
writer.print("\"");
writer.print(StringEscapeUtils.escapeXml(searchRequest.getQueryString()));
writer.print("\"");
writer.print("\t ");
writer.println("\t");
writer.print(StringEscapeUtils.escapeXml(serverURL));
writer.print("\t");
}
private void renderSuffix() {
writer.println(" ");
writer.println(" ");
}
private void renderDocuments() throws IOException, ParseException, SyntaxError, XPathExpressionException,
ParserConfigurationException, SAXException, SearchLibException, TransformerConfigurationException {
AbstractSearchRequest searchRequest = result.getRequest();
int start = searchRequest.getStart();
int end = result.getDocumentCount() + searchRequest.getStart();
writer.println("\t");
writer.print(result.getNumFound());
writer.print("\t ");
writer.println("\t");
writer.print(searchRequest.getStart());
writer.print("\t ");
writer.println("\t");
writer.print(searchRequest.getRows());
writer.print("\t ");
writer.println("\t ");
for (int i = start; i < end; i++)
this.renderDocument(i);
}
private void renderDocument(int pos) throws IOException, ParseException, SyntaxError, XPathExpressionException,
ParserConfigurationException, SAXException, SearchLibException, TransformerConfigurationException {
writer.println("- ");
ResultDocument doc = result.getDocument(pos, null);
for (SnippetField field : searchRequest.getSnippetFieldList())
renderSnippetValue(doc, field);
for (ReturnField field : searchRequest.getReturnFieldList())
renderField(doc, field);
int cc = result.getCollapseCount(pos);
if (cc > 0) {
writer.print("\t\t
");
writer.print(cc);
writer.println(" ");
}
writer.println("\t ");
}
private void renderField(ResultDocument doc, ReturnField field) throws IOException, XPathExpressionException,
ParserConfigurationException, SAXException, TransformerConfigurationException, SearchLibException {
String fieldName = field.getName();
List values = doc.getValues(field);
String openSearchtitleField = getFieldMap("opensearch", "title");
String openSearchDescriptionField = getFieldMap("opensearch", "description");
String openSearchUrlField = getFieldMap("opensearch", "url");
if (values != null)
for (FieldValueItem v : values) {
if (openSearchtitleField != null && openSearchtitleField.equalsIgnoreCase(fieldName)) {
writer.print("\t");
writer.print(StringEscapeUtils.escapeXml(v.getValue()));
writer.println(" ");
} else if (openSearchDescriptionField != null
&& openSearchDescriptionField.equalsIgnoreCase(fieldName)) {
writer.print("\t");
writer.print(StringEscapeUtils.escapeXml(v.getValue()));
writer.println(" ");
} else if (openSearchUrlField != null && openSearchUrlField.equalsIgnoreCase(fieldName)) {
writer.print("\t");
writer.print(StringEscapeUtils.escapeXml(v.getValue()));
writer.println("");
} else {
writer.print("\t\t');
writer.print(xmlTextRender(v.getValue()));
writer.print(" ');
}
}
}
private String getFieldMap(String apiName, String field)
throws XPathExpressionException, ParserConfigurationException, SAXException, IOException,
TransformerConfigurationException, SearchLibException {
return searchRequest.getConfig().getApiManager().getMappedValue(apiName, field);
}
private void renderSnippetValue(ResultDocument doc, SnippetField field)
throws IOException, XPathExpressionException, ParserConfigurationException, SAXException,
TransformerConfigurationException, SearchLibException {
String fieldName = field.getName();
List snippets = doc.getSnippetValues(field);
if (snippets == null)
return;
String openSearchtitleField = getFieldMap("opensearch", "title");
String openSearchDescriptionField = getFieldMap("opensearch", "description");
String openSearchUrlField = getFieldMap("opensearch", "url");
for (FieldValueItem snippet : snippets) {
if (openSearchtitleField != null && openSearchtitleField.equalsIgnoreCase(fieldName)) {
writer.print("\t");
writer.print(StringEscapeUtils.escapeXml(snippet.getValue()));
writer.println(" ");
} else if (openSearchDescriptionField != null && openSearchDescriptionField.equalsIgnoreCase(fieldName)) {
writer.print("\t");
writer.print(StringEscapeUtils.escapeXml(snippet.getValue()));
writer.println(" ");
} else if (openSearchUrlField != null && openSearchUrlField.equalsIgnoreCase(fieldName)) {
writer.print("\t");
writer.print(StringEscapeUtils.escapeXml(snippet.getValue()));
writer.println("");
} else {
writer.print("\t\t');
writer.print(xmlTextRender(snippet.getValue()));
writer.print(" ');
}
}
}
private void renderFacet(Facet facet) throws Exception {
FacetField facetField = facet.getFacetField();
writer.print("\t\t");
for (Map.Entry facetItem : facet) {
writer.print("\t\t");
writer.print(facetItem.getValue().count);
writer.print(" ");
}
writer.print(" ");
}
private void renderFacets() throws Exception {
FacetList facetList = result.getFacetList();
if (facetList == null)
return;
writer.println("\t\t\t");
for (Facet facet : facetList)
renderFacet(facet);
writer.println(" ");
}
// private void renderSpellCheck(SpellCheck spellCheck) throws Exception {
// String fieldName = spellCheck.getFieldName();
// writer.print("\t\t");
// for (SpellCheckItem spellCheckItem : spellCheck) {
// writer.print("\t\t\t");
// for (SuggestionItem suggest : spellCheckItem.getSuggestions()) {
// writer.print("\t\t\t\t");
// writer.print("\t\t\t\t\t");
// writer.print(StringEscapeUtils.escapeXml(suggest.getTerm()));
// writer.println(" ");
// writer.print("\t\t\t\t\t");
// writer.print(suggest.getFreq());
// writer.println(" ");
// writer.println(" ");
// }
// writer.print(" ");
// }
// writer.print("\t\t ");
// }
// private void renderSpellChecks() throws Exception {
// List spellChecklist = result.getSpellCheckList();
// if (spellChecklist == null)
// return;
// writer.println("\t\t\t");
// for (SpellCheck spellCheck : spellChecklist)
// renderSpellCheck(spellCheck);
// writer.println(" ");
// }
public void render(PrintWriter writer) throws Exception {
this.writer = writer;
renderPrefix();
renderDocuments();
renderFacets();
// renderSpellChecks();
renderSuffix();
}
@Override
public void render(ServletTransaction servletTransaction) throws Exception {
servletTransaction.setResponseContentType("text/xml");
if (outputEncoding != null && !outputEncoding.equals(""))
render(servletTransaction.getWriter(outputEncoding));
else
render(servletTransaction.getWriter("UTF-8"));
}
}