replace stringBuilder with queryMap
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java
index bef19a1..d0fba37 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CQLTree.java
@@ -1,17 +1,17 @@
 package de.ids_mannheim.korap.query.serialize;
 
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import de.ids_mannheim.korap.util.QueryException;
 import org.z3950.zing.cql.*;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
 /**
  * @author margaretha
+ * @date 	23.04.14
  */
 public class CQLTree extends AbstractSyntaxTree {
 
@@ -22,25 +22,23 @@
     private static final String TERM_RELATION_CQL_1_1 = "scr";
     private static final String TERM_RELATION_CQL_1_2 = "=";
     private static final String SUPPORTED_RELATION_EXACT = "exact"; // not in the doc    
-    private static final String OPERATION_OR = "\"operation:or\"";
-    private static final String OPERATION_SEQUENCE = "\"operation:sequence\"";
+    private static final String OPERATION_OR = "operation:or";
+    private static final String OPERATION_SEQUENCE = "operation:sequence";
     private static final String KORAP_CONTEXT = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
 
     private LinkedHashMap<String, Object> requestMap;
     private String version;
     private boolean isCaseSensitive; // default true
-    private StringBuilder sb;
-
+    
     public CQLTree(String query) throws QueryException {
         this(query, VERSION_1_2, true);
     }
 
     public CQLTree(String query, String version) throws QueryException {
-        this(query, version, true);
+        this(query, version, true);                
     }
 
     public CQLTree(String query, String version, boolean isCaseSensitive) throws QueryException {
-        this.sb = new StringBuilder();
         this.version = version;
         this.isCaseSensitive = isCaseSensitive;
         this.requestMap = new LinkedHashMap<>();
@@ -60,17 +58,8 @@
              throw new QueryException(27, "An empty query is unsupported.");
     	
         CQLNode cqlNode = parseQuerytoCQLNode(query);
-        parseCQLNode(cqlNode);
-        ObjectMapper mapper = new ObjectMapper();
-        JsonNode node;
-        try {
-            node = mapper.readTree(sb.toString());
-        } catch (IOException e) {
-            e.printStackTrace();
-            throw new QueryException("Something went wrong!");
-        }
-        requestMap.put("query", node);
-
+        Map<String,Object> queryMap = parseCQLNode(cqlNode);
+        requestMap.put("query", queryMap);
     }
 
     private CQLNode parseQuerytoCQLNode(String query) throws QueryException {
@@ -90,79 +79,99 @@
         }
     }
 
