1 33 package com.icesoft.applications.faces.address; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import java.beans.XMLEncoder ; 39 import java.io.BufferedOutputStream ; 40 import java.io.BufferedReader ; 41 import java.io.FileNotFoundException ; 42 import java.io.FileOutputStream ; 43 import java.io.IOException ; 44 import java.io.InputStream ; 45 import java.io.InputStreamReader ; 46 47 53 public class XWrapperUtil { 54 55 private static final String CSV_ADDRESS_DB = "address.db"; 56 private static final String XML_GZ_ADDRESS_DB = "address.xml"; 57 58 private static final Log log = LogFactory.getLog(XWrapperUtil.class); 60 61 public static void main(String [] args) { 62 63 if (log.isDebugEnabled()) { 64 log.debug("Converting database..."); 65 } 66 67 InputStream is = 69 MatchAddressDB.class.getResourceAsStream(CSV_ADDRESS_DB); 70 BufferedReader buff = new BufferedReader (new InputStreamReader (is)); 71 72 XMLEncoder xEncode = null; 73 XAddressDataWrapper xData; 74 75 try { 77 xEncode = new XMLEncoder (new BufferedOutputStream ( 78 new FileOutputStream (XML_GZ_ADDRESS_DB))); 79 } catch (FileNotFoundException ex) { 80 log.error("Database could not be written.", ex); 81 } 82 83 char[] line = getNextLine(buff); 85 86 while (line != null) { 87 88 String addressValues[] = new String [3]; 90 int stringValueStart = 0, stringValueEnd; 91 92 for (int i = 0; i < 3; i++) { 93 while (line[stringValueStart++] != '\"') { 95 } 96 stringValueEnd = stringValueStart + 1; 97 while (line[stringValueEnd] != '\"') { 99 stringValueEnd++; 100 } 101 addressValues[i] = new String (line, stringValueStart, 103 stringValueEnd - 104 stringValueStart); 105 stringValueStart = stringValueEnd + 1; 106 } 107 108 xData = new XAddressDataWrapper(); 110 xData.setCity(addressValues[1]); 111 xData.setState(addressValues[2]); 112 xData.setZip(addressValues[0]); 113 114 line = getNextLine(buff); 116 } 117 try { 119 xEncode.close(); 120 } 121 catch (NullPointerException npe) { 122 log.error("Could not close XML Encoder.", npe); 123 return; 124 } 125 if (log.isDebugEnabled()) { 126 log.debug("Closed XML Encoder."); 127 } 128 } 129 130 131 137 private static char[] getNextLine(BufferedReader buff) { 138 String inputLine = null; 139 140 try { 142 inputLine = buff.readLine(); 143 } catch (IOException e) { 144 System.err.println("MatchAddressDB.getNextLine(): " + 145 "error reading address database \n " + e); 146 } 147 if (inputLine == null) { 149 return null; 150 } else { 151 return inputLine.toCharArray(); 152 } 153 } 154 } 155 | Popular Tags |