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.
19
* Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
22
package weka.core.xml;
25
import java.io.FileInputStream;
26
import java.io.FileOutputStream;
27
import java.io.InputStream;
28
import java.io.OutputStream;
31
* This class is a helper class for XML serialization using <a href="http://koala.ilog.fr/XML/serialization/" target="_blank">KOML</a> .
32
* KOML does not need to be present, since the class-calls are done generically via Reflection.
34
* @author FracPete (fracpete at waikato dot ac dot nz)
35
* @version $Revision 1.0$
40
* indicates whether <a href="http://koala.ilog.fr/XML/serialization/" target="_blank">KOML</a>
41
* (Koala Object Markup Language) is present
43
protected static boolean m_Present = false;
45
/** the extension for KOML files (including '.') */
46
public final static String FILE_EXTENSION = ".koml";
48
/** check for KOML statically (needs only to be done once) */
54
* checks whether the KOML is present in the class path
56
private static void checkForKOML() {
58
Class.forName("fr.dyade.koala.xml.koml.KOMLSerializer");
67
* returns whether KOML is present or not, i.e. whether the classes are in the
70
* @return whether KOML is available
72
public static boolean isPresent() {
77
* reads the XML-serialized object from the given file
78
* @param filename the file to deserialize the object from
79
* @return the deserialized object
80
* @throws Exception if something goes wrong while reading from the file
82
public static Object read(String filename) throws Exception {
83
return read(new FileInputStream(filename));
87
* reads the XML-serialized object from the given file
88
* @param file the file to deserialize the object from
89
* @return the deserialized object
90
* @throws Exception if something goes wrong while reading from the file
92
public static Object read(File file) throws Exception {
93
return read(new FileInputStream(file));
97
* reads the XML-serialized object from a stream
98
* @param stream the stream to deserialize the object from
99
* @return the deserialized object
100
* @throws Exception if something goes wrong while reading from the stream
102
public static Object read(InputStream stream) throws Exception {
104
Class[] komlClassArgs;
106
java.lang.reflect.Constructor constructor;
108
java.lang.reflect.Method methodRead;
109
java.lang.reflect.Method methodClose;
110
Class[] readArgsClasses;
111
Class[] closeArgsClasses;
119
komlClass = Class.forName("fr.dyade.koala.xml.koml.KOMLDeserializer");
120
komlClassArgs = new Class[2];
121
komlClassArgs[0] = java.io.InputStream.class;
122
komlClassArgs[1] = Boolean.TYPE;
123
komlArgs = new Object[2];
124
komlArgs[0] = stream;
125
komlArgs[1] = new Boolean(false);
126
constructor = komlClass.getConstructor(komlClassArgs);
127
koml = constructor.newInstance(komlArgs);
128
readArgsClasses = new Class[0];
129
methodRead = komlClass.getMethod("readObject", readArgsClasses);
130
readArgs = new Object[0];
131
closeArgsClasses = new Class[0];
132
methodClose = komlClass.getMethod("close", closeArgsClasses);
133
closeArgs = new Object[0];
137
result = methodRead.invoke(koml, readArgs);
139
catch (Exception e) {
143
methodClose.invoke(koml, closeArgs);
150
* writes the XML-serialized object to the given file
151
* @param filename the file to serialize the object to
152
* @param o the object to write to the file
153
* @return whether writing was successful or not
154
* @throws Exception if something goes wrong while writing to the file
156
public static boolean write(String filename, Object o) throws Exception {
157
return write(new FileOutputStream(filename), o);
161
* write the XML-serialized object to the given file
162
* @param file the file to serialize the object to
163
* @param o the object to write to the file
164
* @return whether writing was successful or not
165
* @throws Exception if something goes wrong while writing to the file
167
public static boolean write(File file, Object o) throws Exception {
168
return write(new FileOutputStream(file), o);
172
* writes the XML-serialized object to a stream
173
* @param stream the stream to serialize the object to
174
* @param o the object to write to the stream
175
* @return whether writing was successful or not
176
* @throws Exception if something goes wrong while writing to the stream
178
public static boolean write(OutputStream stream, Object o) throws Exception {
180
Class[] komlClassArgs;
182
java.lang.reflect.Constructor constructor;
184
java.lang.reflect.Method methodAdd;
185
java.lang.reflect.Method methodClose;
186
Class[] addArgsClasses;
187
Class[] closeArgsClasses;
195
komlClass = Class.forName("fr.dyade.koala.xml.koml.KOMLSerializer");
196
komlClassArgs = new Class[2];
197
komlClassArgs[0] = java.io.OutputStream.class;
198
komlClassArgs[1] = Boolean.TYPE;
199
komlArgs = new Object[2];
200
komlArgs[0] = stream;
201
komlArgs[1] = new Boolean(false);
202
constructor = komlClass.getConstructor(komlClassArgs);
203
koml = constructor.newInstance(komlArgs);
204
addArgsClasses = new Class[1];
205
addArgsClasses[0] = Object.class;
206
methodAdd = komlClass.getMethod("addObject", addArgsClasses);
207
addArgs = new Object[1];
209
closeArgsClasses = new Class[0];
210
methodClose = komlClass.getMethod("close", closeArgsClasses);
211
closeArgs = new Object[0];
215
methodAdd.invoke(koml, addArgs);
218
catch (Exception e) {
222
methodClose.invoke(koml, closeArgs);