PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/weka-3-6-9/weka-src/src/main/java/weka/core/xml/XMLBasicSerialization.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 572 lines | 279 code | 95 blank | 198 comment | 29 complexity | d3f6ae2bcd0d9b6c6d344ffb4be7ef60 MD5 | raw file
  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. * XMLBasicSerialization.java
  18. * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
  19. *
  20. */
  21. package weka.core.xml;
  22. import weka.core.RevisionUtils;
  23. import java.io.StringReader;
  24. import java.io.StringWriter;
  25. import java.util.Collection;
  26. import java.util.HashMap;
  27. import java.util.HashSet;
  28. import java.util.Hashtable;
  29. import java.util.Iterator;
  30. import java.util.LinkedList;
  31. import java.util.Map;
  32. import java.util.Properties;
  33. import java.util.Stack;
  34. import java.util.TreeMap;
  35. import java.util.TreeSet;
  36. import java.util.Vector;
  37. import javax.swing.DefaultListModel;
  38. import org.w3c.dom.Element;
  39. /**
  40. * This serializer contains some read/write methods for common classes that
  41. * are not beans-conform. Currently supported are:
  42. * <ul>
  43. * <li>java.util.HashMap</li>
  44. * <li>java.util.HashSet</li>
  45. * <li>java.util.Hashtable</li>
  46. * <li>java.util.LinkedList</li>
  47. * <li>java.util.Properties</li>
  48. * <li>java.util.Stack</li>
  49. * <li>java.util.TreeMap</li>
  50. * <li>java.util.TreeSet</li>
  51. * <li>java.util.Vector</li>
  52. * <li>javax.swing.DefaultListModel</li>
  53. * </ul>
  54. *
  55. * Weka classes:
  56. * <ul>
  57. * <li>weka.core.Matrix</li>
  58. * <li>weka.core.matrix.Matrix</li>
  59. * </ul>
  60. *
  61. * @author FracPete (fracpete at waikato dot ac dot nz)
  62. * @version $Revision: 1.6 $
  63. */
  64. public class XMLBasicSerialization
  65. extends XMLSerialization {
  66. /** the value for mapping, e.g., Maps */
  67. public final static String VAL_MAPPING = "mapping";
  68. /** the value for a mapping-key, e.g., Maps */
  69. public final static String VAL_KEY = "key";
  70. /** the value for mapping-value, e.g., Maps */
  71. public final static String VAL_VALUE = "value";
  72. /** the matrix cells */
  73. public final static String VAL_CELLS = "cells";
  74. /**
  75. * initializes the serialization
  76. *
  77. * @throws Exception if initialization fails
  78. */
  79. public XMLBasicSerialization() throws Exception {
  80. super();
  81. }
  82. /**
  83. * generates internally a new XML document and clears also the IgnoreList
  84. * and the mappings for the Read/Write-Methods
  85. *
  86. * @throws Exception if initializing fails
  87. */
  88. public void clear() throws Exception {
  89. super.clear();
  90. // Java classes
  91. m_CustomMethods.register(this, DefaultListModel.class, "DefaultListModel");
  92. m_CustomMethods.register(this, HashMap.class, "Map");
  93. m_CustomMethods.register(this, HashSet.class, "Collection");
  94. m_CustomMethods.register(this, Hashtable.class, "Map");
  95. m_CustomMethods.register(this, LinkedList.class, "Collection");
  96. m_CustomMethods.register(this, Properties.class, "Map");
  97. m_CustomMethods.register(this, Stack.class, "Collection");
  98. m_CustomMethods.register(this, TreeMap.class, "Map");
  99. m_CustomMethods.register(this, TreeSet.class, "Collection");
  100. m_CustomMethods.register(this, Vector.class, "Collection");
  101. // Weka classes
  102. m_CustomMethods.register(this, weka.core.matrix.Matrix.class, "Matrix");
  103. m_CustomMethods.register(this, weka.core.Matrix.class, "MatrixOld");
  104. m_CustomMethods.register(this, weka.classifiers.CostMatrix.class, "CostMatrixOld");
  105. }
  106. /**
  107. * adds the given DefaultListModel to a DOM structure.
  108. *
  109. * @param parent the parent of this object, e.g. the class this object is a
  110. * member of
  111. * @param o the Object to describe in XML
  112. * @param name the name of the object
  113. * @return the node that was created
  114. * @throws Exception if the DOM creation fails
  115. * @see javax.swing.DefaultListModel
  116. */
  117. public Element writeDefaultListModel(Element parent, Object o, String name)
  118. throws Exception {
  119. Element node;
  120. int i;
  121. DefaultListModel model;
  122. // for debugging only
  123. if (DEBUG)
  124. trace(new Throwable(), name);
  125. m_CurrentNode = parent;
  126. model = (DefaultListModel) o;
  127. node = addElement(parent, name, o.getClass().getName(), false);
  128. for (i = 0; i < model.getSize(); i++)
  129. invokeWriteToXML(node, model.get(i), Integer.toString(i));
  130. return node;
  131. }
  132. /**
  133. * builds the DefaultListModel from the given DOM node.
  134. *
  135. * @param node the associated XML node
  136. * @return the instance created from the XML description
  137. * @throws Exception if instantiation fails
  138. * @see javax.swing.DefaultListModel
  139. */
  140. public Object readDefaultListModel(Element node) throws Exception {
  141. DefaultListModel model;
  142. Vector children;
  143. Element child;
  144. int i;
  145. int index;
  146. int currIndex;
  147. // for debugging only
  148. if (DEBUG)
  149. trace(new Throwable(), node.getAttribute(ATT_NAME));
  150. m_CurrentNode = node;
  151. children = XMLDocument.getChildTags(node);
  152. model = new DefaultListModel();
  153. // determine highest index for size
  154. index = children.size() - 1;
  155. for (i = 0; i < children.size(); i++) {
  156. child = (Element) children.get(i);
  157. currIndex = Integer.parseInt(child.getAttribute(ATT_NAME));
  158. if (currIndex > index)
  159. index = currIndex;
  160. }
  161. model.setSize(index + 1);
  162. // set values
  163. for (i = 0; i < children.size(); i++) {
  164. child = (Element) children.get(i);
  165. model.set(
  166. Integer.parseInt(child.getAttribute(ATT_NAME)),
  167. invokeReadFromXML(child));
  168. }
  169. return model;
  170. }
  171. /**
  172. * adds the given Collection to a DOM structure.
  173. *
  174. * @param parent the parent of this object, e.g. the class this object is a
  175. * member of
  176. * @param o the Object to describe in XML
  177. * @param name the name of the object
  178. * @return the node that was created
  179. * @throws Exception if the DOM creation fails
  180. * @see java.util.Collection
  181. */
  182. public Element writeCollection(Element parent, Object o, String name)
  183. throws Exception {
  184. Element node;
  185. Iterator iter;
  186. int i;
  187. // for debugging only
  188. if (DEBUG)
  189. trace(new Throwable(), name);
  190. m_CurrentNode = parent;
  191. iter = ((Collection) o).iterator();
  192. node = addElement(parent, name, o.getClass().getName(), false);
  193. i = 0;
  194. while (iter.hasNext()) {
  195. invokeWriteToXML(node, iter.next(), Integer.toString(i));
  196. i++;
  197. }
  198. return node;
  199. }
  200. /**
  201. * builds the Collection from the given DOM node.
  202. *
  203. * @param node the associated XML node
  204. * @return the instance created from the XML description
  205. * @throws Exception if instantiation fails
  206. * @see java.util.Collection
  207. */
  208. public Object readCollection(Element node) throws Exception {
  209. Collection coll;
  210. Vector v;
  211. Vector children;
  212. Element child;
  213. int i;
  214. int index;
  215. int currIndex;
  216. // for debugging only
  217. if (DEBUG)
  218. trace(new Throwable(), node.getAttribute(ATT_NAME));
  219. m_CurrentNode = node;
  220. children = XMLDocument.getChildTags(node);
  221. v = new Vector();
  222. // determine highest index for size
  223. index = children.size() - 1;
  224. for (i = 0; i < children.size(); i++) {
  225. child = (Element) children.get(i);
  226. currIndex = Integer.parseInt(child.getAttribute(ATT_NAME));
  227. if (currIndex > index)
  228. index = currIndex;
  229. }
  230. v.setSize(index + 1);
  231. // put the children in the vector to sort them according their index
  232. for (i = 0; i < children.size(); i++) {
  233. child = (Element) children.get(i);
  234. v.set(
  235. Integer.parseInt(child.getAttribute(ATT_NAME)),
  236. invokeReadFromXML(child));
  237. }
  238. // populate collection
  239. coll = (Collection) Class.forName(
  240. node.getAttribute(ATT_CLASS)).newInstance();
  241. coll.addAll(v);
  242. return coll;
  243. }
  244. /**
  245. * adds the given Map to a DOM structure.
  246. *
  247. * @param parent the parent of this object, e.g. the class this object is a
  248. * member of
  249. * @param o the Object to describe in XML
  250. * @param name the name of the object
  251. * @return the node that was created
  252. * @throws Exception if the DOM creation fails
  253. * @see java.util.Map
  254. */
  255. public Element writeMap(Element parent, Object o, String name)
  256. throws Exception {
  257. Map map;
  258. Object key;
  259. Element node;
  260. Element child;
  261. Iterator iter;
  262. // for debugging only
  263. if (DEBUG)
  264. trace(new Throwable(), name);
  265. m_CurrentNode = parent;
  266. map = (Map) o;
  267. iter = map.keySet().iterator();
  268. node = addElement(parent, name, o.getClass().getName(), false);
  269. while (iter.hasNext()) {
  270. key = iter.next();
  271. child = addElement(
  272. node, VAL_MAPPING, Object.class.getName(), false);
  273. invokeWriteToXML(child, key, VAL_KEY);
  274. invokeWriteToXML(child, map.get(key), VAL_VALUE);
  275. }
  276. return node;
  277. }
  278. /**
  279. * builds the Map from the given DOM node.
  280. *
  281. * @param node the associated XML node
  282. * @return the instance created from the XML description
  283. * @throws Exception if instantiation fails
  284. * @see java.util.Map
  285. */
  286. public Object readMap(Element node) throws Exception {
  287. Map map;
  288. Object key;
  289. Object value;
  290. Vector children;
  291. Vector cchildren;
  292. Element child;
  293. Element cchild;
  294. int i;
  295. int n;
  296. String name;
  297. // for debugging only
  298. if (DEBUG)
  299. trace(new Throwable(), node.getAttribute(ATT_NAME));
  300. m_CurrentNode = node;
  301. map = (Map) Class.forName(
  302. node.getAttribute(ATT_CLASS)).newInstance();
  303. children = XMLDocument.getChildTags(node);
  304. for (i = 0; i < children.size(); i++) {
  305. child = (Element) children.get(i);
  306. cchildren = XMLDocument.getChildTags(child);
  307. key = null;
  308. value = null;
  309. for (n = 0; n < cchildren.size(); n++) {
  310. cchild = (Element) cchildren.get(n);
  311. name = cchild.getAttribute(ATT_NAME);
  312. if (name.equals(VAL_KEY))
  313. key = invokeReadFromXML(cchild);
  314. else if (name.equals(VAL_VALUE))
  315. value = invokeReadFromXML(cchild);
  316. else
  317. System.out.println("WARNING: '"
  318. + name + "' is not a recognized name for maps!");
  319. }
  320. map.put(key, value);
  321. }
  322. return map;
  323. }
  324. /**
  325. * adds the given Matrix to a DOM structure.
  326. *
  327. * @param parent the parent of this object, e.g. the class this object is a
  328. * member of
  329. * @param o the Object to describe in XML
  330. * @param name the name of the object
  331. * @return the node that was created
  332. * @throws Exception if the DOM creation fails
  333. * @see weka.core.matrix.Matrix
  334. */
  335. public Element writeMatrix(Element parent, Object o, String name)
  336. throws Exception {
  337. weka.core.matrix.Matrix matrix;
  338. Element node;
  339. // for debugging only
  340. if (DEBUG)
  341. trace(new Throwable(), name);
  342. m_CurrentNode = parent;
  343. matrix = (weka.core.matrix.Matrix) o;
  344. node = addElement(parent, name, o.getClass().getName(), false);
  345. invokeWriteToXML(node, matrix.getArray(), VAL_CELLS);
  346. return node;
  347. }
  348. /**
  349. * builds the Matrix from the given DOM node.
  350. *
  351. * @param node the associated XML node
  352. * @return the instance created from the XML description
  353. * @throws Exception if instantiation fails
  354. * @see weka.core.matrix.Matrix
  355. */
  356. public Object readMatrix(Element node) throws Exception {
  357. weka.core.matrix.Matrix matrix;
  358. Vector children;
  359. Element child;
  360. int i;
  361. String name;
  362. Object o;
  363. // for debugging only
  364. if (DEBUG)
  365. trace(new Throwable(), node.getAttribute(ATT_NAME));
  366. m_CurrentNode = node;
  367. matrix = null;
  368. children = XMLDocument.getChildTags(node);
  369. for (i = 0; i < children.size(); i++) {
  370. child = (Element) children.get(i);
  371. name = child.getAttribute(ATT_NAME);
  372. if (name.equals(VAL_CELLS)) {
  373. o = invokeReadFromXML(child);
  374. matrix = new weka.core.matrix.Matrix(
  375. (double[][]) o);
  376. }
  377. }
  378. return matrix;
  379. }
  380. /**
  381. * adds the given Matrix (old) to a DOM structure.
  382. *
  383. * @param parent the parent of this object, e.g. the class this object is a
  384. * member of
  385. * @param o the Object to describe in XML
  386. * @param name the name of the object
  387. * @return the node that was created
  388. * @throws Exception if the DOM creation fails
  389. * @see weka.core.Matrix
  390. */
  391. public Element writeMatrixOld(Element parent, Object o, String name)
  392. throws Exception {
  393. weka.core.Matrix matrix;
  394. Element node;
  395. double[][] array;
  396. int i;
  397. // for debugging only
  398. if (DEBUG)
  399. trace(new Throwable(), name);
  400. m_CurrentNode = parent;
  401. matrix = (weka.core.Matrix) o;
  402. node = addElement(parent, name, o.getClass().getName(), false);
  403. array = new double[matrix.numRows()][];
  404. for (i = 0; i < array.length; i++)
  405. array[i] = matrix.getRow(i);
  406. invokeWriteToXML(node, array, VAL_CELLS);
  407. return node;
  408. }
  409. /**
  410. * builds the Matrix (old) from the given DOM node.
  411. *
  412. * @param node the associated XML node
  413. * @return the instance created from the XML description
  414. * @throws Exception if instantiation fails
  415. * @see weka.core.Matrix
  416. */
  417. public Object readMatrixOld(Element node) throws Exception {
  418. weka.core.Matrix matrix;
  419. weka.core.matrix.Matrix matrixNew;
  420. // for debugging only
  421. if (DEBUG)
  422. trace(new Throwable(), node.getAttribute(ATT_NAME));
  423. m_CurrentNode = node;
  424. matrixNew = (weka.core.matrix.Matrix) readMatrix(node);
  425. matrix = new weka.core.Matrix(matrixNew.getArrayCopy());
  426. return matrix;
  427. }
  428. /**
  429. * adds the given CostMatrix (old) to a DOM structure.
  430. *
  431. * @param parent the parent of this object, e.g. the class this object is a
  432. * member of
  433. * @param o the Object to describe in XML
  434. * @param name the name of the object
  435. * @return the node that was created
  436. * @throws Exception if the DOM creation fails
  437. * @see weka.classifiers.CostMatrix
  438. */
  439. public Element writeCostMatrixOld(Element parent, Object o, String name)
  440. throws Exception {
  441. // for debugging only
  442. if (DEBUG)
  443. trace(new Throwable(), name);
  444. m_CurrentNode = parent;
  445. return writeMatrixOld(parent, o, name);
  446. }
  447. /**
  448. * builds the Matrix (old) from the given DOM node.
  449. *
  450. * @param node the associated XML node
  451. * @return the instance created from the XML description
  452. * @throws Exception if instantiation fails
  453. * @see weka.classifiers.CostMatrix
  454. */
  455. public Object readCostMatrixOld(Element node) throws Exception {
  456. weka.classifiers.CostMatrix matrix;
  457. weka.core.matrix.Matrix matrixNew;
  458. StringWriter writer;
  459. // for debugging only
  460. if (DEBUG)
  461. trace(new Throwable(), node.getAttribute(ATT_NAME));
  462. m_CurrentNode = node;
  463. matrixNew = (weka.core.matrix.Matrix) readMatrix(node);
  464. writer = new StringWriter();
  465. matrixNew.write(writer);
  466. matrix = new weka.classifiers.CostMatrix(new StringReader(writer.toString()));
  467. return matrix;
  468. }
  469. /**
  470. * Returns the revision string.
  471. *
  472. * @return the revision
  473. */
  474. public String getRevision() {
  475. return RevisionUtils.extract("$Revision: 1.6 $");
  476. }
  477. }