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