Subversion Repositories XServices

Rev

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

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