1 19 20 package org.netbeans.modules.convertor; 21 22 import java.util.Properties ; 23 import org.netbeans.api.convertor.ConvertorException; 24 import org.netbeans.spi.convertor.Convertor; 25 import org.w3c.dom.Document ; 26 import org.w3c.dom.Element ; 27 import org.w3c.dom.Node ; 28 import org.w3c.dom.NodeList ; 29 30 34 35 final public class XMLInstanceConvertor implements Convertor { 36 37 public XMLInstanceConvertor() { 38 } 39 40 public Object read(Element element) { 41 assert element.getNodeName().equals("instance") : "Element "+ element+" is not <instance> element"; 44 String clazz = null; 45 String method = null; 46 Properties properties = null; 47 48 NodeList nodes = element.getChildNodes(); 49 for (int i = 0; i < nodes.getLength(); i++) { 50 Node node = nodes.item(i); 51 if (node.getNodeType() == Node.ELEMENT_NODE) { 52 Element e = (Element )node; 53 if (e.getNodeName().equals("class")) { clazz = PropertiesConvertor.getTextValue(e); 55 } 56 if (e.getNodeName().equals("method")) { method = PropertiesConvertor.getTextValue(e); 58 } 59 if (e.getNodeName().equals("property")) { if (properties == null) { 61 properties = new Properties (); 62 } 63 String propName = e.getAttribute("name"); String propVal = PropertiesConvertor.getTextValue(e); 65 properties.setProperty(propName, propVal); 66 } 67 } 68 } 69 70 if (clazz != null) { 71 return InstanceUtils.newValue(clazz, properties); 72 } else if (method != null) { 73 return InstanceUtils.methodValue(method, properties); 74 } else { 75 throw new ConvertorException("Do not know how to create instance. " + "The <method> nor <class> element not found in "+element); } 78 } 79 80 public Element write(Document doc, Object inst) { 81 throw new ConvertorException("InstanceConvertor does "+ "not support writing."); } 84 85 } 86 | Popular Tags |