/***************************************************************************
 *   Copyright (C) 2009 by Fabrizio Montesi <famontesi@gmail.com>          *
 *                                                                         *
 *   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 jolie.runtime;

import java.io.IOException;
import java.io.Writer;
import java.util.Map.Entry;

/**
 * Pretty printer for {@link Value} objects.
 * This class is not thread safe.
 * @author Fabrizio Montesi
 */
public class ValuePrettyPrinter
{
	private final Value root;
	private final Writer writer;
	private final String header;
	private int indentation = 0;

	public ValuePrettyPrinter( Value root, Writer writer, String header )
	{
		this.root = root;
		this.writer = writer;
		this.header = header;
	}

	private void indent()
	{
		indentation++;
	}

	private void unindent()
	{
		indentation--;
	}

	public void run()
		throws IOException
	{
		writeIndented( header );
		writeNativeValue( root );
		indent();
		writeChildren( root );
		unindent();
	}

	private void writeNativeValue( Value value )
		throws IOException
	{
		if ( value.isUsedInCorrelation() ) {
			writer.write( " (cset)" );
		}
		if ( value.valueObject() != null ) {
			writer.write( " = " );
			writer.write( value.valueObject().toString() );
			writer.write( " : " );
			writer.write( value.valueObject().getClass().getName() );
		}
		writer.write( '\n' );
	}

	private void writeChildren( Value value )
		throws IOException
	{
		Integer i;
		for( Entry< String, ValueVector > entry : value.children().entrySet() ) {
			i = 0;
			for( Value child : entry.getValue() ) {
				writeIndented( "." );
				writer.write( entry.getKey() );
				writer.write( '[' );
				writer.write( i.toString() );
				writer.write( ']' );
				writeNativeValue( child );
				indent();
				writeChildren( child );
				unindent();
				i++;
			}
		}
	}

	private void writeIndented( String s )
		throws IOException
	{
		for( int i = 0; i < indentation; i++ ) {
			writer.write( '\t' );
		}
		writer.write( s );
	}
}

@@ -2844,35 +2844,11 @@
 %09%09%09i
- = 0;%0A%09%09%09for( Value child :
+f (
  ent
@@ -2860,16 +2860,26 @@
 tValue()
+.isEmpty()
  ) %7B%0A%09%09%09
@@ -2958,16 +2958,180 @@
 ite(
+ %22 (empty)%22 );%0A%09%09%09%7D else %7B%0A%09%09%09%09i = 0;%0A%09%09%09%09for( Value child : entry.getValue() ) %7B%0A%09%09%09%09%09writeIndented( %22.%22 );%0A%09%09%09%09%09writer.write( entry.getKey() );%0A%09%09%09%09%09writer.write(
  '%5B' );%0A
 %09%09%09%09
@@ -3122,24 +3122,25 @@
 ite( '%5B' );%0A
+%09
 %09%09%09%09writer.w
@@ -3157,24 +3157,25 @@
 String() );%0A
+%09
 %09%09%09%09writer.w
@@ -3187,24 +3187,25 @@
  '%5D' );%0A%09%09%09%09
+%09
 writeNativeV
@@ -3223,16 +3223,17 @@
  );%0A%09%09%09%09
+%09
 indent()
@@ -3230,24 +3230,25 @@
 %09%09indent();%0A
+%09
 %09%09%09%09writeChi
@@ -3267,16 +3267,17 @@
  );%0A%09%09%09%09
+%09
 unindent
@@ -3288,13 +3288,20 @@
 %09%09%09%09
+%09
 i++;%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D

Print empty vectors.