KickJava   Java API By Example, From Geeks To Geeks.

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


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.netbeans.core.registry.oldformats.SerialDataConvertor;
23 import org.netbeans.spi.registry.BasicContext;
24 import org.openide.filesystems.FileObject;
25 import org.w3c.dom.Document JavaDoc;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 /**
31  * Provides ObjectBinding.Reader and ObjectBinding.Writer for ObjectBinding implementation.
32  * (copy & pasted & refactored from original org.netbeans.core.registry.ObjectBinding)
33  */

34 final class SettingBinding {
35     private static final String JavaDoc SETTINGS_EXTENSION = "settings";//NOI18N
36
static final ObjectBinding.Reader READER = new ReaderImpl();
37     static final ObjectBinding.Writer WRITER = new WriterImpl();
38
39     private SettingBinding() {
40     }
41
42     private static class ReaderImpl extends ObjectBinding.Reader {
43         boolean canRead(FileObject fo) {
44             boolean isRightExtension = fo.getExt().equalsIgnoreCase(SETTINGS_EXTENSION) || fo.getExt().equalsIgnoreCase("xml");
45             Document JavaDoc dom = (isRightExtension) ? DocumentUtils.DocumentRef.getDOM(fo) : null;
46             
47             return isRightExtension && dom != null && SerialDataConvertor.isSettingsFormat(dom.getDocumentElement());
48         }
49
50         String JavaDoc getFileExtension() {
51             return SETTINGS_EXTENSION;
52         }
53
54         ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) {
55             return new ObjectBindingImpl(fo);
56         }
57     }
58
59     private static class WriterImpl extends ObjectBinding.Writer {
60         boolean canWrite(Object JavaDoc obj) {
61             return (obj instanceof Serializable JavaDoc);
62         }
63
64         void write(FileObject fo, Object JavaDoc obj) throws IOException JavaDoc {
65             SerialDataConvertor sdc = new SerialDataConvertor();
66             sdc.write(fo, obj);
67         }
68
69         String JavaDoc getFileExtension() {
70             return SETTINGS_EXTENSION;
71         }
72     }
73
74     private static class ObjectBindingImpl extends ObjectBinding {
75         private DocumentUtils.DocumentRef docRef;
76
77         ObjectBindingImpl(FileObject fo) {
78             super(fo);
79             docRef = new DocumentUtils.DocumentRef();
80         }
81
82         public Object JavaDoc createInstance() throws IOException JavaDoc {
83             Object JavaDoc retVal = null;
84             Document JavaDoc d = docRef.getDocument(getFile());
85             try {
86                 retVal = SerialDataConvertor.read(d.getDocumentElement(), getFile());
87             } catch (ClassNotFoundException JavaDoc e) {
88                 throw new IOException JavaDoc(e.getLocalizedMessage());
89             }
90
91             return retVal;
92         }
93
94         public boolean isEnabled() {
95             String JavaDoc moduleName = getModuleName();
96             return (moduleName == null) || StateUpdater.getDefault().isModuleEnabled(moduleName);
97         }
98
99         public Object JavaDoc getModuleDescriptor() {
100             return new Object JavaDoc() {
101                 public boolean equals(Object JavaDoc obj) {
102                     if (obj instanceof String JavaDoc) {
103                         String JavaDoc modName = (String JavaDoc)obj;
104                         String JavaDoc moduleName = getModuleName();
105                         return moduleName == null ? false : moduleName.equals(modName);
106                     }
107                     return false;
108                 }
109             };
110         }
111
112         private String JavaDoc getModuleName() {
113             String JavaDoc moduleName = SerialDataConvertor.getModuleCodeName(docRef.getDocument(getFile()).getDocumentElement());
114             return (moduleName != null) ? StateUpdater.cutOffVersion(moduleName) : null;
115         }
116     }
117
118 }
119
Popular Tags