1 19 20 package org.netbeans.modules.convertor; 21 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.Method ; 24 import java.util.Iterator ; 25 import java.util.Properties ; 26 import java.util.Set ; 27 import java.util.TreeSet ; 28 import org.netbeans.api.convertor.ConvertorException; 29 import org.netbeans.spi.convertor.Convertor; 30 import org.netbeans.spi.convertor.SimplyConvertible; 31 import org.openide.ErrorManager; 32 import org.w3c.dom.CDATASection ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.Text ; 38 39 43 public final class PropertiesConvertor implements Convertor { 44 45 private String namespace; 46 private String rootElement; 47 private String writes; 48 49 public PropertiesConvertor(String namespace, String rootElement, String writes) { 50 assert namespace != null; 51 assert rootElement != null; 52 assert writes != null; 53 this.namespace = namespace; 54 this.rootElement = rootElement; 55 this.writes = writes; 56 } 57 58 public Object read(Element element) { 59 assert element.getNodeName().equals(rootElement) : "Element "+element+ " cannot be converted by instance of PropertiesConvertor setuped "+ "for root elment "+element; 62 Properties p = new Properties (); 63 NodeList nodes = element.getChildNodes(); 64 for (int i = 0; i < nodes.getLength(); i++) { 65 Node node = nodes.item(i); 66 if (node.getNodeType() == Node.ELEMENT_NODE) { 67 Element e = (Element )node; 68 String propName = e.getNodeName(); 69 String propVal = getTextValue(e); 70 p.setProperty(propName, propVal); 71 } 72 } 73 return createInstance(element, p, writes); 74 } 75 76 public Element write(Document doc, Object inst) { 77 Properties p = new Properties (); 78 if (inst instanceof SimplyConvertible) { 79 SimplyConvertible sc = (SimplyConvertible)inst; 80 sc.write(p); 81 } else { 82 Method m; 83 try { 84 m = inst.getClass().getDeclaredMethod("write", new Class []{Properties .class}); m.setAccessible(true); 86 inst.getClass().getDeclaredMethod("read", new Class []{Properties .class}); } catch (Exception ex) { 89 ConvertorException ce = new ConvertorException("Class "+inst.getClass().getName()+ " cannot be stored as SimplyConvertible, because it does not implement SimplyConvertible interface nor"+ " it has read(Properties) and write(Properties) methods."); ce.initCause(ex); 93 throw ce; 94 } 95 try { 96 m.invoke(inst, new Object []{p}); 97 } catch (Exception ex) { 98 ConvertorException ce = new ConvertorException("Could not call "+ "introspected write(Properties) method on class "+ inst.getClass().getName()); 101 ce.initCause(ex); 102 throw ce; 103 } 104 } 105 Element ee = doc.createElementNS(namespace, rootElement); 106 Set keys = new TreeSet (p.keySet()); 107 Iterator it = keys.iterator(); 108 while (it.hasNext()) { 109 String key = (String )it.next(); 110 String val = p.getProperty(key); 112 Element e = doc.createElementNS(namespace, key); 113 Text t = doc.createTextNode(val); 114 e.appendChild(t); 115 ee.appendChild(e); 116 } 117 return ee; 118 } 119 120 private Object createInstance(Element element, Properties p, String className) { 121 try { 122 Class c = InstanceUtils.findClass(className); 123 if (SimplyConvertible.class.isAssignableFrom(c)) { 124 SimplyConvertible sc = (SimplyConvertible)c.newInstance(); 125 sc.read(p); 126 return sc; 127 } else { 128 Constructor co; 129 Method m; 130 try { 131 co = c.getDeclaredConstructor(new Class []{}); 132 co.setAccessible(true); 133 m = c.getDeclaredMethod("read", new Class []{Properties .class}); m.setAccessible(true); 135 c.getDeclaredMethod("write", new Class []{Properties .class}); } catch (Exception ex) { 138 ConvertorException ce = new ConvertorException("Class "+c.getName()+ " cannot be instantiated as SimplyConvertible, because it does not implement SimplyConvertible interface nor"+ " it has read(Properties) and write(Properties) methods."); ce.initCause(ex); 142 throw ce; 143 } 144 Object o = co.newInstance(new Object []{}); 145 m.invoke(o, new Object []{p}); 146 return o; 147 } 148 } catch (ConvertorException ex) { 149 throw ex; 150 } catch (Exception ex) { 151 ConvertorException ex2 = new ConvertorException("Unexpected exception. SimplyConvertible could "+ "not instantiate element "+element); ErrorManager.getDefault().annotate(ex2, ex); 154 throw ex2; 155 } 156 } 157 158 static String getTextValue(Element element) { 159 StringBuffer sb = new StringBuffer (); 160 NodeList nodes = element.getChildNodes(); 161 for (int i = 0; i < nodes.getLength(); i++) { 162 Node node = nodes.item(i); 163 if (node.getNodeType() == Node.TEXT_NODE) { 164 sb.append(((Text )node).getData()); 165 } 166 if (node.getNodeType() == Node.CDATA_SECTION_NODE) { 167 sb.append(((CDATASection )node).getData()); 168 } 169 } 170 return sb.toString(); 171 } 172 173 } 174 | Popular Tags |