001 package com.croftsoft.apps.tab2xml; 002 003 import java.io.*; 004 import java.util.*; 005 006 /********************************************************************* 007 * Converts a tab-delimited file to XML. 008 * 009 * @version 010 * 2002-11-13 011 * @since 012 * 2002-11-13 013 * @author 014 * <a href="http://alumni.caltech.edu/~croft">David Wallace Croft</a> 015 *********************************************************************/ 016 017 public final class Tab2Xml 018 ////////////////////////////////////////////////////////////////////// 019 ////////////////////////////////////////////////////////////////////// 020 { 021 022 public static void main ( String [ ] args ) 023 throws Exception 024 ////////////////////////////////////////////////////////////////////// 025 { 026 FileReader fileReader = new FileReader ( args [ 0 ] ); 027 028 BufferedReader bufferedReader = new BufferedReader ( fileReader ); 029 030 List tokenList = new ArrayList ( ); 031 032 String line = bufferedReader.readLine ( ); 033 034 StringTokenizer stringTokenizer 035 = new StringTokenizer ( line, "\t" ); 036 037 while ( stringTokenizer.hasMoreTokens ( ) ) 038 { 039 tokenList.add ( stringTokenizer.nextToken ( ).toLowerCase ( ) ); 040 } 041 042 String [ ] columnHeaders 043 = ( String [ ] ) tokenList.toArray ( new String [ 0 ] ); 044 045 int columnCount = columnHeaders.length; 046 047 PrintWriter printWriter = new PrintWriter ( 048 new BufferedWriter ( new FileWriter ( args [ 1 ] ) ) ); 049 050 printWriter.println ( "<data>" ); 051 052 while ( ( line = bufferedReader.readLine ( ) ) != null ) 053 { 054 int columnIndex = 0; 055 056 String [ ] values = new String [ columnCount ]; 057 058 stringTokenizer = new StringTokenizer ( line, "\t", true ); 059 060 while ( stringTokenizer.hasMoreTokens ( ) ) 061 { 062 String token = stringTokenizer.nextToken ( ); 063 064 if ( "\t".equals ( token ) ) 065 { 066 ++columnIndex; 067 } 068 else 069 { 070 values [ columnIndex ] = token; 071 } 072 } 073 074 printWriter.println ( " <record>" ); 075 076 for ( int i = 0; i < columnCount; i++ ) 077 { 078 if ( values [ i ] != null ) 079 { 080 printWriter.println ( 081 " <" + columnHeaders [ i ] + ">" 082 + values [ i ] 083 + "</" + columnHeaders [ i ] + ">" ); 084 } 085 } 086 087 printWriter.println ( " </record>" ); 088 } 089 090 printWriter.println ( "</data>" ); 091 092 printWriter.flush ( ); 093 094 printWriter.close ( ); 095 } 096 097 ////////////////////////////////////////////////////////////////////// 098 ////////////////////////////////////////////////////////////////////// 099 }