/Users/lyon/j4p/src/xml/adbk/AddressBook.java
|
1 package xml.adbk;
2
3 import futils.CsvParser;
4 import futils.ReaderUtil;
5 import futils.SerialUtils;
6 import futils.WriterUtil;
7 import gui.html.HtmlSynthesizer;
8
9 import java.io.*;
10 import java.util.Collections;
11 import java.util.Vector;
12
13 /**
14 * xml.AddressBook is responsible for holding
15 * Address instances and converting them to XML or
16 * CSV format.
17 */
18 //todo: make sortable.
19
20 public class AddressBook
21 implements Serializable {
22 private int recordNumber = 0;
23 public Vector v = new Vector();
24 // v is a vector of address instances.
25 public String dtd =
26 "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n" +
27 "<!DOCTYPE AddressBook [\n" +
28 " <!ELEMENT Address (\n" +
29 " (title | firstName| lastName | userId | password)*,\n" +
30 " (streetAddress|address1 | address2| address3 )*,\n" +
31 " (homePage | emailAddress)*,\n" +
32 " (homePhone | businessPhone | faxPhone)*,\n" +
33 " city,\n" +
34 " state,\n" +
35 " zip?)>\n" +
36 " <!ELEMENT title (#PCDATA)>\n" +
37 " <!ELEMENT firstName (#PCDATA)>\n" +
38 " <!ELEMENT lastName (#PCDATA)>\n" +
39 " <!ELEMENT address1 (#PCDATA)>\n" +
40 " <!ELEMENT address2 (#PCDATA)>\n" +
41 " <!ELEMENT address3 (#PCDATA)>\n" +
42 " <!ELEMENT streetAddress (#PCDATA)>\n" +
43 " <!ELEMENT emailAddress (#PCDATA)>\n" +
44 " <!ELEMENT homePage (#PCDATA)>\n" +
45 " <!ELEMENT city (#PCDATA)>\n" +
46 " <!ELEMENT state (#PCDATA)>\n" +
47 " <!ELEMENT zip (#PCDATA)>\n" +
48 " <!ELEMENT homePhone (#PCDATA)>\n" +
49 " <!ELEMENT businessPhone (#PCDATA)>\n" +
50 " <!ELEMENT faxPhone (#PCDATA)>\n" +
51 " <!ELEMENT AddressBook (Address*)>\n" +
52 "]>\n";
53
54
55 public void addAddress(Address a) {
56 v.addElement(a);
57 }
58
59 public void sort() {
60 System.out.println("sorting");
61 //***** Un comment on JDK 1.2 or better to sort
62 Collections.sort(v);
63 }
64
65 public void deleteRecord(Address abr) {
66 v.removeElement(abr);
67 }
68
69 public String getAddressXml() {
70 StringBuffer sb = new StringBuffer("\n");
71 for (int i = 0; i < v.size(); i++)
72 sb.append(((Address) v.elementAt(i)).toXml());
73 return sb.toString();
74 }
75
76 public String toXml() {
77 return dtd
78 + "\n<AddressBook>"
79 + getAddressXml()
80 + "\n</AddressBook>";
81 }
82
83 public String toHtmlTable() {
84 HtmlSynthesizer hs =
85 new HtmlSynthesizer();
86 String aa[] = new String[v.size()];
87 for (int i = 0; i < v.size(); i++) {
88 aa[i] =
89 ((Address) v.elementAt(i)).toHtml();
90 }
91 return hs.getTable(hs.getSheet(aa));
92 }
93
94 public String toHtmlCsv() {
95 StringBuffer sb = new StringBuffer();
96 for (int i = 0; i < v.size(); i++) {
97 sb.append(
98 ((Address) v.elementAt(i)).toCsv() +
99 "<p>");
100 }
101 return sb.toString();
102 }
103
104 public String toCsv() {
105 String s = "\n";
106 for (int i = 0; i < v.size(); i++)
107 s =
108 s +
109 ((Address) v.elementAt(i)).toCsv();
110 return s;
111 }
112
113 public String toString() {
114 return toXml();
115 }
116
117 public void processLine(String l) {
118 String[] tokens = new CsvParser(l).getTokens();
119 v.addElement(new Address(tokens));
120 }
121
122 /**
123 * getAddress from another addressBook and add
124 * them to your vector!
125 */
126 public void merge(AddressBook ab) {
127 Address aa[] = ab.getAddresses();
128 for (int i = 0; i < aa.length; i++)
129 addAddress(aa[i]);
130 }
131
132 public Address[] getAddresses() {
133 Address aa[] = new Address[v.size()];
134 v.copyInto(aa);
135 return aa;
136 }
137
138 public void getLines(BufferedReader br) {
139 try {
140 for (String l = br.readLine(); l !=
141 null; l =
142 br.readLine())
143 processLine(l);
144 } catch (IOException e) {
145 e.printStackTrace();
146 }
147 }
148
149 public void open(BufferedReader br)
150 throws IOException {
151 getLines(br);
152 br.close();
153 }
154
155 public void open() {
156 try {
157 open(
158 ReaderUtil.getBufferedReader(
159 "select *.csv"));
160 } catch (IOException e) {
161 }
162 }
163
164 public void openXmlGz() {
165 try {
166 String s = SerialUtils.readGzipString();
167 System.out.println("Read in:" + s);
168 } catch (IOException e) {
169 e.printStackTrace();
170 }
171 }
172
173 public void saveXmlGz() {
174 try {
175 SerialUtils.writeGzipString(toXml());
176 } catch (IOException e) {
177 e.printStackTrace();
178 }
179 }
180
181 public void save() {
182 BufferedWriter bw =
183 WriterUtil.getBufferedWriter(
184 "output xml");
185 WriterUtil.println(bw, toXml());
186 WriterUtil.close(bw);
187 }
188
189 public void saveAsCsv() {
190 BufferedWriter bw =
191 WriterUtil.getBufferedWriter(
192 "output xml");
193 WriterUtil.println(bw, toCsv());
194 WriterUtil.close(bw);
195 }
196
197 public void saveAsXml() {
198 BufferedWriter bw =
199 WriterUtil.getBufferedWriter(
200 "output xml");
201 WriterUtil.println(bw, toXml());
202 WriterUtil.close(bw);
203 }
204
205 public void saveAsXml(File f) {
206 BufferedWriter bw =
207 WriterUtil.getBufferedWriter(f);
208 WriterUtil.println(bw, toXml());
209 WriterUtil.close(bw);
210 }
211
212 public AddressBook search(
213 String searchString) {
214 Address a;
215 AddressBook ab = new AddressBook();
216 for (int i = 0; i < v.size(); i++) {
217 a = (Address) v.elementAt(i);
218 if (a.match(searchString))
219 ab.addAddress(a);
220 }
221 return ab;
222 }
223
224 public int getNumRecords() {
225 return (v.size());
226 }
227
228 public void deleteCurrentRecord() {
229 v.removeElementAt(recordNumber);
230 }
231
232 public Address getPreviousAddress() {
233 decrementRecord();
234 return (Address) v.elementAt(
235 recordNumber);
236 }
237
238 public Address getNextAddress() {
239 incrementRecord();
240 return (Address) v.elementAt(
241 recordNumber);
242 }
243
244 public void incrementRecord() {
245 if (recordNumber < (v.size() - 1))
246 recordNumber++;
247 }
248
249 public void decrementRecord() {
250 if (recordNumber > 0)
251 recordNumber--;
252 }
253 public void printLastNames() {
254 for (int i=0 ; i < v.size(); i++){
255 Address a = (Address) v.elementAt(i);
256 System.out.println("lastName="+a.getLastName());
257 }
258 }
259
260 public void setRecordNumber(int index) {
261 if ((index >= 0) && (index <= v.size()))
262 recordNumber = index;
263 System.out.println(recordNumber);
264 }
265
266 public static void main(String args[]) {
267 Address a = new Address();
268 a.setLastName("adler");
269 a.setFirstName("Joe");
270 a.setCompany("Vectron");
271 a.setHomePage("http://www.vectron.com");
272 AddressBook ab = new AddressBook();
273 ab.addAddress(a);
274 ab.open();
275 ab.printLastNames();
276
277 //System.out.println(ab.toXml());
278 ab.sort();
279 ab.printLastNames();
280 //System.out.println("ab=" + ab.toXml());
281 //ab.saveXmlGz();
282 //ab.openXmlGz();
283 }
284
285 }
286