You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-commits@ws.apache.org by da...@apache.org on 2007/02/12 16:43:33 UTC
svn commit: r506497 - in
/webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util:
StringUtils.java messages/Messages.java
Author: danj
Date: Mon Feb 12 07:43:33 2007
New Revision: 506497
URL: http://svn.apache.org/viewvc?view=rev&rev=506497
Log:
Added StringUtils to replace the missing replaceFirst/replaceAll methods from J2ME String API. The
implementations of these methods do not use java.util.regex.* like the J2SE JDK does.
Added:
webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/StringUtils.java
Modified:
webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/messages/Messages.java
Added: webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/StringUtils.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/StringUtils.java?view=auto&rev=506497
==============================================================================
--- webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/StringUtils.java (added)
+++ webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/StringUtils.java Mon Feb 12 07:43:33 2007
@@ -0,0 +1,60 @@
+/*=============================================================================*
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *=============================================================================*/
+
+package org.apache.muse.util;
+
+import java.util.StringTokenizer;
+
+/**
+ *
+ * StringUtils provides java.lang.String-related methods that are part of
+ * the J2SE API but not its J2ME equivalent.
+ *
+ * @author Dan Jemiolo (danj)
+ *
+ */
+
+public class StringUtils
+{
+ public static String replaceFirst(String s, String pattern, String replacement)
+ {
+ int patternStart = s.indexOf(pattern);
+
+ if (patternStart < 0)
+ return s;
+
+ String start = s.substring(0, patternStart);
+ String end = s.substring(patternStart + pattern.length());
+ return start + replacement + end;
+ }
+
+ public static String[] split(String s)
+ {
+ return split(s, " ");
+ }
+
+ public static String[] split(String s, String separator)
+ {
+ StringTokenizer parser = new StringTokenizer(s, separator);
+ int numberOfTokens = parser.countTokens();
+ String[] results = new String[numberOfTokens];
+
+ for (int n = 0; n < numberOfTokens; ++n)
+ results[n] = parser.nextToken();
+
+ return results;
+ }
+}
Modified: webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/messages/Messages.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/messages/Messages.java?view=diff&rev=506497&r1=506496&r2=506497
==============================================================================
--- webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/messages/Messages.java (original)
+++ webservices/muse/trunk/modules/muse-util/src/org/apache/muse/util/messages/Messages.java Mon Feb 12 07:43:33 2007
@@ -19,6 +19,8 @@
import java.util.Locale;
import java.util.ResourceBundle;
+import org.apache.muse.util.StringUtils;
+
/**
*
* Messages is a wrapper for a java.util.ResourceBundle that provides a
@@ -123,13 +125,6 @@
* If the number of filler values is less than the number of placeholders,
* the placeholders will be left in the string. If the number of filler
* values is greater, the leftover values will simply not be used.
- * <br><br>
- * <b>NOTE:</b> If any of the filler values contains a regex, the
- * replacement mechanism (String.replaceFirst()) may try to perform
- * grouping or other regex functions, causing the replacement to fail.
- * If this method throws a RuntimeException that has to do with regular
- * expressions or string replacement, the problem is that one of your
- * filler values has regex symbols in it.
*
* @param name
* The name of the property/message to lookup.
@@ -155,13 +150,6 @@
* If the number of filler values is less than the number of placeholders,
* the placeholders will be left in the string. If the number of filler
* values is greater, the leftover values will simply not be used.
- * <br><br>
- * <b>NOTE:</b> If any of the filler values contains a regex, the
- * replacement mechanism (String.replaceFirst()) may try to perform
- * grouping or other regex functions, causing the replacement to fail.
- * If this method throws a RuntimeException that has to do with regular
- * expressions or string replacement, the problem is that one of your
- * filler values has regex symbols in it.
*
* @param name
* The name of the property/message to lookup.
@@ -179,34 +167,25 @@
*/
public String get(String name, Object[] filler, boolean includeID)
{
- String message = _messages.getString(name);
-
+ String[] fillerStrings = new String[filler.length];
+
//
- // this is slow because it's an n^2 operation
+ // convert all filler values to strings
+ //
+ // NOTE: we replace all \ in the user's text because they get
+ // stripped out in XML/SOAP messages
//
for (int n = 0; n < filler.length; ++n)
{
- //
- // we replace all \ in the user's text because they get
- // stripped out in XML/SOAP messages
- //
- String next = filler[n] == null ? "null" : filler[n].toString();
- next = next.replace('\\', '/');
-
- //
- // NOTE: this will throw a runtime exception if the value
- // of the "next" filler item contains a regex that
- // causes the pattern matcher to try and group results
- // together.
- //
- message = message.replaceFirst(_PLACE_HOLDER, next);
+ fillerStrings[n] = String.valueOf(filler[n]);
+ fillerStrings[n] = fillerStrings[n].replace('\\', '/');
}
//
- // build up the error text - include the bundle ID for debugging
+ // build up the error text - include the message ID for debugging
//
- int length = name.length() + message.length() + 16;
- StringBuffer buffer = new StringBuffer(length);
+ String message = _messages.getString(name);
+ StringBuffer buffer = new StringBuffer(message.length() + name.length() + 512);
if (includeID)
{
@@ -214,6 +193,17 @@
buffer.append(name);
buffer.append("'] ");
}
+ //
+ // HACK - we can't use String.replaceFirst() because J2ME's String
+ // class does not have this method. this is slow because
+ // it's O(n^2). we could make it faster by calculating the
+ // lengths and using the buffer more, but this brings up
+ // too many edge cases to test for (re: placeholders in
+ // the message vs. filler values we actually get)
+ //
+
+ for (int n = 0; n < fillerStrings.length; ++n)
+ message = StringUtils.replaceFirst(message, _PLACE_HOLDER, fillerStrings[n]);
buffer.append(message);
return buffer.toString();
---------------------------------------------------------------------
To unsubscribe, e-mail: muse-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: muse-commits-help@ws.apache.org