Skip to content

Commit

Permalink
Start to use some precompiled patterns for common places where we spl…
Browse files Browse the repository at this point in the history
…it strings

git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1423981 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Dec 19, 2012
1 parent fc8177c commit c0e5c55
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
30 changes: 27 additions & 3 deletions api/src/main/java/org/apache/cxf/common/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,38 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class StringUtils {

public static final Map<String, Pattern> PATTERN_MAP = new HashMap<String, Pattern>();
static {
String patterns[] = {"/", " ", ":", "," , ";", "="};
for (String p : patterns) {
PATTERN_MAP.put(p, Pattern.compile(p));
}
}

private StringUtils() {
}

public static String[] split(String s, String regex) {
Pattern p = PATTERN_MAP.get(regex);
if (p != null) {
return p.split(s);
}
return s.split(regex);
}
public static String[] split(String s, String regex, int limit) {
Pattern p = PATTERN_MAP.get(regex);
if (p != null) {
return p.split(s, limit);
}
return s.split(regex, limit);
}

public static String extract(String string, String startToken, String endToken) {
int start = string.indexOf(startToken) + startToken.length();
Expand Down Expand Up @@ -112,7 +136,7 @@ public static String diff(String str1, String str2) {

public static List<String> getParts(String str, String seperator) {
List<String> ret = new ArrayList<String>();
List<String> parts = Arrays.asList(str.split(seperator));
List<String> parts = Arrays.asList(split(str, seperator));
for (String part : parts) {
if (!isEmpty(part)) {
ret.add(part);
Expand All @@ -122,7 +146,7 @@ public static List<String> getParts(String str, String seperator) {
}

public static String getFirstNotEmpty(String str, String seperator) {
List<String> parts = Arrays.asList(str.split(seperator));
List<String> parts = Arrays.asList(split(str, seperator));
for (String part : parts) {
if (!isEmpty(part)) {
return part;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ private AuthorizationPolicy getAuthorizationPolicyFromMessage(String credentials
if (credentials == null || StringUtils.isEmpty(credentials.trim())) {
return null;
}
String authType = credentials.split(" ")[0];
String creds[] = StringUtils.split(credentials, " ");
String authType = creds[0];
if ("Basic".equals(authType)) {
String authEncoded = credentials.split(" ")[1];
String authEncoded = creds[1];
try {
String authDecoded = new String(Base64Utility.decode(authEncoded));
int idx = authDecoded.indexOf(':');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageUtils;

Expand Down Expand Up @@ -68,11 +69,11 @@ private void handleSetCookie(List<String> headers) {
}

for (String header : headers) {
String[] cookies = header.split(",");
String[] cookies = StringUtils.split(header, ",");
for (String cookie : cookies) {
String[] parts = cookie.split(";");
String[] parts = StringUtils.split(cookie, ";");

String[] kv = parts[0].split("=", 2);
String[] kv = StringUtils.split(parts[0], "=", 2);
if (kv.length != 2) {
continue;
}
Expand All @@ -81,7 +82,7 @@ private void handleSetCookie(List<String> headers) {
Cookie newCookie = new Cookie(name, value);

for (int i = 1; i < parts.length; i++) {
kv = parts[i].split("=", 2);
kv = StringUtils.split(parts[i], "=", 2);
name = kv[0].trim();
value = (kv.length > 1) ? kv[1].trim() : null;
if (name.equalsIgnoreCase(Cookie.DISCARD_ATTRIBUTE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Enumeration<String> getInitParameterNames() {
protected static List<Pattern> parseListSequence(String values) {
if (values != null) {
List<Pattern> list = new LinkedList<Pattern>();
String[] pathValues = values.split(" ");
String[] pathValues = StringUtils.split(values, " ");
for (String value : pathValues) {
String theValue = value.trim();
if (theValue.length() > 0) {
Expand All @@ -139,13 +139,13 @@ protected static Map<String, String> parseMapSequence(String sequence) {
if (sequence != null) {
sequence = sequence.trim();
Map<String, String> map = new HashMap<String, String>();
String[] pairs = sequence.split(" ");
String[] pairs = StringUtils.split(sequence, " ");
for (String pair : pairs) {
String thePair = pair.trim();
if (thePair.length() == 0) {
continue;
}
String[] value = thePair.split("=");
String[] value = StringUtils.split(thePair, "=");
if (value.length == 2) {
map.put(value[0].trim(), value[1].trim());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import org.apache.cxf.common.logging.LogUtils;
import org.apache.cxf.common.util.SOAPConstants;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.helpers.HttpHeaderHelper;
import org.apache.cxf.message.MessageUtils;
Expand Down Expand Up @@ -339,7 +340,7 @@ static String getEncoding(String ct) throws UnsupportedEncodingException {
String contentType = ct.toLowerCase();
String enc = null;

String[] tokens = contentType.split(";");
String[] tokens = StringUtils.split(contentType, ";");
for (String token : tokens) {
int index = token.indexOf("charset=");
if (index >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.common.util.StringUtils;

/**
* URI utilities.
*
Expand Down Expand Up @@ -113,7 +115,7 @@ public static Map<String, String> parseQuery(String uri) throws URISyntaxExcepti
try {
Map<String, String> rc = new HashMap<String, String>();
if (uri != null) {
String[] parameters = uri.split("&");
String[] parameters = StringUtils.split(uri, "&");
for (String parameter : parameters) {
int p = parameter.indexOf("=");
if (p >= 0) {
Expand Down

0 comments on commit c0e5c55

Please sign in to comment.