~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/core/xml/XMLBasicSerialization.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
 *
 
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.
 
11
 *
 
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.
 
15
 */
 
16
 
 
17
/*
 
18
 * XMLBasicSerialization.java
 
19
 * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.core.xml;
 
24
 
 
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;
 
33
import java.util.Map;
 
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;
 
39
 
 
40
import javax.swing.DefaultListModel;
 
41
 
 
42
import org.w3c.dom.Element;
 
43
 
 
44
/**
 
45
 * This serializer contains some read/write methods for common classes that
 
46
 * are not beans-conform. Currently supported are:
 
47
 * <ul>
 
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>
 
58
 * </ul>
 
59
 *
 
60
 * Weka classes:
 
61
 * <ul>
 
62
 *    <li>weka.core.Matrix</li>
 
63
 *    <li>weka.core.matrix.Matrix</li>
 
64
 * </ul>
 
65
 * 
 
66
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
67
 * @version $Revision: 1.5 $ 
 
68
 */
 
69
public class XMLBasicSerialization extends XMLSerialization {
 
70
 
 
71
   /** the value for mapping, e.g., Maps */
 
72
   public final static String VAL_MAPPING = "mapping";
 
73
 
 
74
   /** the value for a mapping-key, e.g., Maps */
 
75
   public final static String VAL_KEY = "key";
 
76
 
 
77
   /** the value for mapping-value, e.g., Maps */
 
78
   public final static String VAL_VALUE = "value";
 
79
 
 
80
   /** the matrix cells */
 
81
   public final static String VAL_CELLS = "cells";
 
82
 
 
83
   /**
 
84
    * initializes the serialization
 
85
    * 
 
86
    * @throws Exception if initialization fails
 
87
    */
 
88
   public XMLBasicSerialization() throws Exception {
 
89
      super();
 
90
   }
 
91
   
 
92
   /**
 
93
    * generates internally a new XML document and clears also the IgnoreList
 
94
    * and the mappings for the Read/Write-Methods
 
95
    * 
 
96
    * @throws Exception if initializing fails
 
97
    */
 
98
   public void clear() throws Exception {
 
99
      super.clear();
 
100
      
 
101
      // Java classes
 
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");
 
112
 
 
113
      // Weka classes
 
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");
 
117
   }
 
118
   
 
119
   /**
 
120
    * adds the given DefaultListModel to a DOM structure. 
 
121
    * 
 
122
    * @param parent the parent of this object, e.g. the class this object is a
 
123
    * member of
 
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
 
129
    */
 
130
   public Element writeDefaultListModel(Element parent, Object o, String name) 
 
131
      throws Exception {
 
132
 
 
133
      Element              node;
 
134
      int                  i;
 
135
      DefaultListModel     model;
 
136
 
 
137
      // for debugging only
 
138
      if (DEBUG)
 
139
         trace(new Throwable(), name);
 
140
      
 
141
      m_CurrentNode = parent;
 
142
      
 
143
      model = (DefaultListModel) o;
 
144
      node = addElement(parent, name, o.getClass().getName(), false);
 
145
 
 
146
      for (i = 0; i < model.getSize(); i++)
 
147
         invokeWriteToXML(node, model.get(i), Integer.toString(i));
 
148
      
 
149
      return node;
 
150
   }
 
151
 
 
152
   /**
 
153
    * builds the DefaultListModel from the given DOM node. 
 
154
    * 
 
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
 
159
    */
 
160
   public Object readDefaultListModel(Element node) throws Exception {
 
161
      DefaultListModel     model;
 
162
      Vector               children;
 
163
      Element              child;
 
164
      int                  i;
 
165
      int                  index;
 
166
      int                  currIndex;
 
167
 
 
168
      // for debugging only
 
169
      if (DEBUG)
 
170
         trace(new Throwable(), node.getAttribute(ATT_NAME));
 
171
 
 
172
      m_CurrentNode = node;
 
173
      
 
174
      children = XMLDocument.getChildTags(node); 
 
175
      model    = new DefaultListModel();
 
176
      
 
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)
 
183
          index = currIndex;
 
184
      }
 
185
      model.setSize(index + 1);
 
186
 
 
187
      // set values
 
188
      for (i = 0; i < children.size(); i++) {
 
189
         child = (Element) children.get(i);
 
190
         model.set(
 
191
             Integer.parseInt(child.getAttribute(ATT_NAME)), 
 
192
             invokeReadFromXML(child));
 
193
      }
 
194
      
 
195
      return model;
 
196
   }
 
197
   
 
198
   /**
 
199
    * adds the given Collection to a DOM structure. 
 
200
    * 
 
201
    * @param parent the parent of this object, e.g. the class this object is a
 
202
    * member of
 
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
 
208
    */
 
209
   public Element writeCollection(Element parent, Object o, String name) 
 
