KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > address > XWrapperUtil


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

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 JavaDoc;
39 import java.io.BufferedOutputStream JavaDoc;
40 import java.io.BufferedReader JavaDoc;
41 import java.io.FileNotFoundException JavaDoc;
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.InputStream JavaDoc;
45 import java.io.InputStreamReader JavaDoc;
46
47 /**
48  * Converts a CSV address list into an file of XML encoded objects
49  * (XAddressDataWrapper) using the JavaBeans utilities.
50  *
51  * @see XAddressDataWrapper, MatchAddressDB
52  */

53 public class XWrapperUtil {
54
55     private static final String JavaDoc CSV_ADDRESS_DB = "address.db";
56     private static final String JavaDoc XML_GZ_ADDRESS_DB = "address.xml";
57
58     //logger
59
private static final Log log = LogFactory.getLog(XWrapperUtil.class);
60
61     public static void main(String JavaDoc[] args) {
62
63         if (log.isDebugEnabled()) {
64             log.debug("Converting database...");
65         }
66
67         //load the CSV file
68
InputStream JavaDoc is =
69                 MatchAddressDB.class.getResourceAsStream(CSV_ADDRESS_DB);
70         BufferedReader JavaDoc buff = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
71
72         XMLEncoder JavaDoc xEncode = null;
73         XAddressDataWrapper xData;
74
75         //open the XML encoder and attempt to write the xml file
76
try {
77             xEncode = new XMLEncoder JavaDoc(new BufferedOutputStream JavaDoc(
78                     new FileOutputStream JavaDoc(XML_GZ_ADDRESS_DB)));
79         } catch (FileNotFoundException JavaDoc ex) {
80             log.error("Database could not be written.", ex);
81         }
82
83         //first line
84
char[] line = getNextLine(buff);
85
86         while (line != null) {
87
88             //get three strings within quotes
89
String JavaDoc addressValues[] = new String JavaDoc[3];
90             int stringValueStart = 0, stringValueEnd;
91
92             for (int i = 0; i < 3; i++) {
93                 //opening quote
94
while (line[stringValueStart++] != '\"') {
95                 }
96                 stringValueEnd = stringValueStart + 1;
97                 //closing quote
98
while (line[stringValueEnd] != '\"') {
99                     stringValueEnd++;
100                 }
101                 //value
102
addressValues[i] = new String JavaDoc(line, stringValueStart,
103                                               stringValueEnd -
104                                               stringValueStart);
105                 stringValueStart = stringValueEnd + 1;
106             }
107
108             //assign the data to the wrapper
109
xData = new XAddressDataWrapper();
110             xData.setCity(addressValues[1]);
111             xData.setState(addressValues[2]);
112             xData.setZip(addressValues[0]);
113
114             //read the next line (entry) in the CSV file
115
line = getNextLine(buff);
116         }
117         //close the XML Encoder
118
try {
119             xEncode.close();
120         }
121         catch (NullPointerException JavaDoc 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     /**
132      * Reads the next line in the CSV file.
133      *
134      * @param buff the CSV file
135      * @return the next line
136      */

137     private static char[] getNextLine(BufferedReader JavaDoc buff) {
138         String JavaDoc inputLine = null;
139
140         //see if the next line exists
141
try {
142             inputLine = buff.readLine();
143         } catch (IOException JavaDoc e) {
144             System.err.println("MatchAddressDB.getNextLine(): " +
145                                "error reading address database \n " + e);
146         }
147         //if the next line exists, return it as a char[]
148
if (inputLine == null) {
149             return null;
150         } else {
151             return inputLine.toCharArray();
152         }
153     }
154 }
155
Popular Tags