move example code SearchClient out of yacycore package

to example directory
pull/46/head
reger 8 years ago
parent 6a8f3f3eba
commit 6783ef5540

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.yacy</groupId>
<artifactId>SimpleSearchClient</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<description>Simple search client example to query YaCy and receive search results as RSS feed</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
</build>
</project>

@ -23,18 +23,17 @@
* If not, see <http://www.gnu.org/licenses/>. * If not, see <http://www.gnu.org/licenses/>.
*/ */
package net.yacy; package net.yacy.simplesearchclient;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import net.yacy.cora.util.CommonPattern;
import org.w3c.dom.CharacterData; import org.w3c.dom.CharacterData;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@ -55,6 +54,7 @@ public class YaCySearchClient {
private final String host, query; private final String host, query;
private final int port; private final int port;
private int offset; private int offset;
private final static Pattern SPACE = Pattern.compile(" ");
public YaCySearchClient(final String host, final int port, final String query) { public YaCySearchClient(final String host, final int port, final String query) {
this.host = host; this.host = host;
@ -80,7 +80,7 @@ public class YaCySearchClient {
.append("/yacysearch.rss?verify=false&startRecord=") .append("/yacysearch.rss?verify=false&startRecord=")
.append(YaCySearchClient.this.offset) .append(YaCySearchClient.this.offset)
.append("&maximumRecords=10&resource=local&query=") .append("&maximumRecords=10&resource=local&query=")
.append(CommonPattern.SPACE.matcher(YaCySearchClient.this.query).replaceAll("+")).toString(); .append(SPACE.matcher(YaCySearchClient.this.query).replaceAll("+")).toString();
try { url = new URL(u); } catch (final MalformedURLException e) { throw new IOException (e); } try { url = new URL(u); } catch (final MalformedURLException e) { throw new IOException (e); }
try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream()); } try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream()); }
catch (final ParserConfigurationException e) { throw new IOException (e); } catch (final ParserConfigurationException e) { throw new IOException (e); }
@ -131,14 +131,22 @@ public class YaCySearchClient {
* Use this method as stub for an integration in your own programs * Use this method as stub for an integration in your own programs
*/ */
public static void main(String[] args) { public static void main(String[] args) {
for (String query: args) try { if (args.length < 1) {
long t = System.currentTimeMillis(); System.out.println("No query string as argument found.");
YaCySearchClient search = new YaCySearchClient("localhost", 8090, query); System.out.println("Call the main method with one argument, the query string,");
System.out.println("Search result for '" + query + "':"); System.out.println("while YaCy is running on localhost.");
System.out.print(search.next().toString()); // get 10 results; you may repeat this for next 10 } else {
System.out.println("Search Time: " + (System.currentTimeMillis() - t) + " milliseconds\n"); for (String query : args) {
} catch (final IOException e) { try {
e.printStackTrace(); long t = System.currentTimeMillis();
YaCySearchClient search = new YaCySearchClient("localhost", 8090, query);
System.out.println("Search result for '" + query + "':");
System.out.print(search.next().toString()); // get 10 results; you may repeat this for next 10
System.out.println("Search Time: " + (System.currentTimeMillis() - t) + " milliseconds\n");
} catch (final IOException e) {
e.printStackTrace();
}
}
} }
} }
} }
Loading…
Cancel
Save