KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > deployment > impl > ServerStringConverter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.deployment.impl;
21
22 import org.w3c.dom.Element JavaDoc;
23 import org.w3c.dom.DOMException JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
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 /**
34  * @author nn136682
35  */

36 public class ServerStringConverter extends org.netbeans.spi.settings.DOMConvertor {
37
38     private static final String JavaDoc E_SERVER_STRING = "server-string";
39     private static final String JavaDoc E_TARGET = "target";
40     private static final String JavaDoc A_PLUGIN = "plugin";
41     private static final String JavaDoc A_URL = "url";
42     private static final String JavaDoc A_NAME = "name";
43     private static final String JavaDoc PUBLIC_ID = "-//org_netbeans_modules_j2ee//DTD ServerString 1.0//EN"; // NOI18N
44
private static final String JavaDoc SYSTEM_ID = "nbres:/org/netbeans/modules/j2ee/deployment/impl/server-string.dtd"; // NOI18N
45

46     public static boolean writeServerInstance(ServerString instance, String JavaDoc destDir, String JavaDoc 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 JavaDoc 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 JavaDoc e) {
66                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.WARNING, e);
67             }
68         }
69     }
70
71     public static ServerString readServerInstance(String JavaDoc fromDir, String JavaDoc 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 JavaDoc 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 JavaDoc e) {
89                 ErrorManager.getDefault().notify(ErrorManager.WARNING, e);
90             }
91         }
92     }
93     
94     public static DOMConvertor create() {
95         return new ServerStringConverter();
96     }
97     
98     /** Creates a new instance of ServerStringConverter */
99     protected ServerStringConverter() {
100         super(PUBLIC_ID, SYSTEM_ID, E_SERVER_STRING);
101     }
102     
103     protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
104         NodeList JavaDoc targetElements = element.getElementsByTagName(E_TARGET);
105         String JavaDoc[] targets = new String JavaDoc[targetElements.getLength()];
106         for (int i=0; i<targets.length; i++) {
107             Element JavaDoc te = (Element JavaDoc) 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 JavaDoc plugin = element.getAttribute(A_PLUGIN);
113         if (plugin == null)
114             throw new IOException(NbBundle.getMessage(ServerStringConverter.class, "MSG_ServerStringParseError", A_PLUGIN));
115         
116         String JavaDoc url = element.getAttribute(A_URL);
117         //if (plugin == null)
118
// throw new IOException(NbBundle.getMessage(ServerStringConverter.class, "MSG_ServerStringParseError", A_URL));
119

120         return new ServerString(plugin, url, targets);
121     }
122     
123     public void registerSaver(Object JavaDoc inst, org.netbeans.spi.settings.Saver s) {
124         // Not needed: there is not editing of ServerName
125
}
126     public void unregisterSaver(Object JavaDoc inst, org.netbeans.spi.settings.Saver s) {
127         // Not needed: there is not editing of ServerName
128
}
129     protected void writeElement(org.w3c.dom.Document JavaDoc doc, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj) throws IOException, DOMException JavaDoc {
130         if (obj == null)
131             return;
132         
133         if (! (obj instanceof ServerString))
134             throw new DOMException JavaDoc(
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 JavaDoc[] targets = ss.getTargets();
143         if (targets == null)
144             targets = new String JavaDoc[0];
145         
146         for (int i=0; i<targets.length; i++) {
147             Element JavaDoc targetElement = doc.createElement (E_TARGET);
148             targetElement.setAttribute(A_NAME, targets[i]);
149             element.appendChild (targetElement);
150         }
151         String JavaDoc 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