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

io.earcam.utilitarian.site.search.offline.Search Maven / Gradle / Ivy

/*-
 * #%L
 * io.earcam.utilitarian.site.search.offline
 * %%
 * Copyright (C) 2017 earcam
 * %%
 * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
 *
 * You must choose to accept, in full - any individual or combination of
 * the following licenses:
 * 
 * #L%
 */
package io.earcam.utilitarian.site.search.offline;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import io.earcam.unexceptional.Exceptional;
import io.earcam.utilitarian.site.search.offline.ConfigurationModel.Crawling;
import io.earcam.utilitarian.site.search.offline.ConfigurationModel.Indexing;
import io.earcam.utilitarian.site.search.offline.jsonb.JsonBind;

public class Search {

	public static void main(String[] args)
	{
		System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "trace");
		if(args.length < 2) {
			throw new IllegalArgumentException(
					"Expected first arg to be json-output-dir, then at least a further argument of the form 'baseDir@baseUri'");
		}

		final String jsonDir = args[0];
		Indexer indexer = null;
		for(int i = 1; i < args.length; i++) {
			int index = args[i].indexOf('@');
			if(index == -1) {
				throw new IllegalArgumentException(
						"Only arguments expected are 'baseDir@baseUri', recieved '" +
								args[i] + "' at index " + i);
			}
			Path baseDir = Paths.get(args[i].substring(0, index));
			URI baseUri = Exceptional.uri(args[i].substring(index + 1));
			Map searchReplace = searchReplaceMap(jsonDir, baseDir, baseUri);

			String crawlerJson = Resources.getResource(Resources.DEFAULT_CRAWLER_JSON, UTF_8, searchReplace);
			Crawling crawler = JsonBind.readJson(crawlerJson, Crawling.class);

			if(indexer == null) {
				String indexerJson = Resources.getResource(Resources.DEFAULT_INDEXER_JSON, UTF_8, searchReplace);
				indexer = JsonBind.readJson(indexerJson, Indexing.class).build();
			}
			indexer.add(crawler.build().documents());
		}
		// @SuppressWarnings("squid:S2259")
		indexer.writeJson();  // NOSONAR false positive
	}


	private static Map searchReplaceMap(String jsonDir, Path baseDir, URI baseUri)
	{
		Map searchReplace = new HashMap<>();
		searchReplace.put("${outputCharset}", UTF_8.name());
		searchReplace.put("${jsonDir}", jsonDir);
		searchReplace.put("${baseDir}", baseDir.toAbsolutePath().toString());
		searchReplace.put("${baseUri}", baseUri.toString());
		return searchReplace;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy