Subversion Repositories XServices

Rev

Blame | Last modification | View Log | Download | RSS feed

/*
 *   Copyright 2012 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.brutex.xservices.ws.impl;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jws.WebService;

import net.brutex.xservices.types.StringReplaceType;
import net.brutex.xservices.util.BrutexNamespaces;
import net.brutex.xservices.ws.StringService;
import net.brutex.xservices.ws.XServicesFault;

/**
 * @author Brian Rosenberger
 * @since 0.5.0
 * 
 */
@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES, endpointInterface = "net.brutex.xservices.ws.StringService", serviceName = StringService.SERVICE_NAME)
public class StringServiceImpl implements StringService {

        public StringReplaceType replaceRegEx(String res, String search, String replace,
                        String flags) throws XServicesFault {
                
                int allflags = 0;
                if(flags.contains("i")) {
                        allflags = allflags + Pattern.CASE_INSENSITIVE;
                }
                Pattern pattern = Pattern.compile(search, allflags);
                Matcher matcher = pattern.matcher(res);
                int count = 0;
                while( matcher.find()) {
                        count++;
                }
                if(flags.contains("g")) {
                        return new StringReplaceType(matcher.replaceAll(replace), count);       
                } else {
                        if(count>1) count = 1;
                        return new StringReplaceType(matcher.replaceFirst(replace), count);
                }
                
        }

        
}