Subversion Repositories XServices

Rev

Rev 87 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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