001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * KOML.java
019: * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
020: */
021:
022: package weka.core.xml;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileOutputStream;
027: import java.io.InputStream;
028: import java.io.OutputStream;
029:
030: /**
031: * This class is a helper class for XML serialization using <a href="http://koala.ilog.fr/XML/serialization/" target="_blank">KOML</a> .
032: * KOML does not need to be present, since the class-calls are done generically via Reflection.
033: *
034: * @author FracPete (fracpete at waikato dot ac dot nz)
035: * @version $Revision 1.0$
036: */
037: public class KOML {
038:
039: /**
040: * indicates whether <a href="http://koala.ilog.fr/XML/serialization/" target="_blank">KOML</a>
041: * (Koala Object Markup Language) is present
042: */
043: protected static boolean m_Present = false;
044:
045: /** the extension for KOML files (including '.') */
046: public final static String FILE_EXTENSION = ".koml";
047:
048: /** check for KOML statically (needs only to be done once) */
049: static {
050: checkForKOML();
051: }
052:
053: /**
054: * checks whether the KOML is present in the class path
055: */
056: private static void checkForKOML() {
057: try {
058: Class.forName("fr.dyade.koala.xml.koml.KOMLSerializer");
059: m_Present = true;
060: } catch (Exception e) {
061: m_Present = false;
062: }
063: }
064:
065: /**
066: * returns whether KOML is present or not, i.e. whether the classes are in the
067: * classpath or not
068: *
069: * @return whether KOML is available
070: */
071: public static boolean isPresent() {
072: return m_Present;
073: }
074:
075: /**
076: * reads the XML-serialized object from the given file
077: * @param filename the file to deserialize the object from
078: * @return the deserialized object
079: * @throws Exception if something goes wrong while reading from the file
080: */
081: public static Object read(String filename) throws Exception {
082: return read(new FileInputStream(filename));
083: }
084:
085: /**
086: * reads the XML-serialized object from the given file
087: * @param file the file to deserialize the object from
088: * @return the deserialized object
089: * @throws Exception if something goes wrong while reading from the file
090: */
091: public static Object read(File file) throws Exception {
092: return read(new FileInputStream(file));
093: }
094:
095: /**
096: * reads the XML-serialized object from a stream
097: * @param stream the stream to deserialize the object from
098: * @return the deserialized object
099: * @throws Exception if something goes wrong while reading from the stream
100: */
101: public static Object read(InputStream stream) throws Exception {
102: Class komlClass;
103: Class[] komlClassArgs;
104: Object[] komlArgs;
105: java.lang.reflect.Constructor constructor;
106: Object koml;
107: java.lang.reflect.Method methodRead;
108: java.lang.reflect.Method methodClose;
109: Class[] readArgsClasses;
110: Class[] closeArgsClasses;
111: Object[] readArgs;
112: Object[] closeArgs;
113: Object result;
114:
115: result = null;
116:
117: // get Deserializer
118: komlClass = Class
119: .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];
134:
135: // execute it
136: try {
137: result = methodRead.invoke(koml, readArgs);
138: } catch (Exception e) {
139: result = null;
140: } finally {
141: methodClose.invoke(koml, closeArgs);
142: }
143:
144: return result;
145: }
146:
147: /**
148: * writes the XML-serialized object to the given file
149: * @param filename the file to serialize the object to
150: * @param o the object to write to the file
151: * @return whether writing was successful or not
152: * @throws Exception if something goes wrong while writing to the file
153: */
154: public static boolean write(String filename, Object o)
155: throws Exception {
156: return write(new FileOutputStream(filename), o);
157: }
158:
159: /**
160: * write the XML-serialized object to the given file
161: * @param file the file to serialize the object to
162: * @param o the object to write to the file
163: * @return whether writing was successful or not
164: * @throws Exception if something goes wrong while writing to the file
165: */
166: public static boolean write(File file, Object o) throws Exception {
167: return write(new FileOutputStream(file), o);
168: }
169:
170: /**
171: * writes the XML-serialized object to a stream
172: * @param stream the stream to serialize the object to
173: * @param o the object to write to the stream
174: * @return whether writing was successful or not
175: * @throws Exception if something goes wrong while writing to the stream
176: */
177: public static boolean write(OutputStream stream, Object o)
178: throws Exception {
179: Class komlClass;
180: Class[] komlClassArgs;
181: Object[] komlArgs;
182: java.lang.reflect.Constructor constructor;
183: Object koml;
184: java.lang.reflect.Method methodAdd;
185: java.lang.reflect.Method methodClose;
186: Class[] addArgsClasses;
187: Class[] closeArgsClasses;
188: Object[] addArgs;
189: Object[] closeArgs;
190: boolean result;
191:
192: result = false;
193:
194: // get Deserializer
195: komlClass = Class
196: .forName("fr.dyade.koala.xml.koml.KOMLSerializer");
197: komlClassArgs = new Class[2];
198: komlClassArgs[0] = java.io.OutputStream.class;
199: komlClassArgs[1] = Boolean.TYPE;
200: komlArgs = new Object[2];
201: komlArgs[0] = stream;
202: komlArgs[1] = new Boolean(false);
203: constructor = komlClass.getConstructor(komlClassArgs);
204: koml = constructor.newInstance(komlArgs);
205: addArgsClasses = new Class[1];
206: addArgsClasses[0] = Object.class;
207: methodAdd = komlClass.getMethod("addObject", addArgsClasses);
208: addArgs = new Object[1];
209: addArgs[0] = o;
210: closeArgsClasses = new Class[0];
211: methodClose = komlClass.getMethod("close", closeArgsClasses);
212: closeArgs = new Object[0];
213:
214: // execute it
215: try {
216: methodAdd.invoke(koml, addArgs);
217: result = true;
218: } catch (Exception e) {
219: result = false;
220: } finally {
221: methodClose.invoke(koml, closeArgs);
222: }
223:
224: return result;
225: }
226: }
|