KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > convertor > PropertiesConvertor


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.modules.convertor;
21
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import org.netbeans.api.convertor.ConvertorException;
29 import org.netbeans.spi.convertor.Convertor;
30 import org.netbeans.spi.convertor.SimplyConvertible;
31 import org.openide.ErrorManager;
32 import org.w3c.dom.CDATASection JavaDoc;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.w3c.dom.Text JavaDoc;
38
39 /**
40  *
41  * @author David Konecny
42  */

43 public final class PropertiesConvertor implements Convertor {
44     
45     private String JavaDoc namespace;
46     private String JavaDoc rootElement;
47     private String JavaDoc writes;
48
49     public PropertiesConvertor(String JavaDoc namespace, String JavaDoc rootElement, String JavaDoc writes) {
50         assert namespace != null;
51         assert rootElement != null;
52         assert writes != null;
53         this.namespace = namespace;
54         this.rootElement = rootElement;
55         this.writes = writes;
56     }
57
58     public Object JavaDoc read(Element JavaDoc element) {
59         assert element.getNodeName().equals(rootElement) : "Element "+element+ // NOI18N
60
" cannot be converted by instance of PropertiesConvertor setuped "+ // NOI18N
61
"for root elment "+element;
62         Properties JavaDoc p = new Properties JavaDoc();
63         NodeList JavaDoc nodes = element.getChildNodes();
64         for (int i = 0; i < nodes.getLength(); i++) {
65             Node JavaDoc node = nodes.item(i);
66             if (node.getNodeType() == Node.ELEMENT_NODE) {
67                 Element JavaDoc e = (Element JavaDoc)node;
68                 String JavaDoc propName = e.getNodeName();
69                 String JavaDoc propVal = getTextValue(e);
70                 p.setProperty(propName, propVal);
71             }
72         }
73         return createInstance(element, p, writes);
74     }
75     
76     public Element JavaDoc write(Document JavaDoc doc, Object JavaDoc inst) {
77         Properties JavaDoc p = new Properties JavaDoc();
78         if (inst instanceof SimplyConvertible) {
79             SimplyConvertible sc = (SimplyConvertible)inst;
80             sc.write(p);
81         } else {
82             Method JavaDoc m;
83             try {
84                 m = inst.getClass().getDeclaredMethod("write", new Class JavaDoc[]{Properties JavaDoc.class}); // NOI18N
85
m.setAccessible(true);
86                 // check that there is also read(Properties) method
87
inst.getClass().getDeclaredMethod("read", new Class JavaDoc[]{Properties JavaDoc.class}); // NOI18N
88
} catch (Exception JavaDoc ex) {
89                 ConvertorException ce = new ConvertorException("Class "+inst.getClass().getName()+ // NOI18N
90
" cannot be stored as SimplyConvertible, because it does not implement SimplyConvertible interface nor"+ // NOI18N
91
" it has read(Properties) and write(Properties) methods."); // NOI18N
92
ce.initCause(ex);
93                 throw ce;
94             }
95             try {
96                 m.invoke(inst, new Object JavaDoc[]{p});
97             } catch (Exception JavaDoc ex) {
98                 ConvertorException ce = new ConvertorException("Could not call "+ // NOI18N
99
"introspected write(Properties) method on class "+ // NOI18N
100
inst.getClass().getName());
101                 ce.initCause(ex);
102                 throw ce;
103             }
104         }
105         Element JavaDoc ee = doc.createElementNS(namespace, rootElement);
106         Set JavaDoc keys = new TreeSet JavaDoc(p.keySet());
107         Iterator JavaDoc it = keys.iterator();
108         while (it.hasNext()) {
109             String JavaDoc key = (String JavaDoc)it.next();
110             // TODO: check here that key is valid name for XML element
111
String JavaDoc val = p.getProperty(key);
112             Element JavaDoc e = doc.createElementNS(namespace, key);
113             Text JavaDoc t = doc.createTextNode(val);
114             e.appendChild(t);
115             ee.appendChild(e);
116         }
117         return ee;
118     }
119     
120     private Object JavaDoc createInstance(Element JavaDoc element, Properties JavaDoc p, String JavaDoc className) {
121         try {
122             Class JavaDoc c = InstanceUtils.findClass(className);
123             if (SimplyConvertible.class.isAssignableFrom(c)) {
124                 SimplyConvertible sc = (SimplyConvertible)c.newInstance();
125                 sc.read(p);
126                 return sc;
127             } else {
128                 Constructor JavaDoc co;
129                 Method JavaDoc m;
130                 try {
131                     co = c.getDeclaredConstructor(new Class JavaDoc[]{});
132                     co.setAccessible(true);
133                     m = c.getDeclaredMethod("read", new Class JavaDoc[]{Properties JavaDoc.class}); // NOI18N
134
m.setAccessible(true);
135                     // check that there is also write(Properties) method
136
c.getDeclaredMethod("write", new Class JavaDoc[]{Properties JavaDoc.class}); // NOI18N
137
} catch (Exception JavaDoc ex) {
138                     ConvertorException ce = new ConvertorException("Class "+c.getName()+ // NOI18N
139
" cannot be instantiated as SimplyConvertible, because it does not implement SimplyConvertible interface nor"+ // NOI18N
140
" it has read(Properties) and write(Properties) methods."); // NOI18N
141
ce.initCause(ex);
142                     throw ce;
143                 }
144                 Object JavaDoc o = co.newInstance(new Object JavaDoc[]{});
145                 m.invoke(o, new Object JavaDoc[]{p});
146                 return o;
147             }
148         } catch (ConvertorException ex) {
149             throw ex;
150         } catch (Exception JavaDoc ex) {
151             ConvertorException ex2 = new ConvertorException("Unexpected exception. SimplyConvertible could "+ // NOI18N
152
"not instantiate element "+element); // NOI18N
153
ErrorManager.getDefault().annotate(ex2, ex);
154             throw ex2;
155         }
156     }
157     
158     static String JavaDoc getTextValue(Element JavaDoc element) {
159         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
160         NodeList JavaDoc nodes = element.getChildNodes();
161         for (int i = 0; i < nodes.getLength(); i++) {
162             Node JavaDoc node = nodes.item(i);
163             if (node.getNodeType() == Node.TEXT_NODE) {
164                 sb.append(((Text JavaDoc)node).getData());
165             }
166             if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
167                 sb.append(((CDATASection JavaDoc)node).getData());
168             }
169         }
170         return sb.toString();
171     }
172     
173 }
174
Popular Tags