/***************************************************************************
 *   Copyright (C) by Fabrizio Montesi                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package joliex.util;


import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jolie.net.CommMessage;
import jolie.runtime.JavaService;
import jolie.runtime.Value;
import jolie.runtime.ValueVector;

public class StringUtils extends JavaService
{
	public CommMessage replaceAll( CommMessage message )
	{
		String regex = message.value().getChildren( "regex" ).first().strValue();
		String replacement = message.value().getChildren( "replacement" ).first().strValue();
		return new CommMessage(
				"replaceAll", "/",
				Value.create( message.value().strValue().replaceAll( regex, replacement ) )
						);
	}
	
	public CommMessage trim( CommMessage message )
	{
		return new CommMessage( "trim", "/", Value.create( message.value().strValue().trim() ) );
	}
	
	public CommMessage split( CommMessage message )
	{
		String str = message.value().strValue();
		int limit = 0;
		Value lValue = message.value().getFirstChild( "limit" );
		if ( lValue.isDefined() ) {
			limit = lValue.intValue();
		}
		String[] ss = str.split(
				message.value().getFirstChild( "regex" ).strValue(),
				limit
			);
		Value value = Value.create();
		for( int i = 0; i < ss.length; i++ ) {
			value.getNewChild( "result" ).add( Value.create( ss[ i ] ) );
		}

		return new CommMessage( "split", "/", value );
	}

	public CommMessage match( CommMessage message )
	{
		Pattern p = Pattern.compile( message.value().getFirstChild( "regex" ).strValue() );
		Matcher m = p.matcher( message.value().strValue() );
		Value response = Value.create();
		if ( m.matches() ) {
			response.setValue( 1 );
			ValueVector groups = response.getChildren( "group" );
			groups.add( Value.create( m.group( 0 ) ) );
			for( int i = 0; i < m.groupCount(); i++ ) {
				groups.add( Value.create( m.group( i+1 ) ) );
			}
		} else {
			response.setValue( 0 );
		}
		return new CommMessage( "match", "/", response );
	}
}

@@ -3419,11 +3419,649 @@
 se );%0A%09%7D
+%0A%0A%09public CommMessage leftPad( CommMessage request )%0A%09%7B%0A%09%09String orig = request.value().strValue();%0A%09%09int length = request.value().getFirstChild( %22length%22 ).intValue();%0A%09%09if ( orig.length() %3E= length ) %7B%0A%09%09%09return CommMessage.createResponse( request, Value.create( orig ) );%0A%09%09%7D%0A%0A%09%09char padChar = request.value().getFirstChild( %22char%22 ).strValue().charAt( 0 );%0A%0A%09%09StringBuilder builder = new StringBuilder();%0A%09%09int padLength = orig.length() - length;%0A%09%09for( int i = 0; i %3C padLength; i++ ) %7B%0A%09%09%09builder.append( padChar );%0A%09%09%7D%0A%09%09builder.append( orig );%0A%09%09return CommMessage.createResponse( request, Value.create( builder.toString() ) );%0A%09%7D
 %0A%7D%0A

Support for left padding.