View Javadoc

1   /*
2    * $Id: VowlidatorPlugin.java,v 1.15 2005/06/01 14:37:05 jlerner Exp $
3    * Created on May 11, 2004
4    *
5    * Copyright (c) 1999-2004, BBN Technologies, LLC.
6    * All rights reserved.
7    * http://www.daml.org/legal/opensource/bbn_license.html
8    */
9   
10  package com.bbn.swede.library.vowlidator;
11  
12  import org.eclipse.core.runtime.Plugin;
13  import org.eclipse.core.runtime.Platform;
14  
15  import com.bbn.semweb.owl.vowlidator.Preferences;
16  import com.bbn.semweb.owl.vowlidator.ValidatorAPI;
17  import com.bbn.swede.core.OWLCore;
18  
19  import java.net.URL;
20  
21  /***
22   * Plugin class for com.bbn.swede.library.vowlidator.
23   * @author tself
24   */
25  public class VowlidatorPlugin extends Plugin
26  {
27     /***
28      * Plugin ID string.
29      */
30     public static final String PLUGIN_ID = "com.bbn.swede.library.vowlidator";
31     private static ValidatorAPI _validatorAPI;
32     // cached URLs
33     /***
34      * Contains the URLs of documents which are available in the cache.
35      */
36     static final String[] CACHE_URLS =
37     {
38        "http://www.w3.org/2002/07/owl#",
39        "http://purl.org/dc/elements/1.0/#",
40        "http://purl.org/dc/elements/1.1/#",
41        "http://www.w3.org/2001/03/XMLSchema.xsd",
42        "http://www.w3.org/2001/XMLSchema.xsd",
43        "http://www.w3.org/2000/01/rdf-schema#",
44        "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
45        "http://www.w3.org/2001/XMLSchema#",
46     };
47     
48     /***
49      * Contains the filenames of the local cache for each of the URLs.
50      * The order of the filenames should correspond to the order of the
51      * URLs in CACHE_URLS.
52      */
53     static final String[] CACHE_LOCALS =
54     {
55        "www.w3.org_2002_07_owl",
56        "purl.org_dc_elements_1.0",
57        "purl.org_dc_elements_1.1",
58        "www_w3_org_2001_03_XMLSchema.xsd",
59        "www_w3_org_2001_XMLSchema.xsd",
60        "www.w3.org_2000_01_rdf-schema",
61        "www.w3.org_1999_02_22-rdf-syntax-ns",
62        "www_w3_org_2001_XMLSchema.xsd",
63     };
64     
65     /***
66      * Directory to prepend to each of the local cache filenames.
67      */
68     static final String CACHE_DIR = "/cache";
69     /***
70      * Files to import by default.
71      */
72     static final String[] DEFAULT_IMPORTS =
73     {
74        "http://www.w3.org/2002/07/owl#",
75        "http://www.w3.org/2000/01/rdf-schema#",
76        "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
77        "http://www.w3.org/2001/XMLSchema#",
78     };
79     // validator default properties
80     /***
81      * Validation options property names.
82      */
83     static final String[] PROPERTY_NAMES =
84     {
85        "recurseNS", 
86        "maxRecurseNS", 
87        "recurseImports", 
88        "maxRecurseImports",
89     };
90     /***
91      * Validation option property values.
92      */
93     static final String[] PROPERTY_VALUES = 
94     {
95        "false", 
96        "1", 
97        "true", 
98        "-1",
99     };
100    private static VowlidatorPlugin _plugin;
101    /***
102     * Creates a VowlidatorPlugin and sets it as the singleton instance.
103     */
104    public VowlidatorPlugin()
105    {
106       super();
107       _plugin = this;
108    }
109    
110    /***
111     * Returns the one and only instance of VowlidatorPlugin.
112     * @return The singleton instance.
113     */
114    public static VowlidatorPlugin getVowlidatorPlugin()
115    {
116       return _plugin;
117    }
118    
119    /***
120     * Returns the Validator API used by the singleton instance of the plugin.
121     * @return The validator API.
122     */
123    public static ValidatorAPI getValidatorAPI()
124    {
125       if (_validatorAPI != null)
126       {
127          _validatorAPI.initialize();
128          return addDefaultImports(_validatorAPI);
129       }
130       Preferences prefs = new Preferences();
131       // setup cache
132       prefs = setCache(prefs);
133       // set default properties
134       prefs = setDefaultProperties(prefs);
135       try
136       {
137          _validatorAPI = new ValidatorAPI(prefs);
138       }
139       catch (Exception e)
140       {
141          OWLCore.logError(Platform.getBundle(PLUGIN_ID).getSymbolicName(),
142             "Validator creation failed.",
143             e);
144       }
145       _validatorAPI = addDefaultImports(_validatorAPI);
146       return _validatorAPI;
147    }
148    private static ValidatorAPI addDefaultImports(ValidatorAPI va)
149    {
150       for (int i = 0; i < DEFAULT_IMPORTS.length; i++)
151       {
152          va.addURL(DEFAULT_IMPORTS[i], false);
153       }
154       return va;
155    }
156    private static Preferences setDefaultProperties(Preferences p)
157    {
158       for (int i = 0; i < PROPERTY_NAMES.length; i++)
159       {
160          p.setProperty(PROPERTY_NAMES[i], PROPERTY_VALUES[i]);
161       }
162       return p;
163    }
164    private static Preferences setCache(Preferences p)
165    {
166       String url = null;
167       String localName = null;
168       try
169       {
170          for (int i = 0; i < CACHE_URLS.length; i++)
171          {
172             localName = CACHE_DIR + "/" + CACHE_LOCALS[i];
173             URL u = VowlidatorPlugin.class.getResource(CACHE_DIR + "/" + CACHE_LOCALS[i]);
174             //URL u = getVowlidatorPlugin().find(new Path(localName));
175             //System.out.println("URL: " + u.toString());
176             if (u == null)
177             {
178                OWLCore.logWarning(
179                   "com.bbn.swede.library.vowlidator", 
180                   "Could not find " + localName,
181                   new Exception());
182                continue;
183             }
184             //            System.out.println("Adding cache item: " + u.toString());
185             p.addToCache(CACHE_URLS[i], u.toString());
186          }
187       }
188       catch (ArrayIndexOutOfBoundsException e)
189       {
190          //         System.err.println(
191          //            "VowlidatorPlugin:Error: inconsistent cache arrays.");
192          OWLCore.logError(
193             Platform.getBundle(PLUGIN_ID).getSymbolicName(),
194             "Inconsistent cache arrays",
195             e);
196       }
197       return p;
198    }
199 }