Subversion Repositories XServices

Rev

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

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