Subversion Repositories XServices

Rev

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();
94 brianR 68
		while (matcher.find()) {
69
			for(int i=0; i<=matcher.groupCount();i++){
70
				rm.addStringMatch(matcher.start(), matcher.end(), matcher.group(i));
71
			}
93 brianR 72
		}
73
		return rm;
74
	}
75
 
76
	private int getFlags(String flags) {
77
		int allflags = 0;
78
		if(flags.contains("i")) {
79
			allflags = allflags + Pattern.CASE_INSENSITIVE;
80
		}
81
		if(flags.contains("m")) {
82
			allflags = allflags + Pattern.MULTILINE;
83
		}
84
		if(flags.contains("u")) {
85
			allflags = allflags + Pattern.UNICODE_CASE;
86
		}
87
		if(flags.contains("s")) {
88
			allflags = allflags + Pattern.DOTALL;
89
		}
90
		if(flags.contains("d")) {
91
			allflags = allflags + Pattern.UNIX_LINES;
92
		}
93
		if(flags.contains("x")) {
94
			allflags = allflags + Pattern.COMMENTS;
95
		}
96
		return allflags;
97
	}
98
 
86 brianR 99
}