[723] | 1 | function varargout = attributes(varargin) |
---|
| 2 | % XMLTREE/ATTRIBUTES Method (handle attributes of an element node) |
---|
| 3 | % FORMAT varargout = attributes(varargin) |
---|
| 4 | % |
---|
| 5 | % tree - XMLTree object |
---|
| 6 | % method - 'set','get','add','del' or 'length' |
---|
| 7 | % uid - the UID of an element node |
---|
| 8 | % n - indice of the attribute |
---|
| 9 | % key - string key="..." |
---|
| 10 | % val - string ...="val" |
---|
| 11 | % attr - cell array of struct(key,val) or just struct(key,val) |
---|
| 12 | % l - number of attributes of the element node uid |
---|
| 13 | % |
---|
| 14 | % tree = attributes(tree,'set',uid,n,key,val) |
---|
| 15 | % attr = attributes(tree,'get',uid[,n]) |
---|
| 16 | % tree = attributes(tree,'add',uid,key,val) |
---|
| 17 | % tree = attributes(tree,'del',uid[,n]) |
---|
| 18 | % l = attributes(tree,'length',uid) |
---|
| 19 | %_______________________________________________________________________ |
---|
| 20 | % |
---|
| 21 | % Handle attributes of an element node. |
---|
| 22 | % The tree parameter must be in input AND in output for 'set', 'add' and |
---|
| 23 | % 'del' methods. |
---|
| 24 | %_______________________________________________________________________ |
---|
| 25 | % @(#)attributes.m Guillaume Flandin 02/04/05 |
---|
| 26 | |
---|
| 27 | error(nargchk(3,6,nargin)); |
---|
| 28 | tree = varargin{1}; |
---|
| 29 | if ~ischar(varargin{2}) | ... |
---|
| 30 | ~any(strcmp(varargin{2},{'set','get','add','del','length'})) |
---|
| 31 | error('[XMLTree] Unknown method.'); |
---|
| 32 | end |
---|
| 33 | uid = varargin{3}; |
---|
| 34 | if ~isa(uid,'double') | any(uid>length(tree)) | any(uid<1) |
---|
| 35 | error('[XMLTree] UID must be a positive integer scalar.'); |
---|
| 36 | end |
---|
| 37 | |
---|
| 38 | if ~strcmp(tree.tree{uid}.type,'element') |
---|
| 39 | error('[XMLTree] This node has no attributes.'); |
---|
| 40 | end |
---|
| 41 | |
---|
| 42 | switch varargin{2} |
---|
| 43 | case 'set' |
---|
| 44 | error(nargchk(6,6,nargin)); |
---|
| 45 | if ~isa(varargin{4},'double') | ... |
---|
| 46 | any(varargin{4}>length(tree.tree{uid}.attributes)) | ... |
---|
| 47 | any(varargin{4}<1) |
---|
| 48 | error('[XMLTree] Invalid attribute indice.'); |
---|
| 49 | end |
---|
| 50 | ind = varargin{4}; |
---|
| 51 | tree.tree{uid}.attributes{ind} = struct('key',varargin{5},'val',varargin{6}); |
---|
| 52 | varargout{1} = tree; |
---|
| 53 | case 'get' |
---|
| 54 | error(nargchk(3,4,nargin)); |
---|
| 55 | if nargin == 4 |
---|
| 56 | if ~isa(varargin{4},'double') | ... |
---|
| 57 | any(varargin{4}>length(tree.tree{uid}.attributes)) | ... |
---|
| 58 | any(varargin{4}<1) |
---|
| 59 | error('[XMLTree] Invalid attribute indice.'); |
---|
| 60 | end |
---|
| 61 | if length(varargin{4}) == 1 |
---|
| 62 | varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)}; |
---|
| 63 | else |
---|
| 64 | varargout{1} = {}; |
---|
| 65 | for i=1:length(varargin{4}) |
---|
| 66 | varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)}; |
---|
| 67 | end |
---|
| 68 | end |
---|
| 69 | else |
---|
| 70 | if length(tree.tree{uid}.attributes) == 1 |
---|
| 71 | varargout{1} = tree.tree{uid}.attributes{1}; |
---|
| 72 | else |
---|
| 73 | varargout{1} = {}; |
---|
| 74 | for i=1:length(tree.tree{uid}.attributes) |
---|
| 75 | varargout{1}{i} = tree.tree{uid}.attributes{i}; |
---|
| 76 | end |
---|
| 77 | end |
---|
| 78 | end |
---|
| 79 | case 'add' |
---|
| 80 | error(nargchk(5,5,nargin)); |
---|
| 81 | ind = length(tree.tree{uid}.attributes) + 1; |
---|
| 82 | tree.tree{uid}.attributes{ind} = struct('key',varargin{4},'val',varargin{5}); |
---|
| 83 | varargout{1} = tree; |
---|
| 84 | case 'del' |
---|
| 85 | error(nargchk(3,4,nargin)); |
---|
| 86 | if nargin == 4 |
---|
| 87 | if ~isa(varargin{4},'double') | ... |
---|
| 88 | any(varargin{4}>length(tree.tree{uid}.attributes)) | ... |
---|
| 89 | any(varargin{4}<1) |
---|
| 90 | error('[XMLTree] Invalid attribute indice.'); |
---|
| 91 | end |
---|
| 92 | ind = varargin{4}; |
---|
| 93 | tree.tree{uid}.attributes(ind) = []; |
---|
| 94 | else |
---|
| 95 | tree.tree{uid}.attributes = []; |
---|
| 96 | end |
---|
| 97 | varargout{1} = tree; |
---|
| 98 | case 'length' |
---|
| 99 | error(nargchk(3,3,nargin)); |
---|
| 100 | varargout{1} = length(tree.tree{uid}.attributes); |
---|
| 101 | otherwise |
---|
| 102 | error('[XMLTree] Unknown method.'); |
---|
| 103 | end |
---|