Source Code Cross Referenced for KOML.java in  » Science » weka » weka » core » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » weka » weka.core.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.