[Schmitzm-commits] r2353 - in trunk/schmitzm-core/src: main/java/de/schmitzm/lang main/java/de/schmitzm/swing main/java/de/schmitzm/versionnumber test/java/de/schmitzm/lang
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Mon Jul 8 14:22:18 CEST 2013
Author: mojays
Date: 2013-07-08 14:22:18 +0200 (Mon, 08 Jul 2013)
New Revision: 2353
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java
trunk/schmitzm-core/src/main/java/de/schmitzm/versionnumber/ApplicationUpdater.java
trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
Log:
ApplicationUpdater: debug messages
new StringTokenizerQuoted
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/StringTokenizerQuoted.java 2013-07-08 12:22:18 UTC (rev 2353)
@@ -0,0 +1,176 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.lang;
+
+import java.util.StringTokenizer;
+
+import org.apache.commons.lang.StringUtils;
+
+
+/**
+ * A {@link StringTokenizer} which supports quoted tokens.<br>
+ * <b>Note:<b><br>
+ * {@link #countTokens()} does not (yet) consider quotes. The use of this method
+ * should be avoided!!
+ * @author Martin O.J. Schmitz
+ */
+public class StringTokenizerQuoted extends StringTokenizer {
+ /** Default delimiters (whitespaces, tabs, returns) used if
+ * no delimiter specified. */
+ public static final String DEFAULT_DELIM = " \t\n\r\f";
+ /** Default quote sign (double quote) used if no quote sign specified. */
+ public static final String DEFAULT_QUOTE = "\"";
+
+ /** Delimiter(s) used to separate tokens. */
+ protected String delim;
+ /** Quote sign used to mask tokens which contains delimiters. */
+ protected String quote;
+ /** Indicates whether the delimiter tokens themselves are returned as
+ * token. */
+ protected boolean returnDelims;
+
+ /**
+ * Creates new tokenizer with default delimiters and default quote sign.
+ * @param text text to parse
+ */
+ public StringTokenizerQuoted(String text) {
+ this(text, null, null);
+ }
+
+ /**
+ * 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 delim delimiter(s) used to separate tokens
+ */
+ public StringTokenizerQuoted(String str, String quote, String delim) {
+ this(str, quote, 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 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) {
+ super(str, delim != null ? delim : DEFAULT_DELIM, true);
+ if ( quote == null )
+ quote = DEFAULT_QUOTE;
+ if ( delim == null )
+ delim = DEFAULT_DELIM;
+ this.quote = quote;
+ this.delim = delim;
+ this.returnDelims = returnDelims;
+ }
+
+ /**
+ * Returns the quote sign used to mask tokens which contains delimiters.
+ */
+ public String getQuote() {
+ return quote;
+ }
+
+ /**
+ * Returns the delimiter(s) used to separate tokens.
+ */
+ public String getDelimiters() {
+ return delim;
+ }
+
+ /**
+ * Returns whether the delimiters themselves are returned as token.
+ */
+ public boolean isReturnDelimiters() {
+ return returnDelims;
+ }
+
+ /**
+ * Changes the delimiters and returns the next token.
+ * @param delim new delimiters used to separate tokens
+ */
+ public String nextToken(String delim) {
+ this.delim = delim;
+ return nextToken(this.delim);
+ }
+
+ /**
+ * Returns the next token.
+ */
+ public String nextToken() {
+ // determine next token
+ String token = super.nextToken();
+
+ //
+ if ( isTokenInQuotes(token) ) {
+ String followingToken = null;
+ do {
+ followingToken = super.nextToken();
+ token += followingToken;
+ } while ( !isTokenInQuotes(followingToken) );
+ }
+
+ // 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) )
+ token = token.substring(1, token.length()-1);
+ // return delimiter token only if respective flag is set
+ if ( token.equals(delim) && !returnDelims )
+ token = nextToken();
+
+ return token;
+ }
+
+ /**
+ * Checks whether a token in in quotes.
+ */
+ protected boolean isTokenInQuotes(String token) {
+ token = StringUtils.trimToEmpty(token);
+
+// // if token starts and ends with delimiter then
+// if ( token.length() > 1 && token.startsWith(quote) && token.endsWith(quote) )
+// return false;
+// return token.contains(quote);
+
+ // count number of quote signs
+ final char quoteChar = quote.charAt(0);
+ int i = -1;
+ int cnt = 0;
+ while ( (i = token.indexOf(quoteChar, i+1)) >= 0 )
+ cnt++;
+ // Assumption/Heuristic:
+ // Token starts/ends quoted part if number of quote signs is odd
+ return cnt > 0 && cnt % 2 == 1;
+ }
+}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java 2013-06-21 23:02:34 UTC (rev 2352)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListMaintainancePanel.java 2013-07-08 12:22:18 UTC (rev 2353)
@@ -12,7 +12,6 @@
import javax.swing.Action;
import javax.swing.JButton;
-import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/versionnumber/ApplicationUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/versionnumber/ApplicationUpdater.java 2013-06-21 23:02:34 UTC (rev 2352)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/versionnumber/ApplicationUpdater.java 2013-07-08 12:22:18 UTC (rev 2353)
@@ -176,6 +176,10 @@
// Updater-Command um wimecrem.exe ins Zielverzeichnis zu kopieren
File destFile = applFile;
+
+ LOGGER.debug("updaterFile: "+updaterFile);
+ LOGGER.debug("downloadFile: "+downloadFile);
+ LOGGER.debug("destFile: "+destFile);
String moveCmd = LangUtil.replaceParameters("java -cp ${0} ${1} MOVE ${2} ${3} 2000",
"\""+updaterFile.getAbsolutePath()+"\"",
IOUtilBasic.class.getName(),
@@ -183,6 +187,7 @@
"\""+destFile.getAbsoluteFile()+"\"");
// Updater-Command um wimecrem.exe neu zu starten
String restartCommand = APPL_RESTART_COMMAND;
+ LOGGER.debug("restartCommand: "+restartCommand);
String restartCmd = LangUtil.replaceParameters("java -cp ${0} ${1} EXEC 4000 ${2}",
"\""+updaterFile.getAbsolutePath()+"\"",
IOUtilBasic.class.getName(),
Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java 2013-06-21 23:02:34 UTC (rev 2352)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/lang/LangUtilTest.java 2013-07-08 12:22:18 UTC (rev 2353)
@@ -1,12 +1,20 @@
package de.schmitzm.lang;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.List;
+import java.util.StringTokenizer;
+import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
+import org.junit.Ignore;
import org.junit.Test;
import de.schmitzm.testing.TestingClass;
@@ -140,4 +148,115 @@
{
System.out.println( LangUtil.formatInterval(ManagementFactory.getRuntimeMXBean().getUptime()) );
}
+
+ @Ignore
+ @Test
+ public void testQuotedStringTokenizer() {
+ String text = "Item1, \"Item2\", \"Item3, and, Item4\" bla, \"blub\" bla, \"Item5, item5a\",,\"\",";
+ final String delim = ",";
+ final String quote = "\"";
+ final boolean returnTokens = true;
+ text = text.replaceAll(delim,delim+" ");
+ System.out.println("Text: '"+text+"'");
+
+
+// StringTokenizer st1 = new StringTokenizer(text, quote, true);
+// for (;st1.hasMoreTokens();) {
+// String token = null;
+// String subStr = st1.nextToken();
+// if ( quote.equals(subStr) ) {
+// if ( st1.hasMoreTokens() )
+// token = st1.nextToken();
+// if ( st1.hasMoreTokens() )
+// st1.nextToken(); // eliminate quote end
+// System.out.println(token);
+// } else {
+// StringTokenizer st2 = new StringTokenizer(subStr, delim, false);
+// for (;st2.hasMoreTokens();) {
+// token = st2.nextToken();
+// System.out.println(token);
+// }
+// }
+// }
+
+// StringTokenizer st1 = new StringTokenizer(text, delim+quote, true);
+// String lastToken = null;
+// boolean inQuote = false;
+// boolean afterQuote = false;
+// List<String> items = new ArrayList<String>();
+// for (;st1.hasMoreTokens();) {
+// String token = st1.nextToken();
+//
+// if ( quote.equals(token)) {
+// inQuote = !inQuote;
+// if ( !inQuote )
+// afterQuote = true;
+// String lastItem = items.remove( items.size()-1 );
+// lastItem += token;
+// items.add(lastItem);
+//
+// } else {
+// if ( inQuote || (afterQuote && !delim.equals(token)) ) {
+// String lastItem = items.remove( items.size()-1 );
+// lastItem += token;
+// items.add(lastItem);
+// } else {
+// if ( !delim.equals(token) )
+// items.add(StringUtils.stripStart(token,null));
+// else
+// afterQuote = false;
+// }
+// }
+// lastToken = token;
+// }
+//
+// for (String item : items)
+// System.out.println("'"+item+"'");
+
+
+// StringTokenizer st1 = new StringTokenizer(text, delim, true);
+// List<String> items = new ArrayList<String>();
+// for (;st1.hasMoreTokens();) {
+// String token = st1.nextToken();
+// String followingToken = null;
+// if ( token.contains(quote) ) {
+// do {
+// followingToken = st1.nextToken();
+// token += followingToken;
+// } while ( !followingToken.contains(quote) );
+// }
+// token = token.trim();
+// if ( !token.equals(delim) || returnTokens )
+// items.add(token);
+// }
+//
+// for (String item : items)
+// System.out.println("'"+item+"'");
+
+ StringTokenizer st1 = new StringTokenizerQuoted(text, quote, delim, false);
+ for (;st1.hasMoreTokens();)
+ System.out.println("'"+st1.nextToken()+"'");
+
+
+
+
+
+
+
+//// String regex = "\"([^\"]*)\"|(\\S+)";
+// String regex = quote+"([^"+quote+"]*)"+quote+"|([^("+delim+"|"+quote+")]+)";
+// System.out.println("RegEx: "+regex);
+// Matcher m = Pattern.compile(regex).matcher(text);
+// while (m.find()) {
+// if (m.group(1) != null) {
+// System.out.println("Quoted [" + m.group(1) + "]");
+// } else {
+// System.out.println("Plain [" + m.group(2) + "]");
+// }
+// }
+
+
+
+
+ }
}
\ No newline at end of file
More information about the Schmitzm-commits
mailing list