/***************************************************************************
 *   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.Pattern;
import jolie.net.CommMessage;
import jolie.runtime.JavaService;
import jolie.runtime.Value;

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 )
	{
		String str = message.value().strValue();
		String regex = message.value().getFirstChild( "regex" ).strValue();
		int ret = ( Pattern.matches( regex, str ) ) ? 1 : 0;
		return new CommMessage( "match", "/", Value.create( ret ) );
	}
}

@@ -1558,16 +1558,48 @@
 util;%0A%0A%0A
+import java.util.regex.Matcher;%0A
 import j
@@ -1713,16 +1713,50 @@
 e.Value;
+%0Aimport jolie.runtime.ValueVector;
 %0A%0Apublic
@@ -2834,26 +2834,25 @@
 value );%0A%09%7D%0A
-%09
 %0A
+
 %09public Comm
@@ -2889,36 +2889,132 @@
 sage )%0A%09%7B%0A%09%09
-String str =
+Pattern p = Pattern.compile( message.value().getFirstChild( %22regex%22 ).strValue() );%0A%09%09Matcher m = p.matcher(
  message.val
@@ -3032,134 +3032,345 @@
 ue()
+ )
 ;%0A%09%09
-String regex = message.value().getFirstChild( %22regex%22 ).strValue();%0A%09%09int ret = ( Pattern.matches( regex, str ) ) ? 1 : 0;
+Value response = Value.create();%0A%09%09if ( m.matches() ) %7B%0A%09%09%09response.setValue( 1 );%0A%09%09%09ValueVector groups = response.getChildren( %22groups%22 );%0A%09%09%09groups.add( Value.create( m.group( 0 ) ) );%0A%09%09%09for( int i = 0; i %3C m.groupCount(); i++ ) %7B%0A%09%09%09%09groups.add( Value.create( m.group( i+1 ) ) );%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09response.setValue( 0 );%0A%09%09%7D
 %0A%09%09r
@@ -3410,27 +3410,16 @@
 /%22, 
-Value.create( ret )
+response
  );%0A

Support for complex string regex matching.