~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/validate/DictionaryValidator.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: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
 
4
 * $Revision: 7636 $
 
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
 * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Lesser General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 
 
23
 * 
 
24
 */
 
25
package org.openscience.cdk.validate;
 
26
 
 
27
import java.util.Iterator;
 
28
import java.util.Map;
 
29
 
 
30
import org.openscience.cdk.interfaces.IChemObject;
 
31
import org.openscience.cdk.dict.DictionaryDatabase;
 
32
import org.openscience.cdk.tools.LoggingTool;
 
33
 
 
34
/**
 
35
 * Validates the existence of references to dictionaries.
 
36
 *
 
37
 * @author   Egon Willighagen
 
38
 * @cdk.created  2003-03-28
 
39
 */ 
 
40
public class DictionaryValidator extends AbstractValidator {
 
41
 
 
42
    private static LoggingTool logger;
 
43
    
 
44
    static {
 
45
        logger = new LoggingTool(DictionaryValidator.class);
 
46
    }
 
47
    
 
48
    private DictionaryDatabase db;
 
49
    
 
50
    public DictionaryValidator(DictionaryDatabase db) {
 
51
        this.db = db;
 
52
    }
 
53
 
 
54
    public ValidationReport validateChemObject(IChemObject subject) {
 
55
        ValidationReport report = new ValidationReport();
 
56
        Map properties = subject.getProperties();
 
57
        Iterator iter = properties.keySet().iterator();
 
58
        ValidationTest noNamespace = new ValidationTest(subject,
 
59
            "Dictionary Reference lacks a namespace indicating the dictionary."
 
60
        );
 
61
        ValidationTest noDict = new ValidationTest(subject,
 
62
            "The referenced dictionary does not exist."
 
63
        );
 
64
        ValidationTest noEntry = new ValidationTest(subject,
 
65
            "The referenced entry does not exist in the dictionary."
 
66
        );
 
67
        while (iter.hasNext()) {
 
68
            Object key = iter.next();
 
69
            if (key instanceof String) {
 
70
                String keyName = (String)key;
 
71
                if (keyName.startsWith(DictionaryDatabase.DICTREFPROPERTYNAME)) {
 
72
                    String dictRef = (String)properties.get(keyName);
 
73
                    String details = "Dictref being anaylyzed: " + dictRef + ". ";
 
74
                    noNamespace.setDetails(details);
 
75
                    noDict.setDetails(details);
 
76
                    noEntry.setDetails(details);
 
77
                    int index = dictRef.indexOf(':');
 
78
                    if (index != -1) {
 
79
                        report.addOK(noNamespace);
 
80
                        String dict = dictRef.substring(0,index);
 
81
                        logger.debug("Looking for dictionary:" + dict);
 
82
                        if (db.hasDictionary(dict)) {
 
83
                            report.addOK(noDict);
 
84
                            if (dictRef.length() > index+1) {
 
85
                                String entry = dictRef.substring(index+1);
 
86
                                logger.debug("Looking for entry:" + entry);
 
87
                                if (db.hasEntry(dict, entry)) {
 
88
                                    report.addOK(noEntry);
 
89
                                } else {
 
90
                                    report.addError(noEntry);
 
91
                                }
 
92
                            } else {
 
93
                                report.addError(noEntry);
 
94
                            }
 
95
                        } else {
 
96
                            details += "The dictionary searched: " + dict + ".";
 
97
                            noDict.setDetails(details);
 
98
                            report.addError(noDict);
 
99
                            report.addError(noEntry);
 
100
                        }
 
101
                    } else {
 
102
                        // The dictRef has no namespace
 
103
                        details += "There is not a namespace given.";
 
104
                        noNamespace.setDetails(details);
 
105
                        report.addError(noNamespace);
 
106
                        report.addError(noDict);
 
107
                        report.addError(noEntry);
 
108
                    }
 
109
                } else {
 
110
                    // not a dictref
 
111
                }
 
112
            } else {
 
113
                // not a dictref
 
114
            }
 
115
        }
 
116
        return report;
 
117
    }
 
118
    
 
119
}