From 0bdc2353e62ff5cd392b36adaa46dc9cc1d82d81 Mon Sep 17 00:00:00 2001
From: Antoni Mylka <antoni.mylka@basis06.ch>
Date: Tue, 13 Nov 2012 14:08:00 +0100
Subject: [PATCH] [FIX] A change in StringParser that restores support for
 multiline commands

---
 .../java/org/basex/core/parse/StringParser.java    | 119 ++++++++++++++++-----
 1 file changed, 92 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/basex/core/parse/StringParser.java b/src/main/java/org/basex/core/parse/StringParser.java
index 958a77d..832049d 100644
--- a/src/main/java/org/basex/core/parse/StringParser.java
+++ b/src/main/java/org/basex/core/parse/StringParser.java
@@ -9,7 +9,17 @@ import org.basex.core.*;
 import org.basex.core.cmd.*;
 import org.basex.core.cmd.List;
 import org.basex.core.cmd.Set;
-import org.basex.core.parse.Commands.*;
+import org.basex.core.parse.Commands.Cmd;
+import org.basex.core.parse.Commands.CmdAlter;
+import org.basex.core.parse.Commands.CmdCreate;
+import org.basex.core.parse.Commands.CmdDrop;
+import org.basex.core.parse.Commands.CmdIndex;
+import org.basex.core.parse.Commands.CmdIndexInfo;
+import org.basex.core.parse.Commands.CmdInfo;
+import org.basex.core.parse.Commands.CmdOptimize;
+import org.basex.core.parse.Commands.CmdPerm;
+import org.basex.core.parse.Commands.CmdRepo;
+import org.basex.core.parse.Commands.CmdShow;
 import org.basex.io.*;
 import org.basex.query.*;
 import org.basex.query.value.item.*;
@@ -24,6 +34,8 @@ import org.basex.util.list.*;
  * @author Christian Gruen
  */
 final class StringParser extends CmdParser {
+  /** The whole input */
+  private final String fullInput;
   /** Input lines. */
   private final String[] lines;
   /** Context. */
@@ -39,23 +51,46 @@ final class StringParser extends CmdParser {
    */
   StringParser(final String in, final Context context) {
     final String input = in.indexOf('\r') != -1 ? in.replaceAll("\r\n?", "\n") : in;
+    fullInput = input.trim();
     lines = input.indexOf('\n') == -1 ? new String[] { input } : input.split("\n");
-    for(int l = 0; l < lines.length; l++) lines[l] = lines[l].trim();
+    for(int l = 0; l < lines.length; l++) {
+      lines[l] = lines[l].trim();
+    }
     ctx = context;
   }
 
   @Override
   void parse(final ArrayList<Command> list) throws QueryException {
-    for(final String line : lines) {
-      if(line.isEmpty() || line.startsWith("#")) continue;
-      parser = new InputParser(line);
-      while(true) {
-        final Cmd cmd = consume(Cmd.class, null);
-        list.add(parse(cmd));
-        if(!parser.more()) break;
-        if(!eoc()) throw help(null, cmd);
-        parser.consume();
+    if (single) {
+      parser = new InputParser(fullInput);
+      processParser(list);
+    } else {
+      for(final String line : lines) {
+        if(line.isEmpty() || line.startsWith("#")) {
+          continue;
+        }
+        parser = new InputParser(line);
+        processParser(list);
+      }
+    }
+  }
+
+  /**
+   * 
+   * @param list
+   * @throws QueryException
+   */
+  private void processParser(final ArrayList<Command> list) throws QueryException {
+    while(true) {
+      final Cmd cmd = consume(Cmd.class, null);
+      list.add(parse(cmd));
+      if(!parser.more()) {
+        break;
       }
+      if(!eoc()) {
+        throw help(null, cmd);
+      }
+      parser.consume();
     }
   }
 
@@ -120,7 +155,9 @@ final class StringParser extends CmdParser {
           case STORAGE:
             String arg1 = number(null);
             final String arg2 = arg1 != null ? number(null) : null;
-            if(arg1 == null) arg1 = xquery(null);
+            if(arg1 == null) {
+              arg1 = xquery(null);
+            }
             return new InfoStorage(arg1, arg2);
         }
         break;
@@ -195,7 +232,9 @@ final class StringParser extends CmdParser {
         break;
       case GRANT:
         final CmdPerm perm = consume(CmdPerm.class, cmd);
-        if(perm == null) throw help(null, cmd);
+        if(perm == null) {
+          throw help(null, cmd);
+        }
         final String db = key(ON, null) ? glob(cmd) : null;
         key(C_TO, cmd);
         return new Grant(perm, glob(cmd), db);
@@ -228,9 +267,14 @@ final class StringParser extends CmdParser {
     boolean q = false;
     while(parser.more()) {
       final char c = parser.curr();
-      if(!q && (c <= ' ' || eoc())) break;
-      if(c == '"') q ^= true;
-      else sb.append(c);
+      if(!q && (c <= ' ' || eoc())) {
+        break;
+      }
+      if(c == '"') {
+        q ^= true;
+      } else {
+        sb.append(c);
+      }
       parser.consume();
     }
     return finish(cmd, sb);
@@ -246,12 +290,18 @@ final class StringParser extends CmdParser {
   private String remaining(final Cmd cmd) throws QueryException {
     final StringBuilder sb = new StringBuilder();
     consumeWS();
-    while(parser.more()) sb.append(parser.consume());
+    while(parser.more()) {
+      sb.append(parser.consume());
+    }
     String arg = finish(cmd, sb);
     if(arg != null) {
       // chop quotes; substrings are faster than replaces...
-      if(arg.startsWith("\"")) arg = arg.substring(1);
-      if(arg.endsWith("\"")) arg = arg.substring(0, arg.length() - 1);
+      if(arg.startsWith("\"")) {
+        arg = arg.substring(1);
+      }
+      if(arg.endsWith("\"")) {
+        arg = arg.substring(0, arg.length() - 1);
+      }
     }
     return arg;
   }
@@ -335,7 +385,9 @@ final class StringParser extends CmdParser {
         key.toLowerCase(Locale.ENGLISH))) && (parser.curr(0) || ws(parser.curr()));
     if(!ok) {
       parser.ip = p;
-      if(cmd != null) throw help(null, cmd);
+      if(cmd != null) {
+        throw help(null, cmd);
+      }
     }
     return ok;
   }
@@ -348,8 +400,12 @@ final class StringParser extends CmdParser {
    * @throws QueryException query exception
    */
   private String finish(final Cmd cmd, final StringBuilder s) throws QueryException {
-    if(s != null && s.length() != 0) return s.toString();
-    if(cmd != null) throw help(null, cmd);
+    if(s != null && s.length() != 0) {
+      return s.toString();
+    }
+    if(cmd != null) {
+      throw help(null, cmd);
+    }
     return null;
   }
 
@@ -362,8 +418,12 @@ final class StringParser extends CmdParser {
   private String number(final Cmd cmd) throws QueryException {
     consumeWS();
     final StringBuilder sb = new StringBuilder();
-    if(parser.curr() == '-') sb.append(parser.consume());
-    while(digit(parser.curr())) sb.append(parser.consume());
+    if(parser.curr() == '-') {
+      sb.append(parser.consume());
+    }
+    while(digit(parser.curr())) {
+      sb.append(parser.consume());
+    }
     return finish(cmd, eoc() || ws(parser.curr()) ? sb : null);
   }
 
@@ -372,7 +432,9 @@ final class StringParser extends CmdParser {
    * query.
    */
   private void consumeWS() {
-    while(parser.ip < parser.il && parser.input.charAt(parser.ip) <= ' ') ++parser.ip;
+    while(parser.ip < parser.il && parser.input.charAt(parser.ip) <= ' ') {
+      ++parser.ip;
+    }
     parser.im = parser.ip - 1;
   }
 
@@ -408,8 +470,9 @@ final class StringParser extends CmdParser {
     final Levenshtein ls = new Levenshtein();
     for(final Enum<?> s : list(cmp, null)) {
       final byte[] sm = uc(token(s.name()));
-      if(ls.similar(name, sm, 0) && Cmd.class.isInstance(s))
+      if(ls.similar(name, sm, 0) && Cmd.class.isInstance(s)) {
         throw error(list(alt), UNKNOWN_SIMILAR_X, name, sm);
+      }
     }
 
     // show unknown command error or available command extensions
@@ -475,7 +538,9 @@ final class StringParser extends CmdParser {
    */
   private static StringList list(final Enum<?>[] comp) {
     final StringList list = new StringList();
-    for(final Enum<?> c : comp) list.add(c.name().toLowerCase(Locale.ENGLISH));
+    for(final Enum<?> c : comp) {
+      list.add(c.name().toLowerCase(Locale.ENGLISH));
+    }
     return list;
   }
 }
-- 
1.7.11.msysgit.1

