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

« back to all changes in this revision

Viewing changes to weka/core/xml/KOML.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
 * KOML.java
 
19
 * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
 
20
 */
 
21
 
 
22
package weka.core.xml;
 
23
 
 
24
import java.io.File;
 
25
import java.io.FileInputStream;
 
26
import java.io.FileOutputStream;
 
27
import java.io.InputStream;
 
28
import java.io.OutputStream;
 
29
 
 
30
/**
 
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.
 
33
 *
 
34
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
35
 * @version $Revision 1.0$
 
36
 */
 
37
public class KOML {
 
38
  
 
39
   /**
 
40
    * indicates whether <a href="http://koala.ilog.fr/XML/serialization/" target="_blank">KOML</a> 
 
41
    * (Koala Object Markup Language) is present
 
42
    */
 
43
   protected static boolean m_Present = false;
 
44
 
 
45
   /** the extension for KOML files (including '.') */
 
46
   public final static String FILE_EXTENSION = ".koml";
 
47
   
 
48
   /** check for KOML statically (needs only to be done once) */
 
49
   static {
 
50
      checkForKOML();
 
51
   }
 
52
 
 
53
   /**
 
54
    * checks whether the KOML is present in the class path
 
55
    */
 
56
   private static void checkForKOML() {
 
57
      try {
 
58
         Class.forName("fr.dyade.koala.xml.koml.KOMLSerializer");
 
59
         m_Present = true;
 
60
      }
 
61
      catch (Exception e) {
 
62
         m_Present = false;
 
63
      }
 
64
   }
 
65
  
 
66
   /**
 
67
    * returns whether KOML is present or not, i.e. whether the classes are in the
 
68
    * classpath or not
 
69
    *
 
70
    * @return whether KOML is available
 
71
    */
 
72
   public static boolean isPresent() {
 
73
      return m_Present;
 
74
   }
 
75
   
 
76
   /**
 
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
 
81
    */
 
82
   public static Object read(String filename) throws Exception {
 
83
      return read(new FileInputStream(filename));
 
84
   }
 
85
   
 
86
   /**
 
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
 
91
    */
 
92
   public static Object read(File file) throws Exception {
 
93
      return read(new FileInputStream(file));
 
94
   }
 
95
   
 
96
   /**
 
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
 
101
    */
 
102
   public static Object read(InputStream stream) throws Exception {
 
103
      Class                            komlClass;
 
104
      Class[]                          komlClassArgs;
 
105
      Object[]                         komlArgs;
 
106
      java.lang.reflect.Constructor    constructor;
 
107
      Object                           koml;
 
108
      java.lang.reflect.Method         methodRead;
 
109
      java.lang.reflect.Method         methodClose;
 
110
      Class[]                          readArgsClasses;
 
111
      Class[]                          closeArgsClasses;
 
112
      Object[]                         readArgs;
 
113
      Object[]                         closeArgs;
 
114
      Object                           result;
 
115
 
 
116
      result = null;
 
117
      
 
118
      // get Deserializer
 
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];
 
134
 
 
135
      // execute it
 
136
      try {
 
137
         result = methodRead.invoke(koml, readArgs);
 
138
      }
 
139
      catch (Exception e) {
 
140
         result = null;
 
141
      } 
 
142
      finally {
 
143
         methodClose.invoke(koml, closeArgs);
 
144
      }
 
145
      
 
146
      return result;
 
147
   }
 
148
   
 
149
   /**
 
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
 
155
    */
 
156
   public static boolean write(String filename, Object o) throws Exception {
 
157
      return write(new FileOutputStream(filename), o);
 
158
   }
 
159
   
 
160
   /**
 
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
 
166
    */
 
167
   public static boolean write(File file, Object o) throws Exception {
 
168
      return write(new FileOutputStream(file), o);
 
169
   }
 
170
   
 
171
   /**
 
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
 
177
    */
 
178
   public static boolean write(OutputStream stream, Object o) 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.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];
 
208
      addArgs[0]       = o;
 
209
      closeArgsClasses = new Class[0];
 
210
      methodClose      = komlClass.getMethod("close", closeArgsClasses);
 
211
      closeArgs        = new Object[0];
 
212
 
 
213
      // execute it
 
214
      try {
 
215
         methodAdd.invoke(koml, addArgs);
 
216
         result = true;
 
217
      }
 
218
      catch (Exception e) {
 
219
         result = false;
 
220
      } 
 
221
      finally {
 
222
         methodClose.invoke(koml, closeArgs);
 
223
      }
 
224
      
 
225
      return result;
 
226
   }
 
227
}