1.1 restoration
[mir.git] / source / mir / util / StringParseRoutines.java
index ee08f21..04bbc72 100755 (executable)
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with  any library licensed under the Apache Software License, 
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
- * (or with modified versions of the above that use the same license as the above), 
- * and distribute linked combinations including the two.  You must obey the 
- * GNU General Public License in all respects for all of the code used other than 
- * the above mentioned libraries.  If you modify this file, you may extend this 
- * exception to your version of the file, but you are not obligated to do so.  
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
  * If you do not wish to do so, delete this exception statement from your version.
  */
 
@@ -32,6 +32,8 @@ package mir.util;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
 
 public class StringParseRoutines {
 
@@ -41,8 +43,9 @@ public class StringParseRoutines {
   private final static String VALUE = "[^;]*";
   private final static String SEMICOLON = ";";
 
-  //  a=sdfb; c=d; e=f
-
+  /**
+   * Parses an expression of the form " a = b ; c = d" into a map
+   */
   public static Map parseValueList(String anExpression) throws SimpleParser.SimpleParserFailure, SimpleParser.SimpleParserExc {
     String key;
     String value;
@@ -67,4 +70,58 @@ public class StringParseRoutines {
 
     return result;
   }
+
+  /**
+   * Utility function for {@link #parseBracketedExpression}:
+   * Adds a single part of a bracketed expression by looking for a
+   * terminator.
+   */
+  private static  int findNextBracketedPart(String anExpression, int aStartingPosition, char anEscape, String aTerminator, List aResult) {
+    int position = aStartingPosition;
+    StringBuffer result = new StringBuffer();
+
+    while (position<anExpression.length()) {
+      if (anExpression.charAt(position)==anEscape && position+1<anExpression.length()) {
+        result.append(anExpression.charAt(position+1));
+        position++;
+        position++;
+      }
+      else if (anExpression.startsWith(aTerminator, position)) {
+        aResult.add(result.toString());
+        return position;
+      }
+      else {
+        result.append(anExpression.charAt(position));
+        position++;
+      }
+    }
+
+    aResult.add(result.toString());
+    return -1;
+  }
+
+  /**
+   * Parses a "bracketed expression" into parts.
+   * "Bracketed" here means the expression consists of plain text and bracketed
+   * parts. e.g. <code>"some string {some expression} some string"</code>.
+   * { here starts a bracket, } ends one.
+   */
+  public static List parseBracketedExpression(String anExpression, char anEscape,
+      String anOpeningBracket, String aClosingBracket) {
+    List result = new ArrayList();
+    int position = 0;
+
+    do {
+      position = findNextBracketedPart(anExpression, position, anEscape, anOpeningBracket, result);
+      if (position>=0) {
+        position+=anOpeningBracket.length();
+        position = findNextBracketedPart(anExpression, position, anEscape, aClosingBracket, result);
+        if (position>0) {
+          position+=aClosingBracket.length();
+        }
+      }
+    } while (position>=0);
+
+    return result;
+  }
 }
\ No newline at end of file