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

com.jaeksoft.searchlib.crawler.rest.RestCrawlList 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.

There is a newer version: 1.5.14
Show newest version
/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2013 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.crawler.rest;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.TreeMap;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.jaeksoft.searchlib.util.ReadWriteLock;
import com.jaeksoft.searchlib.util.XPathParser;
import com.jaeksoft.searchlib.util.XmlWriter;

public class RestCrawlList {

	final private ReadWriteLock rwl = new ReadWriteLock();

	private TreeMap map;
	private RestCrawlItem[] array;

	private RestCrawlList() {
		map = new TreeMap();
	}

	private final static String REST_CRAWLLIST_ROOTNODE_NAME = "restCrawlList";

	public static RestCrawlList fromXml(RestCrawlMaster crawlMaster, File file)
			throws XPathExpressionException, ParserConfigurationException,
			SAXException, IOException {
		RestCrawlList crawlList = new RestCrawlList();
		if (!file.exists())
			return crawlList;
		XPathParser xpp = new XPathParser(file);
		Node rootNode = xpp.getNode(REST_CRAWLLIST_ROOTNODE_NAME);
		if (rootNode == null)
			return crawlList;
		NodeList nodes = xpp.getNodeList(rootNode,
				RestCrawlItem.REST_CRAWL_NODE_NAME);
		if (nodes == null)
			return crawlList;
		for (int i = 0; i < nodes.getLength(); i++) {
			RestCrawlItem crawlItem = new RestCrawlItem(crawlMaster, xpp,
					nodes.item(i));
			if (crawlItem != null)
				crawlList.add(crawlItem);
		}
		return crawlList;
	}

	public void add(RestCrawlItem crawlItem) {
		rwl.w.lock();
		try {
			map.put(crawlItem.getName(), crawlItem);
			buildArray();
		} finally {
			rwl.w.unlock();
		}
	}

	public RestCrawlItem get(String name) {
		rwl.r.lock();
		try {
			return map.get(name);
		} finally {
			rwl.r.unlock();
		}
	}

	public void remove(RestCrawlItem crawlItem) {
		rwl.w.lock();
		try {
			map.remove(crawlItem.getName());
			buildArray();
		} finally {
			rwl.w.unlock();
		}
	}

	public void writeXml(XmlWriter xmlWriter) throws SAXException,
			UnsupportedEncodingException {
		rwl.r.lock();
		try {
			xmlWriter.startElement(REST_CRAWLLIST_ROOTNODE_NAME);
			for (RestCrawlItem item : map.values())
				item.writeXml(xmlWriter);
			xmlWriter.endElement();
		} finally {
			rwl.r.unlock();
		}
	}

	private void buildArray() {
		array = new RestCrawlItem[map.size()];
		map.values().toArray(array);
	}

	public RestCrawlItem[] getArray() {
		rwl.r.lock();
		try {
			return array;
		} finally {
			rwl.r.unlock();
		}
	}

	public String[] getNameArray() {
		rwl.r.lock();
		try {
			if (array == null)
				return null;
			String[] names = new String[array.length];
			int i = 0;
			for (RestCrawlItem item : array)
				names[i++] = item.getName();
			return names;
		} finally {
			rwl.r.unlock();
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy