1 19 20 package org.dvdconvertor; 21 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import org.netbeans.api.convertor.ConvertorDescriptor; 27 import org.netbeans.spi.convertor.Convertor; 28 import org.netbeans.api.convertor.dvd.DVD; 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.Text ; 34 import org.xml.sax.ErrorHandler ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 38 39 43 public class DVDConvertor implements Convertor { 44 45 private static final String NAMESPACE = "http://www.netbeans.org/ns/dvd"; 46 47 public DVDConvertor() { 48 } 49 50 public Object read(org.w3c.dom.Element element) { 51 int id = 0; 52 String title = null; 53 String publisher = null; 54 int price = 0; 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 (e.getNodeName().equals("id")) { 62 id = Integer.parseInt(getTextValue(e)); 63 } 64 if (e.getNodeName().equals("title")) { 65 title = getTextValue(e); 66 } 67 if (e.getNodeName().equals("publisher")) { 68 publisher = getTextValue(e); 69 } 70 if (e.getNodeName().equals("price")) { 71 price = Integer.parseInt(getTextValue(e)); 72 } 73 } 74 } 75 return new DVD(id, title, publisher, price); 76 } 77 78 public org.w3c.dom.Element write(Document doc, Object inst) { 79 DVD dvd = (DVD)inst; 80 Element element = doc.createElementNS(NAMESPACE, "dvd"); 81 82 Element e = doc.createElementNS(NAMESPACE, "id"); 83 Text t = doc.createTextNode(Integer.toString(dvd.ID)); 84 e.appendChild(t); 85 element.appendChild(e); 86 87 e = doc.createElementNS(NAMESPACE, "title"); 88 t = doc.createTextNode(dvd.title); 89 e.appendChild(t); 90 element.appendChild(e); 91 92 e = doc.createElementNS(NAMESPACE, "publisher"); 93 t = doc.createTextNode(dvd.publisher); 94 e.appendChild(t); 95 element.appendChild(e); 96 97 e = doc.createElementNS(NAMESPACE, "price"); 98 t = doc.createTextNode(Integer.toString(dvd.price)); 99 e.appendChild(t); 100 element.appendChild(e); 101 102 return element; 103 } 104 105 private String getTextValue(Element element) { 107 NodeList nodes = element.getChildNodes(); 108 for (int i = 0; i < nodes.getLength(); i++) { 109 Node node = nodes.item(i); 110 if (node.getNodeType() == Node.TEXT_NODE) { 111 return ((Text )node).getData(); 112 } 113 } 114 return null; 115 } 116 117 } 118 | Popular Tags |