Fixed incorrect segment offsets.

Change-Id: I5cc0ad18df12875e503be3a7b16f32a1b5feedcf
diff --git a/src/main/java/de/ids_mannheim/korap/sru/AnnotationHandler.java b/src/main/java/de/ids_mannheim/korap/sru/AnnotationHandler.java
index 71196d1..72cb286 100644
--- a/src/main/java/de/ids_mannheim/korap/sru/AnnotationHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/sru/AnnotationHandler.java
@@ -4,8 +4,6 @@
 import java.util.List;
 import java.util.Map;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
@@ -19,8 +17,8 @@
  */
 public class AnnotationHandler extends DefaultHandler {
 
-    private Logger logger = (Logger) LoggerFactory
-            .getLogger(AnnotationHandler.class);
+//    private Logger logger = (Logger) LoggerFactory
+//            .getLogger(AnnotationHandler.class);
 
     private boolean startSegment = true;
     private boolean startSentence = false;
@@ -163,8 +161,14 @@
         // add a text segment to the text layer
         text = textBuilder.toString();
         textBuilder = new StringBuilder();
-        addAnnotationToMap(text, annotationLayers.get(0), matchLevel,
-                textStartOffset, textEndOffset);
+        if (!text.isEmpty()) {
+            addAnnotationToMap(text, annotationLayers.get(0), matchLevel,
+                    textStartOffset, textEndOffset);
+        }
+        textStartOffset = 0;
+        textEndOffset = 0;
+        segmentStartOffset = 0; 
+        segmentEndOffset = 0;
     }
 
     @Override
diff --git a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
index 042851c..fdd0c8b 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
@@ -4,19 +4,13 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URI;
 import java.net.URISyntaxException;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
-import org.apache.http.HttpResponse;
 import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.utils.URIBuilder;
-import org.apache.http.impl.client.HttpClients;
 import org.junit.Test;
 import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
