3
* $Date: 2007-03-07 22:47:48 +0100 (Wed, 07 Mar 2007) $
6
* Copyright (C) 2003-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.dict;
31
import java.io.IOException;
32
import java.io.Reader;
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;
41
import org.openscience.cdk.tools.LoggingTool;
44
* Dictionary with entries build from an OWL file.
46
* @author Egon Willighagen <egonw@users.sf.net>
47
* @cdk.created 2005-11-18
48
* @cdk.keyword dictionary
50
* @cdk.depends xom-1.0.jar
52
public class OWLFile extends Dictionary {
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#";
62
public static Dictionary unmarshal(Reader reader) {
63
LoggingTool logger = new LoggingTool(OWLFile.class);
64
Dictionary dict = new OWLFile();
66
Builder parser = new Builder();
67
Document doc = parser.build(reader);
68
Element root = doc.getRootElement();
69
logger.debug("Found root element: ", root.getQualifiedName());
71
// Extract ownNS from root element
72
// final String ownNS = root.getBaseURI();
73
final String ownNS = root.getBaseURI();
76
logger.debug("Found ontology namespace: ", ownNS);
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);
88
logger.debug("Found a non-fact: ", entry.getQualifiedName());
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());
96
} catch (IOException ex) {
97
logger.error("Due to an IOException, the parser could not check:",
106
public static Entry unmarshal(Element entry, String ownNS) {
107
LoggingTool logger = new LoggingTool(OWLFile.class);
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());
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());
119
dbEntry.setClassName(entry.getQualifiedName());
120
logger.debug("class name: ", dbEntry.getClassName());
122
Element definition = entry.getFirstChildElement("definition", ownNS);
123
if (definition != null) {
124
dbEntry.setDefinition(definition.getValue());
126
Element description = entry.getFirstChildElement("description", ownNS);
127
if (description != null) {
128
dbEntry.setDescription(description.getValue());
131
if (entry.getQualifiedName().equals("Descriptor")) dbEntry.setRawContent(entry);