From 6783ef5540d630fc8c7543531c6ca32f1e47db79 Mon Sep 17 00:00:00 2001 From: reger Date: Mon, 14 Mar 2016 02:22:06 +0100 Subject: [PATCH] move example code SearchClient out of yacycore package to example directory --- examples/SimpleSearchClient/pom.xml | 20 ++++++++++++ .../src}/YaCySearchClient.java | 32 ++++++++++++------- 2 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 examples/SimpleSearchClient/pom.xml rename {source/net/yacy => examples/SimpleSearchClient/src}/YaCySearchClient.java (81%) diff --git a/examples/SimpleSearchClient/pom.xml b/examples/SimpleSearchClient/pom.xml new file mode 100644 index 000000000..87b8c08b3 --- /dev/null +++ b/examples/SimpleSearchClient/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + net.yacy + SimpleSearchClient + 1.0 + jar + Simple search client example to query YaCy and receive search results as RSS feed + + + UTF-8 + 1.7 + 1.7 + + + + src + + + \ No newline at end of file diff --git a/source/net/yacy/YaCySearchClient.java b/examples/SimpleSearchClient/src/YaCySearchClient.java similarity index 81% rename from source/net/yacy/YaCySearchClient.java rename to examples/SimpleSearchClient/src/YaCySearchClient.java index 77db5a4cf..0c2a6f535 100644 --- a/source/net/yacy/YaCySearchClient.java +++ b/examples/SimpleSearchClient/src/YaCySearchClient.java @@ -23,18 +23,17 @@ * If not, see . */ -package net.yacy; +package net.yacy.simplesearchclient; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; +import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import net.yacy.cora.util.CommonPattern; - import org.w3c.dom.CharacterData; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -55,6 +54,7 @@ public class YaCySearchClient { private final String host, query; private final int port; private int offset; + private final static Pattern SPACE = Pattern.compile(" "); public YaCySearchClient(final String host, final int port, final String query) { this.host = host; @@ -80,7 +80,7 @@ public class YaCySearchClient { .append("/yacysearch.rss?verify=false&startRecord=") .append(YaCySearchClient.this.offset) .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 { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream()); } 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 */ public static void main(String[] args) { - for (String query: args) try { - 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(); + if (args.length < 1) { + System.out.println("No query string as argument found."); + System.out.println("Call the main method with one argument, the query string,"); + System.out.println("while YaCy is running on localhost."); + } else { + for (String query : args) { + try { + 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(); + } + } } } }