1 19 20 package org.netbeans.spi.settings; 21 22 import java.io.IOException ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 import org.netbeans.junit.NbTestCase; 26 import org.openide.filesystems.FileObject; 27 28 import org.openide.filesystems.FileSystem; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.loaders.DataFolder; 31 import org.openide.loaders.DataObject; 32 import org.openide.loaders.InstanceDataObject; 33 import org.openide.modules.ModuleInfo; 34 import org.openide.util.Lookup; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 39 43 public class DOMConvertorTest extends NbTestCase { 44 FileSystem fs; 45 46 47 public DOMConvertorTest(String name) { 48 super(name); 49 } 50 51 protected void setUp() throws Exception { 52 super.setUp(); 53 54 Lookup.getDefault().lookup(ModuleInfo.class); 55 fs = org.openide.filesystems.Repository.getDefault().getDefaultFileSystem(); 56 } 57 58 public void testCreateSetting() throws Exception { 59 try { 60 org.openide.filesystems.FileUtil.createFolder(fs.getRoot(), "testCreateSetting"); 61 DataFolder folder = DataFolder.findFolder(fs.findResource("testCreateSetting")); 62 63 ComposedSetting cs = new ComposedSetting(); 64 cs.b1 = new java.awt.Button (); 65 cs.b2 = cs.b1; 66 cs.cs = new ComposedSetting(); 67 cs.cs.b1 = new java.awt.Button (); 68 DataObject dobj = InstanceDataObject.create(folder, "testCreateSetting", cs, null); 69 70 FileObject fo = dobj.getPrimaryFile().copy(fs.getRoot(), dobj.getPrimaryFile().getName() + "_copy", "settings"); 72 org.openide.cookies.InstanceCookie ic = (org.openide.cookies.InstanceCookie) 73 DataObject.find(fo).getCookie(org.openide.cookies.InstanceCookie.class); 74 assertNotNull("missing InstanceCookie", ic); 75 assertEquals(cs.getClass(), ic.instanceClass()); 76 77 try { 78 ComposedSetting cs2 = (ComposedSetting) ic.instanceCreate(); 79 assertEquals(cs2.b1, cs2.b2); 80 } catch (IOException e) { 81 System.err.println("File contents:\n"); 82 FileUtil.copy(fo.getInputStream(), System.err); 83 throw e; 84 } 85 } catch (Exception ex) { 86 Logger.global.log(Level.WARNING, null, ex); 87 throw ex; 88 } 89 } 90 91 public static class ComposedSetting { 92 java.awt.Button b1; 93 java.awt.Button b2; 94 ComposedSetting cs; 95 } 96 97 public static class ComposedSettingConvertor extends DOMConvertor { 98 private final static String PUBLIC_ID = "-//NetBeans org.netbeans.modules.settings.xtest//DTD ComposedSetting 1.0//EN"; private final static String SYSTEM_ID = "nbres:/org/netbeans/modules/settings/convertors/data/composedsetting-1_0.dtd"; private final static String ELM_COMPOSED_SETTING = "composedsetting"; 102 public ComposedSettingConvertor() { 103 super(PUBLIC_ID, SYSTEM_ID, ELM_COMPOSED_SETTING); 104 } 105 106 protected Object readElement(org.w3c.dom.Element element) throws java.io.IOException , ClassNotFoundException { 107 if (!element.getTagName().equals(ELM_COMPOSED_SETTING)) { 108 throw new IllegalArgumentException ("required element: " + 109 ELM_COMPOSED_SETTING + ", but was: " + element.getTagName()); 110 } 111 112 Lookup l = findContext(element.getOwnerDocument()); 114 if (l == null) throw new NullPointerException ("missing context"); 115 FileObject fo = (FileObject) l.lookup(FileObject.class); 116 if (fo == null) throw new NullPointerException ("missing info about source"); 117 118 ComposedSetting cs = new ComposedSetting(); 119 NodeList nl = element.getChildNodes(); 120 for (int i = 0, len = nl.getLength(); i < len; i++) { 121 Node n = nl.item(i); 122 if (n instanceof Element ) { 123 Object obj = delegateRead((Element ) n); 124 if (obj instanceof java.awt.Button ) { 125 if (cs.b1 == null) { 126 cs.b1 = (java.awt.Button ) obj; 127 } else { 128 cs.b2 = (java.awt.Button ) obj; 129 } 130 } else { 131 cs.cs = (ComposedSetting) obj; 132 } 133 } 134 } 135 return cs; 136 } 137 138 public void registerSaver(Object inst, Saver s) { 139 } 140 141 public void unregisterSaver(Object inst, Saver s) { 142 } 143 144 protected void writeElement(org.w3c.dom.Document doc, org.w3c.dom.Element el, Object inst) throws java.io.IOException , org.w3c.dom.DOMException { 145 if (!(inst instanceof ComposedSetting)) { 146 throw new IllegalArgumentException ("required: " + ComposedSetting.class.getName() + " but was: " + inst.getClass()); 147 } 148 ComposedSetting cs = (ComposedSetting) inst; 149 Element subel; 151 if (cs.b1 != null) { 152 subel = delegateWrite(doc, cs.b1); 153 el.appendChild(subel); 154 } 155 if (cs.b2 != null) { 156 subel = delegateWrite(doc, cs.b2); 157 el.appendChild(subel); 158 } 159 160 if (cs.cs != null) { 161 subel = delegateWrite(doc, cs.cs); 162 el.appendChild(subel); 163 } 164 165 Lookup l = findContext(doc); 167 if (l == null) throw new NullPointerException ("missing context"); 168 FileObject fo = (FileObject) l.lookup(FileObject.class); 169 if (fo == null) throw new NullPointerException ("missing info about source"); 170 } 171 172 } 173 } 174 | Popular Tags |