KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 final class ObjectRefBinding {
37     private static final String JavaDoc OBJECTREF_EXTENSION = "xml";//NOI18N
38
private static final String JavaDoc OBJECTREF_NAMESPACE = "http://www.netbeans.org/ns/registry";
39     private static final String JavaDoc OBJECTREF_ELEMENT = "link";
40
41     static final ObjectBinding.Reader READER = new ReaderImpl();
42     static final ObjectBinding.Writer WRITER = new WriterImpl();
43
44     private ObjectRefBinding() {
45     }
46
47     private static class ReaderImpl extends ObjectBinding.Reader {
48         boolean canRead(FileObject fo) {
49             boolean isRightExtension = fo.getExt().equalsIgnoreCase(OBJECTREF_EXTENSION);
50             Document JavaDoc d = (isRightExtension == true) ? DocumentUtils.DocumentRef.getDOM(fo) : null;
51             
52             return (isRightExtension && d != null && d.getDocumentElement() != null && d.getDocumentElement().getNamespaceURI() != null
53                     && d.getDocumentElement().getNamespaceURI().equals(OBJECTREF_NAMESPACE) &&
54                     d.getDocumentElement().getNodeName().equals(OBJECTREF_ELEMENT));
55         }
56
57         String JavaDoc getFileExtension() {
58             return OBJECTREF_EXTENSION;
59         }
60
61         ObjectBinding getObjectBinding(BasicContext ctx, FileObject fo) {
62             return new ObjectBindingImpl(ctx, fo);
63         }
64     }
65
66     private static class WriterImpl extends ObjectBinding.Writer {
67         boolean canWrite(Object JavaDoc obj) {
68             return (obj instanceof ObjectRef);
69         }
70
71         void write(FileObject fo, Object JavaDoc obj) throws IOException JavaDoc {
72             Document JavaDoc doc = org.netbeans.core.registry.DocumentUtils.createDocument();
73             Element JavaDoc e = doc.createElementNS(OBJECTREF_NAMESPACE, OBJECTREF_ELEMENT);
74             Text JavaDoc t = doc.createTextNode(((ObjectRef) obj).getContextAbsoluteName() + "/" + ((ObjectRef) obj).getBindingName());
75             e.appendChild(t);
76             doc.appendChild(e);
77             org.netbeans.core.registry.DocumentUtils.writeDocument(fo, doc);
78         }
79
80         String JavaDoc getFileExtension() {
81             return OBJECTREF_EXTENSION;
82         }
83     }
84
85     private static final class ObjectBindingImpl extends ObjectBinding {
86         private DocumentUtils.DocumentRef docRef;
87         private BasicContext context;
88
89         ObjectBindingImpl(BasicContext context, FileObject fo) {
90             super(fo);
91             this.context = context;
92             docRef = new DocumentUtils.DocumentRef();
93         }
94
95         public Object JavaDoc createInstance() throws IOException JavaDoc {
96             Object JavaDoc retVal = null;
97             Document JavaDoc d = docRef.getDocument(getFile());
98             String JavaDoc target = DocumentUtils.getTextValue(d.getDocumentElement());
99             target = target.trim();
100             int i = target.lastIndexOf('/');
101             String JavaDoc contextName = target.substring(0, i);
102             String JavaDoc bindingName = target.substring(i + 1);
103             retVal = SpiUtils.createObjectRef(((ContextImpl) context).getRootContextImpl(), contextName, bindingName);
104             return retVal;
105         }
106
107         public boolean isEnabled() {
108             return (getFile() != null && getFile().isValid());
109         }
110     }
111
112 }
113
Popular Tags