001         package com.croftsoft.apps.tab2xml;
002    
003         import java.io.*;
004         import java.util.*;
005    
006         /*********************************************************************
007         * Converts a comma separated value (CSV) file to XML.
008         *
009         * @version
010         *   2003-05-12
011         * @since
012         *   2003-05-12
013         * @author
014         *   <a href="http://www.CroftSoft.com/">David Wallace Croft</a>
015         *********************************************************************/
016    
017         public final class  Csv2Xml
018         //////////////////////////////////////////////////////////////////////
019         //////////////////////////////////////////////////////////////////////
020         {
021    
022         private static final String [ ]  EMPTY_STRING_ARRAY
023           = new String [ 0 ];
024    
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027    
028         public static void  main ( String [ ]  args )
029           throws Exception
030         //////////////////////////////////////////////////////////////////////
031         {
032           convert ( args [ 0 ], args [ 1 ] );
033         }
034    
035         public static void  convert (
036           String  inputCsvFilename,
037           String  outputXmlFilename )
038           throws Exception
039         //////////////////////////////////////////////////////////////////////
040         {
041           FileReader  fileReader = new FileReader ( inputCsvFilename );
042    
043           BufferedReader  bufferedReader = new BufferedReader ( fileReader );
044    
045           String  line = bufferedReader.readLine ( );
046    
047           String [ ]  columnHeaders = parseLine ( line );
048    
049           int  columnCount = columnHeaders.length;
050    
051           PrintWriter  printWriter = new PrintWriter (
052             new BufferedWriter ( new FileWriter ( outputXmlFilename ) ) );
053    
054           printWriter.println ( "<data>" );
055    
056           while ( ( line = bufferedReader.readLine ( ) ) != null )
057           {
058             int  columnIndex = 0;
059    
060             String [ ]  values = parseLine ( line );
061    
062             printWriter.println ( "  <record>" );
063    
064             for ( int  i = 0; i < columnCount; i++ )
065             {
066               if ( values [ i ] != null )
067               {
068                 printWriter.println (
069                   "    <" + columnHeaders [ i ] + ">"
070                   + values [ i ]
071                   + "</" + columnHeaders [ i ] + ">" );
072               }
073             }
074    
075             printWriter.println ( "  </record>" );
076           }
077    
078           printWriter.println ( "</data>" );
079    
080           printWriter.flush ( );
081    
082           printWriter.close ( );
083         }
084    
085         public static String [ ]  parseLine ( String  line )
086         //////////////////////////////////////////////////////////////////////
087         {
088           List  stringList = new ArrayList ( );
089    
090           StringTokenizer  stringTokenizer
091             = new StringTokenizer ( line, ",\"", true );
092    
093           boolean  inQuotes = false;
094    
095           boolean  stringStarted = false;
096    
097           StringBuffer  stringBuffer = new StringBuffer ( );
098    
099           while ( stringTokenizer.hasMoreTokens ( ) )
100           {
101             String  token = stringTokenizer.nextToken ( );
102    
103             if ( token.equals ( "\"" ) )
104             {
105               if ( inQuotes )
106               {
107                 inQuotes = false;
108    
109                 stringList.add ( stringBuffer.toString ( ) );
110    
111                 stringBuffer = new StringBuffer ( );
112    
113                 stringStarted = false;
114               }
115               else
116               {
117                 inQuotes = true;
118    
119                 stringStarted = true;
120    
121                 stringBuffer = new StringBuffer ( );
122               }
123             }
124             else if ( token.equals ( "," ) )
125             {
126               if ( inQuotes )
127               {
128                 stringBuffer.append ( "," );
129               }
130               else
131               {
132                 if ( stringStarted )
133                 {
134                   stringList.add ( stringBuffer.toString ( ) );
135    
136                   stringBuffer = new StringBuffer ( );
137                 }
138    
139                 stringStarted = true;
140               }
141             }
142             else
143             {
144               if ( !stringStarted )
145               {
146                 stringStarted = true;
147    
148                 stringBuffer = new StringBuffer ( );
149               }
150    
151               stringBuffer.append ( token );
152             }
153           }
154    
155           if ( stringStarted )
156           {
157             stringList.add ( stringBuffer.toString ( ) );
158           }
159    
160           return ( String [ ] ) stringList.toArray ( EMPTY_STRING_ARRAY );
161         }
162    
163         //////////////////////////////////////////////////////////////////////
164         //////////////////////////////////////////////////////////////////////
165    
166         private Csv2Xml ( ) { }
167    
168         //////////////////////////////////////////////////////////////////////
169         //////////////////////////////////////////////////////////////////////
170         }