1 19 20 package org.netbeans.modules.j2ee.deployment.impl; 21 22 import org.w3c.dom.Element ; 23 import org.w3c.dom.DOMException ; 24 import org.w3c.dom.NodeList ; 25 import java.io.*; 26 27 import org.netbeans.spi.settings.DOMConvertor; 28 29 import org.openide.ErrorManager; 30 import org.openide.util.NbBundle; 31 import org.openide.filesystems.*; 32 33 36 public class ServerStringConverter extends org.netbeans.spi.settings.DOMConvertor { 37 38 private static final String E_SERVER_STRING = "server-string"; 39 private static final String E_TARGET = "target"; 40 private static final String A_PLUGIN = "plugin"; 41 private static final String A_URL = "url"; 42 private static final String A_NAME = "name"; 43 private static final String PUBLIC_ID = "-//org_netbeans_modules_j2ee//DTD ServerString 1.0//EN"; private static final String SYSTEM_ID = "nbres:/org/netbeans/modules/j2ee/deployment/impl/server-string.dtd"; 46 public static boolean writeServerInstance(ServerString instance, String destDir, String destFile) { 47 FileLock lock = null; 48 Writer writer = null; 49 try { 50 FileObject dir = Repository.getDefault().getDefaultFileSystem().findResource(destDir); 51 FileObject fo = FileUtil.createData(dir, destFile); 52 lock = fo.lock(); 53 writer = new OutputStreamWriter(fo.getOutputStream(lock)); 54 create().write(writer, instance); 55 return true; 56 57 } catch(Exception ioe) { 58 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ioe); 59 return false; 60 } 61 finally { 62 try { 63 if (lock != null) lock.releaseLock(); 64 if (writer != null) writer.close(); 65 } catch (Exception e) { 66 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, e); 67 } 68 } 69 } 70 71 public static ServerString readServerInstance(String fromDir, String fromFile) { 72 Reader reader = null; 73 try { 74 FileObject dir = Repository.getDefault().getDefaultFileSystem().findResource(fromDir); 75 if (dir == null) { 76 return null; 77 } 78 FileObject fo = dir.getFileObject (fromFile); 79 if (fo == null) 80 return null; 81 82 reader = new InputStreamReader(fo.getInputStream()); 83 return (ServerString) create().read(reader); 84 } catch(Exception ioe) { 85 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, ioe); 86 return null; 87 } finally { 88 try { if (reader != null) reader.close(); } catch(Exception e) { 89 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 90 } 91 } 92 } 93 94 public static DOMConvertor create() { 95 return new ServerStringConverter(); 96 } 97 98 99 protected ServerStringConverter() { 100 super(PUBLIC_ID, SYSTEM_ID, E_SERVER_STRING); 101 } 102 103 protected Object readElement(org.w3c.dom.Element element) throws java.io.IOException , ClassNotFoundException { 104 NodeList targetElements = element.getElementsByTagName(E_TARGET); 105 String [] targets = new String [targetElements.getLength()]; 106 for (int i=0; i<targets.length; i++) { 107 Element te = (Element ) targetElements.item(i); 108 targets[i] = te.getAttribute(A_NAME); 109 if (targets[i] == null) 110 throw new IOException(NbBundle.getMessage(ServerStringConverter.class, "MSG_ServerStringParseError", E_TARGET)); 111 } 112 String plugin = element.getAttribute(A_PLUGIN); 113 if (plugin == null) 114 throw new IOException(NbBundle.getMessage(ServerStringConverter.class, "MSG_ServerStringParseError", A_PLUGIN)); 115 116 String url = element.getAttribute(A_URL); 117 120 return new ServerString(plugin, url, targets); 121 } 122 123 public void registerSaver(Object inst, org.netbeans.spi.settings.Saver s) { 124 } 126 public void unregisterSaver(Object inst, org.netbeans.spi.settings.Saver s) { 127 } 129 protected void writeElement(org.w3c.dom.Document doc, org.w3c.dom.Element element, Object obj) throws IOException, DOMException { 130 if (obj == null) 131 return; 132 133 if (! (obj instanceof ServerString)) 134 throw new DOMException ( 135 DOMException.NOT_SUPPORTED_ERR, 136 NbBundle.getMessage(ServerStringConverter.class, "MSG_NotSupportedObject", obj.getClass())); 137 138 ServerString ss = (ServerString) obj; 139 if (ss.getPlugin() == null) 140 throw new IOException(NbBundle.getMessage(ServerStringConverter.class, "MSG_BadServerString", ss)); 141 142 String [] targets = ss.getTargets(); 143 if (targets == null) 144 targets = new String [0]; 145 146 for (int i=0; i<targets.length; i++) { 147 Element targetElement = doc.createElement (E_TARGET); 148 targetElement.setAttribute(A_NAME, targets[i]); 149 element.appendChild (targetElement); 150 } 151 String url = ss.getUrl(); 152 if (url == null) 153 url = ""; 154 element.setAttribute(A_URL, url); 155 element.setAttribute(A_PLUGIN, ss.getPlugin()); 156 } 157 } 158 | Popular Tags |