3
* $Date: 2006-05-09 21:32:32 +0200 (Tue, 09 May 2006) $
6
* Copyright (C) 1997-2007 The Chemistry Development Kit (CDK) project
8
* Contact: cdk-devel@lists.sourceforge.net
10
* This program is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public License
12
* as published by the Free Software Foundation; either version 2.1
13
* of the License, or (at your option) any later version.
14
* All we ask is that proper credit is given for our work, which includes
15
* - but is not limited to - adding the above copyright notice to the beginning
16
* of your source code files, and to any copyright notice that you may distribute
17
* with programs based on this work.
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU Lesser General Public License for more details.
24
* You should have received a copy of the GNU Lesser General Public License
25
* along with this program; if not, write to the Free Software
26
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29
package org.openscience.cdk.io;
31
import java.io.BufferedReader;
32
import java.io.InputStreamReader;
33
import java.lang.reflect.Method;
34
import java.util.ArrayList;
35
import java.util.Iterator;
36
import java.util.List;
38
import org.openscience.cdk.io.formats.IChemFormat;
39
import org.openscience.cdk.io.formats.IResourceFormat;
40
import org.openscience.cdk.tools.LoggingTool;
43
* Helper tool to create IChemObjectWriters.
45
* @author Egon Willighagen <ewilligh@uni-koeln.de>
48
public class WriterFactory {
50
private final static String IO_FORMATS_LIST = "io-formats.set";
52
private LoggingTool logger;
54
private static List formats = null;
57
* Constructs a ChemObjectIOInstantionTests.
59
public WriterFactory() {
60
logger = new LoggingTool(this);
64
* Finds IChemFormats that provide a container for serialization for the
65
* given features. The syntax of the integer is explained in the DataFeatures class.
67
* @param features the data features for which a IChemFormat is searched
68
* @return an array of IChemFormat's that can contain the given features
70
* @see org.openscience.cdk.tools.DataFeatures
72
public IChemFormat[] findChemFormats(int features) {
73
if (formats == null) loadFormats();
75
Iterator iter = formats.iterator();
76
List matches = new ArrayList();
77
while (iter.hasNext()) {
78
IChemFormat format = (IChemFormat)iter.next();
79
if ((format.getSupportedDataFeatures() & features) == features) matches.add(format);
82
return (IChemFormat[])matches.toArray(new IChemFormat[matches.size()]);
85
public int formatCount() {
86
if (formats == null) loadFormats();
88
return formats.size();
91
private void loadFormats() {
92
if (formats == null) {
93
formats = new ArrayList();
95
logger.debug("Starting loading Formats...");
96
BufferedReader reader = new BufferedReader(new InputStreamReader(
97
this.getClass().getClassLoader().getResourceAsStream(IO_FORMATS_LIST)
100
while (reader.ready()) {
101
// load them one by one
102
String formatName = reader.readLine();
105
Class formatClass = this.getClass().getClassLoader().loadClass(formatName);
106
Method getinstanceMethod = formatClass.getMethod("getInstance", new Class[0]);
107
IResourceFormat format = (IResourceFormat)getinstanceMethod.invoke(null, new Object[0]);
108
if (format instanceof IChemFormat) {
110
logger.info("Loaded IChemFormat: " + format.getClass().getName());
112
} catch (ClassNotFoundException exception) {
113
logger.error("Could not find this IResourceFormat: ", formatName);
114
logger.debug(exception);
115
} catch (Exception exception) {
116
logger.error("Could not load this IResourceFormat: ", formatName);
117
logger.debug(exception);
120
logger.info("Number of loaded formats used in detection: ", formatCount);
121
} catch (Exception exception) {
122
logger.error("Could not load this io format list: ", IO_FORMATS_LIST);
123
logger.debug(exception);
129
* Creates a new IChemObjectWriter based on the given IChemFormat.
131
public IChemObjectWriter createWriter(IChemFormat format) {
132
if (format != null) {
133
String writerClassName = format.getWriterClassName();
134
if (writerClassName != null) {
136
// make a new instance of this class
137
return (IChemObjectWriter)this.getClass().getClassLoader().
138
loadClass(writerClassName).newInstance();
139
} catch (ClassNotFoundException exception) {
140
logger.error("Could not find this ChemObjectWriter: ", writerClassName);
141
logger.debug(exception);
142
} catch (Exception exception) {
143
logger.error("Could not create this ChemObjectWriter: ", writerClassName);
144
logger.debug(exception);
147
logger.warn("ChemFormat is recognized, but no writer is available.");
150
logger.warn("ChemFormat is not recognized.");