~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/io/WriterFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$
 
2
 * $Author: egonw $
 
3
 * $Date: 2006-05-09 21:32:32 +0200 (Tue, 09 May 2006) $  
 
4
 * $Revision: 6204 $
 
5
 *
 
6
 * Copyright (C) 1997-2007  The Chemistry Development Kit (CDK) project
 
7
 *
 
8
 * Contact: cdk-devel@lists.sourceforge.net
 
9
 *
 
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.
 
18
 *
 
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.
 
23
 *
 
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.
 
27
 *
 
28
 */
 
29
package org.openscience.cdk.io;
 
30
 
 
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;
 
37
 
 
38
import org.openscience.cdk.io.formats.IChemFormat;
 
39
import org.openscience.cdk.io.formats.IResourceFormat;
 
40
import org.openscience.cdk.tools.LoggingTool;
 
41
 
 
42
/**
 
43
 * Helper tool to create IChemObjectWriters.
 
44
 * 
 
45
 * @author Egon Willighagen <ewilligh@uni-koeln.de>
 
46
 * @cdk.module io
 
47
 **/
 
48
public class WriterFactory {
 
49
 
 
50
    private final static String IO_FORMATS_LIST = "io-formats.set";
 
51
 
 
52
    private LoggingTool logger;
 
53
 
 
54
    private static List formats = null;
 
55
 
 
56
    /**
 
57
     * Constructs a ChemObjectIOInstantionTests.
 
58
     */
 
59
    public WriterFactory() {
 
60
        logger = new LoggingTool(this);
 
61
    }
 
62
 
 
63
    /**
 
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.
 
66
     * 
 
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
 
69
     * 
 
70
     * @see    org.openscience.cdk.tools.DataFeatures
 
71
     */
 
72
    public IChemFormat[] findChemFormats(int features) {
 
73
        if (formats == null) loadFormats();
 
74
        
 
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);
 
80
        }
 
81
        
 
82
        return (IChemFormat[])matches.toArray(new IChemFormat[matches.size()]);
 
83
    }
 
84
    
 
85
    public int formatCount() {
 
86
        if (formats == null) loadFormats();
 
87
        
 
88
        return formats.size(); 
 
89
    }
 
90
    
 
91
    private void loadFormats() {
 
92
        if (formats == null) {
 
93
            formats = new ArrayList();
 
94
            try {
 
95
                logger.debug("Starting loading Formats...");
 
96
                BufferedReader reader = new BufferedReader(new InputStreamReader(
 
97
                    this.getClass().getClassLoader().getResourceAsStream(IO_FORMATS_LIST)
 
98
                ));
 
99
                int formatCount = 0;
 
100
                while (reader.ready()) {
 
101
                    // load them one by one
 
102
                    String formatName = reader.readLine();
 
103
                    formatCount++;
 
104
                    try {
 
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) {
 
109
                                formats.add(format);
 
110
                                logger.info("Loaded IChemFormat: " + format.getClass().getName());
 
111
                        }
 
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);
 
118
                    }
 
119
                }
 
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);
 
124
            }
 
125
        }
 
126
    }
 
127
    
 
128
    /**
 
129
     * Creates a new IChemObjectWriter based on the given IChemFormat.
 
130
     */
 
131
    public IChemObjectWriter createWriter(IChemFormat format) {
 
132
        if (format != null) {
 
133
            String writerClassName = format.getWriterClassName();
 
134
            if (writerClassName != null) {
 
135
                try {
 
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);
 
145
                }
 
146
            } else {
 
147
                logger.warn("ChemFormat is recognized, but no writer is available.");
 
148
            }
 
149
        } else {
 
150
            logger.warn("ChemFormat is not recognized.");
 
151
        } 
 
152
        return null;
 
153
    }
 
154
}
 
155