Subversion Repositories XServices

Rev

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

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