-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadYamlRaw.m
251 lines (228 loc) · 7.92 KB
/
ReadYamlRaw.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
%==========================================================================
% Reads YAML file, converts YAML sequences to MATLAB cell columns and YAML
% mappings to MATLAB structs
%
% filename ... name of yaml file to be imported
% verbose ... verbosity level (0 or absent = no messages,
% 1 = notify imports)
%==========================================================================
function result = ReadYamlRaw(filename, verbose, nosuchfileaction, treatasdata)
if ~exist('verbose','var')
verbose = 0;
end;
if ~exist('nosuchfileaction','var')
nosuchfileaction = 0;
end;
if ~ismember(nosuchfileaction,[0,1])
error('nosuchfileexception parameter must be 0,1 or missing.');
end;
if(~exist('treatasdata','var'))
treatasdata = 0;
end;
if ~ismember(treatasdata,[0,1])
error('treatasdata parameter must be 0,1 or missing.');
end;
[pth,~,~] = fileparts(mfilename('fullpath'));
try
import('org.yaml.snakeyaml.*');
javaObject('Yaml');
catch
dp = [pth filesep 'external' filesep 'snakeyaml-1.9.jar'];
if not(ismember(dp, javaclasspath ('-dynamic')))
javaaddpath(dp); % javaaddpath clears global variables...!?
end
import('org.yaml.snakeyaml.*');
end;
setverblevel(verbose);
% import('org.yaml.snakeyaml.Yaml'); % import here does not affect import in load_yaml ...!?
result = load_yaml(filename, nosuchfileaction, treatasdata);
end
%--------------------------------------------------------------------------
% Actually performs YAML load.
% - If this is a first call during recursion it changes cwd to the path of
% given filename and stores the old path. Then it calls the YAML parser
% and runs the recursive transformation. After transformation or when an
% error occurs, it sets cwd back to the stored value.
% - Otherwise just calls the parser and runs the transformation.
%
function result = load_yaml(inputfilename, nosuchfileaction, treatasdata)
persistent nsfe;
if exist('nosuchfileaction','var') %isempty(nsfe) &&
nsfe = nosuchfileaction;
end;
persistent tadf;
if isempty(tadf) && exist('treatasdata','var')
tadf = treatasdata;
end;
yaml = org.yaml.snakeyaml.Yaml(); % It appears that Java objects cannot be persistent...!?
if ~tadf
[filepath, filename, fileext] = fileparts(inputfilename);
if isempty(filepath)
pathstore = cd();
else
pathstore = cd(filepath);
end;
end;
try
if ~tadf
result = scan(yaml.load(fileread([filename, fileext])));
else
result = scan(yaml.load(inputfilename));
end;
catch ex
if ~tadf
cd(pathstore);
end;
switch ex.identifier
case 'MATLAB:fileread:cannotOpenFile'
if nsfe == 1
error('MATLAB:MATYAML:FileNotFound', ['No such file to read: ',filename,fileext]);
elseif nsfe == 0
warning('MATLAB:MATYAML:FileNotFound', ['No such file to read: ',filename,fileext]);
result = struct();
return;
end;
end;
rethrow(ex);
end;
if ~tadf
cd(pathstore);
end;
end
%--------------------------------------------------------------------------
% Determine node type and call appropriate conversion routine.
%
function result = scan(r)
if isa(r, 'char')
result = scan_string(r);
elseif isa(r, 'double')
result = scan_numeric(r);
elseif isa(r, 'logical')
result = scan_logical(r);
elseif isa(r, 'java.util.Date')
result = scan_datetime(r);
elseif isa(r, 'java.util.List')
result = scan_list(r);
elseif isa(r, 'java.util.Map')
result = scan_map(r);
else
error(['Unknown data type: ' class(r)]);
end;
end
%--------------------------------------------------------------------------
% Transforms Java String to MATLAB char
%
function result = scan_string(r)
result = char(r);
end
%--------------------------------------------------------------------------
% Transforms Java double to MATLAB double
%
function result = scan_numeric(r)
result = double(r);
end
%--------------------------------------------------------------------------
% Transforms Java boolean to MATLAB logical
%
function result = scan_logical(r)
result = logical(r);
end
%--------------------------------------------------------------------------
% Transforms Java Date class to MATLAB DateTime class
%
function result = scan_datetime(r)
result = DateTime(r);
end
%--------------------------------------------------------------------------
% Transforms Java List to MATLAB cell column running scan(...) recursively
% for all ListS items.
%
function result = scan_list(r)
result = cell(r.size(),1);
it = r.iterator();
ii = 1;
while it.hasNext()
i = it.next();
result{ii} = scan(i);
ii = ii + 1;
end;
end
%--------------------------------------------------------------------------
% Transforms Java Map to MATLAB struct running scan(...) recursively for
% content of every Map field.
% When there is field, which is recognized to be the >import keyword<, an
% attempt is made to import file given by the field content.
%
% The result of import is so far stored as a content of the item named 'import'.
%
function result = scan_map(r)
it = r.keySet().iterator();
while it.hasNext()
next = it.next();
i = next;
ich = char(i);
if iskw_import(ich)
result.(ich) = perform_import(r.get(java.lang.String(ich)));
else
result.(genvarname(ich)) = scan(r.get(java.lang.String(ich)));
end;
end;
if not(exist('result','var'))
result={};
end
end
%--------------------------------------------------------------------------
% Determines whether r contains a keyword denoting import.
%
function result = iskw_import(r)
result = isequal(r, 'import');
end
%--------------------------------------------------------------------------
% Transforms input hierarchy the usual way. If the result is char, then
% tries to load file denoted by this char. If the result is cell then tries
% to do just mentioned for each cellS item.
%
function result = perform_import(r)
r = scan(r);
if iscell(r) && all(cellfun(@ischar, r))
result = cellfun(@load_yaml, r, 'UniformOutput', 0);
elseif ischar(r)
result = {load_yaml(r)};
else
disp(r);
error(['Importer does not unterstand given filename. '...
'Invalid node displayed above.']);
end;
end
%--------------------------------------------------------------------------
% Sets verbosity level for all load_yaml infos.
%
function setverblevel(level)
global verbose_readyaml;
verbose_readyaml = 0;
if exist('level','var')
verbose_readyaml = level;
end;
end
%--------------------------------------------------------------------------
% Returns current verbosity level.
%
function result = getverblevel()
global verbose_readyaml;
result = verbose_readyaml;
end
%--------------------------------------------------------------------------
% For debugging purposes. Displays a message as level is more than or equal
% the current verbosity level.
%
function info(level, text, value_to_display)
if getverblevel() >= level
fprintf(text);
if exist('value_to_display','var')
disp(value_to_display);
else
fprintf('\n');
end;
end;
end
%==========================================================================