Skip to content

Commit

Permalink
Fixed repeated javaaddpath call which allmost allways threw an annoyi…
Browse files Browse the repository at this point in the history
…ng warning.

Added possibility to treat filename parameter directly as yaml data.
  • Loading branch information
ptomask committed Dec 23, 2011
1 parent c916f87 commit 7df12eb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 27 deletions.
10 changes: 8 additions & 2 deletions ReadYaml.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
% exception and halts the process
% makeords ... Determines whether to convert cell array to
% ordinary matrix whenever possible (1).
function result = ReadYaml(filename, nosuchfileaction, makeords)
function result = ReadYaml(filename, nosuchfileaction, makeords, treatasdata)
if ~exist('nosuchfileaction','var')
nosuchfileaction = 0;
end;
Expand All @@ -35,7 +35,13 @@
if ~ismember(makeords,[0,1])
error('makeords parameter must be 0,1 or missing.');
end;
ry = ReadYamlRaw(filename, 0, nosuchfileaction);
if(~exist('treatasdata','var'))
treatasdata = 0;
end;
if ~ismember(treatasdata,[0,1])
error('treatasdata parameter must be 0,1 or missing.');
end;
ry = ReadYamlRaw(filename, 0, nosuchfileaction, treatasdata);
ry = deflateimports(ry);
if iscell(ry) && ...
length(ry) == 1 && ...
Expand Down
48 changes: 31 additions & 17 deletions ReadYamlRaw.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
% verbose ... verbosity level (0 or absent = no messages,
% 1 = notify imports)
%==========================================================================
function result = ReadYamlRaw(filename, verbose, nosuchfileaction)
function result = ReadYamlRaw(filename, verbose, nosuchfileaction, treatasdata)
if ~exist('verbose','var')
verbose = 0;
end;

[pth,~,~] = fileparts(mfilename('fullpath'));
javaaddpath([pth filesep 'external' filesep 'snakeyaml-1.9.jar']); % javaaddpath clears global variables...!?
try
import('org.yaml.snakeyaml.*');
javaObject('Yaml');
catch
javaaddpath([pth filesep 'external' filesep 'snakeyaml-1.9.jar']); % javaaddpath clears global variables...!?
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);
result = load_yaml(filename, nosuchfileaction, treatasdata);
end

%--------------------------------------------------------------------------
Expand All @@ -27,40 +33,48 @@
% 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)
function result = load_yaml(inputfilename, nosuchfileaction, treatasdata)

global nsfe;

if isempty(nsfe)
nsfe = nosuchfileaction;
end;



yaml = org.yaml.snakeyaml.Yaml(); % It appears that Java objects cannot be persistent...!?

[filepath, filename, fileext] = fileparts(inputfilename);
if isempty(filepath)
pathstore = cd();
else
pathstore = cd(filepath);
if ~treatasdata
[filepath, filename, fileext] = fileparts(inputfilename);
if isempty(filepath)
pathstore = cd();
else
pathstore = cd(filepath);
end;
end;
try
result = scan(yaml.load(fileread([filename, fileext])));
if ~treatasdata
result = scan(yaml.load(fileread([filename, fileext])));
else
result = scan(yaml.load(inputfilename));
end;
catch ex
cd(pathstore);
if ~treatasdata
cd(pathstore);
end;
switch ex.identifier
case 'MATLAB:fileread:cannotOpenFile'
if nsfe
if nsfe == 1
error('MATLAB:MATYAML:FileNotFound', ['No such file to read: ',filename]);
else
elseif nsfe == 0
warning('MATLAB:MATYAML:FileNotFound', ['No such file to read: ',filename]);
result = struct();
return;
end;
end;
rethrow(ex);
end;
cd(pathstore);
if ~treatasdata
cd(pathstore);
end;
end

%--------------------------------------------------------------------------
Expand Down
22 changes: 16 additions & 6 deletions WriteYaml.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
% hierarchy of java.util.ArrayListS and java.util.MapS. Then calls
% Snakeyaml to write it to a file.
%=========================================================================
function WriteYaml(filename, data)
function result = WriteYaml(filename, data)
result = [];
[pth,~,~] = fileparts(mfilename('fullpath'));
javaaddpath([pth filesep 'external' filesep 'snakeyaml-1.9.jar']);
import('org.yaml.snakeyaml.*');
try
import('org.yaml.snakeyaml.*');
javaObject('Yaml');
catch
javaaddpath([pth filesep 'external' filesep 'snakeyaml-1.9.jar']);
import('org.yaml.snakeyaml.*');
end;
javastruct = scan(data);
dumperopts = DumperOptions();
dumperopts.setLineBreak(...
Expand All @@ -15,9 +21,13 @@ function WriteYaml(filename, data)
yaml = Yaml(dumperopts);

output = yaml.dump(javastruct);
fid = fopen(filename,'w');
fprintf(fid,'%s',char(output) );
fclose(fid);
if ~isempty(filename)
fid = fopen(filename,'w');
fprintf(fid,'%s',char(output) );
fclose(fid);
else
result = output;
end;
end

%--------------------------------------------------------------------------
Expand Down
7 changes: 5 additions & 2 deletions doinheritance.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
% instead of one simple string.
%
%==========================================================================
function result = doinheritance(r)
result = recurse(r, 0, {r});
function result = doinheritance(r, tr)
if ~exist('tr','var')
tr = r;
end;
result = recurse(r, 0, {tr});
end

function result = recurse(data, level, addit)
Expand Down

0 comments on commit 7df12eb

Please sign in to comment.