Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultTemplateLoaderFactory |
|
| 15.0;15 |
1 | /* | |
2 | * Copyright (C) 1998-2000 Semiotek Inc. All Rights Reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted under the terms of either of the following | |
6 | * Open Source licenses: | |
7 | * | |
8 | * The GNU General Public License, version 2, or any later version, as | |
9 | * published by the Free Software Foundation | |
10 | * (http://www.fsf.org/copyleft/gpl.html); | |
11 | * | |
12 | * or | |
13 | * | |
14 | * The Semiotek Public License (http://webmacro.org/LICENSE.) | |
15 | * | |
16 | * This software is provided "as is", with NO WARRANTY, not even the | |
17 | * implied warranties of fitness to purpose, or merchantability. You | |
18 | * assume all risks and liabilities associated with its use. | |
19 | * | |
20 | * See www.webmacro.org for more information on the WebMacro project. | |
21 | */ | |
22 | package org.webmacro.resource; | |
23 | ||
24 | import org.webmacro.Broker; | |
25 | import org.webmacro.InitException; | |
26 | ||
27 | /** | |
28 | * Default implementation of TemplateLoaderFactory interface. | |
29 | * This implementation expects config strings to be in an url-like | |
30 | * format: [protocol:][path]. It will then look for a key in the | |
31 | * WebMacro configuration of the form "TemplateLoader.<protocol>", where | |
32 | * protocol is replaced by "default", if it is ommited in the url. The | |
33 | * value of this configuration option has to be a fully qualified class | |
34 | * name of a class with a no-args constructor, implementing the TemplateLoader | |
35 | * interface. | |
36 | * <br> | |
37 | * After instantiating an object of this class, "path" is passed as configuration | |
38 | * option to this object. | |
39 | * @author Sebastian Kanthak (sebastian.kanthak@muehlheim.de) | |
40 | */ | |
41 | 0 | public class DefaultTemplateLoaderFactory implements TemplateLoaderFactory |
42 | { | |
43 | ||
44 | public TemplateLoader getTemplateLoader (Broker b, String config) throws InitException | |
45 | { | |
46 | String protocol; | |
47 | String options; | |
48 | 0 | int pos = config.indexOf(":"); |
49 | 0 | if (pos == -1) |
50 | { | |
51 | 0 | protocol = "default"; |
52 | 0 | options = config; |
53 | } | |
54 | else | |
55 | { | |
56 | 0 | protocol = config.substring(0, pos); |
57 | 0 | if (pos + 1 < config.length()) |
58 | { | |
59 | 0 | options = config.substring(pos + 1); |
60 | } | |
61 | else | |
62 | { | |
63 | 0 | options = ""; |
64 | } | |
65 | } | |
66 | ||
67 | 0 | String classname = b.getSetting("TemplateLoader.".concat(protocol)); |
68 | 0 | if (classname == null || classname.length() == 0) |
69 | 0 | throw new InitException("No class found for template loader protocol " + protocol); |
70 | ||
71 | try | |
72 | { | |
73 | 0 | TemplateLoader loader = (TemplateLoader) b.classForName(classname).newInstance(); |
74 | 0 | loader.init(b, b.getSettings()); |
75 | 0 | loader.setConfig(options); |
76 | 0 | return loader; |
77 | } | |
78 | 0 | catch (ClassNotFoundException e) |
79 | { | |
80 | 0 | throw new InitException("Class " + classname + " for template loader " + protocol + " not found", e); |
81 | } | |
82 | 0 | catch (InstantiationException e) |
83 | { | |
84 | 0 | throw new InitException("Could not instantiate class " + classname + " for template loader " + protocol, e); |
85 | } | |
86 | 0 | catch (IllegalAccessException e) |
87 | { | |
88 | 0 | throw new InitException("Could not instantiate class " + classname + " for template loader " + protocol, e); |
89 | } | |
90 | 0 | catch (ClassCastException e) |
91 | { | |
92 | 0 | throw new InitException("Class " + classname + " for template loader" + protocol + " does not implement " + |
93 | "interface org.webmacro.resource.TemplateLoader", e); | |
94 | } | |
95 | } | |
96 | } |