Release 0.41.92
[vala-gnome.git] / libvaladoc / moduleloader.vala
blobd73adda7492774ece97db801e84c73ec18890092
1 /* moduleloader.vala
3 * Copyright (C) 2008-2009 Florian Brosch
4 * Copyright (C) 2011 Florian Brosch
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Author:
21 * Brosch Florian <flo.brosch@gmail.com>
24 [CCode (has_target = false)]
25 public delegate void Valadoc.TagletRegisterFunction (ModuleLoader loader);
27 public class Valadoc.ModuleLoader : Object {
28 private Vala.HashMap<string, ModuleData> doclets = new Vala.HashMap<string, ModuleData> (str_hash, str_equal);
29 private Vala.HashMap<string, GLib.Type> taglets = new Vala.HashMap<string, GLib.Type> (str_hash, str_equal);
31 private static ModuleLoader instance;
33 public static ModuleLoader get_instance () {
34 if (instance == null) {
35 instance = new ModuleLoader ();
36 Taglets.init (instance);
38 return instance;
41 private ModuleLoader () {
44 private class ModuleData : Object {
45 public Module module;
46 public Type type;
51 // path helpers:
54 public static bool is_doclet (string path) {
55 string library_path = Path.build_filename (path, "libdoclet." + Module.SUFFIX);
56 return FileUtils.test (path, FileTest.EXISTS) && FileUtils.test (library_path, FileTest.EXISTS);
59 private static string get_plugin_path (string pluginpath, string pluginsubdir) {
60 if (Path.is_absolute (pluginpath) == false) {
61 // Test to see if the plugin exists in the expanded path and then fallback
62 // to using the configured plugin directory
63 string local_path = Path.build_filename (Environment.get_current_dir(), pluginpath);
64 if (is_doclet(local_path)) {
65 return local_path;
66 } else {
67 return Path.build_filename (Config.PACKAGE_DATADIR, pluginsubdir, pluginpath);
71 return pluginpath;
74 public static string get_doclet_path (string? docletpath, ErrorReporter reporter) {
75 if (docletpath == null) {
76 return Path.build_filename (Config.PACKAGE_DATADIR, "doclets", "html");
79 return get_plugin_path (docletpath, "doclets");
83 // Creation methods:
86 public Content.Taglet? create_taglet (string keyword) {
87 return (taglets.contains (keyword))? (Content.Taglet) GLib.Object.new (taglets.get (keyword)) : null;
90 public void register_taglet (string keyword, Type type) {
91 taglets.set (keyword, type);
94 public Doclet? create_doclet (string _path) {
95 string path = Vala.CodeContext.realpath (_path);
97 ModuleData? data = doclets.get (path);
98 if (data == null) {
99 void* function;
101 Module? module = Module.open (Module.build_path (path, "libdoclet"), ModuleFlags.BIND_LAZY | ModuleFlags.BIND_LOCAL);
102 if (module == null) {
103 return null;
106 module.symbol("register_plugin", out function);
107 if (function == null) {
108 return null;
111 Valadoc.DocletRegisterFunction register_func = (Valadoc.DocletRegisterFunction) function;
112 data = new ModuleData ();
113 doclets.set (path, data);
115 data.type = register_func (this);
116 data.module = (owned) module;
119 return (Doclet) GLib.Object.new (data.type);