Author: kwright
Date: Fri Mar 24 05:15:02 2017
New Revision: 1788362
URL: http://svn.apache.org/viewvc?rev=1788362&view=rev
Log:
Convert json to json-simple
Modified:
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/main/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocument.java
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/test/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocumentTest.java
Modified:
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/main/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocument.java
URL:
http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/main/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocument.java?rev=1788362&r1=1788361&r2=1788362&view=diff
==============================================================================
---
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/main/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocument.java
(original)
+++
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/main/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocument.java
Fri Mar 24 05:15:02 2017
@@ -23,9 +23,8 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.manifoldcf.agents.interfaces.RepositoryDocument;
import org.apache.manifoldcf.crawler.system.Logging;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
import org.jsoup.Jsoup;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -206,60 +205,57 @@ public class SearchBloxDocument {
if (apiKey == null)
throw new SearchBloxException(
"The API Key for accessing SearchBlox Server CAN'T be NULL");
- try {
- result.put(APIKEY_ATTRIBUTE, apiKey);
+
+ result.put(APIKEY_ATTRIBUTE, apiKey);
- JSONObject document = new JSONObject();
- if (colName == null)
- throw new SearchBloxException(
- "The Collection Name of the SearchBlox Server CAN'T be NULL");
- document.put(COLNAME_ATTRIBUTE, colName);
- document.put(UID_ATTRIBUTE, uid);
-
- if(action == DocumentAction.ADD_UPDATE){
- for(String element:xmlElements){
- if (!element.equals(xmlElements.get(12))) {
- Collection<Object> values = data_fields.get(element);
- if (values!=null && values.size()>0) {
- Object next = values.iterator()
- .next();
- String value =(String) next;
- if (value != null && !value.isEmpty()) {
- if(element.equals("keywords"))
- document.put(element, StringUtils.join(values, ','));
- else
- document.put(element, value);
+ JSONObject document = new JSONObject();
+ if (colName == null)
+ throw new SearchBloxException(
+ "The Collection Name of the SearchBlox Server CAN'T be NULL");
+ document.put(COLNAME_ATTRIBUTE, colName);
+ document.put(UID_ATTRIBUTE, uid);
+
+ if(action == DocumentAction.ADD_UPDATE){
+ for(String element:xmlElements){
+ if (!element.equals(xmlElements.get(12))) {
+ Collection<Object> values = data_fields.get(element);
+ if (values!=null && values.size()>0) {
+ Object next = values.iterator()
+ .next();
+ String value =(String) next;
+ if (value != null && !value.isEmpty()) {
+ if(element.equals("keywords"))
+ document.put(element, StringUtils.join(values, ','));
+ else
+ document.put(element, value);
- }
}
}
}
+ }
- // Metadata
- Collection<Object> metadataSet = data_fields
- .get(xmlElements.get(12));
- JSONObject metaObject = new JSONObject();
- if(metadataSet!=null && metadataSet.size()>0){
- Multimap<String, String> metadata = (Multimap<String, String>)
metadataSet.iterator().next();
- if (metadata != null && !metadata.isEmpty()) {
- for (String name : metadata.keySet()){
- JSONArray nextMetadata = new JSONArray();
- for (String value : metadata.get(name)) {
- nextMetadata.put(value);
- }
- metaObject.put(name, nextMetadata);
+ // Metadata
+ Collection<Object> metadataSet = data_fields
+ .get(xmlElements.get(12));
+ JSONObject metaObject = new JSONObject();
+ if(metadataSet!=null && metadataSet.size()>0){
+ Multimap<String, String> metadata = (Multimap<String, String>)
metadataSet.iterator().next();
+ if (metadata != null && !metadata.isEmpty()) {
+ for (String name : metadata.keySet()){
+ JSONArray nextMetadata = new JSONArray();
+ for (String value : metadata.get(name)) {
+ nextMetadata.add(value);
}
- }
- }
- document.put(xmlElements.get(12), metaObject);
+ metaObject.put(name, nextMetadata);
+ }
+ }
}
+ document.put(xmlElements.get(12), metaObject);
+ }
- result.put(xmlElements.get(1), document);
+ result.put(xmlElements.get(1), document);
- } catch (JSONException e) {
- throw new SearchBloxException("Error while building Document JSON
object", e);
- }
- return result.toString();
+ return result.toJSONString();
}
private String toStringXML(DocumentAction action) throws SearchBloxException{
Modified:
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/test/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocumentTest.java
URL:
http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/test/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocumentTest.java?rev=1788362&r1=1788361&r2=1788362&view=diff
==============================================================================
---
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/test/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocumentTest.java
(original)
+++
manifoldcf/branches/CONNECTORS-1399/connectors/searchblox/connector/src/test/java/org/apache/manifoldcf/agents/output/searchblox/SearchBloxDocumentTest.java
Fri Mar 24 05:15:02 2017
@@ -20,9 +20,9 @@ import org.apache.manifoldcf.agents.inte
import
org.apache.manifoldcf.agents.output.searchblox.SearchBloxDocument.DocumentAction;
import
org.apache.manifoldcf.agents.output.searchblox.SearchBloxDocument.IndexingFormat;
import org.apache.manifoldcf.core.interfaces.ManifoldCFException;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
@@ -152,13 +152,15 @@ public class SearchBloxDocumentTest {
}
@Test
- public void updateJsonString() throws SearchBloxException, JSONException {
+ public void updateJsonString() throws Exception {
String jsonGenerated = toTest.toString(IndexingFormat.JSON,
DocumentAction.ADD_UPDATE);
- JSONObject json = new JSONObject(jsonGenerated);
- assertTrue(json.has(APIKEY_ATTRIBUTE));
- assertTrue(json.has("document"));
+ final JSONParser parser = new JSONParser();
+ JSONObject json = (JSONObject)parser.parse(new
java.io.StringReader(jsonGenerated));
+
+ assertTrue(json.get(APIKEY_ATTRIBUTE) != null);
+ assertTrue(json.get("document") != null);
Object apiObject = json.get(APIKEY_ATTRIBUTE);
assertTrue(apiObject instanceof String);
@@ -168,25 +170,25 @@ public class SearchBloxDocumentTest {
assertTrue(documentObject instanceof JSONObject);
JSONObject document = (JSONObject) documentObject;
- assertTrue(document.has(UID_ATTRIBUTE));
+ assertTrue(document.get(UID_ATTRIBUTE) != null);
assertTrue(document.get(UID_ATTRIBUTE) instanceof String);
assertEquals("URI", document.get(UID_ATTRIBUTE));
- assertTrue(document.has(COLNAME_ATTRIBUTE));
+ assertTrue(document.get(COLNAME_ATTRIBUTE) != null);
assertTrue(document.get(COLNAME_ATTRIBUTE) instanceof String);
assertEquals("collection1", document.get(COLNAME_ATTRIBUTE));
- assertTrue(document.has("size"));
+ assertTrue(document.get("size") != null);
assertTrue(document.get("size") instanceof String);
assertEquals("100", document.get("size"));
- assertTrue(document.has("meta"));
+ assertTrue(document.get("meta") != null);
Object metaObject = document.get("meta");
assertTrue(metaObject instanceof JSONObject);
JSONObject meta = (JSONObject) metaObject;
- assertEquals(6, meta.length());
+ assertEquals(6, meta.size());
assertTrue(find(meta, "meta2", "I am META2!", 1));
assertTrue(find(meta, "meta1", "I am META1!", 1));
@@ -206,45 +208,47 @@ public class SearchBloxDocumentTest {
assertTrue(find(meta, "document_allow", "user12", 3));
- assertTrue(document.has("description"));
+ assertTrue(document.get("description") != null);
assertTrue(document.get("description") instanceof String);
assertEquals("I am a little tiny description",
document.get("description"));
- assertTrue(document.has("title"));
+ assertTrue(document.get("title") != null);
assertTrue(document.get("title") instanceof String);
assertEquals("I am a nice title", document.get("title"));
- assertTrue(document.has("content"));
+ assertTrue(document.get("content") != null);
assertTrue(document.get("content") instanceof String);
assertEquals("I am a nice content in english!",
document.get("content"));
- assertTrue(document.has("contenttype"));
+ assertTrue(document.get("contenttype") != null);
assertTrue(document.get("contenttype") instanceof String);
assertEquals("html", document.get("contenttype"));
}
private boolean find(JSONObject meta, String name, String textContent, int
size) {
- assertTrue(meta.has(name));
+ assertTrue(meta.get(name) != null);
assertTrue(meta.get(name) instanceof JSONArray);
- assertEquals(size, ((JSONArray) meta.get(name)).length());
+ assertEquals(size, ((JSONArray) meta.get(name)).size());
for (int i = 0; i < size; i++) {
- if (textContent.equals(((JSONArray) meta.get(name)).getString(i)))
+ if (textContent.equals(((JSONArray)
meta.get(name)).get(i).toString()))
return true;
}
return false;
}
@Test
- public void deleteJsonString() throws SearchBloxException, JSONException {
+ public void deleteJsonString() throws Exception {
String jsonGenerated=toTest.toString(IndexingFormat.JSON,
DocumentAction.DELETE);
- JSONObject json = new JSONObject(jsonGenerated);
- assertTrue(json.has(APIKEY_ATTRIBUTE));
- assertTrue(json.has("document"));
+ final JSONParser parser = new JSONParser();
+ JSONObject json = (JSONObject)parser.parse(new
java.io.StringReader(jsonGenerated));
+
+ assertTrue(json.get(APIKEY_ATTRIBUTE) != null);
+ assertTrue(json.get("document") != null);
Object apiObject = json.get(APIKEY_ATTRIBUTE);
assertTrue(apiObject instanceof String);
@@ -254,8 +258,8 @@ public class SearchBloxDocumentTest {
assertTrue(documentObject instanceof JSONObject);
JSONObject document = (JSONObject) documentObject;
- assertTrue(document.has(UID_ATTRIBUTE));
- assertTrue(document.has(COLNAME_ATTRIBUTE));
+ assertTrue(document.get(UID_ATTRIBUTE) != null);
+ assertTrue(document.get(COLNAME_ATTRIBUTE) != null);
Object uidObject = document.get(UID_ATTRIBUTE);
assertTrue(uidObject instanceof String);