Subversion Repositories XServices

Rev

Rev 94 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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