KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Properties JavaDoc;
23 import org.netbeans.api.convertor.ConvertorException;
24 import org.netbeans.spi.convertor.Convertor;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30 /**
31  *
32  * @author David Konecny
33  */

34
35 final public class XMLInstanceConvertor implements Convertor {
36
37     public XMLInstanceConvertor() {
38     }
39
40     public Object JavaDoc read(Element JavaDoc element) {
41         assert element.getNodeName().equals("instance") : "Element "+ // NOI18N
42
element+" is not <instance> element"; // NOI18N
43

44         String JavaDoc clazz = null;
45         String JavaDoc method = null;
46         Properties JavaDoc properties = null;
47         
48         NodeList JavaDoc nodes = element.getChildNodes();
49         for (int i = 0; i < nodes.getLength(); i++) {
50             Node JavaDoc node = nodes.item(i);
51             if (node.getNodeType() == Node.ELEMENT_NODE) {
52                 Element JavaDoc e = (Element JavaDoc)node;
53                 if (e.getNodeName().equals("class")) { // NOI18N
54
clazz = PropertiesConvertor.getTextValue(e);
55                 }
56                 if (e.getNodeName().equals("method")) { // NOI18N
57
method = PropertiesConvertor.getTextValue(e);
58                 }
59                 if (e.getNodeName().equals("property")) { // NOI18N
60
if (properties == null) {
61                         properties = new Properties JavaDoc();
62                     }
63                     String JavaDoc propName = e.getAttribute("name"); // NOI18N
64
String JavaDoc propVal = PropertiesConvertor.getTextValue(e);
65                     properties.setProperty(propName, propVal);
66                 }
67             }
68         }
69
70         if (clazz != null) {
71             return InstanceUtils.newValue(clazz, properties);
72         } else if (method != null) {
73             return InstanceUtils.methodValue(method, properties);
74         } else {
75             throw new ConvertorException("Do not know how to create instance. " + // NOI18N
76
"The <method> nor <class> element not found in "+element); // NOI18N
77
}
78     }
79     
80     public Element JavaDoc write(Document JavaDoc doc, Object JavaDoc inst) {
81         throw new ConvertorException("InstanceConvertor does "+ // NOI18N
82
"not support writing."); // NOI18N
83
}
84
85 }
86
Popular Tags