210
      throws Exception {
 
211
 
 
212
      Element         node;
 
213
      Iterator        iter;
 
214
      int             i;
 
215
 
 
216
      // for debugging only
 
217
      if (DEBUG)
 
218
         trace(new Throwable(), name);
 
219
      
 
220
      m_CurrentNode = parent;
 
221
      
 
222
      iter = ((Collection) o).iterator();
 
223
      node = addElement(parent, name, o.getClass().getName(), false);
 
224
 
 
225
      i = 0;
 
226
      while (iter.hasNext()) {
 
227
         invokeWriteToXML(node, iter.next(), Integer.toString(i));
 
228
         i++;
 
229
      }
 
230
      
 
231
      return node;
 
232
   }
 
233
 
 
234
   /**
 
235
    * builds the Collection from the given DOM node. 
 
236
    * 
 
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
 
241
    */
 
242
   public Object readCollection(Element node) throws Exception {
 
243
      Collection           coll;
 
244
      Vector               v;
 
245
      Vector               children;
 
246
      Element              child;
 
247
      int                  i;
 
248
      int                  index;
 
249
      int                  currIndex;
 
250
 
 
251
      // for debugging only
 
252
      if (DEBUG)
 
253
         trace(new Throwable(), node.getAttribute(ATT_NAME));
 
254
 
 
255
      m_CurrentNode = node;
 
256
      
 
257
      children = XMLDocument.getChildTags(node); 
 
258
      v        = new Vector();
 
259
 
 
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)
 
266
          index = currIndex;
 
267
      }
 
268
      v.setSize(index + 1);
 
269
 
 
270
 
 
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);
 
274
         v.set(
 
275
               Integer.parseInt(child.getAttribute(ATT_NAME)), 
 
276
               invokeReadFromXML(child));
 
277
      }
 
278
      
 
279
      // populate collection
 
280
      coll = (Collection) Class.forName(
 
281
                  node.getAttribute(ATT_CLASS)).newInstance();
 
282
      coll.addAll(v);
 
283
      
 
284
      return coll;
 
285
   }
 
286
   
 
287
   /**
 
288
    * adds the given Map to a DOM structure. 
 
289
    * 
 
290
    * @param parent the parent of this object, e.g. the class this object is a
 
291
    * member of
 
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
 
296
    * @see java.util.Map
 
297
    */
 
298
   public Element writeMap(Element parent, Object o, String name) 
 
299
      throws Exception {
 
300
 
 
301
      Map            map;
 
302
      Object         key;
 
303
      Element        node;
 
304
      Element        child;
 
305
      Iterator       iter;
 
306
 
 
307
      // for debugging only
 
308
      if (DEBUG)
 
309
         trace(new Throwable(), name);
 
310
      
 
311
      m_CurrentNode = parent;
 
312
      
 
313
      map  = (Map) o;
 
314
      iter = map.keySet().iterator();
 
315
      node = addElement(parent, name, o.getClass().getName(), false);
 
316
 
 
317
      while (iter.hasNext()) {
 
318
         key   = iter.next();
 
319
         child = addElement(
 
320
                     node, VAL_MAPPING, Object.class.getName(), false);
 
321
         invokeWriteToXML(child, key,          VAL_KEY);
 
322
         invokeWriteToXML(child, map.get(key), VAL_VALUE);
 
323
      }
 
324
      
 
325
      return node;
 
326
   }
 
327
 
 
328
   /**
 
329
    * builds the Map from the given DOM node. 
 
330
    * 
 
331
    * @param node the associated XML node
 
332
    * @return the instance created from the XML description
 
333
    * @throws Exception if instantiation fails 
 
334
    * @see java.util.Map
 
335
    */
 
336
   public Object readMap(Element node) throws Exception {
 
337
      Map                  map;
 
338
      Object               key;
 
339
      Object               value;
 
340
      Vector               children;
 
341
      Vector               cchildren;
 
342
      Element              child;
 
343
      Element              cchild;
 
344
      int                  i;
 
345
      int                  n;
 
346
      String               name;
 
347
 
 
348
      // for debugging only
 
349
      if (DEBUG)
 
350
         trace(new Throwable(), node.getAttribute(ATT_NAME));
 
351
 
 
352
      m_CurrentNode = node;
 
353
      
 
354
      map      = (Map) Class.forName(
 
355
                     node.getAttribute(ATT_CLASS)).newInstance();
 
356
      children = XMLDocument.getChildTags(node); 
 
357
 
 
358
      for (i = 0; i < children.size(); i++) {
 
359
         child     = (Element) children.get(i);
 
360
         cchildren = XMLDocument.getChildTags(child);
 
361
         key       = null;
 
362
         value     = null;
 
363
         
 
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);
 
371
            else
 
372
               System.out.println("WARNING: '" 
 
373
                     + name + "' is not a recognized name for maps!");
 
374
         }
 
375
         
 
376
         map.put(key, value);
 
377
      }
 
378
      
 
379
      return map;
 
380
   }
 
