2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* XMLBasicSerialization.java
19
* Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
23
package weka.core.xml;
25
import java.io.StringReader;
26
import java.io.StringWriter;
27
import java.util.Collection;
28
import java.util.HashMap;
29
import java.util.HashSet;
30
import java.util.Hashtable;
31
import java.util.Iterator;
32
import java.util.LinkedList;
34
import java.util.Properties;
35
import java.util.Stack;
36
import java.util.TreeMap;
37
import java.util.TreeSet;
38
import java.util.Vector;
40
import javax.swing.DefaultListModel;
42
import org.w3c.dom.Element;
45
* This serializer contains some read/write methods for common classes that
46
* are not beans-conform. Currently supported are:
48
* <li>java.util.HashMap</li>
49
* <li>java.util.HashSet</li>
50
* <li>java.util.Hashtable</li>
51
* <li>java.util.LinkedList</li>
52
* <li>java.util.Properties</li>
53
* <li>java.util.Stack</li>
54
* <li>java.util.TreeMap</li>
55
* <li>java.util.TreeSet</li>
56
* <li>java.util.Vector</li>
57
* <li>javax.swing.DefaultListModel</li>
62
* <li>weka.core.Matrix</li>
63
* <li>weka.core.matrix.Matrix</li>
66
* @author FracPete (fracpete at waikato dot ac dot nz)
67
* @version $Revision: 1.5 $
69
public class XMLBasicSerialization extends XMLSerialization {
71
/** the value for mapping, e.g., Maps */
72
public final static String VAL_MAPPING = "mapping";
74
/** the value for a mapping-key, e.g., Maps */
75
public final static String VAL_KEY = "key";
77
/** the value for mapping-value, e.g., Maps */
78
public final static String VAL_VALUE = "value";
80
/** the matrix cells */
81
public final static String VAL_CELLS = "cells";
84
* initializes the serialization
86
* @throws Exception if initialization fails
88
public XMLBasicSerialization() throws Exception {
93
* generates internally a new XML document and clears also the IgnoreList
94
* and the mappings for the Read/Write-Methods
96
* @throws Exception if initializing fails
98
public void clear() throws Exception {
102
m_CustomMethods.register(this, DefaultListModel.class, "DefaultListModel");
103
m_CustomMethods.register(this, HashMap.class, "Map");
104
m_CustomMethods.register(this, HashSet.class, "Collection");
105
m_CustomMethods.register(this, Hashtable.class, "Map");
106
m_CustomMethods.register(this, LinkedList.class, "Collection");
107
m_CustomMethods.register(this, Properties.class, "Map");
108
m_CustomMethods.register(this, Stack.class, "Collection");
109
m_CustomMethods.register(this, TreeMap.class, "Map");
110
m_CustomMethods.register(this, TreeSet.class, "Collection");
111
m_CustomMethods.register(this, Vector.class, "Collection");
114
m_CustomMethods.register(this, weka.core.matrix.Matrix.class, "Matrix");
115
m_CustomMethods.register(this, weka.core.Matrix.class, "MatrixOld");
116
m_CustomMethods.register(this, weka.classifiers.CostMatrix.class, "CostMatrixOld");
120
* adds the given DefaultListModel to a DOM structure.
122
* @param parent the parent of this object, e.g. the class this object is a
124
* @param o the Object to describe in XML
125
* @param name the name of the object
126
* @return the node that was created
127
* @throws Exception if the DOM creation fails
128
* @see javax.swing.DefaultListModel
130
public Element writeDefaultListModel(Element parent, Object o, String name)
135
DefaultListModel model;
137
// for debugging only
139
trace(new Throwable(), name);
141
m_CurrentNode = parent;
143
model = (DefaultListModel) o;
144
node = addElement(parent, name, o.getClass().getName(), false);
146
for (i = 0; i < model.getSize(); i++)
147
invokeWriteToXML(node, model.get(i), Integer.toString(i));
153
* builds the DefaultListModel from the given DOM node.
155
* @param node the associated XML node
156
* @return the instance created from the XML description
157
* @throws Exception if instantiation fails
158
* @see javax.swing.DefaultListModel
160
public Object readDefaultListModel(Element node) throws Exception {
161
DefaultListModel model;
168
// for debugging only
170
trace(new Throwable(), node.getAttribute(ATT_NAME));
172
m_CurrentNode = node;
174
children = XMLDocument.getChildTags(node);
175
model = new DefaultListModel();
177
// determine highest index for size
178
index = children.size() - 1;
179
for (i = 0; i < children.size(); i++) {
180
child = (Element) children.get(i);
181
currIndex = Integer.parseInt(child.getAttribute(ATT_NAME));
182
if (currIndex > index)
185
model.setSize(index + 1);
188
for (i = 0; i < children.size(); i++) {
189
child = (Element) children.get(i);
191
Integer.parseInt(child.getAttribute(ATT_NAME)),
192
invokeReadFromXML(child));
199
* adds the given Collection to a DOM structure.
201
* @param parent the parent of this object, e.g. the class this object is a
203
* @param o the Object to describe in XML
204
* @param name the name of the object
205
* @return the node that was created
206
* @throws Exception if the DOM creation fails
207
* @see java.util.Collection
209
public Element writeCollection(Element parent, Object o, String name)
216
// for debugging only
218
trace(new Throwable(), name);
220
m_CurrentNode = parent;
222
iter = ((Collection) o).iterator();
223
node = addElement(parent, name, o.getClass().getName(), false);
226
while (iter.hasNext()) {
227
invokeWriteToXML(node, iter.next(), Integer.toString(i));
235
* builds the Collection from the given DOM node.
237
* @param node the associated XML node
238
* @return the instance created from the XML description
239
* @throws Exception if instantiation fails
240
* @see java.util.Collection
242
public Object readCollection(Element node) throws Exception {
251
// for debugging only
253
trace(new Throwable(), node.getAttribute(ATT_NAME));
255
m_CurrentNode = node;
257
children = XMLDocument.getChildTags(node);
260
// determine highest index for size
261
index = children.size() - 1;
262
for (i = 0; i < children.size(); i++) {
263
child = (Element) children.get(i);
264
currIndex = Integer.parseInt(child.getAttribute(ATT_NAME));
265
if (currIndex > index)
268
v.setSize(index + 1);
271
// put the children in the vector to sort them according their index
272
for (i = 0; i < children.size(); i++) {
273
child = (Element) children.get(i);
275
Integer.parseInt(child.getAttribute(ATT_NAME)),
276
invokeReadFromXML(child));
279
// populate collection
280
coll = (Collection) Class.forName(
281
node.getAttribute(ATT_CLASS)).newInstance();
288
* adds the given Map to a DOM structure.
290
* @param parent the parent of this object, e.g. the class this object is a
292
* @param o the Object to describe in XML
293
* @param name the name of the object
294
* @return the node that was created
295
* @throws Exception if the DOM creation fails
298
public Element writeMap(Element parent, Object o, String name)
307
// for debugging only
309
trace(new Throwable(), name);
311
m_CurrentNode = parent;
314
iter = map.keySet().iterator();
315
node = addElement(parent, name, o.getClass().getName(), false);
317
while (iter.hasNext()) {
320
node, VAL_MAPPING, Object.class.getName(), false);
321
invokeWriteToXML(child, key, VAL_KEY);
322
invokeWriteToXML(child, map.get(key), VAL_VALUE);
329
* builds the Map from the given DOM node.
331
* @param node the associated XML node
332
* @return the instance created from the XML description
333
* @throws Exception if instantiation fails
336
public Object readMap(Element node) throws Exception {
348
// for debugging only
350
trace(new Throwable(), node.getAttribute(ATT_NAME));
352
m_CurrentNode = node;
354
map = (Map) Class.forName(
355
node.getAttribute(ATT_CLASS)).newInstance();
356
children = XMLDocument.getChildTags(node);
358
for (i = 0; i < children.size(); i++) {
359
child = (Element) children.get(i);
360
cchildren = XMLDocument.getChildTags(child);
364
for (n = 0; n < cchildren.size(); n++) {
365
cchild = (Element) cchildren.get(n);
366
name = cchild.getAttribute(ATT_NAME);
367
if (name.equals(VAL_KEY))
368
key = invokeReadFromXML(cchild);
369
else if (name.equals(VAL_VALUE))
370
value = invokeReadFromXML(cchild);
372
System.out.println("WARNING: '"
373
+ name + "' is not a recognized name for maps!");
383
* adds the given Matrix to a DOM structure.
385
* @param parent the parent of this object, e.g. the class this object is a
387
* @param o the Object to describe in XML
388
* @param name the name of the object
389
* @return the node that was created
390
* @throws Exception if the DOM creation fails
391
* @see weka.core.matrix.Matrix
393
public Element writeMatrix(Element parent, Object o, String name)
396
weka.core.matrix.Matrix matrix;
399
// for debugging only
401
trace(new Throwable(), name);
403
m_CurrentNode = parent;
405
matrix = (weka.core.matrix.Matrix) o;
406
node = addElement(parent, name, o.getClass().getName(), false);
408
invokeWriteToXML(node, matrix.getArray(), VAL_CELLS);
414
* builds the Matrix from the given DOM node.
416
* @param node the associated XML node
417
* @return the instance created from the XML description
418
* @throws Exception if instantiation fails
419
* @see weka.core.matrix.Matrix
421
public Object readMatrix(Element node) throws Exception {
422
weka.core.matrix.Matrix matrix;
429
// for debugging only
431
trace(new Throwable(), node.getAttribute(ATT_NAME));
433
m_CurrentNode = node;
436
children = XMLDocument.getChildTags(node);
437
for (i = 0; i < children.size(); i++) {
438
child = (Element) children.get(i);
439
name = child.getAttribute(ATT_NAME);
441
if (name.equals(VAL_CELLS)) {
442
o = invokeReadFromXML(child);
443
matrix = new weka.core.matrix.Matrix(
452
* adds the given Matrix (old) to a DOM structure.
454
* @param parent the parent of this object, e.g. the class this object is a
456
* @param o the Object to describe in XML
457
* @param name the name of the object
458
* @return the node that was created
459
* @throws Exception if the DOM creation fails
460
* @see weka.core.Matrix
462
public Element writeMatrixOld(Element parent, Object o, String name)
465
weka.core.Matrix matrix;
470
// for debugging only
472
trace(new Throwable(), name);
474
m_CurrentNode = parent;
476
matrix = (weka.core.Matrix) o;
477
node = addElement(parent, name, o.getClass().getName(), false);
479
array = new double[matrix.numRows()][];
480
for (i = 0; i < array.length; i++)
481
array[i] = matrix.getRow(i);
482
invokeWriteToXML(node, array, VAL_CELLS);
488
* builds the Matrix (old) from the given DOM node.
490
* @param node the associated XML node
491
* @return the instance created from the XML description
492
* @throws Exception if instantiation fails
493
* @see weka.core.Matrix
495
public Object readMatrixOld(Element node) throws Exception {
496
weka.core.Matrix matrix;
497
weka.core.matrix.Matrix matrixNew;
499
// for debugging only
501
trace(new Throwable(), node.getAttribute(ATT_NAME));
503
m_CurrentNode = node;
505
matrixNew = (weka.core.matrix.Matrix) readMatrix(node);
506
matrix = new weka.core.Matrix(matrixNew.getArrayCopy());
512
* adds the given CostMatrix (old) to a DOM structure.
514
* @param parent the parent of this object, e.g. the class this object is a
516
* @param o the Object to describe in XML
517
* @param name the name of the object
518
* @return the node that was created
519
* @throws Exception if the DOM creation fails
520
* @see weka.classifiers.CostMatrix
522
public Element writeCostMatrixOld(Element parent, Object o, String name)
525
// for debugging only
527
trace(new Throwable(), name);
529
m_CurrentNode = parent;
531
return writeMatrixOld(parent, o, name);
535
* builds the Matrix (old) from the given DOM node.
537
* @param node the associated XML node
538
* @return the instance created from the XML description
539
* @throws Exception if instantiation fails
540
* @see weka.classifiers.CostMatrix
542
public Object readCostMatrixOld(Element node) throws Exception {
543
weka.classifiers.CostMatrix matrix;
544
weka.core.matrix.Matrix matrixNew;
547
// for debugging only
549
trace(new Throwable(), node.getAttribute(ATT_NAME));
551
m_CurrentNode = node;
553
matrixNew = (weka.core.matrix.Matrix) readMatrix(node);
554
writer = new StringWriter();
555
matrixNew.write(writer);
556
matrix = new weka.classifiers.CostMatrix(new StringReader(writer.toString()));