-    private void parseCQLNode(CQLNode node) throws QueryException {
+    private Map<String,Object> parseCQLNode(CQLNode node) throws QueryException {
         if (node instanceof CQLTermNode) {
-            parseTermNode((CQLTermNode) node);
+            return parseTermNode((CQLTermNode) node);
         } else if (node instanceof CQLAndNode) {
-            parseAndNode((CQLAndNode) node);
+            return parseAndNode((CQLAndNode) node);
         } else if (node instanceof CQLOrNode) {
-            parseOrNode((CQLOrNode) node);
+            return parseOrNode((CQLOrNode) node);
         } else {
             throw new QueryException(48, "Only basic search including term-only " +
                     "and boolean operator queries (AND and OR) are currently supported.");
         }
     }
 
-    private void parseTermNode(CQLTermNode node) throws QueryException {
+    private Map<String,Object> parseTermNode(CQLTermNode node) throws QueryException {
         checkTermNode(node);
         final String term = node.getTerm();
         if ((term == null) || term.isEmpty()) {
             throw new QueryException(27, "An empty term is unsupported.");
         } else if (term.contains(" ")) {
-            writeSequence(term);
+            return writeSequence(term);
         } else {
-            writeTerm(term);
+            return writeTerm(term);
         }
     }
 
-    private void parseAndNode(CQLAndNode node) throws QueryException {
+    private Map<String,Object> parseAndNode(CQLAndNode node) throws QueryException {
         checkBooleanModifier(node);
-        sb.append("{\"@type\":\"korap:group\", \"operation\":");
-        sb.append(OPERATION_SEQUENCE);
-        sb.append(", \"distances\":[{\"@type\":\"korap:distance\", \"key\":\"t\"," +
-                " \"min\":0, \"max\":0}]");
-        sb.append(", \"operands\":[");
-        parseCQLNode(node.getLeftOperand());
-        sb.append(", ");
-        parseCQLNode(node.getRightOperand());
-        sb.append("]}");
+        
+        Map<String, Object> map = new LinkedHashMap<String,Object>();
+        map.put("@type", "korap:group");
+        map.put("operation", OPERATION_SEQUENCE);
+        
+        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
+        Map<String, Object> distanceMap = new LinkedHashMap<String,Object>();
+        distanceMap.put("@type", "korap:distance");
+        distanceMap.put("key", "t");
+        distanceMap.put("min", "0");
+        distanceMap.put("max", "0");
+        list.add(distanceMap);
+        map.put("distances", list);
+        
+        List<Map<String, Object>> operandList = new ArrayList<Map<String, Object>>();        
+        operandList.add(parseCQLNode(node.getLeftOperand()));
+        operandList.add(parseCQLNode(node.getRightOperand()));
+        map.put("operands", operandList);
+        
+        return map;
     }
 
-    private void parseOrNode(CQLOrNode node) throws QueryException {
-        checkBooleanModifier(node);
-        sb.append("{\"@type\":\"korap:group\", \"operation\":");
-        sb.append(OPERATION_OR);
-        sb.append(", \"operands\":[");
-        parseCQLNode(node.getLeftOperand());
-        sb.append(", ");
-        parseCQLNode(node.getRightOperand());
-        sb.append("]}");
+    private Map<String,Object> parseOrNode(CQLOrNode node) throws QueryException {
+    	checkBooleanModifier(node);
+    	
+    	Map<String, Object> map = new LinkedHashMap<String,Object>();
+    	map.put("@type", "korap:group");
+        map.put("operation", OPERATION_OR);
+        
+        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
+        list.add(parseCQLNode(node.getLeftOperand()));
+        list.add(parseCQLNode(node.getRightOperand()));
+        map.put("operands", list);
+        
+    	return map;
     }
 
-    private void writeSequence(String str) {
+    private Map<String, Object> writeSequence(String str) {
+    	Map<String, Object> sequenceMap = new LinkedHashMap<String,Object>();       
+        sequenceMap.put("@type", "korap:group");
+        sequenceMap.put("operation", OPERATION_SEQUENCE);
+        
+        List<Map<String, Object>> termList = new ArrayList<Map<String, Object>>();
         String[] terms = str.split(" ");
-        sb.append("{\"@type\":\"korap:group\", \"operation\":");
-        sb.append(OPERATION_SEQUENCE);
-        sb.append(", \"operands\":[");
-
-        int size = terms.length;
-        for (int i = 0; i < size; i++) {
-            writeTerm(terms[i]);
-            if (i < size - 1)
-                sb.append(", ");
+        for (String term : terms){
+        	termList.add(writeTerm(term));
         }
-
-        sb.append("]}");
+        sequenceMap.put("operands", termList);
+        
+        return sequenceMap;
     }
 
-    private void writeTerm(String term) {
-        sb.append("{\"@type\":\"korap:token\", \"wrap\":{\"@type\":\"korap:term\"");
-        if (!isCaseSensitive) {
-            sb.append(", \"caseInsensitive\":true");
-        }
-        sb.append(", \"key\":");
-        sb.append("\"" + term + "\"");
-        sb.append(", \"layer\":\"orth\", \"match\":\"match:eq\"}}");
+    private Map<String, Object> writeTerm(String term) {   	
+    	Map<String, Object> map = new LinkedHashMap<String,Object>();
+    	map.put("@type", "korap:term");
+    	if (!isCaseSensitive) {
+    		map.put("caseInsensitive","true");
+    	}
+    	map.put("key", term);
+    	map.put("layer", "orth");
+    	map.put("match", "match:eq");
+    	
+    	Map<String, Object> tokenMap = new LinkedHashMap<String,Object>();    	
+    	tokenMap.put("@type", "korap:token");    	
+    	tokenMap.put("wrap", map);
+    	return tokenMap;
     }
 
     private void checkBooleanModifier(CQLBooleanNode node) throws QueryException {
diff --git a/src/test/java/CQLTest.java b/src/test/java/CQLTest.java
index 1f7a88b..2686b03 100644
--- a/src/test/java/CQLTest.java
+++ b/src/test/java/CQLTest.java
@@ -5,7 +5,11 @@
 import org.junit.Test;
 import org.z3950.zing.cql.CQLParseException;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 import de.ids_mannheim.korap.query.serialize.CQLTree;
+import de.ids_mannheim.korap.query.serialize.CosmasTree;
 import de.ids_mannheim.korap.util.QueryException;
 
 
@@ -13,6 +17,7 @@
 	
 	String query;
 	String version ="1.2";
+	ObjectMapper mapper = new ObjectMapper();
 	
 	@Test
 	public void testExceptions() throws CQLParseException, IOException {
@@ -53,6 +58,27 @@
 	}
 	
 	@Test
+	public void testAndQuery() throws CQLParseException, IOException, QueryException{
+		query="(Sonne) and (scheint)";	
+		String jsonLd = 
+			"{@type : korap:group, operation : operation:sequence, distances:[ "+
+				"{@type : korap:distance, key : t, min : 0, max : 0 } ],"+
+					"operands : ["+
+						"{@type : korap:token, wrap : {@type : korap:term,key : Sonne, layer : orth, match : match:eq}}," + 
+						"{@type : korap:token,wrap : {@type : korap:term,key : scheint,layer : orth,match : match:eq}" +
+					"}]}";
+			
+			CQLTree cqlTree = new CQLTree(query, version);		
+			String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
+			assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
+			
+		CosmasTree ct = new CosmasTree("Sonne und scheint");
+		serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
+		
+		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
+	}
+	
+	@Test
 	public void testBooleanQuery() throws CQLParseException, IOException, QueryException{		
 		query="((Sonne) or (Mond)) and (scheint)";		
 		String jsonLd = 
@@ -66,7 +92,7 @@
 					"{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
 			"]}";
 		CQLTree cqlTree = new CQLTree(query, version);		
-		String serializedQuery = cqlTree.getRequestMap().get("query").toString();
+		String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 		
 		
@@ -82,7 +108,7 @@
 					"]}" +
 				"]}";
 		cqlTree = new CQLTree(query, version);		
-		serializedQuery = cqlTree.getRequestMap().get("query").toString();
+		serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 		
 	}
@@ -97,7 +123,7 @@
 			"]}";		
 		
 		CQLTree cqlTree = new CQLTree(query, version);		
-		String serializedQuery = cqlTree.getRequestMap().get("query").toString();
+		String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 		
 		query="(\"Sonne scheint\") or (Mond)";		
@@ -111,7 +137,7 @@
 			"]}";
 		
 		cqlTree = new CQLTree(query, version);		
-		serializedQuery = cqlTree.getRequestMap().get("query").toString();
+		serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 				
 		query="(\"Sonne scheint\") or (\"Mond scheint\")";		
@@ -127,7 +153,7 @@
 					"]}" +
 				"]}";
 		cqlTree = new CQLTree(query, version);		
-		serializedQuery = cqlTree.getRequestMap().get("query").toString();
+		serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 	}
 	
@@ -136,7 +162,7 @@
 		query = "Sonne";		
 		String jsonLd = "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}";		
 		CQLTree cqlTree = new CQLTree(query, version);		
-		String serializedQuery = cqlTree.getRequestMap().get("query").toString();		
+		String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));		
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 	}
 	
@@ -150,7 +176,7 @@
 			"]}";
 		
 		CQLTree cqlTree = new CQLTree(query, version);		
-		String serializedQuery = cqlTree.getRequestMap().get("query").toString();
+		String serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 		
 		
@@ -163,7 +189,7 @@
 			"]}";
 		
 		cqlTree = new CQLTree(query, version);		
-		serializedQuery = cqlTree.getRequestMap().get("query").toString();
+		serializedQuery = mapper.writeValueAsString(cqlTree.getRequestMap().get("query"));
 		assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
 	}	
 }