﻿//------------------------------------------------------------------------------
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2003 Adobe Systems Incorporated 
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute 
// this file in accordance with the terms of the Adobe license 
// agreement accompanying it. If you have received this file 
// from a source other than Adobe, then your use, modification, 
// or distribution of it requires the prior written permission 
// of Adobe.
//
//------------------------------------------------------------------------------

function createRTFReport( rtfScanner )
{
	var ms = rtfScanner.rtf;
	
	var listStr = '';
	
	listStr += "<h1>RTF Report</h1>";
	
	if( rtfScanner.source.constructor.name == 'JSXFile' )
		listStr += '<h2>File: ' + rtfScanner.source.absurl + '</h2>';
	
	listStr += "<ul>";
	listStr += writeEntries( ms );
	listStr += "</ul>";

	var doc = app.newDocument();

	if( doc != null )
	{
		doc.mainTextArea.insertHTML( listStr );
	}
}

function writeEntries( entry )
{
	var ret = '';
	
	if( entry.constructor.name == "RTFGroup" )
	{
		ret += "<li> <b><i>Group";
		
		if( entry.type != '' )
			ret += " ("+entry.type+")";
			
		ret += "</i></b>";
	
		if( entry.entries.length > 0 )
		{
			ret += "<ul>";
			
			for( var i=0; i<entry.entries.length; i++ )
			{
				ret += writeEntries( entry.entries[i] );
			}
			
			ret += "</ul>";
		}
	}
	else
	{
		var type = entry.type;
		var text = entry.name;
		var formatStart = '';
		var formatEnd   = '';
		
		switch( type )
		{
			case "character":
			{
				type = "Text ( " + entry.value.length + " chars )";
				text = entry.value;
				formatStart = "<b>\"</b><font color=\"blue\">";
				formatEnd   = "</font><b>\"</b>";
			}
			break;
			
			case "ctrlSymbol":
			{
				formatEnd = " ("+entry.name.charCodeAt(0)+")";
			}
			break;
			
			case "ctrlWord":
			{
				if( !isNaN(entry.value) && entry.value.toString().length > 0 )
					formatEnd = " ["+entry.value+"]";
			}
			break;
			
			
		}
		
		ret += "<li><b> " + type + " :</b> " + formatStart + text + formatEnd;
	}
	
	return ret;
}
