Subversion Repositories XServices

Rev

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