1 19 20 package org.cdmodule; 21 22 import org.netbeans.spi.convertor.Convertor; 23 import org.w3c.dom.*; 24 25 26 30 public class CDConvertor implements Convertor { 31 32 private static final String NAMESPACE = "http://www.netbeans.org/ns/cd2"; 33 34 public CDConvertor() { 35 } 36 37 public Object read(org.w3c.dom.Element element) { 38 String artist = null; 39 String album = null; 40 NodeList nodes = element.getChildNodes(); 41 for (int i = 0; i < nodes.getLength(); i++) { 42 Node node = nodes.item(i); 43 if (node.getNodeType() == Node.ELEMENT_NODE) { 44 Element e = (Element)node; 45 if (e.getNodeName().equals("artist")) { 46 artist = getTextValue(e); 47 } 48 if (e.getNodeName().equals("album")) { 49 album = getTextValue(e); 50 } 51 } 52 } 53 return new CD(artist, album); 54 } 55 56 public org.w3c.dom.Element write(Document doc, Object inst) { 57 CD cd = (CD)inst; 58 Element element = doc.createElementNS(NAMESPACE, "cd"); 59 60 Element e = doc.createElementNS(NAMESPACE, "artist"); 61 Text t = doc.createTextNode(cd.artist); 62 e.appendChild(t); 63 element.appendChild(e); 64 65 e = doc.createElementNS(NAMESPACE, "album"); 66 t = doc.createTextNode(cd.album); 67 e.appendChild(t); 68 element.appendChild(e); 69 70 return element; 71 } 72 73 private String getTextValue(Element element) { 75 NodeList nodes = element.getChildNodes(); 76 for (int i = 0; i < nodes.getLength(); i++) { 77 Node node = nodes.item(i); 78 if (node.getNodeType() == Node.TEXT_NODE) { 79 return ((Text)node).getData(); 80 } 81 } 82 return null; 83 } 84 85 } 86 | Popular Tags |