From 995a8b8119453a98e30c2c187da7a84c8a6494cf Mon Sep 17 00:00:00 2001 From: Brian Rosenberger Date: Mon, 21 May 2012 18:23:11 +0000 Subject: [PATCH] new StringService git-svn-id: https://brutex.net/svn/xservices/trunk@86 e7e49efb-446e-492e-b9ec-fcafc1997a86 --- .../xservices/types/StringReplaceType.java | 37 ++++++++++ .../brutex/xservices/ws/StringService.java | 71 +++++++++++++++++++ .../xservices/ws/impl/StringServiceImpl.java | 61 ++++++++++++++++ web/WEB-INF/cxf-beans.xml | 4 ++ 4 files changed, 173 insertions(+) create mode 100644 src/java/net/brutex/xservices/types/StringReplaceType.java create mode 100644 src/java/net/brutex/xservices/ws/StringService.java create mode 100644 src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java diff --git a/src/java/net/brutex/xservices/types/StringReplaceType.java b/src/java/net/brutex/xservices/types/StringReplaceType.java new file mode 100644 index 0000000..1230bf7 --- /dev/null +++ b/src/java/net/brutex/xservices/types/StringReplaceType.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012 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. + * 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 net.brutex.xservices.types; + +import javax.xml.bind.annotation.XmlType; + + + +/** + * + * @author Brian Rosenberger, bru@brutex.de + */ +@XmlType +public class StringReplaceType { + public String string; + public int count; + public StringReplaceType() {} + public StringReplaceType(String string, int count) { + this(); + this.string = string; + this.count = count; + } +} diff --git a/src/java/net/brutex/xservices/ws/StringService.java b/src/java/net/brutex/xservices/ws/StringService.java new file mode 100644 index 0000000..c2ae865 --- /dev/null +++ b/src/java/net/brutex/xservices/ws/StringService.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012 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. + * 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 net.brutex.xservices.ws; + +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; + +import net.brutex.xservices.types.ReturnCode; +import net.brutex.xservices.types.StringReplaceType; +import net.brutex.xservices.types.TargetNodeType; +import net.brutex.xservices.types.ant.AttachmentType; +import net.brutex.xservices.types.ant.CollectionType; +import net.brutex.xservices.types.ant.FileResource; +import net.brutex.xservices.util.BrutexNamespaces; + +import org.apache.cxf.annotations.WSDLDocumentation; + +/** + * String operation services. + * @author Brian Rosenberger + * @since 0.5.0 + * + */ +@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES) +public interface StringService { + + public static final String SERVICE_NAME = "StringService"; + + final String OPERATION_REPLACEREGEX = "replaceRegEx"; + + + final static String PARAM_STRING = "string"; + final static String PARAM_SEARCH = "search"; + final static String PARAM_REPLACE = "replace"; + final static String PARAM_FLAGS = "regexflags"; + ; + /** + * String replace using regular expression. + * @param res String + * @param search regex search pattern + * @param replace string replacement + * @param flags regex flags + * + * @return replacement + * @throws XServicesFault + */ + @WebMethod(operationName=OPERATION_REPLACEREGEX) + @WSDLDocumentation(value="Store text based data") + public abstract StringReplaceType replaceRegEx( + @WebParam(name = PARAM_STRING) String res, + @WebParam(name = PARAM_SEARCH) String search, + @WebParam(name = PARAM_REPLACE) String replace, + @WebParam(name = PARAM_FLAGS) String flags) throws XServicesFault; + + +} diff --git a/src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java b/src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java new file mode 100644 index 0000000..37f0a63 --- /dev/null +++ b/src/java/net/brutex/xservices/ws/impl/StringServiceImpl.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012 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. + * 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 net.brutex.xservices.ws.impl; + + + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.jws.WebService; + +import net.brutex.xservices.types.StringReplaceType; +import net.brutex.xservices.util.BrutexNamespaces; +import net.brutex.xservices.ws.StringService; +import net.brutex.xservices.ws.XServicesFault; + +/** + * @author Brian Rosenberger + * @since 0.5.0 + * + */ +@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.StringService", serviceName = StringService.SERVICE_NAME) +public class StringServiceImpl implements StringService { + + public StringReplaceType replaceRegEx(String res, String search, String replace, + String flags) throws XServicesFault { + + int allflags = 0; + if(flags.contains("i")) { + allflags = allflags + Pattern.CASE_INSENSITIVE; + } + 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); + } + + } + + +} diff --git a/web/WEB-INF/cxf-beans.xml b/web/WEB-INF/cxf-beans.xml index 1889b6b..c67f820 100644 --- a/web/WEB-INF/cxf-beans.xml +++ b/web/WEB-INF/cxf-beans.xml @@ -55,6 +55,10 @@ implementor="net.brutex.xservices.ws.impl.MiscServiceImpl" address="/MiscService"> + + +