manifoldcf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1788361 - /manifoldcf/branches/CONNECTORS-1399/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/Configuration.java
Date Fri, 24 Mar 2017 04:46:18 GMT
Author: kwright
Date: Fri Mar 24 04:46:17 2017
New Revision: 1788361

URL: http://svn.apache.org/viewvc?rev=1788361&view=rev
Log:
Finish rework of Configuration class

Modified:
    manifoldcf/branches/CONNECTORS-1399/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/Configuration.java

Modified: manifoldcf/branches/CONNECTORS-1399/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/Configuration.java
URL: http://svn.apache.org/viewvc/manifoldcf/branches/CONNECTORS-1399/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/Configuration.java?rev=1788361&r1=1788360&r2=1788361&view=diff
==============================================================================
--- manifoldcf/branches/CONNECTORS-1399/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/Configuration.java
(original)
+++ manifoldcf/branches/CONNECTORS-1399/framework/core/src/main/java/org/apache/manifoldcf/core/interfaces/Configuration.java
Fri Mar 24 04:46:17 2017
@@ -781,9 +781,7 @@ public class Configuration implements IH
     }
     
     public void startObject() {
-      objectStack.add(currentObject);
-      keyStack.add(currentKey);
-      arrayStack.add(currentArray);
+      pushState();
       currentObject = new JSONObject();
       currentKey = null;
       currentArray = null;
@@ -800,9 +798,7 @@ public class Configuration implements IH
     public void endObject() {
       final JSONObject object = currentObject;
       // Pop everything
-      currentObject = objectStack.remove(objectStack.size()-1);
-      currentKey = keyStack.remove(keyStack.size()-1);
-      currentArray = arrayStack.remove(arrayStack.size()-1);
+      popState();
       // Either an array is active, or an object is active, or nothing is active
       if (currentObject != null) {
         currentObject.put(currentKey, object);
@@ -814,9 +810,7 @@ public class Configuration implements IH
     }
     
     public void startArray() {
-      objectStack.add(currentObject);
-      keyStack.add(currentKey);
-      arrayStack.add(currentArray);
+      pushState();
       currentObject = null;
       currentKey = null;
       currentArray = new JSONArray();
@@ -825,9 +819,7 @@ public class Configuration implements IH
     public void endArray() {
       final JSONArray array = currentArray;
       // Pop everything
-      currentObject = objectStack.remove(objectStack.size()-1);
-      currentKey = keyStack.remove(keyStack.size()-1);
-      currentArray = arrayStack.remove(arrayStack.size()-1);
+      popState();
       // Either an array is active, or an object is active, or nothing is active
       if (currentObject != null) {
         currentObject.put(currentKey, array);
@@ -845,47 +837,103 @@ public class Configuration implements IH
       return finalObject.toJSONString();
     }
     
+    protected void pushState() {
+      objectStack.add(currentObject);
+      keyStack.add(currentKey);
+      arrayStack.add(currentArray);
+    }
+    
+    protected void popState() {
+      currentObject = objectStack.remove(objectStack.size()-1);
+      currentKey = keyStack.remove(keyStack.size()-1);
+      currentArray = arrayStack.remove(arrayStack.size()-1);
+    }
+
   }
   
   protected static class JSONReader {
     
+    private final List<JSONObject> objectStack = new ArrayList<>();
+    private final List<Iterator> arrayStack = new ArrayList<>();
+    private JSONObject currentObject = null;
+    private Iterator currentArrayIterator = null;
+    
+    private Object next = null;
+
     public JSONReader(final String json) throws ManifoldCFException {
+      final JSONParser parser = new JSONParser();
+      try {
+        next = parser.parse(new StringReader(json));
+      } catch (Exception e) {
+        throw new ManifoldCFException("Bad json: "+e.getMessage(),e);
+      }
     }
     
     public boolean isObject() {
+      return next != null && (next instanceof JSONObject);
     }
     
     public void startObject() {
+      pushState();
+      currentObject = (JSONObject)next;
+      currentArrayIterator = null;
     }
 
     public Iterator<String> getKeys() {
+      return currentObject.keySet().iterator();
     }
     
     public boolean valueForKey(final String key) {
+      next = currentObject.get(key);
+      return next != null;
     }
     
     public void endObject() {
+      popState();
     }
     
     public boolean isArray() {
+      return next != null && (next instanceof JSONArray);
     }
     
     public void startArray() {
+      pushState();
+      currentObject = null;
+      currentArrayIterator = ((JSONArray)next).iterator();
     }
     
     public boolean nextElement() {
+      if (currentArrayIterator.hasNext()) {
+        next = currentArrayIterator.next();
+        return true;
+      }
+      return false;
     }
     
     public void endArray() {
+      popState();
     }
 
     public boolean isNull() {
+      return next == null;
     }
     
     public boolean isValue() {
+      return next != null && !(next instanceof JSONObject || next instanceof JSONArray);
     }
     
     public String readValue() {
+      return next.toString();
+    }
+    
+    protected void pushState() {
+      objectStack.add(currentObject);
+      arrayStack.add(currentArrayIterator);
+    }
+    
+    protected void popState() {
+      currentObject = objectStack.remove(objectStack.size()-1);
+      currentArrayIterator = arrayStack.remove(arrayStack.size()-1);
     }
     
   }



Mime
View raw message