KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shoppingcartconvertor > ShoppingCartConvertor


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.shoppingcartconvertor;
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.Convertors;
27 import org.netbeans.api.convertor.dvd.DVD;
28 import org.netbeans.api.convertor.shoppingcart.ShoppingCart;
29 import org.netbeans.spi.convertor.Convertor;
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.Node JavaDoc;
33 import org.w3c.dom.NodeList JavaDoc;
34 import org.w3c.dom.Text JavaDoc;
35 import org.xml.sax.ErrorHandler JavaDoc;
36 import org.xml.sax.InputSource JavaDoc;
37 import org.xml.sax.SAXException JavaDoc;
38
39
40 /**
41  *
42  * @author David Konecny
43  */

44 public class ShoppingCartConvertor implements Convertor {
45
46     private static final String JavaDoc NAMESPACE = "http://www.netbeans.org/ns/shoppingcart";
47     
48     public ShoppingCartConvertor() {
49     }
50
51     public Object JavaDoc read(org.w3c.dom.Element JavaDoc element) {
52         ShoppingCart sc = new ShoppingCart();
53         // assert element == <shoppingcart>
54
NodeList JavaDoc nodes = element.getChildNodes();
55         for (int i = 0; i < nodes.getLength(); i++) {
56             Node JavaDoc node = nodes.item(i);
57             if (node.getNodeType() == Node.ELEMENT_NODE) {
58                 Element JavaDoc e = (Element JavaDoc)node;
59                 if (Convertors.canRead(e)) {
60                     Object JavaDoc o = Convertors.read(e);
61                     if (o instanceof Book) {
62                       sc.addBook((Book)o);
63                     } else if (o instanceof DVD) {
64                       sc.addDVD((DVD)o);
65                     } else {
66                         //ErrorManager.getDefault().log(ErrorManager.WARNING, "Shopping cart contains unknown item: "+o);
67
System.err.println("Shopping cart contains unknown item: "+o);
68                     }
69                 } else {
70                     //ErrorManager.getDefault().log(ErrorManager.WARNING, "Shopping cart contains item which cannot be convertor: "+e);
71
System.err.println("Shopping cart contains item which cannot be convertor: "+e);
72                 }
73             }
74         }
75         return sc;
76     }
77     
78     public org.w3c.dom.Element JavaDoc write(Document JavaDoc doc, Object JavaDoc inst) {
79         ShoppingCart sc = (ShoppingCart)inst;
80         Element JavaDoc element = doc.createElementNS(NAMESPACE, "shoppingcart");
81
82         Iterator JavaDoc it = sc.books.iterator();
83         while (it.hasNext()) {
84             Object JavaDoc o = it.next();
85             Element JavaDoc e = Convertors.write(doc, o);
86             element.appendChild(e);
87         }
88         
89         it = sc.dvds.iterator();
90         while (it.hasNext()) {
91             Object JavaDoc o = it.next();
92             Element JavaDoc e = Convertors.write(doc, o);
93             element.appendChild(e);
94         }
95         
96         return element;
97     }
98     
99 }
100
Popular Tags