Lines 13-38
Link Here
|
13 |
|
13 |
|
14 |
package org.netbeans.core.registry.olddsimpl; |
14 |
package org.netbeans.core.registry.olddsimpl; |
15 |
|
15 |
|
16 |
|
16 |
import java.io.IOException; |
|
|
17 |
import java.io.OutputStream; |
17 |
import java.util.Arrays; |
18 |
import java.util.Arrays; |
18 |
import java.util.Enumeration; |
|
|
19 |
import java.util.Hashtable; |
20 |
import java.util.Iterator; |
19 |
import java.util.Iterator; |
21 |
import java.util.List; |
20 |
import java.util.List; |
|
|
21 |
import org.w3c.dom.Document; |
22 |
import org.w3c.dom.Element; |
23 |
import org.xml.sax.SAXException; |
24 |
|
22 |
import org.netbeans.api.registry.ContextException; |
25 |
import org.netbeans.api.registry.ContextException; |
23 |
import org.netbeans.core.registry.ContextImpl; |
|
|
24 |
import org.netbeans.spi.registry.BasicContext; |
26 |
import org.netbeans.spi.registry.BasicContext; |
25 |
import org.netbeans.spi.registry.SpiUtils; |
27 |
import org.netbeans.spi.registry.SpiUtils; |
26 |
import org.openide.ErrorManager; |
|
|
27 |
|
28 |
|
|
|
29 |
import org.netbeans.core.registry.ContextImpl; |
30 |
|
31 |
import org.openide.ErrorManager; |
28 |
import org.openide.cookies.InstanceCookie; |
32 |
import org.openide.cookies.InstanceCookie; |
29 |
import org.openide.filesystems.FileObject; |
33 |
import org.openide.filesystems.*; |
30 |
import org.openide.loaders.DataObject; |
34 |
import org.openide.loaders.*; |
31 |
import org.openide.loaders.DataObjectNotFoundException; |
|
|
32 |
|
35 |
|
|
|
36 |
import org.netbeans.api.convertor.ConvertorException; |
37 |
import org.netbeans.api.convertor.Convertors; |
33 |
|
38 |
|
34 |
/** |
39 |
/** |
35 |
* |
|
|
36 |
* @author Peter Zavadsky |
40 |
* @author Peter Zavadsky |
37 |
*/ |
41 |
*/ |
38 |
public class DSUtils { |
42 |
public class DSUtils { |
Lines 41-46
Link Here
|
41 |
private DSUtils() { |
45 |
private DSUtils() { |
42 |
} |
46 |
} |
43 |
|
47 |
|
|
|
48 |
/** |
49 |
* Try to get an object binding from a file. |
50 |
* @param ctx the basic context being used |
51 |
* @param folder the folder in which to look |
52 |
* @param name the file name (like a binding, no ext) |
53 |
* @return an object bound to it, or null |
54 |
* @throws ContextException if the binding exists but there is a problem loading it |
55 |
*/ |
44 |
public static Object getObjectForFile(BasicContext ctx, FileObject folder, String name) throws ContextException { |
56 |
public static Object getObjectForFile(BasicContext ctx, FileObject folder, String name) throws ContextException { |
45 |
FileObject fo = findInstanceFile(folder, name); |
57 |
FileObject fo = findInstanceFile(folder, name); |
46 |
if (fo == null) { |
58 |
if (fo == null) { |
Lines 70-79
Link Here
|
70 |
return instanceCreate (obj); |
82 |
return instanceCreate (obj); |
71 |
} |
83 |
} |
72 |
|
84 |
|
73 |
public static Object instanceCreate (DataObject obj) throws ContextException { |
85 |
private static Object instanceCreate (DataObject obj) throws ContextException { |
74 |
InstanceCookie ic = (InstanceCookie)obj.getCookie (InstanceCookie.class); |
86 |
InstanceCookie ic = (InstanceCookie)obj.getCookie (InstanceCookie.class); |
75 |
if (ic == null) { |
87 |
if (ic == null) { |
76 |
return null; |
88 |
return instanceCreateViaConvertors(obj); |
77 |
} |
89 |
} |
78 |
|
90 |
|
79 |
try { |
91 |
try { |
Lines 112-118
Link Here
|
112 |
return null; |
124 |
return null; |
113 |
} |
125 |
} |
114 |
|
126 |
|
115 |
public static List getInstanceExtensions() { |
127 |
private static List getInstanceExtensions() { |
116 |
return Arrays.asList(new String[] { |
128 |
return Arrays.asList(new String[] { |
117 |
"instance", "xml", "settings", "shadow", "link"}); // NOI18N |
129 |
"instance", "xml", "settings", "shadow", "link"}); // NOI18N |
118 |
} |
130 |
} |
Lines 164-169
Link Here
|
164 |
} |
176 |
} |
165 |
} |
177 |
} |
166 |
return escaped.toString (); |
178 |
return escaped.toString (); |
|
|
179 |
} |
180 |
|
181 |
/** |
182 |
* Try to create an instance via the Convertors API. |
183 |
* @param _d a data object, which may be an XMLDataObject |
184 |
* @return an instance if its root element can be converted, or null if it is not a .xml file, or it has no convertor |
185 |
* @throws ContextException if it is a .xml file that cannot be parsed, or the convertor fails |
186 |
*/ |
187 |
private static Object instanceCreateViaConvertors(DataObject _d) throws ContextException { |
188 |
if (!(_d instanceof XMLDataObject)) { |
189 |
return null; |
190 |
} |
191 |
XMLDataObject d = (XMLDataObject)_d; |
192 |
Document doc; |
193 |
try { |
194 |
doc = d.getDocument(); |
195 |
} catch (IOException ex) { |
196 |
ContextException e = SpiUtils.createContextException(null, null); |
197 |
ErrorManager.getDefault().annotate(e, ex); |
198 |
throw e; |
199 |
} catch (SAXException ex) { |
200 |
ContextException e = SpiUtils.createContextException(null, null); |
201 |
ErrorManager.getDefault().annotate(e, ex); |
202 |
throw e; |
203 |
} |
204 |
Element el = doc.getDocumentElement(); |
205 |
assert el.getNodeName() != null; |
206 |
if (el.getNamespaceURI() != null && Convertors.canRead(el)) { |
207 |
try { |
208 |
return Convertors.read(el); |
209 |
} catch (ConvertorException ex) { |
210 |
ContextException e = SpiUtils.createContextException(null, null); |
211 |
ErrorManager.getDefault().annotate(e, ErrorManager.UNKNOWN, "From " + d.getPrimaryFile(), null, ex, null); |
212 |
throw e; |
213 |
} catch (Exception e) { |
214 |
assert false : e + " from " + d.getPrimaryFile(); |
215 |
return null; |
216 |
} |
217 |
} else { |
218 |
return null; |
219 |
} |
220 |
} |
221 |
|
222 |
/** |
223 |
* Make a binding for an object to a file. |
224 |
* @param folder the folder to store it in |
225 |
* @param name the name of the binding (file basename) |
226 |
* @param o a structured object to store |
227 |
* @throws IOException if there is a problem storing it |
228 |
*/ |
229 |
public static void bindObjectToFile(DataFolder folder, final String name, final Object o) throws IOException { |
230 |
if (Convertors.canWrite(o)) { |
231 |
// XXX delete other kinds of bindings first? very unlikely to matter... |
232 |
final FileObject fileFolder = folder.getPrimaryFile(); |
233 |
fileFolder.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { |
234 |
public void run() throws IOException { |
235 |
FileObject xml = fileFolder.getFileObject(name, "xml"); |
236 |
if (xml == null) { |
237 |
xml = fileFolder.createData(name, "xml"); |
238 |
} |
239 |
FileLock l = xml.lock(); |
240 |
try { |
241 |
OutputStream os = xml.getOutputStream(l); |
242 |
try { |
243 |
Convertors.write(os, o); |
244 |
} finally { |
245 |
os.close(); |
246 |
} |
247 |
} finally { |
248 |
l.releaseLock(); |
249 |
} |
250 |
} |
251 |
}); |
252 |
} else { |
253 |
// Fallback to old-style storage, e.g. .settings with <serialdata>. |
254 |
InstanceDataObject.create(folder, name, o, null); |
255 |
} |
167 |
} |
256 |
} |
168 |
|
257 |
|
169 |
} |
258 |
} |