source: trunk/src/@xmltree/attributes.m @ 1167

Last change on this file since 1167 was 925, checked in by sommeria, 9 years ago

xmltree updated

File size: 4.3 KB
Line 
1function 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% Copyright (C) 2002-2011  http://www.artefact.tk/
26
27% Guillaume Flandin
28% $Id: attributes.m 4460 2011-09-05 14:52:16Z guillaume $
29
30
31%error(nargchk(3,6,nargin));
32
33tree = varargin{1};
34if ~ischar(varargin{2}) || ...
35   ~any(strcmp(varargin{2},{'set','get','add','del','length'}))
36    error('[XMLTree] Unknown method.');
37end
38uid = varargin{3};
39if ~isa(uid,'double') || any(uid>length(tree)) || any(uid<1)
40    error('[XMLTree] UID must be a positive integer scalar.');
41end
42
43if ~strcmp(tree.tree{uid}.type,'element')
44    error('[XMLTree] This node has no attributes.');
45end
46
47switch varargin{2}
48    case 'set'
49        %error(nargchk(6,6,nargin));
50        if ~isa(varargin{4},'double') || ...
51           any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
52           any(varargin{4}<1)
53            error('[XMLTree] Invalid attribute indice.');
54        end
55        ind = varargin{4};
56        tree.tree{uid}.attributes{ind} = struct('key',varargin{5},'val',varargin{6});
57        varargout{1} = tree;
58    case 'get'
59        %error(nargchk(3,4,nargin));
60        if nargin == 4
61            if ischar(varargin{4})
62                for i=1:length(tree.tree{uid}.attributes)
63                    if strcmp(varargin{4},tree.tree{uid}.attributes{i}.key)
64                        varargout{1} = tree.tree{uid}.attributes{i}.val;
65                        return;
66                    end
67                end
68                varargout{1} = [];
69            elseif ~isa(varargin{4},'double') || ...
70               any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
71               any(varargin{4}<1)
72                error('[XMLTree] Invalid attribute indice.');
73            else
74                if length(varargin{4}) == 1
75                    varargout{1} = tree.tree{uid}.attributes{varargin{4}(1)};
76                else
77                    varargout{1} = {};
78                    for i=1:length(varargin{4})
79                        varargout{1}{i} = tree.tree{uid}.attributes{varargin{4}(i)};
80                    end
81                end
82            end
83        else
84            if length(tree.tree{uid}.attributes) == 1
85                varargout{1} = tree.tree{uid}.attributes{1};
86            else
87                varargout{1} = {};
88                for i=1:length(tree.tree{uid}.attributes)
89                    varargout{1}{i} = tree.tree{uid}.attributes{i};
90                end
91            end
92        end
93    case 'add'
94        %error(nargchk(5,5,nargin));
95        ind = length(tree.tree{uid}.attributes) + 1;
96        tree.tree{uid}.attributes{ind} = struct('key',varargin{4},'val',varargin{5});
97        varargout{1} = tree;
98    case 'del'
99        %error(nargchk(3,4,nargin));
100        if nargin == 4
101            if ~isa(varargin{4},'double') || ...
102              any(varargin{4}>length(tree.tree{uid}.attributes)) || ...
103              any(varargin{4}<1)
104                error('[XMLTree] Invalid attribute indice.');
105            end
106            ind = varargin{4};
107            tree.tree{uid}.attributes(ind) = [];
108        else
109            tree.tree{uid}.attributes = [];
110        end
111        varargout{1} = tree;
112    case 'length'
113        %error(nargchk(3,3,nargin));
114        varargout{1} = length(tree.tree{uid}.attributes);
115    otherwise
116        error('[XMLTree] Unknown method.');
117end
Note: See TracBrowser for help on using the repository browser.