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
 
93 brianR 23
import net.brutex.xservices.types.StringMatchType;
86 brianR 24
import net.brutex.xservices.types.StringReplaceType;
25
import net.brutex.xservices.util.BrutexNamespaces;
26
import net.brutex.xservices.ws.StringService;
27
import net.brutex.xservices.ws.XServicesFault;
28
 
29
/**
30
 * @author Brian Rosenberger
31
 * @since 0.5.0
32
 *
33
 */
34
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.StringService", serviceName = StringService.SERVICE_NAME)
35
public class StringServiceImpl implements StringService {
36
 
87 brianR 37
	public StringReplaceType replaceRegEx(String res, String search,
38
			String replace, String flags) throws XServicesFault {
39
		try {
93 brianR 40
			int allflags = getFlags(flags);
87 brianR 41
			Pattern pattern = Pattern.compile(search, allflags);
42
			Matcher matcher = pattern.matcher(res);
43
			int count = 0;
44
			while (matcher.find()) {
45
				count++;
46
			}
47
			if (flags.contains("g")) {
48
				return new StringReplaceType(matcher.replaceAll(replace), count);
49
			} else {
50
				if (count > 1)
51
					count = 1;
52
				return new StringReplaceType(matcher.replaceFirst(replace),
53
						count);
54
			}
55
		} catch (Exception e) {
56
			throw new XServicesFault(e);
86 brianR 57
		}
87 brianR 58
 
86 brianR 59
	}
60
 
93 brianR 61
	@Override
62
	public StringMatchType matchRegEx(String res, String search, String flags)
63
			throws XServicesFault {
64
		int allflags = getFlags(flags);
65
		Pattern pattern = Pattern.compile(search, allflags);
66
		Matcher matcher = pattern.matcher(res);
67
		StringMatchType rm = new StringMatchType();
68
		while (matcher.find()) {
69
			rm.addStringMatch(matcher.start(), matcher.end(), matcher.group());
70
		}
71
		return rm;
72
	}
73
 
74
	private int getFlags(String flags) {
75
		int allflags = 0;
76
		if(flags.contains("i")) {
77
			allflags = allflags + Pattern.CASE_INSENSITIVE;
78
		}
79
		if(flags.contains("m")) {
80
			allflags = allflags + Pattern.MULTILINE;
81
		}
82
		if(flags.contains("u")) {
83
			allflags = allflags + Pattern.UNICODE_CASE;
84
		}
85
		if(flags.contains("s")) {
86
			allflags = allflags + Pattern.DOTALL;
87
		}
88
		if(flags.contains("d")) {
89
			allflags = allflags + Pattern.UNIX_LINES;
90
		}
91
		if(flags.contains("x")) {
92
			allflags = allflags + Pattern.COMMENTS;
93
		}
94
		return allflags;
95
	}
96
 
86 brianR 97
}