
com.jaeksoft.searchlib.renderer.RendererField 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.
/**
* License Agreement for OpenSearchServer
*
* Copyright (C) 2011-2014 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.renderer;
import java.util.List;
import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import com.jaeksoft.searchlib.result.ResultDocument;
import com.jaeksoft.searchlib.schema.FieldValueItem;
import com.jaeksoft.searchlib.util.DomUtils;
import com.jaeksoft.searchlib.util.StringUtils;
import com.jaeksoft.searchlib.util.XPathParser;
import com.jaeksoft.searchlib.util.XmlWriter;
public class RendererField {
private String fieldName;
private RendererFieldType fieldType;
/**
* @deprecated
*/
private String oldStyle;
private String urlFieldName;
private RendererWidgets widgetName;
private String pattern;
private String replacement;
private final static String RENDERER_FIELD_ATTR_FIELDNAME = "fieldName";
private final static String RENDERER_FIELD_ATTR_FIELD_TYPE = "fieldType";
private final static String RENDERER_FIELD_NODE_CSS_STYLE = "cssStyle";
private final static String RENDERER_FIELD_ATTR_URL_FIELDNAME = "urlFieldName";
private final static String RENDERER_FIELD_ATTR_WIDGETNAME = "widgetName";
private final static String RENDERER_FIELD_ATTR_REGEXP_PATTERN = "regexpPattern";
private final static String RENDERER_FIELD_ATTR_REGEXP_REPLACE = "regexpReplace";
public RendererField() {
fieldName = StringUtils.EMPTY;
fieldType = RendererFieldType.FIELD;
oldStyle = StringUtils.EMPTY;
urlFieldName = StringUtils.EMPTY;
widgetName = RendererWidgets.TEXT;
pattern = null;
replacement = null;
}
public RendererField(XPathParser xpp, Node node)
throws XPathExpressionException {
fieldName = XPathParser.getAttributeString(node,
RENDERER_FIELD_ATTR_FIELDNAME);
setFieldType(RendererFieldType.find(XPathParser.getAttributeString(
node, RENDERER_FIELD_ATTR_FIELD_TYPE)));
oldStyle = xpp.getSubNodeTextIfAny(node, RENDERER_FIELD_NODE_CSS_STYLE,
true);
urlFieldName = XPathParser.getAttributeString(node,
RENDERER_FIELD_ATTR_URL_FIELDNAME);
setWidgetName(RendererWidgets.find(XPathParser.getAttributeString(node,
RENDERER_FIELD_ATTR_WIDGETNAME)));
setPattern(DomUtils.getAttributeText(node,
RENDERER_FIELD_ATTR_REGEXP_PATTERN));
setReplacement(DomUtils.getAttributeText(node,
RENDERER_FIELD_ATTR_REGEXP_REPLACE));
}
public RendererField(RendererField field) {
field.copyTo(this);
}
public void copyTo(RendererField target) {
target.fieldName = fieldName;
target.fieldType = fieldType;
target.oldStyle = oldStyle;
target.urlFieldName = urlFieldName;
target.widgetName = widgetName;
target.pattern = pattern;
target.replacement = replacement;
}
/**
* @return the fieldName
*/
public String getFieldName() {
return fieldName;
}
/**
* @param fieldName
* the fieldName to set
*/
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
/**
* @return the style
*/
public String getOldStyle() {
return oldStyle;
}
/**
* @return the urlFieldName
*/
public String getUrlFieldName() {
return urlFieldName;
}
/**
* @param urlFieldName
* the urlFieldName to set
*/
public void setUrlFieldName(String urlFieldName) {
this.urlFieldName = urlFieldName;
}
/**
* @param fieldType
* the fieldType to set
*/
public void setFieldType(RendererFieldType fieldType) {
this.fieldType = fieldType;
}
/**
* @return the fieldType
*/
public RendererFieldType getFieldType() {
return fieldType;
}
private String[] getValues(List fieldValueItems,
boolean replace) {
if (fieldValueItems == null)
return null;
replace = replace && !StringUtils.isEmpty(pattern);
String[] fields = new String[fieldValueItems.size()];
int i = 0;
for (FieldValueItem fieldValueItem : fieldValueItems) {
String value = fieldValueItem.value;
if (value != null && replace)
value = value.replaceAll(pattern, replacement);
fields[i++] = value;
}
return fields;
}
final public String[] getFieldValue(ResultDocument resultDocument) {
if (fieldType == RendererFieldType.FIELD) {
boolean replace = StringUtils.isEmpty(urlFieldName)
|| (urlFieldName != null && urlFieldName.equals(fieldName));
return getValues(resultDocument.getValues(fieldName), replace);
} else if (fieldType == RendererFieldType.SNIPPET)
return getValues(resultDocument.getSnippetValues(fieldName), false);
return null;
}
final public String getUrlField(ResultDocument resultDocument) {
if (urlFieldName == null)
return null;
String url = resultDocument.getValueContent(urlFieldName, 0);
if (url == null)
return null;
if (url.length() == 0)
return null;
if (!(StringUtils.isEmpty(pattern))
&& !(StringUtils.isEmpty(replacement)))
url = url.replaceAll(pattern, replacement);
return url;
}
public RendererWidgets getWidgetName() {
return widgetName;
}
public void setWidgetName(RendererWidgets widgetName) {
this.widgetName = widgetName;
}
public void writeXml(XmlWriter xmlWriter, String nodeName)
throws SAXException {
xmlWriter.startElement(nodeName, RENDERER_FIELD_ATTR_FIELDNAME,
fieldName, RENDERER_FIELD_ATTR_FIELD_TYPE, fieldType.name(),
RENDERER_FIELD_ATTR_URL_FIELDNAME, urlFieldName,
RENDERER_FIELD_ATTR_WIDGETNAME, widgetName.name(),
RENDERER_FIELD_ATTR_REGEXP_PATTERN, pattern,
RENDERER_FIELD_ATTR_REGEXP_REPLACE, replacement);
xmlWriter.endElement();
}
/**
* @return the pattern
*/
public String getPattern() {
return pattern;
}
/**
* @param pattern
* the pattern to set
*/
public void setPattern(String pattern) {
this.pattern = pattern;
}
/**
* @return the replacement
*/
public String getReplacement() {
return replacement;
}
/**
* @param replacement
* the replacement to set
*/
public void setReplacement(String replacement) {
this.replacement = replacement;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy