All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jaeksoft.searchlib.util.map.SourceField Maven / Gradle / Ivy

Go to download

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) 2012-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.util.map;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jaeksoft.searchlib.SearchLibException;
import com.jaeksoft.searchlib.index.FieldContent;
import com.jaeksoft.searchlib.index.IndexDocument;
import com.jaeksoft.searchlib.request.AbstractSearchRequest;
import com.jaeksoft.searchlib.result.ResultDocument;
import com.jaeksoft.searchlib.schema.FieldValueItem;

public class SourceField implements Comparable {

	final private String[] names;

	final private char separator;

	public SourceField(String name) {
		names = new String[1];
		names[0] = name.intern();
		separator = '|';
	}

	public SourceField(String name, char separator) {
		String[] splitNames = StringUtils.split(name, separator);
		names = new String[splitNames.length];
		int i = 0;
		for (String splitName : splitNames)
			names[i++] = splitName.intern();
		this.separator = separator;
	}

	@Override
	final public boolean equals(Object o) {
		if (!(o instanceof SourceField))
			return false;
		SourceField s2 = (SourceField) o;
		if (s2.names.length != names.length)
			return false;
		for (int i = 0; i < names.length; i++)
			if (!names[i].equals(s2.names[i]))
				return false;
		return true;
	}

	final public boolean contains(String source) {
		if (names == null || source == null)
			return false;
		for (String name : names)
			if (source.equals(name))
				return true;
		return false;
	}

	@Override
	final public int compareTo(SourceField o) {
		int l = names.length;
		if (o.names.length < l)
			l = o.names.length;
		for (int i = 0; i < l; i++) {
			int c = names[i].compareTo(o.names[i]);
			if (c != 0)
				return c;
		}
		return o.names.length - names.length;
	}

	final public String toXmlAttribute() {
		if (names.length == 1)
			return names[0];
		StringBuilder sb = new StringBuilder(names[0]);
		for (int i = 1; i < names.length; i++) {
			sb.append(separator);
			sb.append(names[i]);
		}
		return sb.toString();
	}

	final public boolean isUnique() {
		return names.length == 1;
	}

	final public String getUniqueName() {
		return names[0];
	}

	final public FieldContent getUniqueString(IndexDocument source) {
		return source.getField(names[0]);
	}

	final public List getUniqueString(ResultDocument source) {
		return source.getValues(names[0]);
	}

	final public String getConcatString(IndexDocument source,
			IndexDocument target) {
		StringBuilder sb = new StringBuilder();
		for (String name : names) {
			FieldValueItem fvi = source.getFieldValue(name, 0);
			if (fvi == null)
				fvi = target.getFieldValue(name, 0);
			if (fvi != null) {
				String value = fvi.getValue();
				if (value != null) {
					if (sb.length() != 0)
						sb.append(separator);
					sb.append(value);
				}
			}
		}
		return sb.length() == 0 ? null : sb.toString();
	}

	public String getConcatString(ResultDocument source, IndexDocument target) {
		StringBuilder sb = new StringBuilder();
		for (String name : names) {
			String value = source.getValueContent(name, 0);
			if (value == null) {
				FieldValueItem fvi = target.getFieldValue(name, 0);
				if (fvi != null)
					value = fvi.getValue();
			}
			if (value != null) {
				if (sb.length() != 0)
					sb.append(separator);
				sb.append(value);
			}
		}
		return sb.length() == 0 ? null : sb.toString();
	}

	public void addReturnField(AbstractSearchRequest searchRequest)
			throws SearchLibException {
		for (String name : names)
			searchRequest.addReturnField(name);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy