Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 brianR 1
/*
2
 *   Copyright 2012 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
package net.brutex.xservices.ws.impl;
17
 
18
import java.util.regex.Matcher;
19
import java.util.regex.Pattern;
20
 
21
import javax.jws.WebService;
22
 
23
import net.brutex.xservices.types.StringReplaceType;
24
import net.brutex.xservices.util.BrutexNamespaces;
25
import net.brutex.xservices.ws.StringService;
26
import net.brutex.xservices.ws.XServicesFault;
27
 
28
/**
29
 * @author Brian Rosenberger
30
 * @since 0.5.0
31
 *
32
 */
33
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.StringService", serviceName = StringService.SERVICE_NAME)
34
public class StringServiceImpl implements StringService {
35
 
87 brianR 36
	public StringReplaceType replaceRegEx(String res, String search,
37
			String replace, String flags) throws XServicesFault {
38
		try {
39
			int allflags = 0;
40
			if (flags.contains("i")) {
41
				allflags = allflags + Pattern.CASE_INSENSITIVE;
42
			}
43
			Pattern pattern = Pattern.compile(search, allflags);
44
			Matcher matcher = pattern.matcher(res);
45
			int count = 0;
46
			while (matcher.find()) {
47
				count++;
48
			}
49
			if (flags.contains("g")) {
50
				return new StringReplaceType(matcher.replaceAll(replace), count);
51
			} else {
52
				if (count > 1)
53
					count = 1;
54
				return new StringReplaceType(matcher.replaceFirst(replace),
55
						count);
56
			}
57
		} catch (Exception e) {
58
			throw new XServicesFault(e);
86 brianR 59
		}
87 brianR 60
 
86 brianR 61
	}
62
 
63
}