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 ischar(varargin{4}) |
---|
57 | for i=1:length(tree.tree{uid}.attributes) |
---|
58 | if strcmp(varargin{4},tree.tree{uid}.attributes{i}.key) |
---|
59 | varargout{1} = tree.tree{uid}.attributes{i}.val; |
---|
60 | return; |
---|
61 | end |
---|
62 | end |
---|
63 | varargout{1} = []; |
---|
64 | elseif ~isa(varargin{4},'double') | ... |
---|
65 | any(varargin{4}>length(tree.tree{uid}.attributes)) | ... |
---|
66 | any(varargin{4}<1) |
---|
67 | error('[XMLTree] Invalid attribute indice.'); |
---|
68 | else |
---|
69 | if length(varargin{4}) == 1 |
---|
70 | varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)}; |
---|
71 | else |
---|
72 | varargout{1} = {}; |
---|
73 | for i=1:length(varargin{4}) |
---|
74 | varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)}; |
---|
75 | end |
---|
76 | end |
---|
77 | end |
---|
78 | else |
---|
79 | if length(tree.tree{uid}.attributes) == 1 |
---|
80 | varargout{1} = tree.tree{uid}.attributes{1}; |
---|
81 | else |
---|
82 | varargout{1} = {}; |
---|
83 | for i=1:length(tree.tree{uid}.attributes) |
---|
84 | varargout{1}{i} = tree.tree{uid}.attributes{i}; |
---|
85 | end |
---|
86 | end |
---|
87 | end |
---|
88 | case 'add' |
---|
89 | error(nargchk(5,5,nargin)); |
---|
90 | ind = length(tree.tree{uid}.attributes) + 1; |
---|
91 | tree.tree{uid}.attributes{ind} = struct('key',varargin{4},'val',varargin{5}); |
---|
92 | varargout{1} = tree; |
---|
93 | case 'del' |
---|
94 | error(nargchk(3,4,nargin)); |
---|
95 | if nargin == 4 |
---|
96 | if ~isa(varargin{4},'double') | ... |
---|
97 | any(varargin{4}>length(tree.tree{uid}.attributes)) | ... |
---|
98 | any(varargin{4}<1) |
---|
99 | error('[XMLTree] Invalid attribute indice.'); |
---|
100 | end |
---|
101 | ind = varargin{4}; |
---|
102 | tree.tree{uid}.attributes(ind) = []; |
---|
103 | else |
---|
104 | tree.tree{uid}.attributes = []; |
---|
105 | end |
---|
106 | varargout{1} = tree; |
---|
107 | case 'length' |
---|
108 | error(nargchk(3,3,nargin)); |
---|
109 | varargout{1} = length(tree.tree{uid}.attributes); |
---|
110 | otherwise |
---|
111 | error('[XMLTree] Unknown method.'); |
---|
112 | end |
---|