381
   
 
382
   /**
 
383
    * adds the given Matrix to a DOM structure. 
 
384
    * 
 
385
    * @param parent the parent of this object, e.g. the class this object is a
 
386
    * member of
 
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
 
392
    */
 
393
   public Element writeMatrix(Element parent, Object o, String name) 
 
394
      throws Exception {
 
395
 
 
396
      weka.core.matrix.Matrix    matrix;
 
397
      Element                    node;
 
398
 
 
399
      // for debugging only
 
400
      if (DEBUG)
 
401
         trace(new Throwable(), name);
 
402
      
 
403
      m_CurrentNode = parent;
 
404
      
 
405
      matrix = (weka.core.matrix.Matrix) o;
 
406
      node   = addElement(parent, name, o.getClass().getName(), false);
 
407
 
 
408
      invokeWriteToXML(node, matrix.getArray(), VAL_CELLS);
 
409
      
 
410
      return node;
 
411
   }
 
412
 
 
413
   /**
 
414
    * builds the Matrix from the given DOM node. 
 
415
    * 
 
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
 
420
    */
 
421
   public Object readMatrix(Element node) throws Exception {
 
422
      weka.core.matrix.Matrix    matrix;
 
423
      Vector                     children;
 
424
      Element                    child;
 
425
      int                        i;
 
426
      String                     name;
 
427
      Object                     o;
 
428
 
 
429
      // for debugging only
 
430
      if (DEBUG)
 
431
         trace(new Throwable(), node.getAttribute(ATT_NAME));
 
432
 
 
433
      m_CurrentNode = node;
 
434
      
 
435
      matrix   = null;
 
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);
 
440
 
 
441
         if (name.equals(VAL_CELLS)) {
 
442
            o = invokeReadFromXML(child);
 
443
            matrix = new weka.core.matrix.Matrix(
 
444
                        (double[][]) o);
 
445
         }
 
446
      }
 
447
      
 
448
      return matrix;
 
449
   }
 
450
   
 
451
   /**
 
452
    * adds the given Matrix (old) to a DOM structure. 
 
453
    * 
 
454
    * @param parent the parent of this object, e.g. the class this object is a
 
455
    * member of
 
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
 
461
    */
 
462
   public Element writeMatrixOld(Element parent, Object o, String name) 
 
463
      throws Exception {
 
464
 
 
465
      weka.core.Matrix  matrix;
 
466
      Element           node;
 
467
      double[][]        array;
 
468
      int               i;
 
469
 
 
470
      // for debugging only
 
471
      if (DEBUG)
 
472
         trace(new Throwable(), name);
 
473
      
 
474
      m_CurrentNode = parent;
 
475
      
 
476
      matrix = (weka.core.Matrix) o;
 
477
      node   = addElement(parent, name, o.getClass().getName(), false);
 
478
 
 
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);
 
483
      
 
484
      return node;
 
485
   }
 
486
 
 
487
   /**
 
488
    * builds the Matrix (old) from the given DOM node. 
 
489
    * 
 
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
 
494
    */
 
495
   public Object readMatrixOld(Element node) throws Exception {
 
496
      weka.core.Matrix           matrix;
 
497
      weka.core.matrix.Matrix    matrixNew;
 
498
 
 
499
      // for debugging only
 
500
      if (DEBUG)
 
501
         trace(new Throwable(), node.getAttribute(ATT_NAME));
 
502
 
 
503
      m_CurrentNode = node;
 
504
      
 
505
      matrixNew = (weka.core.matrix.Matrix) readMatrix(node);
 
506
      matrix    = new weka.core.Matrix(matrixNew.getArrayCopy());
 
507
      
 
508
      return matrix;
 
509
   }
 
510
   
 
511
   /**
 
512
    * adds the given CostMatrix (old) to a DOM structure. 
 
513
    * 
 
514
    * @param parent the parent of this object, e.g. the class this object is a
 
515
    * member of
 
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
 
521
    */
 
522
   public Element writeCostMatrixOld(Element parent, Object o, String name) 
 
523
      throws Exception {
 
524
 
 
525
      // for debugging only
 
526
      if (DEBUG)
 
527
         trace(new Throwable(), name);
 
528
      
 
529
      m_CurrentNode = parent;
 
530
      
 
531
      return writeMatrixOld(parent, o, name);
 
532
   }
 
533
 
 
534
   /**
 
535
    * builds the Matrix (old) from the given DOM node. 
 
536
    * 
 
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
 
541
    */
 
542
   public Object readCostMatrixOld(Element node) throws Exception {
 
543
      weka.classifiers.CostMatrix   matrix;
 
544
      weka.core.matrix.Matrix       matrixNew;
 
545
      StringWriter                  writer;
 
546
 
 
547
      // for debugging only
 
548
      if (DEBUG)
 
549
         trace(new Throwable(), node.getAttribute(ATT_NAME));
 
550
 
 
551
      m_CurrentNode = node;
 
552
      
 
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()));
 
557
      
 
558
      return matrix;
 
559
   }
 
560
}