diff --git a/src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java b/src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java index ff10c6c..4ccd1c5 100644 --- a/src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java +++ b/src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Brian Rosenberger (Brutex Network) + * Copyright 2013 Brian Rosenberger (Brutex Network) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,87 +13,110 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package net.brutex.xservices.ws.impl; +import java.util.ArrayList; +import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.jws.WebService; - import net.brutex.xservices.types.StringMatchType; import net.brutex.xservices.types.StringReplaceType; -import net.brutex.xservices.util.BrutexNamespaces; +import net.brutex.xservices.types.StringSplitType; import net.brutex.xservices.ws.StringService; import net.brutex.xservices.ws.XServicesFault; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.lang3.text.translate.CharSequenceTranslator; +import org.apache.commons.lang3.text.translate.NumericEntityEscaper; /** - * @author Brian Rosenberger - * @since 0.5.0 - * + * @author Brian Rosenberger, bru(at)brutex.de + * */ -@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.StringService", serviceName = StringService.SERVICE_NAME) -public class StringServiceImpl implements StringService { +@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.StringService", serviceName="StringService") +public class StringServiceImpl + implements StringService +{ + public StringReplaceType replaceRegEx(String res, String search, String replace, String flags) + throws XServicesFault + { + try + { + int allflags = getFlags(flags); + Pattern pattern = Pattern.compile(search, allflags); + Matcher matcher = pattern.matcher(res); + int count = 0; + while (matcher.find()) { + count++; + } + if (flags.contains("g")) { + return new StringReplaceType(matcher.replaceAll(replace), count); + } + if (count > 1) + count = 1; + return new StringReplaceType(matcher.replaceFirst(replace), + count); + } + catch (Exception e) { + throw new XServicesFault(e); + } + } - public StringReplaceType replaceRegEx(String res, String search, - String replace, String flags) throws XServicesFault { - try { - int allflags = getFlags(flags); - Pattern pattern = Pattern.compile(search, allflags); - Matcher matcher = pattern.matcher(res); - int count = 0; - while (matcher.find()) { - count++; - } - if (flags.contains("g")) { - return new StringReplaceType(matcher.replaceAll(replace), count); - } else { - if (count > 1) - count = 1; - return new StringReplaceType(matcher.replaceFirst(replace), - count); - } - } catch (Exception e) { - throw new XServicesFault(e); + public StringMatchType matchRegEx(String res, String search, String flags) + throws XServicesFault + { + int allflags = getFlags(flags); + Pattern pattern = Pattern.compile(search, allflags); + Matcher matcher = pattern.matcher(res); + StringMatchType rm = new StringMatchType(); + + for (int i=0; i <= matcher.groupCount(); i++) { + while(matcher.find()) { + rm.addStringMatch(matcher.start(i), matcher.end(i), "group-"+i, matcher.group(i)); + } + matcher.reset(); + } + return rm; + } + + public String encodeToXMLEntities(String res) + throws XServicesFault + { + StringEscapeUtils fac = new StringEscapeUtils(); + StringEscapeUtils.ESCAPE_XML.with(new CharSequenceTranslator[] { NumericEntityEscaper.between(128, 2147483647) }); + return StringEscapeUtils.escapeXml(res); + } + + public StringSplitType splitString(String paramString, String delimiter) throws XServicesFault { + StringTokenizer tk = new StringTokenizer(paramString, delimiter); + StringSplitType result = new StringSplitType(); + while(tk.hasMoreTokens()) { + result.addStringMatch( tk.nextToken() ); } - - } - - @Override - public StringMatchType matchRegEx(String res, String search, String flags) - throws XServicesFault { - int allflags = getFlags(flags); - Pattern pattern = Pattern.compile(search, allflags); - Matcher matcher = pattern.matcher(res); - StringMatchType rm = new StringMatchType(); - while (matcher.find()) { - for(int i=0; i<=matcher.groupCount();i++){ - rm.addStringMatch(matcher.start(), matcher.end(), matcher.group(i)); - } - } - return rm; - } - - private int getFlags(String flags) { - int allflags = 0; - if(flags.contains("i")) { - allflags = allflags + Pattern.CASE_INSENSITIVE; - } - if(flags.contains("m")) { - allflags = allflags + Pattern.MULTILINE; - } - if(flags.contains("u")) { - allflags = allflags + Pattern.UNICODE_CASE; - } - if(flags.contains("s")) { - allflags = allflags + Pattern.DOTALL; - } - if(flags.contains("d")) { - allflags = allflags + Pattern.UNIX_LINES; - } - if(flags.contains("x")) { - allflags = allflags + Pattern.COMMENTS; - } - return allflags; + return result; } + private int getFlags(String flags) { + int allflags = 0; + if (flags.contains("i")) { + allflags += 2; + } + if (flags.contains("m")) { + allflags += 8; + } + if (flags.contains("u")) { + allflags += 64; + } + if (flags.contains("s")) { + allflags += 32; + } + if (flags.contains("d")) { + allflags++; + } + if (flags.contains("x")) { + allflags += 4; + } + return allflags; + } }