예제 #1
0
  public void run() throws IOException {
    if (verbose) {
      System.out.println(
          String.format(
              "Connected to https://appengine.google.com/dashboard?&app_id=%s as %s",
              app_id, email, password));
    }
    Interpreter inter = new Interpreter();
    BufferedReader script = new BufferedReader(new InputStreamReader(System.in));
    DatastoreConnection conn;
    if (isLocalhost) {
      conn = new DatastoreConnection("localhost", 8080, email, password);
    } else {
      conn = new DatastoreConnection(app_id + ".appspot.com", 443, email, password);
    }

    boolean needp = true;
    for (String l = ""; l != null; ) {

      // print prompt
      String namespace = NamespaceManager.get();
      if (namespace == null) namespace = "";
      if (needp) System.out.print(namespace + "> ");

      // read line
      l = script.readLine();
      inter.addLine(l);

      // interpret the line
      try {

        String scommand = inter.popCommand();
        needp = scommand != null;
        if (scommand == null) {
          continue;
        }
        Parser p = new Parser(new StringReader(scommand));
        Object o = null;
        try {
          o = p.parse();
        } catch (com.inpun.json.ParseException e) {
          for (int i = 0; i < p.getPosition() + 1 + namespace.length(); ++i) {
            System.err.print(' ');
          }
          System.err.println('^');
          System.err.println("Error in position " + p.getPosition());
          continue;
        }

        if (o == null || !(o instanceof VerifiedCommand)) {
          // empty command
          continue;
        }

        VerifiedCommand vc = (VerifiedCommand) o;
        ExecutionResult er = vc.execute(conn);
        if (er.getError() != 0) {
          System.out.println("Error: " + er.getMessage());
        } else if (er.getObject() != null) {
          System.out.println(er.getObject().toPrettyString());
        } else if (er.getArray() != null) {
          JsonArray a = er.getArray();
          if (a.isEmpty()) {
            System.out.println("[]");
            continue;
          }
          System.out.println("[");
          Iterator<?> i = a.iterator();
          while (i.hasNext()) {
            String s = ((JsonObject) i.next()).toString();
            if (i.hasNext()) System.out.println(s + ",");
            else System.out.println(s);
          }
          System.out.println("]");
        } else if (er.getMessage() != null) {
          System.out.println(er.getMessage());
        }
      } catch (InterpreterException e) {
        System.err.println(e.getMessage());
      }
    }
  }