1 19 20 package org.storeconvertor; 21 22 import java.io.IOException ; 23 import java.util.Iterator ; 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 ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 import org.w3c.dom.Text ; 36 import org.xml.sax.ErrorHandler ; 37 import org.xml.sax.InputSource ; 38 import org.xml.sax.SAXException ; 39 40 41 45 public class StoreConvertor implements Convertor { 46 47 private static final String NAMESPACE = "http://www.netbeans.org/ns/store"; 48 49 public StoreConvertor() { 50 } 51 52 public Object read(org.w3c.dom.Element element) { 53 if (element.getNodeName().equals("store")) { 54 Store st = new Store(); 55 NodeList nodes = element.getChildNodes(); 57 for (int i = 0; i < nodes.getLength(); i++) { 58 Node node = nodes.item(i); 59 if (node.getNodeType() == Node.ELEMENT_NODE) { 60 Element e = (Element )node; 61 if (Convertors.canRead(e)) { 62 Object 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 System.err.println("Shopping cart contains unknown item: "+o); 70 } 71 } else { 72 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 title = null; 82 String publisher = null; 83 int price = 0; 84 NodeList nodes = element.getChildNodes(); 86 for (int i = 0; i < nodes.getLength(); i++) { 87 Node node = nodes.item(i); 88 if (node.getNodeType() == Node.ELEMENT_NODE) { 89 Element e = (Element )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 author = null; 109 String title = null; 110 String publisher = null; 111 int price = 0; 112 NodeList nodes = element.getChildNodes(); 114 for (int i = 0; i < nodes.getLength(); i++) { 115 Node node = nodes.item(i); 116 if (node.getNodeType() == Node.ELEMENT_NODE) { 117 Element e = (Element )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 write(Document doc, Object inst) { 141 if (inst instanceof Store) { 142 Store st = (Store)inst; 143 Element element = doc.createElementNS(NAMESPACE, "store"); 144 145 Iterator it = st.books.iterator(); 146 while (it.hasNext()) { 147 Object o = it.next(); 148 Element e = Convertors.write(doc, o); 149 element.appendChild(e); 150 } 151 152 it = st.dvds.iterator(); 153 while (it.hasNext()) { 154 Object o = it.next(); 155 Element 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 element = doc.createElementNS(NAMESPACE, "dvd"); 164 165 Element e = doc.createElementNS(NAMESPACE, "id"); 166 Text 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 element = doc.createElementNS(NAMESPACE, "book"); 190 191 Element e = doc.createElementNS(NAMESPACE, "id"); 192 Text 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 private String getTextValue(Element element) { 223 NodeList nodes = element.getChildNodes(); 224 for (int i = 0; i < nodes.getLength(); i++) { 225 Node node = nodes.item(i); 226 if (node.getNodeType() == Node.TEXT_NODE) { 227 return ((Text )node).getData(); 228 } 229 } 230 return null; 231 } 232 233 } 234 | Popular Tags |