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

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/dict/OWLFile.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: sea36 $
 
3
 * $Date: 2007-03-07 22:47:48 +0100 (Wed, 07 Mar 2007) $
 
4
 * $Revision: 8045 $
 
5
 *
 
6
 * Copyright (C) 2003-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.dict;
 
30
 
 
31
import java.io.IOException;
 
32
import java.io.Reader;
 
33
 
 
34
import nu.xom.Attribute;
 
35
import nu.xom.Builder;
 
36
import nu.xom.Document;
 
37
import nu.xom.Element;
 
38
import nu.xom.Elements;
 
39
import nu.xom.ParsingException;
 
40
 
 
41
import org.openscience.cdk.tools.LoggingTool;
 
42
 
 
43
/**
 
44
 * Dictionary with entries build from an OWL file.
 
45
 *
 
46
 * @author       Egon Willighagen <egonw@users.sf.net>
 
47
 * @cdk.created  2005-11-18
 
48
 * @cdk.keyword  dictionary
 
49
 *
 
50
 * @cdk.depends  xom-1.0.jar
 
51
 */
 
52
public class OWLFile extends Dictionary {
 
53
 
 
54
    private static String rdfNS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 
55
    private static String rdfsNS = "http://www.w3.org/2000/01/rdf-schema#";
 
56
 
 
57
    public OWLFile() {
 
58
        super();
 
59
    }
 
60
 
 
61
 
 
62
    public static Dictionary unmarshal(Reader reader) {
 
63
        LoggingTool logger = new LoggingTool(OWLFile.class);
 
64
        Dictionary dict = new OWLFile();
 
65
        try {
 
66
            Builder parser = new Builder();
 
67
            Document doc = parser.build(reader);
 
68
            Element root = doc.getRootElement();
 
69
            logger.debug("Found root element: ", root.getQualifiedName());
 
70
            
 
71
            // Extract ownNS from root element
 
72
//            final String ownNS = root.getBaseURI();
 
73
            final String ownNS = root.getBaseURI();
 
74
            dict.setNS(ownNS);
 
75
 
 
76
            logger.debug("Found ontology namespace: ", ownNS);
 
77
            
 
78
            // process the defined facts
 
79
            Elements entries = root.getChildElements();
 
80
            logger.info("Found #elements in OWL dict:", entries.size());
 
81
            for (int i=0; i<entries.size(); i++) {
 
82
                Element entry = entries.get(i);
 
83
                if (entry.getNamespaceURI().equals(ownNS)) {
 
84
                        Entry dbEntry = unmarshal(entry, ownNS); 
 
85
                        dict.addEntry(dbEntry);
 
86
                        logger.debug("Added entry: ", dbEntry);
 
87
                } else {
 
88
                        logger.debug("Found a non-fact: ", entry.getQualifiedName());
 
89
                }
 
90
            }
 
91
        } catch (ParsingException ex) {
 
92
            logger.error("Dictionary is not well-formed: ", ex.getMessage());
 
93
            logger.debug("Error at line " + ex.getLineNumber(),
 
94
                         ", column " + ex.getColumnNumber());
 
95
            dict = null;
 
96
        } catch (IOException ex) { 
 
97
            logger.error("Due to an IOException, the parser could not check:",
 
98
                ex.getMessage()
 
99
            );
 
100
            logger.debug(ex);
 
101
            dict = null;
 
102
        }
 
103
        return dict;
 
104
    }
 
105
 
 
106
    public static Entry unmarshal(Element entry, String ownNS) {
 
107
        LoggingTool logger = new LoggingTool(OWLFile.class);
 
108
 
 
109
        // create a new entry by ID
 
110
        Attribute id = entry.getAttribute("ID", rdfNS);
 
111
        logger.debug("ID: ", id);
 
112
        Entry dbEntry = new Entry(id.getValue());
 
113
 
 
114
        // set additional, optional data
 
115
        Element label = entry.getFirstChildElement("label", rdfsNS);
 
116
        logger.debug("label: ", label);
 
117
        if (label != null) dbEntry.setLabel(label.getValue());
 
118
 
 
119
        dbEntry.setClassName(entry.getQualifiedName());
 
120
        logger.debug("class name: ", dbEntry.getClassName());
 
121
 
 
122
        Element definition = entry.getFirstChildElement("definition", ownNS);
 
123
        if (definition != null) {
 
124
            dbEntry.setDefinition(definition.getValue());
 
125
        }
 
126
        Element description = entry.getFirstChildElement("description", ownNS);
 
127
        if (description != null) {
 
128
                dbEntry.setDescription(description.getValue());
 
129
        }
 
130
 
 
131
        if (entry.getQualifiedName().equals("Descriptor")) dbEntry.setRawContent(entry);
 
132
 
 
133
        return dbEntry;
 
134
    }
 
135
    
 
136
}