[Schmitzm-commits] r2354 - in trunk/schmitzm-core/src: main/java/de/schmitzm/lang test/java/de/schmitzm/lang

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon Jul 8 14:55:39 CEST 2013


Author: mojays
Date: 2013-07-08 14:55:39 +0200 (Mon, 08 Jul 2013)
New Revision: 2354

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
Log:


Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java	2013-07-08 12:22:18 UTC (rev 2353)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java	2013-07-08 12:55:39 UTC (rev 2354)
@@ -55,9 +55,13 @@
   /** Indicates whether the delimiter tokens themselves are returned as
    *  token. */
   protected boolean returnDelims;
+  /** Indicates whether leading/trailing quotes are kept in token. */
+  protected boolean keepQuotes;
 
   /**
    * Creates new tokenizer with default delimiters and default quote sign.
+   * Delimiters are not returned as token and leading/trailing quotes are
+   * eliminated.
    * @param text text to parse
    */
   public StringTokenizerQuoted(String text) {
@@ -66,29 +70,33 @@
 
   /**
    * Creates new tokenizer with default delimiters and default quote sign.
+   * Delimiters are not returned as token and leading/trailing quotes are
+   * eliminated.
    * @param text text to parse
    * @param quote quote sign used to mask tokens which contains delimiters
    * @param delim delimiter(s) used to separate tokens
    */
   public StringTokenizerQuoted(String str, String quote, String delim) {
-    this(str, quote, delim, false);
+    this(str, quote, false, delim, false);
   }
 
   /**
    * Creates new tokenizer with default delimiters and default quote sign.
    * @param text text to parse
    * @param quote quote sign used to mask tokens which contains delimiters
+   * @param keepQuotes indicates whether leading/trailing quotes are kept in token
    * @param delim delimiter(s) used to separate tokens
    * @param returnDelims indicates whether the delimiter tokens themselves are
    *                     returned as token
    */
-  public StringTokenizerQuoted(String str, String quote, String delim, boolean returnDelims) {
+  public StringTokenizerQuoted(String str, String quote, boolean keepQuotes, String delim, boolean returnDelims) {
     super(str, delim != null ? delim : DEFAULT_DELIM, true);
     if ( quote == null )
       quote = DEFAULT_QUOTE;
     if ( delim == null )
       delim = DEFAULT_DELIM;
     this.quote = quote;
+    this.keepQuotes = keepQuotes;
     this.delim = delim;
     this.returnDelims = returnDelims;
   }
@@ -114,10 +122,18 @@
     return returnDelims;
   }
     
+  /**
+   * Returns whether leading/trailing quotes are kept in token.
+   */
+  public boolean isQuoteKept() {
+    return returnDelims;
+  }
+    
   /** 
    * Changes the delimiters and returns the next token.
    * @param delim new delimiters used to separate tokens
    */
+  @Override
   public String nextToken(String delim) {
     this.delim = delim;
     return nextToken(this.delim);
@@ -126,24 +142,25 @@
   /** 
    * Returns the next token.
    */
+  @Override
   public String nextToken() {
     // determine next token
     String token = super.nextToken();
     
     //
-    if ( isTokenInQuotes(token) ) {
+    if ( isTokenInQuotes(token) && hasMoreTokens() ) {
       String followingToken = null;
       do {
         followingToken = super.nextToken();
         token += followingToken;
-      } while ( !isTokenInQuotes(followingToken) );
+      } while ( !isTokenInQuotes(followingToken) && hasMoreTokens()  );
     }
     
     // remove whitespaces (before/after delimiter)
     token = token.trim();  
     // if token starts and ends with quote sign, remove quotes from
     // token
-    if ( token.length() > 1 && token.startsWith(quote) && token.endsWith(quote) )
+    if ( !keepQuotes && token.length() > 1 && token.startsWith(quote) && token.endsWith(quote) )
       token = token.substring(1, token.length()-1);
     // return delimiter token only if respective flag is set 
     if ( token.equals(delim) && !returnDelims )

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java	2013-07-08 12:22:18 UTC (rev 2353)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java	2013-07-08 12:55:39 UTC (rev 2354)
@@ -149,7 +149,7 @@
 		System.out.println( LangUtil.formatInterval(ManagementFactory.getRuntimeMXBean().getUptime()) );
 	}
 	
-	@Ignore
+//	@Ignore
 	@Test
 	public void testQuotedStringTokenizer() {
       String text = "Item1, \"Item2\", \"Item3, and, Item4\" bla, \"blub\" bla, \"Item5, item5a\",,\"\",";
@@ -233,7 +233,7 @@
 //      for (String item : items)
 //        System.out.println("'"+item+"'");
       
-      StringTokenizer st1 = new StringTokenizerQuoted(text, quote, delim, false);
+      StringTokenizer st1 = new StringTokenizerQuoted(text, quote, true, delim, false);
       for (;st1.hasMoreTokens();)
         System.out.println("'"+st1.nextToken()+"'");
       



More information about the Schmitzm-commits mailing list