KickJava   Java API By Example, From Geeks To Geeks.

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


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

35 final class ConvertorBinding {
36     private static final String JavaDoc CONVERTOR_EXTENSION = "xml";//NOI18N
37
static final ObjectBinding.Reader READER = new ReaderImpl();
38     static final ObjectBinding.Writer WRITER = new WriterImpl();
39
40     private ConvertorBinding() {
41     }
42
43     private static final class ReaderImpl extends ObjectBinding.Reader {
44         final boolean canRead(final FileObject fo) {
45             boolean isRightExtension = fo.getExt().equalsIgnoreCase(getFileExtension());
46             Document JavaDoc dom = (isRightExtension == true) ? DocumentUtils.DocumentRef.getDOM(fo) : null;
47             
48             final Element JavaDoc documentElement = (dom != null) ? dom.getDocumentElement() : null;
49             return isRightExtension && documentElement != null && (documentElement.getNamespaceURI() != null);/*Convertors.canRead(documentElement)*/
50         }
51
52         final String JavaDoc getFileExtension() {
53             return CONVERTOR_EXTENSION;
54         }
55
56         final ObjectBinding getObjectBinding(final BasicContext ctx, final FileObject fo) {
57             return new ObjectBindingImpl(fo);
58         }
59     }
60
61     private static final class WriterImpl extends ObjectBinding.Writer {
62         final boolean canWrite(final Object JavaDoc obj) {
63             return Convertors.canWrite(obj);
64         }
65
66         final void write(final FileObject fo, final Object JavaDoc obj) throws IOException JavaDoc {
67             final Document JavaDoc doc = DocumentUtils.createDocument();
68             final Element JavaDoc e = Convertors.write(doc, obj);
69             doc.appendChild(e);
70             DocumentUtils.writeDocument(fo, doc);
71         }
72
73         final String JavaDoc getFileExtension() {
74             return CONVERTOR_EXTENSION;
75         }
76     }
77
78     private static final class ObjectBindingImpl extends ObjectBinding {
79         private final DocumentUtils.DocumentRef docRef;
80
81         ObjectBindingImpl(final FileObject fo) {
82             super(fo);
83             docRef = new DocumentUtils.DocumentRef();
84         }
85
86         public final Object JavaDoc createInstance() throws IOException JavaDoc {
87             return Convertors.read(docRef.getDocument(getFile()).getDocumentElement());
88         }
89
90         public final Object JavaDoc getModuleDescriptor() {
91             return new Object JavaDoc() {
92                 public boolean equals(final Object JavaDoc obj) {
93                     if (obj instanceof ConvertorDescriptor) {
94                         final ConvertorDescriptor cd = (ConvertorDescriptor) obj;
95                         return cd.getElementName().equals(getElement()) && cd.getNamespace().equals(getNamespace());
96                     }
97                     return false;
98                 }
99             };
100         }
101
102         public final boolean isEnabled() {
103             return Convertors.canRead(getNamespace(), getElement());
104         }
105
106         private String JavaDoc getNamespace() {
107             return docRef.getDocument(getFile()).getDocumentElement().getNamespaceURI();
108         }
109
110         private String JavaDoc getElement() {
111             return docRef.getDocument(getFile()).getDocumentElement().getNodeName();
112         }
113
114     }
115
116 }
117
Popular Tags