KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > XMLBasedBinding


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.core.registry;
21
22 import org.openide.filesystems.FileObject;
23 import org.openide.loaders.DataObject;
24 import org.openide.loaders.DataObjectNotFoundException;
25 import org.openide.cookies.InstanceCookie;
26 import org.netbeans.spi.registry.BasicContext;
27
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Provides ObjectBinding.Reader for ObjectBinding implementation.
32  *
33  * Backward compatibility for XML based instances. Impl. based on
34  * datasystems.
35  */

36
37 final class XMLBasedBinding extends ObjectBinding.Reader {
38     static final ObjectBinding.Reader READER = new XMLBasedBinding ();
39     
40     private XMLBasedBinding () {}
41     
42     boolean canRead(FileObject fo) {
43         boolean retVal = (fo.getExt().equals(getFileExtension()));
44
45         if (retVal) {
46             try {
47                 InstanceCookie ic = getInstanceCookie(fo);
48                 retVal = (ic != null);
49             } catch (DataObjectNotFoundException e) {
50                 retVal = false;
51             }
52         }
53         return retVal;
54     }
55
56     private static InstanceCookie getInstanceCookie(FileObject fo) throws DataObjectNotFoundException {
57         DataObject d = DataObject.find(fo);
58         return (InstanceCookie)d.getCookie(InstanceCookie.class);
59     }
60
61     String JavaDoc getFileExtension() {
62         return "xml";//NOI18N
63
}
64
65     ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) {
66         return new ObjectBindingImpl (fo);
67     }
68     
69     private class ObjectBindingImpl extends ObjectBinding {
70         InstanceCookie ic;
71         ObjectBindingImpl(FileObject fo) {
72             super(fo);
73         }
74
75         public Object JavaDoc createInstance() throws IOException JavaDoc {
76             InstanceCookie icLocal = (ic != null) ? ic : getInstanceCookie(getFile());
77             try {
78                 return icLocal.instanceCreate();
79             } catch (ClassNotFoundException JavaDoc e) {
80                 throw new IOException JavaDoc(e.getLocalizedMessage());
81             }
82         }
83
84         public boolean isEnabled() {
85             return (getFile() != null && getFile().isValid());
86         }
87     }
88 }
89
Popular Tags