1 /**
2  * Loader module
3  *
4  * Copyright: (c) 2015-2016, Milofon Project.
5  * License: Subject to the terms of the BSD license, as written in the included LICENSE.txt file.
6  * Authors: Maksim Galanin
7  */
8 module proped.loader;
9 
10 private
11 {
12     import std.path : extension;
13     import std.algorithm.searching : canFind;
14 
15     import proped.properties : Properties;
16     import proped.exception : PropertiesException;
17 }
18 
19  
20 
21 /**
22  * Interface properties loader
23  */
24 interface PropertiesLoader
25 {
26     /**
27      * Loading properties from a file
28      *
29      * Params:
30      *
31      * fileName = Path to the file system
32      */
33     Properties loadPropertiesFile(string fileName);
34 
35 
36     /**
37      * Loading properties from a string
38      *
39      * Params:
40      *
41      * data = Source string
42      */
43     Properties loadPropertiesString(string data);
44 
45 
46     /**
47      * Returns the file extension to delermite the type loader
48      */
49     string[] getExtensions();
50 
51 
52     /**
53      * Checking the possibility to download the file current loader
54      *
55      * Verification occurs by file extension
56      *
57      * Params:
58      *
59      * fileName = File
60      */
61     final bool isPropertiesFile(string fileName)
62     { 
63         return canFind(getExtensions(), fileName.extension);
64     }
65 }
66 
67 
68 
69 /**
70  * Loading properties from the file with the necessary loader
71  *
72  * Params:
73  *
74  * loaders  = Loaders
75  * fileName = Path
76  */
77 Properties loadProperties(PropertiesLoader[] loaders, string fileName)
78 {
79     foreach(PropertiesLoader loader; loaders)
80         if (loader.isPropertiesFile(fileName))
81             return loader.loadPropertiesFile(fileName);     
82     throw new PropertiesException("Not defined loader for " ~ fileName);
83 }
84 
85 
86 /**
87  * The function loads the configuration object from a file
88  * Params:
89  *
90  * fileName = File name
91  */
92 alias Loader = Properties delegate(string fileName);
93 
94 
95 /**
96  * Create properties loader
97  */
98 Loader createPropertiesLoader()
99 {
100     PropertiesLoader[] loaders;
101 
102     version(Have_sdlang_d) 
103     {
104         import proped.loaders.sdl : SDLPropertiesLoader;
105         loaders ~= new SDLPropertiesLoader();
106     }
107 
108     version(Have_dyaml_dlang_tour) 
109     {
110         import proped.loaders.yaml : YAMLPropertiesLoader;
111         loaders ~= new YAMLPropertiesLoader();
112     }
113 
114     import proped.loaders.json : JSONPropertiesLoader;
115     loaders ~= new JSONPropertiesLoader();
116 
117     import proped.loaders.properties : PropertiesPropertiesLoader;
118     loaders ~= new PropertiesPropertiesLoader();
119 
120     return (string fileName) 
121     {
122         return loaders.loadProperties(fileName);
123     };
124 }
125