/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.lib.json;

import foam.core.ContextAwareSupport;
import foam.core.FObject;
import foam.core.ProxyX;
import foam.core.X;

/**
 * Class responsible for parsing journal files.
 * journal files follows this structure.
 *
 * p({"class":"foam.dao.Guitar","name":"Gibs1" }))
 * r({"id": "1"}))
 */
public class JournalParser
  extends ContextAwareSupport {

  protected JSONParser jsonParser;

  public JournalParser() {
    // do not fail if context is null
    X ctx = (getX() == null) ? new ProxyX() : getX();
    this.jsonParser = new JSONParser();
    jsonParser.setX(ctx);
  }


  // TODO(drish): do not generate a second copy of the journalLine
  public FObject parseObject(String journalLine) {

    // get the actual object
    int objectIndex = journalLine.indexOf("{");
    String object = journalLine.substring(objectIndex, journalLine.lastIndexOf("}") + 1);

    return this.jsonParser.parseString(object);
  }

  public Object parseObjectId(String journalLine) {

    int idIndex = journalLine.lastIndexOf(":");
    String id = journalLine.substring(idIndex + 2, journalLine.lastIndexOf("}") - 1);

    return id;
  }
}

@@ -162,98 +162,9 @@
 ore.
-ContextAwareSupport;%0Aimport foam.core.FObject;%0Aimport foam.core.ProxyX;%0Aimport foam.core.X
+*
 ;%0A%0A/
@@ -1008,24 +1008,39 @@
 nalLine) %7B%0A%0A
+    String id;%0A
     int idIn
@@ -1075,26 +1075,96 @@
 f(%22:%22);%0A
+%0A
    
+ if (
  String
+.valueOf(journalLine.charAt(idIndex + 1)).equals(%22%5C%22%22) ) %7B%0A     
  id = jo
@@ -1226,24 +1226,120 @@
 f(%22%7D%22) - 1);
+%0A    %7D else %7B%0A      id = journalLine.substring(idIndex + 1, journalLine.lastIndexOf(%22%7D%22));%0A    %7D
 %0A%0A    return

fix object id parsing