@@ -24,6 +18,8 @@
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
+import com.sun.jersey.api.client.ClientResponse;
+
 /**
  * The tests are based on the sample corpus from the Goethe corpus.
  * 
@@ -35,8 +31,6 @@
  *
  */
 public class KorapSRUTest extends KorapJerseyTest{
-	private int port = 8080;
-	private String host = "localhost";
 	private DocumentBuilder docBuilder;
 	
 	public KorapSRUTest() throws ParserConfigurationException {
@@ -45,57 +39,52 @@
 	}
 	
 	@Test
-	public void searchRetrieveCQLTest() throws IOException, URISyntaxException, IllegalStateException, SAXException{
-		HttpClient httpclient = HttpClients.createDefault();
-		URIBuilder builder = new URIBuilder();
-		builder.setScheme("http").setHost(host).setPort(port).setPath("/KorapSRU")
-				.setParameter("operation", "searchRetrieve")
-				.setParameter("query", "fein");
-		
-		URI uri = builder.build();
-		HttpGet request = new HttpGet(uri);
-		HttpResponse response = httpclient.execute(request);
-		checkSRUSearchRetrieveResponse(response);
+	public void searchRetrieveCQLTest() throws IOException, SAXException{
+	    ClientResponse response = resource().queryParam("operation", "searchRetrieve")
+                .queryParam("query", "fein")
+                .get(ClientResponse.class);
+        
+	    InputStream entity = response.getEntity(InputStream.class);
+        checkSRUSearchRetrieveResponse(entity);
 	} 
 	
 	@Test
-	public void searchRetrieveFCSQLTest() throws IOException, URISyntaxException, IllegalStateException, SAXException{
-		HttpClient httpclient = HttpClients.createDefault();
-		URIBuilder builder = new URIBuilder();
-		builder.setScheme("http").setHost(host).setPort(port).setPath("/KorapSRU")
-				.setParameter("operation", "searchRetrieve")
-				.setParameter("query", "[tt:lemma=\"fein\"]")
-				.setParameter("queryType", "fcs");
-		
-		URI uri = builder.build();
-		HttpGet request = new HttpGet(uri);
-		HttpResponse response = httpclient.execute(request);
-		checkSRUSearchRetrieveResponse(response);
+	public void searchRetrieveFCSQLTest() throws IOException, SAXException{
+	    ClientResponse response = resource().queryParam("operation", "searchRetrieve")
+                .queryParam("query", "[tt:lemma=\"fein\"]")
+                .queryParam("queryType", "fcs")
+                .queryParam("maximumRecords", "5")
+                .get(ClientResponse.class);
+        
+//	    String entity = response.getEntity(String.class);
+//	    System.out.println(entity);
+//	    ByteArrayInputStream bis = new ByteArrayInputStream(entity.getBytes());
+//	    checkSRUSearchRetrieveResponse(bis);
+	    
+        InputStream entity = response.getEntity(InputStream.class);
+		checkSRUSearchRetrieveResponse(entity);
 	} 
 	
-	private void checkSRUSearchRetrieveResponse(HttpResponse response) throws IllegalStateException, SAXException, IOException {
-		assertEquals(200,response.getStatusLine().getStatusCode());
+	private void checkSRUSearchRetrieveResponse(InputStream entity) throws SAXException, IOException {
 		
-		InputStream is = response.getEntity().getContent();
-		Document doc = docBuilder.parse(is);
+		Document doc = docBuilder.parse(entity);
+		
 		NodeList nodelist = doc.getElementsByTagName("sruResponse:version");
 		assertEquals("2.0", nodelist.item(0).getTextContent());
 		nodelist = doc.getElementsByTagName("sruResponse:recordSchema");
 		assertEquals("http://clarin.eu/fcs/resource", nodelist.item(0).getTextContent());
 		
-		nodelist = doc.getElementsByTagName("fcs:Resource");
-		String attr  = nodelist.item(0).getAttributes().getNamedItem("pid").getNodeValue();
-//		assertEquals("match-GOE/AGF/00000-p15205-15206", attr);
+		NodeList resources = doc.getElementsByTagName("fcs:Resource");
+		String attr  = resources.item(0).getAttributes().getNamedItem("pid").getNodeValue();
 		
 		nodelist = doc.getElementsByTagName("fcs:DataView");
 		attr = nodelist.item(0).getAttributes().getNamedItem("type").getNodeValue();
 		assertEquals("application/x-clarin-fcs-hits+xml", attr);
+		
 		Node node = nodelist.item(0).getFirstChild();
 		assertEquals("hits:Result", node.getNodeName());
-		
 		NodeList children = node.getChildNodes();
 		assertEquals("hits:Hit", children.item(1).getNodeName());
-//		assertEquals("feineren", children.item(1).getTextContent());
 		
 		attr = nodelist.item(1).getAttributes().getNamedItem("type").getNodeValue();
 		assertEquals("application/x-clarin-fcs-adv+xml", attr);
@@ -105,47 +94,53 @@
 		nodelist = node.getChildNodes();
 		node = nodelist.item(0);
 		assertEquals("adv:Segments", node.getNodeName());
-//		assertEquals(52, node.getChildNodes().getLength());
 		node = nodelist.item(1);
 		assertEquals("adv:Layers", node.getNodeName());
-//		assertEquals(6, node.getChildNodes().getLength());
 		
-//		node = node.getFirstChild();
-//		attr = node.getAttributes().getNamedItem("id").getNodeValue();
-//		assertEquals("http://clarin.ids-mannheim.de/korapsru/layers/pos/marmot", attr);
-//		assertEquals(50, node.getChildNodes().getLength());
+		checkSegmentPosition(resources);
 	}
 	
 	
-	@Test
+	private void checkSegmentPosition (NodeList resources) {
+        // dataviews
+        NodeList childNodes = resources.item(1).getFirstChild().getChildNodes();
+        // segments
+        childNodes = childNodes.item(1).getFirstChild().getChildNodes().item(0)
+                .getChildNodes();
+
+        Node node = childNodes.item(0);
+        // 1st Segment
+        assertEquals("adv:Segment", node.getNodeName());
+        assertEquals("0",
+                node.getAttributes().getNamedItem("start").getNodeValue());
+
+        // 4th segment
+        node = childNodes.item(3);
+        assertEquals("0",
+                node.getAttributes().getNamedItem("start").getNodeValue());
+    }
+
+    @Test
     public void searchRetrieveWithResourceId() throws IOException, URISyntaxException, IllegalStateException, SAXException{
-        HttpClient httpclient = HttpClients.createDefault();
-        URIBuilder builder = new URIBuilder();
-        builder.setScheme("http").setHost(host).setPort(port).setPath("/KorapSRU")
-                .setParameter("operation", "searchRetrieve")
-                .setParameter("query", "fein")
-                .setParameter("x-fcs-context", "GOE");
+	    ClientResponse response = resource().queryParam("operation", "searchRetrieve")
+                .queryParam("query", "fein")
+                .queryParam("x-fcs-context", "GOE")
+                .get(ClientResponse.class);
         
-        URI uri = builder.build();
-        HttpGet request = new HttpGet(uri);
-        HttpResponse response = httpclient.execute(request);
-        checkSRUSearchRetrieveResponse(response);
+	    InputStream entity = response.getEntity(InputStream.class);
+        checkSRUSearchRetrieveResponse(entity);
     } 
 	
 	@Test
 	public void explainTest() throws URISyntaxException, ClientProtocolException, IOException, IllegalStateException, SAXException{
-		HttpClient httpclient = HttpClients.createDefault();
-		URIBuilder builder = new URIBuilder();
-		builder.setScheme("http").setHost(host).setPort(port).setPath("/KorapSRU")
-				.setParameter("operation", "explain");
+	    ClientResponse response = resource().queryParam("operation", "explain")
+                .get(ClientResponse.class);
+        
+        InputStream entity = response.getEntity(InputStream.class);
+        
+		assertEquals(200,response.getStatus());
 		
-		URI uri = builder.build();
-		HttpGet request = new HttpGet(uri);
-		HttpResponse response = httpclient.execute(request);
-		
-		assertEquals(200,response.getStatusLine().getStatusCode());
-		
-		Document doc = docBuilder.parse(response.getEntity().getContent());
+		Document doc = docBuilder.parse(entity);
 		NodeList nodelist = doc.getElementsByTagName("sruResponse:version");
 		assertEquals("2.0", nodelist.item(0).getTextContent());
 		
@@ -196,19 +191,15 @@
 	
 	@Test
 	public void explainEndpointDescriptionTest() throws URISyntaxException, ClientProtocolException, IOException, IllegalStateException, SAXException{
-		HttpClient httpclient = HttpClients.createDefault();
-		URIBuilder builder = new URIBuilder();
-		builder.setScheme("http").setHost(host).setPort(port).setPath("/KorapSRU")
-				.setParameter("operation", "explain")
-				.setParameter("x-fcs-endpoint-description", "true");
-		
-		URI uri = builder.build();
-		HttpGet request = new HttpGet(uri);
-		HttpResponse response = httpclient.execute(request);
-		
-		assertEquals(200,response.getStatusLine().getStatusCode());
-		
-		Document doc = docBuilder.parse(response.getEntity().getContent());
+	    ClientResponse response = resource().queryParam("operation", "explain")
+	            .queryParam("x-fcs-endpoint-description", "true")
+                .get(ClientResponse.class);
+        
+        InputStream entity = response.getEntity(InputStream.class);
+        
+        assertEquals(200,response.getStatus());
+        
+        Document doc = docBuilder.parse(entity);
 		NodeList nodelist = doc.getElementsByTagName("ed:EndpointDescription");
 		assertEquals("2", nodelist.item(0).getAttributes().getNamedItem("version").getNodeValue());