Subversion Repositories XServices

Rev

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

Rev 94 Rev 115
1
/*
1
/*
2
 *   Copyright 2012 Brian Rosenberger (Brutex Network)
2
 *   Copyright 2013 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
 
16
package net.brutex.xservices.ws.impl;
17
package net.brutex.xservices.ws.impl;
-
 
18
 
-
 
19
import java.util.ArrayList;
17
 
20
import java.util.StringTokenizer;
18
import java.util.regex.Matcher;
21
import java.util.regex.Matcher;
19
import java.util.regex.Pattern;
-
 
20
 
22
import java.util.regex.Pattern;
21
import javax.jws.WebService;
-
 
22
 
23
import javax.jws.WebService;
23
import net.brutex.xservices.types.StringMatchType;
24
import net.brutex.xservices.types.StringMatchType;
24
import net.brutex.xservices.types.StringReplaceType;
25
import net.brutex.xservices.types.StringReplaceType;
25
import net.brutex.xservices.util.BrutexNamespaces;
26
import net.brutex.xservices.types.StringSplitType;
26
import net.brutex.xservices.ws.StringService;
27
import net.brutex.xservices.ws.StringService;
-
 
28
import net.brutex.xservices.ws.XServicesFault;
-
 
29
import org.apache.commons.lang3.StringEscapeUtils;
-
 
30
import org.apache.commons.lang3.text.translate.CharSequenceTranslator;
27
import net.brutex.xservices.ws.XServicesFault;
31
import org.apache.commons.lang3.text.translate.NumericEntityEscaper;
28
 
32
 
29
/**
-
 
30
 * @author Brian Rosenberger
33
/**
31
 * @since 0.5.0
34
 * @author Brian Rosenberger, bru(at)brutex.de
32
 * 
35
 *
33
 */
36
 */
-
 
37
@WebService(targetNamespace="http://ws.xservices.brutex.net", endpointInterface="net.brutex.xservices.ws.StringService", serviceName="StringService")
34
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.StringService", serviceName = StringService.SERVICE_NAME)
38
public class StringServiceImpl
35
public class StringServiceImpl implements StringService {
39
  implements StringService
36
 
40
{
-
 
41
  public StringReplaceType replaceRegEx(String res, String search, String replace, String flags)
-
 
42
    throws XServicesFault
37
	public StringReplaceType replaceRegEx(String res, String search,
43
  {
38
			String replace, String flags) throws XServicesFault {
44
    try
39
		try {
45
    {
40
			int allflags = getFlags(flags);
46
      int allflags = getFlags(flags);
41
			Pattern pattern = Pattern.compile(search, allflags);
47
      Pattern pattern = Pattern.compile(search, allflags);
42
			Matcher matcher = pattern.matcher(res);
48
      Matcher matcher = pattern.matcher(res);
43
			int count = 0;
49
      int count = 0;
44
			while (matcher.find()) {
50
      while (matcher.find()) {
45
				count++;
51
        count++;
46
			}
52
      }
47
			if (flags.contains("g")) {
53
      if (flags.contains("g")) {
48
				return new StringReplaceType(matcher.replaceAll(replace), count);
54
        return new StringReplaceType(matcher.replaceAll(replace), count);
49
			} else {
55
      }
50
				if (count > 1)
56
      if (count > 1)
51
					count = 1;
57
        count = 1;
52
				return new StringReplaceType(matcher.replaceFirst(replace),
58
      return new StringReplaceType(matcher.replaceFirst(replace), 
53
						count);
59
        count);
54
			}
60
    }
55
		} catch (Exception e) {
61
    catch (Exception e) {
56
			throw new XServicesFault(e);
62
      throw new XServicesFault(e);
57
		}
63
    }
58
 
-
 
59
	}
64
  }
60
 
-
 
61
	@Override
65
 
62
	public StringMatchType matchRegEx(String res, String search, String flags)
66
  public StringMatchType matchRegEx(String res, String search, String flags)
-
 
67
    throws XServicesFault
63
			throws XServicesFault {
68
  {
64
		int allflags = getFlags(flags);
69
    int allflags = getFlags(flags);
65
		Pattern pattern = Pattern.compile(search, allflags);
70
    Pattern pattern = Pattern.compile(search, allflags);
66
		Matcher matcher = pattern.matcher(res);
71
    Matcher matcher = pattern.matcher(res);
67
		StringMatchType rm = new StringMatchType();
72
    StringMatchType rm = new StringMatchType();
68
		while (matcher.find()) {
-
 
-
 
73
    
69
			for(int i=0; i<=matcher.groupCount();i++){
74
	for (int i=0; i <= matcher.groupCount(); i++) {
-
 
75
	    	while(matcher.find()) {
70
				rm.addStringMatch(matcher.start(), matcher.end(), matcher.group(i));
76
	    		rm.addStringMatch(matcher.start(i), matcher.end(i), "group-"+i, matcher.group(i));
71
			}		
77
	    	}
-
 
78
	    	matcher.reset();
72
		}
79
	 }
73
		return rm;
80
    return rm;
74
	}
81
  }
-
 
82
 
-
 
83
  public String encodeToXMLEntities(String res)
-
 
84
    throws XServicesFault
-
 
85
  {
-
 
86
    StringEscapeUtils fac = new StringEscapeUtils();
-
 
87
    StringEscapeUtils.ESCAPE_XML.with(new CharSequenceTranslator[] { NumericEntityEscaper.between(128, 2147483647) });
-
 
88
    return StringEscapeUtils.escapeXml(res);
-
 
89
  }
-
 
90
  
-
 
91
  public StringSplitType splitString(String paramString, String delimiter) throws XServicesFault {
-
 
92
		StringTokenizer tk = new StringTokenizer(paramString, delimiter);
-
 
93
		StringSplitType result = new StringSplitType();
-
 
94
		while(tk.hasMoreTokens()) {
-
 
95
			result.addStringMatch( tk.nextToken() );
-
 
96
		}
-
 
97
		return result;
-
 
98
	}
75
	
99
 
76
	private int getFlags(String flags) {
100
  private int getFlags(String flags) {
77
		int allflags = 0;
101
    int allflags = 0;
78
		if(flags.contains("i")) {
102
    if (flags.contains("i")) {
79
			allflags = allflags + Pattern.CASE_INSENSITIVE;
103
      allflags += 2;
80
		}
104
    }
81
		if(flags.contains("m")) {
105
    if (flags.contains("m")) {
82
			allflags = allflags + Pattern.MULTILINE;
106
      allflags += 8;
83
		}
107
    }
84
		if(flags.contains("u")) {
108
    if (flags.contains("u")) {
85
			allflags = allflags + Pattern.UNICODE_CASE;
109
      allflags += 64;
86
		}
110
    }
87
		if(flags.contains("s")) {
111
    if (flags.contains("s")) {
88
			allflags = allflags + Pattern.DOTALL;
112
      allflags += 32;
89
		}
113
    }
90
		if(flags.contains("d")) {
114
    if (flags.contains("d")) {
91
			allflags = allflags + Pattern.UNIX_LINES;
115
      allflags++;
92
		}
116
    }
93
		if(flags.contains("x")) {
117
    if (flags.contains("x")) {
94
			allflags = allflags + Pattern.COMMENTS;
118
      allflags += 4;
95
		}
119
    }
96
		return allflags;
120
    return allflags;
97
	}
121
  }
98
 
-
 
99
}
122
}