KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > graph > XMLSerializer


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.web.jsf.navigation.graph;
20
21 //import org.netbeans.graph.api.control.GraphHelper;
22
//import org.netbeans.graph.api.model.IGraphNode;
23
//import org.w3c.dom.Document;
24
//import org.openide.xml.XMLUtil;
25
//import org.xml.sax.InputSource;
26
//import org.xml.sax.ErrorHandler;
27
//import org.xml.sax.SAXParseException;
28
//import org.xml.sax.SAXException;
29
//
30
//import java.awt.*;
31
//import java.util.HashMap;
32
//import java.io.IOException;
33
//import java.io.InputStream;
34
//import java.io.OutputStream;
35

36 /**
37  * @author Joelle Lam
38  */

39 public class XMLSerializer {
40
41 // private static String getAttributeValue(org.w3c.dom.Node node, String attr) {
42
// try {
43
// if (node != null) {
44
// org.w3c.dom.NamedNodeMap map = node.getAttributes();
45
// if (map != null) {
46
// node = map.getNamedItem(attr);
47
// if (node != null)
48
// return node.getNodeValue();
49
// }
50
// }
51
// } catch (org.w3c.dom.DOMException e) {
52
// }
53
// return null;
54
// }
55
//
56
// private static void setAttribute(org.w3c.dom.Document doc, org.w3c.dom.Node node, String name, String value) {
57
// if (doc == null || node == null || name == null || value == null)
58
// return;
59
// org.w3c.dom.NamedNodeMap map = node.getAttributes();
60
// org.w3c.dom.Attr attribute = doc.createAttribute(name);
61
// attribute.setValue(value);
62
// map.setNamedItem(attribute);
63
// }
64
//
65
// /**
66
// * This methods uses deserializes an XML node with
67
// * @param rootNode
68
// */
69
// // WARNING - all GraphComponentPeer instance must be loaded from IPresenterDocument already
70
//
71
//// Algorithm of XMLSertializerTest.readXMLTree method:
72
//// data[] = read_id_and_location_of_all_nodes_in_xmlRootNode(xmlRootNode);
73
////// read data from xml
74
//// nodes[] = helper.getNodes(); // array of nodes in the view
75
//// for (id_loc : data) { // for each id_loc pair in data
76
//// node = find_node_by_id(nodes, id_loc.id); // if appropriate node by id
77
//// if (node == null) // no node found -> do nothing
78
//// continue;
79
//// helper.setNodeLocation(node, id_loc.location); // set a new location
80
//// of a node
81
//// helper.notifyNodeLocationLoaded(node); // notify the view that
82
//// location is loaded
83
//// }
84
// public static synchronized void readXMLTree(GraphHelper helper, org.w3c.dom.Node rootNode) {
85
// Object lock = helper.beginTransaction(false);
86
//
87
// if (rootNode != null) {
88
// String version = getAttributeValue(rootNode, "version"); // NOI18N
89
// if (!"1.0".equals(version)) // NOI18N
90
// return;
91
// HashMap map = new HashMap();
92
// org.w3c.dom.NodeList list = rootNode.getChildNodes();
93
// for (int a = 0; a < list.getLength(); a++) {
94
// org.w3c.dom.Node node = list.item(a);
95
// if ("Node".equals(node.getNodeName())) { // NOI18N
96
// try {
97
// String id = getAttributeValue(node, "id"); // NOI18N
98
// int x = Integer.parseInt(getAttributeValue(node, "x")); // NOI18N
99
// int y = Integer.parseInt(getAttributeValue(node, "y")); // NOI18N
100
// map.put(id, new Point(x, y));
101
// } catch (NumberFormatException e) {
102
// }
103
// }
104
// }
105
// final IGraphNode[] nodes = helper.getNodes();
106
// for (int i = 0; i < nodes.length; i++) {
107
// IGraphNode node = nodes[i];
108
// Point point = (Point) map.get(node.getID());
109
// if (point != null) {
110
// helper.setNodeLocation(node, point);
111
// helper.notifyNodeLocationLoaded(node);
112
// }
113
// }
114
// }
115
// helper.recalculate();
116
//
117
// helper.endTransaction(lock);
118
// }
119
//
120
//
121
//
122
//
123
//// Algorithm of XMLSerializerTest.writeXMLTree method:
124
////
125
////nodes[] = helper.getNodes (); // array of nodes in the view
126
////for (node : nodes) { // for each node
127
//// id = node.getID (); // get its id
128
//// location = helper.getNodeLocation (node); // get its location
129
//// write_id_and_location_variable_in_your_xml_document (xmlDocument, id,
130
////location); // write id, location pair into xml
131
////}
132
// public static synchronized org.w3c.dom.Node writeXMLTree(GraphHelper helper, org.w3c.dom.Document doc, String rootNodeName) {
133
// org.w3c.dom.Node rootNode = rootNodeName != null ? doc.createElement(rootNodeName) : doc.getFirstChild();
134
// setAttribute(doc, rootNode, "version", "1.0"); // NOI18N
135
// final IGraphNode[] nodes = helper.getNodes();
136
// for (int i = 0; i < nodes.length; i++) {
137
// IGraphNode node = nodes[i];
138
// String id = node.getID();
139
// Point location = helper.getNodeLocation(node);
140
// if (id == null || ! helper.isNodeLocationResolved(node))
141
// continue;
142
// org.w3c.dom.Node dn = doc.createElement("Node"); // NOI18N
143
// setAttribute(doc, dn, "id", id); // NOI18N
144
// setAttribute(doc, dn, "x", Integer.toString(location.x)); // NOI18N
145
// setAttribute(doc, dn, "y", Integer.toString(location.y)); // NOI18N
146
// rootNode.appendChild(dn);
147
// }
148
// return rootNode;
149
// }
150
//
151
// public static Document readXMLDocument(InputStream is) throws IOException {
152
// Document doc = null;
153
// InputSource inputSource = new InputSource(is);
154
// try {
155
// doc = XMLUtil.parse(inputSource, false, false, new ErrorHandler() {
156
// public void error(SAXParseException e) throws SAXException {
157
// throw new SAXException(e);
158
// }
159
//
160
// public void fatalError(SAXParseException e) throws SAXException {
161
// throw new SAXException(e);
162
// }
163
//
164
// public void warning(SAXParseException e) throws SAXException {
165
// e.printStackTrace();
166
// }
167
// }, null);
168
// } catch (SAXException e) {
169
// e.printStackTrace();
170
// } finally {
171
// try {
172
// is.close();
173
// } catch (IOException e) {
174
// e.printStackTrace();
175
// }
176
// }
177
// return doc;
178
// }
179
//
180
// public static void writeXMLDocument(OutputStream os, Document doc) throws IOException {
181
// try {
182
// XMLUtil.write(doc, os, "UTF-8"); // NOI18N
183
// } finally {
184
// if (os != null)
185
// try {
186
// os.close();
187
// } catch (IOException e) {
188
// }
189
// }
190
// }
191

192 }
193
Popular Tags