KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > storeconvertor > StoreConvertor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.storeconvertor;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import org.netbeans.api.convertor.book.Book;
25 import org.netbeans.api.convertor.ConvertorDescriptor;
26 import org.netbeans.api.convertor.ConvertorException;
27 import org.netbeans.api.convertor.Convertors;
28 import org.netbeans.api.convertor.dvd.DVD;
29 import org.netbeans.api.convertor.store.Store;
30 import org.netbeans.spi.convertor.Convertor;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35 import org.w3c.dom.Text JavaDoc;
36 import org.xml.sax.ErrorHandler JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39
40
41 /**
42  *
43  * @author David Konecny
44  */

45 public class StoreConvertor implements Convertor {
46
47     private static final String JavaDoc NAMESPACE = "http://www.netbeans.org/ns/store";
48     
49     public StoreConvertor() {
50     }
51
52     public Object JavaDoc read(org.w3c.dom.Element JavaDoc element) {
53         if (element.getNodeName().equals("store")) {
54             Store st = new Store();
55             // assert element == <store>
56
NodeList JavaDoc nodes = element.getChildNodes();
57             for (int i = 0; i < nodes.getLength(); i++) {
58                 Node JavaDoc node = nodes.item(i);
59                 if (node.getNodeType() == Node.ELEMENT_NODE) {
60                     Element JavaDoc e = (Element JavaDoc)node;
61                     if (Convertors.canRead(e)) {
62                         Object JavaDoc o = Convertors.read(e);
63                         if (o instanceof Book) {
64                           st.addBook((Book)o);
65                         } else if (o instanceof DVD) {
66                           st.addDVD((DVD)o);
67                         } else {
68                             //ErrorManager.getDefault().log(ErrorManager.WARNING, "Shopping cart contains unknown item: "+o);
69
System.err.println("Shopping cart contains unknown item: "+o);
70                         }
71                     } else {
72                         //ErrorManager.getDefault().log(ErrorManager.WARNING, "Shopping cart contains item which cannot be convertor: "+e);
73
System.err.println("Shopping cart contains item which cannot be convertor: "+e);
74                     }
75                 }
76             }
77             return st;
78         }
79         if (element.getNodeName().equals("dvd")) {
80             int id = 0;
81             String JavaDoc title = null;
82             String JavaDoc publisher = null;
83             int price = 0;
84             // assert element == <dvd>
85
NodeList JavaDoc nodes = element.getChildNodes();
86             for (int i = 0; i < nodes.getLength(); i++) {
87                 Node JavaDoc node = nodes.item(i);
88                 if (node.getNodeType() == Node.ELEMENT_NODE) {
89                     Element JavaDoc e = (Element JavaDoc)node;
90                     if (e.getNodeName().equals("id")) {
91                         id = Integer.parseInt(getTextValue(e));
92                     }
93                     if (e.getNodeName().equals("title")) {
94                         title = getTextValue(e);
95                     }
96                     if (e.getNodeName().equals("publisher")) {
97                         publisher = getTextValue(e);
98                     }
99                     if (e.getNodeName().equals("price")) {
100                         price = Integer.parseInt(getTextValue(e));
101                     }
102                 }
103             }
104             return new DVD(id, title, publisher, price);
105         }
106         if (element.getNodeName().equals("book")) {
107             int id = 0;
108             String JavaDoc author = null;
109             String JavaDoc title = null;
110             String JavaDoc publisher = null;
111             int price = 0;
112             // assert element == <book>
113
NodeList JavaDoc nodes = element.getChildNodes();
114             for (int i = 0; i < nodes.getLength(); i++) {
115                 Node JavaDoc node = nodes.item(i);
116                 if (node.getNodeType() == Node.ELEMENT_NODE) {
117                     Element JavaDoc e = (Element JavaDoc)node;
118                     if (e.getNodeName().equals("id")) {
119                         id = Integer.parseInt(getTextValue(e));
120                     }
121                     if (e.getNodeName().equals("title")) {
122                         title = getTextValue(e);
123                     }
124                     if (e.getNodeName().equals("publisher")) {
125                         publisher = getTextValue(e);
126                     }
127                     if (e.getNodeName().equals("author")) {
128                         author = getTextValue(e);
129                     }
130                     if (e.getNodeName().equals("price")) {
131                         price = Integer.parseInt(getTextValue(e));
132                     }
133                 }
134             }
135             return new Book(id, title, author, publisher, price);
136         }
137         throw new ConvertorException("This should never happen");
138     }
139     
140     public org.w3c.dom.Element JavaDoc write(Document JavaDoc doc, Object JavaDoc inst) {
141         if (inst instanceof Store) {
142             Store st = (Store)inst;
143             Element JavaDoc element = doc.createElementNS(NAMESPACE, "store");
144
145             Iterator JavaDoc it = st.books.iterator();
146             while (it.hasNext()) {
147                 Object JavaDoc o = it.next();
148                 Element JavaDoc e = Convertors.write(doc, o);
149                 element.appendChild(e);
150             }
151
152             it = st.dvds.iterator();
153             while (it.hasNext()) {
154                 Object JavaDoc o = it.next();
155                 Element JavaDoc e = Convertors.write(doc, o);
156                 element.appendChild(e);
157             }
158
159             return element;
160         }
161         if (inst instanceof DVD) {
162             DVD dvd = (DVD)inst;
163             Element JavaDoc element = doc.createElementNS(NAMESPACE, "dvd");
164
165             Element JavaDoc e = doc.createElementNS(NAMESPACE, "id");
166             Text JavaDoc t = doc.createTextNode(Integer.toString(dvd.ID));
167             e.appendChild(t);
168             element.appendChild(e);
169
170             e = doc.createElementNS(NAMESPACE, "title");
171             t = doc.createTextNode(dvd.title);
172             e.appendChild(t);
173             element.appendChild(e);
174
175             e = doc.createElementNS(NAMESPACE, "publisher");
176             t = doc.createTextNode(dvd.publisher);
177             e.appendChild(t);
178             element.appendChild(e);
179
180             e = doc.createElementNS(NAMESPACE, "price");
181             t = doc.createTextNode(Integer.toString(dvd.price));
182             e.appendChild(t);
183             element.appendChild(e);
184
185             return element;
186         }
187         if (inst instanceof Book) {
188             Book book = (Book)inst;
189             Element JavaDoc element = doc.createElementNS(NAMESPACE, "book");
190
191             Element JavaDoc e = doc.createElementNS(NAMESPACE, "id");
192             Text JavaDoc t = doc.createTextNode(Integer.toString(book.ID));
193             e.appendChild(t);
194             element.appendChild(e);
195
196             e = doc.createElementNS(NAMESPACE, "author");
197             t = doc.createTextNode(book.author);
198             e.appendChild(t);
199             element.appendChild(e);
200
201             e = doc.createElementNS(NAMESPACE, "title");
202             t = doc.createTextNode(book.title);
203             e.appendChild(t);
204             element.appendChild(e);
205
206             e = doc.createElementNS(NAMESPACE, "publisher");
207             t = doc.createTextNode(book.publisher);
208             e.appendChild(t);
209             element.appendChild(e);
210
211             e = doc.createElementNS(NAMESPACE, "price");
212             t = doc.createTextNode(Integer.toString(book.price));
213             e.appendChild(t);
214             element.appendChild(e);
215
216             return element;
217         }
218         throw new ConvertorException("This should never happen");
219     }
220     
221     // THIS IMPL IS NOT ROBUST. DO NOT REUSE IT!
222
private String JavaDoc getTextValue(Element JavaDoc element) {
223         NodeList JavaDoc nodes = element.getChildNodes();
224         for (int i = 0; i < nodes.getLength(); i++) {
225             Node JavaDoc node = nodes.item(i);
226             if (node.getNodeType() == Node.TEXT_NODE) {
227                 return ((Text JavaDoc)node).getData();
228             }
229         }
230         return null;
231     }
232     
233 }
234
Popular Tags