/*	convert_string.java -
		Sample java program that uses the XmlConvert class to 
		convert flat file data into XML. The flat file data, 
		XFlat schema and XML output are stored in strings rather
		than files.

    Copyright (C) 1999 - 2000 Unidex Inc. All rights reserved.
*/
import java.io.StringReader ;
import java.io.StringWriter ;
import com.unidex.xflat.XmlConvert ;
import com.unidex.xflat.XflatException ;

public class convert_string {
	private final static int NUM_ARGS = 0 ;
	private final static String usage_info = "Usage: convert_string" ;

	public static void main( String args[] ) throws Exception {
		// Validate the number of command line arguments.
		if ( args.length != NUM_ARGS ) {
			System.err.println( usage_info ) ;
			System.exit( 1 ) ; // Error.
		}

		final boolean suppress_startup_message = true ;

		String xflat_schema =
			"<XFlat Name='employees_schema' Description='CSV flat file'>" +
			"    <SequenceDef Name='employees' Description='employees flat file'>" +
			"        <RecordDef Name='employee' FieldSep=','" +
                        "                   RecSep='\\r\\n' MaxOccur='0'>" +
			"            <FieldDef Name='ssn' NullAllowed='No'" +
			"                      MinFieldLength='9' MaxFieldLength='9'" +
			"                      DataType='Integer' MinValue='0'" +
			"                      QuotedValue='Yes'/>" +
			"            <FieldDef Name='name' NullAllowed='No'" +
			"                      QuotedValue='Yes'/>" +
			"            <FieldDef Name='salary' NullAllowed='No'" +
			"                      DataType='Float' MinValue='0'" +
			"                      QuotedValue='Yes'/>" +
			"        </RecordDef>" +
			"    </SequenceDef>" +
			"</XFlat>" ;

		String flat_file_data =
			"123456789,\"Carr, Lisa\",100000.00\r\n" +
			"444556666,\"Barr, Clark\",87000.00\r\n" +
			"777227878,\"Parr, Jack\",123000.00\r\n" ;

		try {
			// Create an XmlConvert instance.
			XmlConvert xml_convert =
				new XmlConvert( new StringReader( xflat_schema ),
								suppress_startup_message ) ;

			// Use the flatToXml method of the XmlConvert object
			// to convert the flat file data into XML.
			StringReader flat_file_reader = new StringReader( flat_file_data ) ;
			StringWriter xml_writer = new java.io.StringWriter( ) ;
			xml_convert.flatToXml( flat_file_reader, xml_writer ) ;

			String xml_output =  xml_writer.toString() ;
			System.out.println( xml_output ) ;
		} catch( Exception e ) {
			if ( e instanceof XflatException ) {
				System.err.println( e.getMessage() ) ;
			} else {
				System.err.println( e.toString() ) ;
			}
			System.exit( 1 ) ; // Error.
		}
		System.exit( 0 ) ; // Success.
	}
}
